TGUI  0.8.0
EditBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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 
400  void setFocused(bool focused) override;
401 
402 
407  bool mouseOnWidget(Vector2f pos) const override;
408 
409 
413  void leftMousePressed(Vector2f pos) override;
414 
418  void mouseMoved(Vector2f pos) override;
419 
423  void keyPressed(const sf::Event::KeyEvent& event) override;
424 
428  void textEntered(std::uint32_t Key) override;
429 
430 
438  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
439 
440 
442  protected:
443 
453  Signal& getSignal(std::string signalName) override;
454 
455 
462  void rendererChanged(const std::string& property) override;
463 
464 
468  std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
469 
470 
474  void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
475 
476 
478  // Returns the total width that the text is going to take
480  float getFullTextWidth() const;
481 
482 
484  // Returns the size without the borders
486  Vector2f getInnerSize() const;
487 
488 
490  // Returns the width of the edit box minus the padding.
492  float getVisibleEditBoxWidth() const;
493 
494 
496  // This function will search after which character the caret should be placed. It will not change the caret position.
498  std::size_t findCaretPosition(float posX);
499 
500 
502  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
503  // some characters selected.
505  void deleteSelectedCharacters();
506 
507 
509  // Recalculates the position of the texts.
511  void recalculateTextPositions();
512 
513 
515  // Updates the internal texts after SelStart or SelEnd changed.
517  void updateSelection();
518 
519 
521  // Update the color of the Text objects
523  void updateTextColor();
524 
525 
527  // This function is called every frame with the time passed since the last frame.
529  void update(sf::Time elapsedTime) override;
530 
531 
533  // Makes a copy of the widget
535  Widget::Ptr clone() const override
536  {
537  return std::make_shared<EditBox>(*this);
538  }
539 
540 
542  public:
543 
544  SignalString onTextChange = {"TextChanged"};
545  SignalString onReturnKeyPress = {"ReturnKeyPressed"};
546 
547 
549  protected:
550 
551  // Is the caret visible or not?
552  bool m_caretVisible = true;
553 
554  // When this boolean is true then you can no longer add text when the EditBox is full.
555  // Changing it to false will allow you to scroll the text (default).
556  // You can change the boolean with the limitTextWidth(bool) function.
557  bool m_limitTextWidth = false;
558 
559  bool m_readOnly = false;
560 
561  // The text inside the edit box
562  sf::String m_text;
563 
564  std::string m_regexString = ".*";
565  std::regex m_regex = std::regex{m_regexString};
566 
567  // This will store the size of the text ( 0 to auto size )
568  unsigned int m_textSize = 0;
569 
570  // The text alignment
571  Alignment m_textAlignment = Alignment::Left;
572 
573  // The selection
574  std::size_t m_selChars = 0;
575  std::size_t m_selStart = 0;
576  std::size_t m_selEnd = 0;
577 
578  // The password character
579  char m_passwordChar = '\0';
580 
581  // The maximum allowed characters.
582  // Zero by default, meaning no limit.
583  unsigned int m_maxChars = 0;
584 
585  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
586  unsigned int m_textCropPosition = 0;
587 
588  // The rectangle behind the selected text
589  FloatRect m_selectedTextBackground;
590 
591  // The blinking caret
592  FloatRect m_caret = {{0, 0, 1, 0}};
593 
594  // Is there a possibility that the user is going to double click?
595  bool m_possibleDoubleClick = false;
596 
597  // We need three texts for drawing + one for the default text + one more for calculations.
598  Text m_textBeforeSelection;
599  Text m_textSelection;
600  Text m_textAfterSelection;
601  Text m_defaultText;
602  Text m_textFull;
603 
604  Sprite m_sprite;
605  Sprite m_spriteHover;
606  Sprite m_spriteDisabled;
607  Sprite m_spriteFocused;
608 
609  // Cached renderer properties
610  Borders m_bordersCached;
611  Padding m_paddingCached;
612  Color m_borderColorCached;
613  Color m_borderColorHoverCached;
614  Color m_borderColorDisabledCached;
615  Color m_borderColorFocusedCached;
616  Color m_backgroundColorCached;
617  Color m_backgroundColorHoverCached;
618  Color m_backgroundColorDisabledCached;
619  Color m_backgroundColorFocusedCached;
620  Color m_caretColorCached;
621  Color m_caretColorHoverCached;
622  Color m_caretColorFocusedCached;
623  Color m_selectedTextBackgroundColorCached;
624 
625 
627  };
628 
630 }
631 
633 
634 #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:243
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&#39;t know its exact type.
Definition: EditBox.hpp:535
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