TGUI  0.8.7
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 #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 
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) 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 
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 
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  void 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  public:
563 
564  SignalString onTextChange = {"TextChanged"};
565  SignalString onReturnKeyPress = {"ReturnKeyPressed"};
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  sf::String m_text;
583 
584  std::string m_regexString = ".*";
585  std::regex m_regex = std::regex{m_regexString};
586 
587  // The text alignment
588  Alignment m_textAlignment = Alignment::Left;
589 
590  // The selection
591  std::size_t m_selChars = 0;
592  std::size_t m_selStart = 0;
593  std::size_t m_selEnd = 0;
594 
595  // The password character
596  char m_passwordChar = '\0';
597 
598  // The maximum allowed characters.
599  // Zero by default, meaning no limit.
600  unsigned int m_maxChars = 0;
601 
602  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
603  unsigned int m_textCropPosition = 0;
604 
605  // The rectangle behind the selected text
606  FloatRect m_selectedTextBackground;
607 
608  // The blinking caret
609  FloatRect m_caret = {{0, 0, 1, 0}};
610 
611  // Is there a possibility that the user is going to double click?
612  bool m_possibleDoubleClick = false;
613 
614  // We need three texts for drawing + one for the default text + one more for calculations.
615  Text m_textBeforeSelection;
616  Text m_textSelection;
617  Text m_textAfterSelection;
618  Text m_defaultText;
619  Text m_textFull;
620  Text m_textSuffix;
621 
622  Sprite m_sprite;
623  Sprite m_spriteHover;
624  Sprite m_spriteDisabled;
625  Sprite m_spriteFocused;
626 
627  // Cached renderer properties
628  Borders m_bordersCached;
629  Padding m_paddingCached;
630  Color m_borderColorCached;
631  Color m_borderColorHoverCached;
632  Color m_borderColorDisabledCached;
633  Color m_borderColorFocusedCached;
634  Color m_backgroundColorCached;
635  Color m_backgroundColorHoverCached;
636  Color m_backgroundColorDisabledCached;
637  Color m_backgroundColorFocusedCached;
638  Color m_caretColorCached;
639  Color m_caretColorHoverCached;
640  Color m_caretColorFocusedCached;
641  Color m_selectedTextBackgroundColorCached;
642 
643 
645  };
646 
648 }
649 
651 
652 #endif // TGUI_EDIT_BOX_HPP
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:220
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:77
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
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:555
static const TGUI_API std::string UInt
Accept only positive integers.
Definition: EditBox.hpp:82
Edit box widget.
Definition: EditBox.hpp:45
Alignment
The text alignment.
Definition: EditBox.hpp:56
static const TGUI_API std::string All
Accept any input.
Definition: EditBox.hpp:80
static const TGUI_API std::string Int
Accept negative and positive integers.
Definition: EditBox.hpp:81
Definition: Vector2f.hpp:38
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
static const TGUI_API std::string Float
Accept decimal numbers.
Definition: EditBox.hpp:83
Clickable widget.
Definition: ClickableWidget.hpp:39