TGUI  0.9-dev
EditBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 #ifndef TGUI_EDIT_BOX_HPP
26 #define TGUI_EDIT_BOX_HPP
27 
28 
29 #include <TGUI/Widgets/ClickableWidget.hpp>
30 #include <TGUI/Renderers/EditBoxRenderer.hpp>
31 #include <TGUI/Rect.hpp>
32 #include <TGUI/Text.hpp>
33 #include <regex>
34 
36 
37 namespace tgui
38 {
45  class TGUI_API EditBox : public ClickableWidget
46  {
47  public:
48 
49  typedef std::shared_ptr<EditBox> Ptr;
50  typedef std::shared_ptr<const EditBox> ConstPtr;
51 
52 
56  enum class Alignment
57  {
59  Left,
60 
62  Center,
63 
65  Right
66  };
67 
68 
72  struct Validator
73  {
74  static TGUI_API const char* All;
75  static TGUI_API const char* Int;
76  static TGUI_API const char* UInt;
77  static TGUI_API const char* Float;
78  };
79 
80 
82  // Default constructor
84  EditBox();
85 
86 
93  static EditBox::Ptr create();
94 
95 
104  static EditBox::Ptr copy(EditBox::ConstPtr editBox);
105 
106 
111  EditBoxRenderer* getSharedRenderer();
112  const EditBoxRenderer* getSharedRenderer() const;
113 
119  EditBoxRenderer* getRenderer();
120  const EditBoxRenderer* getRenderer() const;
121 
122 
129  void setSize(const Layout2d& size) override;
130  using Widget::setSize;
131 
132 
140  void setEnabled(bool enabled) override;
141 
142 
156  void setText(const String& text);
157 
158 
165  String getText() const;
166 
167 
176  void setDefaultText(const String& text);
177 
178 
187  const String& getDefaultText() const;
188 
189 
199  void selectText(std::size_t start = 0, std::size_t length = String::npos);
200 
201 
208  String getSelectedText() const;
209 
210 
218  void setTextSize(unsigned int textSize) override;
219 
220 
227  unsigned int getTextSize() const override;
228 
229 
242  void setPasswordCharacter(char32_t passwordChar);
243 
244 
252  char32_t getPasswordCharacter() const;
253 
254 
263  void setMaximumCharacters(unsigned int maxChars);
264 
265 
275  unsigned int getMaximumCharacters() const;
276 
277 
284  void setAlignment(Alignment alignment);
285 
286 
293  Alignment getAlignment() const;
294 
295 
305  void limitTextWidth(bool limitWidth = true);
306 
307 
314  bool isTextWidthLimited() const;
315 
316 
326  void setReadOnly(bool readOnly = true);
327 
328 
338  bool isReadOnly() const;
339 
340 
347  void setCaretPosition(std::size_t charactersBeforeCaret);
348 
349 
356  std::size_t getCaretPosition() const;
357 
358 
375  bool setInputValidator(const String& regex = U".*");
376 
377 
384  const String& getInputValidator() const;
385 
386 
394  void setSuffix(const String& suffix);
395 
396 
402  const String& getSuffix() const;
403 
404 
413  void setFocused(bool focused) override;
414 
415 
420  bool mouseOnWidget(Vector2f pos) const override;
421 
422 
426  void leftMousePressed(Vector2f pos) override;
427 
431  void mouseMoved(Vector2f pos) override;
432 
436  void keyPressed(const sf::Event::KeyEvent& event) override;
437 
441  void textEntered(char32_t key) override;
442 
443 
451  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
452 
453 
455  protected:
456 
466  Signal& getSignal(String signalName) override;
467 
468 
475  void rendererChanged(const String& property) override;
476 
477 
481  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
482 
483 
487  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
488 
489 
491  // Returns the total width that the text is going to take
493  float getFullTextWidth() const;
494 
495 
497  // Returns the size without the borders
499  Vector2f getInnerSize() const;
500 
501 
503  // Returns the width of the edit box minus the padding.
505  float getVisibleEditBoxWidth() const;
506 
507 
509  // This function will search after which character the caret should be placed. It will not change the caret position.
511  std::size_t findCaretPosition(float posX);
512 
513 
515  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
516  // some characters selected.
518  void deleteSelectedCharacters();
519 
520 
522  // Recalculates the position of the texts.
524  void recalculateTextPositions();
525 
526 
528  // Updates the internal texts after SelStart or SelEnd changed.
530  void updateSelection();
531 
532 
534  // Update the color of the Text objects
536  void updateTextColor();
537 
538 
540  // Update auto-sized text
542  void updateTextSize();
543 
544 
546  // This function is called every frame with the time passed since the last frame.
548  void update(Duration elapsedTime) override;
549 
550 
552  // Makes a copy of the widget
554  Widget::Ptr clone() const override
555  {
556  return std::make_shared<EditBox>(*this);
557  }
558 
559 
561  public:
562 
563  SignalString onTextChange = {"TextChanged"};
564  SignalString onReturnKeyPress = {"ReturnKeyPressed"};
565  SignalString onReturnOrUnfocus = {"ReturnOrUnfocused"};
566 
567 
569  protected:
570 
571  // Is the caret visible or not?
572  bool m_caretVisible = true;
573 
574  // When this boolean is true then you can no longer add text when the EditBox is full.
575  // Changing it to false will allow you to scroll the text (default).
576  // You can change the boolean with the limitTextWidth(bool) function.
577  bool m_limitTextWidth = false;
578 
579  bool m_readOnly = false;
580 
581  // The text inside the edit box
582  String m_text;
583  String m_displayedText; // Same as m_text unless a password char is set
584 
585  String m_regexString = U".*";
586  std::wregex m_regex = std::wregex{m_regexString.toWideString()};
587 
588  // The text alignment
589  Alignment m_textAlignment = Alignment::Left;
590 
591  // The selection
592  std::size_t m_selChars = 0;
593  std::size_t m_selStart = 0;
594  std::size_t m_selEnd = 0;
595 
596  // The password character
597  char32_t m_passwordChar = '\0';
598 
599  // The maximum allowed characters.
600  // Zero by default, meaning no limit.
601  unsigned int m_maxChars = 0;
602 
603  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
604  unsigned int m_textCropPosition = 0;
605 
606  // The rectangle behind the selected text
607  FloatRect m_selectedTextBackground;
608 
609  // The blinking caret
610  FloatRect m_caret = {0, 0, 1, 0};
611 
612  // Is there a possibility that the user is going to double click?
613  bool m_possibleDoubleClick = false;
614 
615  // We need three texts for drawing + one for the default text + one more for calculations.
616  Text m_textBeforeSelection;
617  Text m_textSelection;
618  Text m_textAfterSelection;
619  Text m_defaultText;
620  Text m_textFull;
621  Text m_textSuffix;
622 
623  Sprite m_sprite;
624  Sprite m_spriteHover;
625  Sprite m_spriteDisabled;
626  Sprite m_spriteFocused;
627 
628  // Cached renderer properties
629  Borders m_bordersCached;
630  Padding m_paddingCached;
631  Color m_borderColorCached;
632  Color m_borderColorHoverCached;
633  Color m_borderColorDisabledCached;
634  Color m_borderColorFocusedCached;
635  Color m_backgroundColorCached;
636  Color m_backgroundColorHoverCached;
637  Color m_backgroundColorDisabledCached;
638  Color m_backgroundColorFocusedCached;
639  Color m_caretColorCached;
640  Color m_caretColorHoverCached;
641  Color m_caretColorFocusedCached;
642  Color m_selectedTextBackgroundColorCached;
643 
644 
646  };
647 
649 }
650 
652 
653 #endif // TGUI_EDIT_BOX_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:216
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:49
Definition: EditBoxRenderer.hpp:36
Predefined input validators.
Definition: EditBox.hpp:72
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition: EditBox.hpp:50
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:72
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
static const TGUI_API char * Int
Accept negative and positive integers.
Definition: EditBox.hpp:75
static const TGUI_API char * Float
Accept decimal numbers.
Definition: EditBox.hpp:77
static const TGUI_API char * UInt
Accept only positive integers.
Definition: EditBox.hpp:76
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: EditBox.hpp:554
Wrapper for durations.
Definition: Duration.hpp:48
Edit box widget.
Definition: EditBox.hpp:45
Alignment
The text alignment.
Definition: EditBox.hpp:56
Wrapper class to store strings.
Definition: String.hpp:70
static const TGUI_API char * All
Accept any input.
Definition: EditBox.hpp:74
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Clickable widget.
Definition: ClickableWidget.hpp:39