TGUI  0.8-dev
EditBox.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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  static TGUI_API std::string All;
75  static TGUI_API std::string Int;
76  static TGUI_API std::string UInt;
77  static TGUI_API std::string 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 enable() override;
141 
142 
150  void disable() override;
151 
152 
166  void setText(const sf::String& text);
167 
168 
175  const sf::String& getText() const;
176 
177 
186  void setDefaultText(const sf::String& text);
187 
188 
197  const sf::String& getDefaultText() const;
198 
199 
209  void selectText(std::size_t start = 0, std::size_t length = sf::String::InvalidPos);
210 
211 
218  sf::String getSelectedText() const;
219 
220 
228  void setTextSize(unsigned int textSize);
229 
230 
237  unsigned int getTextSize() const;
238 
239 
252  void setPasswordCharacter(char passwordChar);
253 
254 
262  char getPasswordCharacter() const;
263 
264 
273  void setMaximumCharacters(unsigned int maxChars);
274 
275 
285  unsigned int getMaximumCharacters() const;
286 
287 
294  void setAlignment(Alignment alignment);
295 
296 
303  Alignment getAlignment() const;
304 
305 
315  void limitTextWidth(bool limitWidth = true);
316 
317 
324  bool isTextWidthLimited() const;
325 
326 
336  void setReadOnly(bool readOnly = true);
337 
338 
348  bool isReadOnly() const;
349 
350 
357  void setCaretPosition(std::size_t charactersBeforeCaret);
358 
359 
366  std::size_t getCaretPosition() const;
367 
368 
383  void setInputValidator(const std::string& regex = ".*");
384 
385 
392  const std::string& getInputValidator() const;
393 
394 
398  void leftMousePressed(sf::Vector2f pos) override;
399 
403  void mouseMoved(sf::Vector2f pos) override;
404 
408  void keyPressed(const sf::Event::KeyEvent& event) override;
409 
413  void textEntered(sf::Uint32 Key) override;
414 
418  void widgetFocused() override;
419 
423  void widgetUnfocused() override;
424 
425 
433  void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
434 
435 
437  protected:
438 
448  Signal& getSignal(std::string signalName) override;
449 
450 
457  void rendererChanged(const std::string& property) override;
458 
459 
461  // Returns the size without the borders
463  sf::Vector2f getInnerSize() const;
464 
465 
467  // Returns the width of the edit box minus the padding.
469  float getVisibleEditBoxWidth() const;
470 
471 
473  // This function will search after which character the caret should be placed. It will not change the caret position.
475  std::size_t findCaretPosition(float posX);
476 
477 
479  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
480  // some characters selected.
482  void deleteSelectedCharacters();
483 
484 
486  // Recalculates the position of the texts.
488  void recalculateTextPositions();
489 
490 
492  // Updates the internal texts after SelStart or SelEnd changed.
494  void updateSelection();
495 
496 
498  // This function is called every frame with the time passed since the last frame.
500  void update(sf::Time elapsedTime) override;
501 
502 
504  // Makes a copy of the widget
506  Widget::Ptr clone() const override
507  {
508  return std::make_shared<EditBox>(*this);
509  }
510 
511 
513  public:
514 
515  SignalString onTextChange = {"TextChanged"};
516  SignalString onReturnKeyPress = {"ReturnKeyPressed"};
517 
518 
520  protected:
521 
522  // Is the caret visible or not?
523  bool m_caretVisible = true;
524 
525  // When this boolean is true then you can no longer add text when the EditBox is full.
526  // Changing it to false will allow you to scroll the text (default).
527  // You can change the boolean with the limitTextWidth(bool) function.
528  bool m_limitTextWidth = false;
529 
530  bool m_readOnly = false;
531 
532  // The text inside the edit box
533  sf::String m_text;
534 
535  std::string m_regexString = ".*";
536  std::regex m_regex = std::regex{m_regexString};
537 
538  // This will store the size of the text ( 0 to auto size )
539  unsigned int m_textSize = 0;
540 
541  // The text alignment
542  Alignment m_textAlignment = Alignment::Left;
543 
544  // The selection
545  std::size_t m_selChars = 0;
546  std::size_t m_selStart = 0;
547  std::size_t m_selEnd = 0;
548 
549  // The password character
550  char m_passwordChar = '\0';
551 
552  // The maximum allowed characters.
553  // Zero by default, meaning no limit.
554  unsigned int m_maxChars = 0;
555 
556  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
557  unsigned int m_textCropPosition = 0;
558 
559  // The rectangle behind the selected text
560  FloatRect m_selectedTextBackground;
561 
562  // The blinking caret
563  FloatRect m_caret;
564 
565  // Is there a possibility that the user is going to double click?
566  bool m_possibleDoubleClick = false;
567 
568  // We need three texts for drawing + one for the default text + one more for calculations.
569  Text m_textBeforeSelection;
570  Text m_textSelection;
571  Text m_textAfterSelection;
572  Text m_defaultText;
573  Text m_textFull;
574 
575  Sprite m_sprite;
576  Sprite m_spriteHover;
577  Sprite m_spriteDisabled;
578  Sprite m_spriteFocused;
579 
580  // Cached renderer properties
581  Borders m_bordersCached;
582  Padding m_paddingCached;
583  Color m_borderColorCached;
584  Color m_borderColorHoverCached;
585  Color m_borderColorDisabledCached;
586  Color m_backgroundColorCached;
587  Color m_backgroundColorHoverCached;
588  Color m_backgroundColorDisabledCached;
589  Color m_caretColorCached;
590  Color m_caretColorHoverCached;
591  Color m_caretColorDisabledCached;
592  Color m_selectedTextBackgroundColorCached;
593 
594 
596  };
597 
599 }
600 
602 
603 #endif // TGUI_EDIT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
Class to store the position or size of a widget.
Definition: Layout.hpp:243
static TGUI_API std::string Int
Accept negative and positive integers.
Definition: EditBox.hpp:75
Predefined input validators.
Definition: EditBox.hpp:72
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition: EditBox.hpp:50
Definition: Text.hpp:41
Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: EditBox.hpp:506
Clickable widget.
Definition: ClickableWidget.hpp:39
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:66
static TGUI_API std::string UInt
Accept only positive integers.
Definition: EditBox.hpp:76
static TGUI_API std::string Float
Accept decimal numbers.
Definition: EditBox.hpp:77
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
static TGUI_API std::string All
Accept any input.
Definition: EditBox.hpp:74
Edit box widget.
Definition: EditBox.hpp:45
Definition: Outline.hpp:38
Alignment
The text alignment.
Definition: EditBox.hpp:56
Definition: FloatRect.hpp:36