TGUI  0.8.8
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/FloatRect.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 #if TGUI_COMPILED_WITH_CPP_VER >= 17
75  static inline const std::string All = ".*";
76  static inline const std::string Int = "[+-]?[0-9]*";
77  static inline const std::string UInt = "[0-9]*";
78  static inline const std::string Float = "[+-]?[0-9]*\\.?[0-9]*";
79 #else
80  static TGUI_API const std::string All;
81  static TGUI_API const std::string Int;
82  static TGUI_API const std::string UInt;
83  static TGUI_API const std::string Float;
84 #endif
85  };
86 
87 
89  // Default constructor
91  EditBox();
92 
93 
101 
102 
112 
113 
119  const EditBoxRenderer* getSharedRenderer() const;
120 
127  const EditBoxRenderer* getRenderer() const;
128 
129 
136  void setSize(const Layout2d& size) override;
137  using Widget::setSize;
138 
139 
147  void setEnabled(bool enabled) override;
148 
149 
163  void setText(const sf::String& text);
164 
165 
172  const sf::String& getText() const;
173 
174 
183  void setDefaultText(const sf::String& text);
184 
185 
194  const sf::String& getDefaultText() const;
195 
196 
206  void selectText(std::size_t start = 0, std::size_t length = sf::String::InvalidPos);
207 
208 
215  sf::String getSelectedText() const;
216 
217 
225  void setTextSize(unsigned int textSize) override;
226 
227 
234  unsigned int getTextSize() const override;
235 
236 
249  void setPasswordCharacter(char passwordChar);
250 
251 
259  char getPasswordCharacter() const;
260 
261 
270  void setMaximumCharacters(unsigned int maxChars);
271 
272 
282  unsigned int getMaximumCharacters() const;
283 
284 
291  void setAlignment(Alignment alignment);
292 
293 
301 
302 
312  void limitTextWidth(bool limitWidth = true);
313 
314 
321  bool isTextWidthLimited() const;
322 
323 
333  void setReadOnly(bool readOnly = true);
334 
335 
345  bool isReadOnly() const;
346 
347 
354  void setCaretPosition(std::size_t charactersBeforeCaret);
355 
356 
363  std::size_t getCaretPosition() const;
364 
365 
382  bool setInputValidator(const std::string& regex = ".*");
383 
384 
391  const std::string& getInputValidator() const;
392 
393 
401  void setSuffix(const sf::String& suffix);
402 
403 
409  const sf::String& getSuffix() const;
410 
411 
420  void setFocused(bool focused) override;
421 
422 
427  bool mouseOnWidget(Vector2f pos) const override;
428 
429 
433  void leftMousePressed(Vector2f pos) override;
434 
438  void mouseMoved(Vector2f pos) override;
439 
443  void keyPressed(const sf::Event::KeyEvent& event) override;
444 
448  void textEntered(std::uint32_t Key) override;
449 
450 
458  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
459 
460 
462  protected:
463 
473  Signal& getSignal(std::string signalName) override;
474 
475 
482  void rendererChanged(const std::string& property) override;
483 
484 
488  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
489 
490 
494  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
495 
496 
498  // Returns the total width that the text is going to take
500  float getFullTextWidth() const;
501 
502 
504  // Returns the size without the borders
506  Vector2f getInnerSize() const;
507 
508 
510  // Returns the width of the edit box minus the padding.
512  float getVisibleEditBoxWidth() const;
513 
514 
516  // This function will search after which character the caret should be placed. It will not change the caret position.
518  std::size_t findCaretPosition(float posX);
519 
520 
522  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
523  // some characters selected.
525  void deleteSelectedCharacters();
526 
527 
529  // Recalculates the position of the texts.
531  void recalculateTextPositions();
532 
533 
535  // Updates the internal texts after SelStart or SelEnd changed.
537  void updateSelection();
538 
539 
541  // Update the color of the Text objects
543  void updateTextColor();
544 
545 
547  // This function is called every frame with the time passed since the last frame.
549  bool update(sf::Time elapsedTime) override;
550 
551 
553  // Makes a copy of the widget
555  Widget::Ptr clone() const override
556  {
557  return std::make_shared<EditBox>(*this);
558  }
559 
560 
562  private:
563 
565  // Handles "Backspace" key press
567  void backspaceKeyPressed();
568 
570  // Handles "Delete" key press
572  void deleteKeyPressed();
573 
575  // Handles "Ctrl+C" key press (or equivalent on macOS)
577  void copySelectedTextToClipboard();
578 
580  // Handles "Ctrl+X" key press (or equivalent on macOS)
582  void cutSelectedTextToClipboard();
583 
585  // Handles "Ctrl+V" key press (or equivalent on macOS)
587  void pasteTextFromClipboard();
588 
590  // Handles "ArrowLeft" key press
592  void moveCaretLeft(bool shiftPressed);
593 
595  // Handles "ArrowRight" key press
597  void moveCaretRight(bool shiftPressed);
598 
600  // Handles "Ctrl+ArrowLeft" key press (or equivalent on macOS)
602  void moveCaretWordBegin();
603 
605  // Handles "Ctrl+ArrowRight" key press (or equivalent on macOS)
607  void moveCaretWordEnd();
608 
609 
611  public:
612 
613  SignalString onTextChange = {"TextChanged"};
614  SignalString onReturnKeyPress = {"ReturnKeyPressed"};
615 
616 
618  protected:
619 
620  // Is the caret visible or not?
621  bool m_caretVisible = true;
622 
623  // When this boolean is true then you can no longer add text when the EditBox is full.
624  // Changing it to false will allow you to scroll the text (default).
625  // You can change the boolean with the limitTextWidth(bool) function.
626  bool m_limitTextWidth = false;
627 
628  bool m_readOnly = false;
629 
630  // The text inside the edit box
631  sf::String m_text;
632 
633  std::string m_regexString = ".*";
634  std::regex m_regex = std::regex{m_regexString};
635 
636  // The text alignment
637  Alignment m_textAlignment = Alignment::Left;
638 
639  // The selection
640  std::size_t m_selChars = 0;
641  std::size_t m_selStart = 0;
642  std::size_t m_selEnd = 0;
643 
644  // The password character
645  char m_passwordChar = '\0';
646 
647  // The maximum allowed characters.
648  // Zero by default, meaning no limit.
649  unsigned int m_maxChars = 0;
650 
651  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
652  unsigned int m_textCropPosition = 0;
653 
654  // The rectangle behind the selected text
655  FloatRect m_selectedTextBackground;
656 
657  // The blinking caret
658  FloatRect m_caret = {{0, 0, 1, 0}};
659 
660  // Is there a possibility that the user is going to double click?
661  bool m_possibleDoubleClick = false;
662 
663  // We need three texts for drawing + one for the default text + one more for calculations.
664  Text m_textBeforeSelection;
665  Text m_textSelection;
666  Text m_textAfterSelection;
667  Text m_defaultText;
668  Text m_textFull;
669  Text m_textSuffix;
670 
671  Sprite m_sprite;
672  Sprite m_spriteHover;
673  Sprite m_spriteDisabled;
674  Sprite m_spriteFocused;
675 
676  // Cached renderer properties
677  Borders m_bordersCached;
678  Padding m_paddingCached;
679  Color m_borderColorCached;
680  Color m_borderColorHoverCached;
681  Color m_borderColorDisabledCached;
682  Color m_borderColorFocusedCached;
683  Color m_backgroundColorCached;
684  Color m_backgroundColorHoverCached;
685  Color m_backgroundColorDisabledCached;
686  Color m_backgroundColorFocusedCached;
687  Color m_caretColorCached;
688  Color m_caretColorHoverCached;
689  Color m_caretColorFocusedCached;
690  Color m_selectedTextBackgroundColorCached;
691 
692 
694  };
695 
697 }
698 
700 
701 #endif // TGUI_EDIT_BOX_HPP
unsigned int getMaximumCharacters() const
Returns the character limit.
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:224
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:49
char getPasswordCharacter() const
Returns the password character.
void setCaretPosition(std::size_t charactersBeforeCaret)
Sets the blinking caret to after a specific character.
static TGUI_API const std::string Float
Accept decimal numbers.
Definition: EditBox.hpp:83
Definition: EditBoxRenderer.hpp:37
const sf::String & getDefaultText() const
Returns the default text of the edit box. This is the text drawn when the edit box is empty.
EditBoxRenderer * getSharedRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
Predefined input validators.
Definition: EditBox.hpp:73
void limitTextWidth(bool limitWidth=true)
Should the text width be limited or should you be able to type even if the edit box is full?
void setFocused(bool focused) override
Focus or unfocus the widget.
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition: EditBox.hpp:50
void setSize(const Layout2d &size) override
Changes the size of the edit box.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
EditBoxRenderer * getRenderer()
Returns the renderer, which gives access to functions that determine how the widget is displayed.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
void setReadOnly(bool readOnly=true)
Makes the edit box read-only or make it writable again.
bool isTextWidthLimited() const
Checks if the text width is limited to the size of the edit box.
bool isReadOnly() const
Checks if the edit box read-only or writable.
Class to store the position or size of a widget.
Definition: Layout.hpp:260
static TGUI_API const std::string UInt
Accept only positive integers.
Definition: EditBox.hpp:82
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: EditBox.hpp:555
void setTextSize(unsigned int textSize) override
Changes the character size of the text.
bool mouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
static EditBox::Ptr copy(EditBox::ConstPtr editBox)
Makes a copy of another edit box.
void setMaximumCharacters(unsigned int maxChars)
Changes the character limit.
const sf::String & getText() const
Returns the text inside the edit box. This text is not affected by the password character.
Edit box widget.
Definition: EditBox.hpp:46
Alignment
The text alignment.
Definition: EditBox.hpp:57
static TGUI_API const std::string All
Accept any input.
Definition: EditBox.hpp:80
void rendererChanged(const std::string &property) override
Function called when one of the properties of the renderer is changed.
const std::string & getInputValidator() const
Returns the regex to which the text is matched.
void draw(sf::RenderTarget &target, sf::RenderStates states) const override
Draw the widget to a render target.
Definition: Vector2f.hpp:39
void setEnabled(bool enabled) override
Enables or disables the widget.
Alignment getAlignment() const
Gets the current text alignment.
std::size_t getCaretPosition() const
Returns after which character the blinking cursor is currently located.
void setSuffix(const sf::String &suffix)
Places a suffix at the right side of the edit box.
unsigned int getTextSize() const override
Returns the character size of the text.
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
void setDefaultText(const sf::String &text)
Changes the default text of the editbox. This is the text drawn when the edit box is empty.
static EditBox::Ptr create()
Creates a new edit box widget.
sf::String getSelectedText() const
Returns the text that you currently have selected. This text is not affected by the password characte...
void setText(const sf::String &text)
Changes the text of the editbox.
void setPasswordCharacter(char passwordChar)
Sets a password character.
Signal & getSignal(std::string signalName) override
Retrieves a signal based on its name.
bool setInputValidator(const std::string &regex=".*")
Defines how the text input should look like.
Clickable widget.
Definition: ClickableWidget.hpp:40
static TGUI_API const std::string Int
Accept negative and positive integers.
Definition: EditBox.hpp:81
const sf::String & getSuffix() const
Returns the suffix currently displayed on the right side of the edit box.
void selectText(std::size_t start=0, std::size_t length=sf::String::InvalidPos)
Selects text in the edit box.
void setAlignment(Alignment alignment)
Changes the text alignment.