TGUI  0.8.4
EditBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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  #ifdef TGUI_USE_CPP17
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 
100  static EditBox::Ptr create();
101 
102 
111  static EditBox::Ptr copy(EditBox::ConstPtr editBox);
112 
113 
118  EditBoxRenderer* getSharedRenderer();
119  const EditBoxRenderer* getSharedRenderer() const;
120 
126  EditBoxRenderer* getRenderer();
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);
226 
227 
234  unsigned int getTextSize() const;
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 
300  Alignment getAlignment() const;
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 
380  void setInputValidator(const std::string& regex = ".*");
381 
382 
389  const std::string& getInputValidator() const;
390 
391 
399  void setSuffix(const sf::String& suffix);
400 
401 
407  const sf::String& getSuffix() const;
408 
409 
418  void setFocused(bool focused) override;
419 
420 
425  bool mouseOnWidget(Vector2f pos) const override;
426 
427 
431  void leftMousePressed(Vector2f pos) override;
432 
436  void mouseMoved(Vector2f pos) override;
437 
441  void keyPressed(const sf::Event::KeyEvent& event) override;
442 
446  void textEntered(std::uint32_t Key) override;
447 
448 
456  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
457 
458 
460  protected:
461 
471  Signal& getSignal(std::string signalName) override;
472 
473 
480  void rendererChanged(const std::string& property) override;
481 
482 
486  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
487 
488 
492  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
493 
494 
496  // Returns the total width that the text is going to take
498  float getFullTextWidth() const;
499 
500 
502  // Returns the size without the borders
504  Vector2f getInnerSize() const;
505 
506 
508  // Returns the width of the edit box minus the padding.
510  float getVisibleEditBoxWidth() const;
511 
512 
514  // This function will search after which character the caret should be placed. It will not change the caret position.
516  std::size_t findCaretPosition(float posX);
517 
518 
520  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
521  // some characters selected.
523  void deleteSelectedCharacters();
524 
525 
527  // Recalculates the position of the texts.
529  void recalculateTextPositions();
530 
531 
533  // Updates the internal texts after SelStart or SelEnd changed.
535  void updateSelection();
536 
537 
539  // Update the color of the Text objects
541  void updateTextColor();
542 
543 
545  // This function is called every frame with the time passed since the last frame.
547  void update(sf::Time elapsedTime) override;
548 
549 
551  // Makes a copy of the widget
553  Widget::Ptr clone() const override
554  {
555  return std::make_shared<EditBox>(*this);
556  }
557 
558 
560  public:
561 
562  SignalString onTextChange = {"TextChanged"};
563  SignalString onReturnKeyPress = {"ReturnKeyPressed"};
564 
565 
567  protected:
568 
569  // Is the caret visible or not?
570  bool m_caretVisible = true;
571 
572  // When this boolean is true then you can no longer add text when the EditBox is full.
573  // Changing it to false will allow you to scroll the text (default).
574  // You can change the boolean with the limitTextWidth(bool) function.
575  bool m_limitTextWidth = false;
576 
577  bool m_readOnly = false;
578 
579  // The text inside the edit box
580  sf::String m_text;
581 
582  std::string m_regexString = ".*";
583  std::regex m_regex = std::regex{m_regexString};
584 
585  // This will store the size of the text ( 0 to auto size )
586  unsigned int m_textSize = 0;
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  char 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
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
static TGUI_API const std::string Float
Accept decimal numbers.
Definition: EditBox.hpp:83
Class to store the position or size of a widget.
Definition: Layout.hpp:247
Predefined input validators.
Definition: EditBox.hpp:72
Definition: Vector2f.hpp:37
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition: EditBox.hpp:50
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: EditBox.hpp:553
Clickable widget.
Definition: ClickableWidget.hpp:39
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
static TGUI_API const std::string All
Accept any input.
Definition: EditBox.hpp:80
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
static TGUI_API const std::string UInt
Accept only positive integers.
Definition: EditBox.hpp:82
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:59
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:49
Definition: EditBoxRenderer.hpp:36
Edit box widget.
Definition: EditBox.hpp:45
Alignment
The text alignment.
Definition: EditBox.hpp:56
static TGUI_API const std::string Int
Accept negative and positive integers.
Definition: EditBox.hpp:81