TGUI  0.7.4
EditBox.hpp
1 //
3 // TGUI - Texus's 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 
31 #include <regex>
32 
34 
35 namespace tgui
36 {
37  class EditBoxRenderer;
38 
54  class TGUI_API EditBox : public ClickableWidget
55  {
56  public:
57 
58  typedef std::shared_ptr<EditBox> Ptr;
59  typedef std::shared_ptr<const EditBox> ConstPtr;
60 
61 
65  enum class Alignment
66  {
68  Left,
69 
71  Center,
72 
74  Right
75  };
76 
77 
81  struct Validator
82  {
83  static TGUI_API std::string Int;
84  static TGUI_API std::string UInt;
85  static TGUI_API std::string Float;
86  };
87 
88 
90  // Default constructor
92  EditBox();
93 
94 
101  static EditBox::Ptr create();
102 
103 
112  static EditBox::Ptr copy(EditBox::ConstPtr editBox);
113 
114 
121  std::shared_ptr<EditBoxRenderer> getRenderer() const
122  {
123  return std::static_pointer_cast<EditBoxRenderer>(m_renderer);
124  }
125 
126 
139  virtual void setPosition(const Layout2d& position) override;
141 
142 
149  void setSize(const Layout2d& size) override;
151 
152 
161  virtual sf::Vector2f getFullSize() const override;
162 
163 
172  virtual void setFont(const Font& font) override;
173 
174 
188  void setText(const sf::String& text);
189 
190 
197  sf::String getText() const
198  {
199  return m_text;
200  }
201 
202 
211  void setDefaultText(const sf::String& text);
212 
213 
222  sf::String getDefaultText() const
223  {
224  return m_defaultText.getString();
225  }
226 
227 
234  sf::String getSelectedText() const;
235 
236 
244  void setTextSize(unsigned int textSize);
245 
246 
253  unsigned int getTextSize() const
254  {
255  return m_textFull.getCharacterSize();
256  }
257 
258 
271  void setPasswordCharacter(char passwordChar);
272 
273 
281  char getPasswordCharacter() const
282  {
283  return m_passwordChar;
284  }
285 
286 
295  void setMaximumCharacters(unsigned int maxChars);
296 
297 
307  unsigned int getMaximumCharacters() const
308  {
309  return m_maxChars;
310  }
311 
312 
319  void setAlignment(Alignment alignment);
320 
321 
329  {
330  return m_textAlignment;
331  }
332 
333 
343  void limitTextWidth(bool limitWidth = true);
344 
345 
353  {
354  return m_limitTextWidth;
355  }
356 
357 
366  void setCaretPosition(std::size_t charactersBeforeCaret);
367 
368 
375  std::size_t getCaretPosition() const;
376 
377 
384  void setCaretWidth(float width);
385 
386 
394  {
395  return m_caret.getSize().x;
396  }
397 
398 
413  void setInputValidator(const std::string& regex = ".*");
414 
415 
422  const std::string& getInputValidator();
423 
424 
429  void selectText();
430 
431 
438  virtual void setOpacity(float opacity) override;
439 
440 
449  virtual sf::Vector2f getWidgetOffset() const override;
450 
451 
455  virtual void leftMousePressed(float x, float y) override;
456 
460  virtual void mouseMoved(float x, float y) override;
461 
465  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
466 
470  virtual void textEntered(sf::Uint32 Key) override;
471 
475  virtual void widgetFocused() override;
476 
480  virtual void widgetUnfocused() override;
481 
482 
484  protected:
485 
498  virtual void reload(const std::string& primary = "", const std::string& secondary = "", bool force = false) override;
499 
500 
502  // Returns the width of the edit box minus the padding.
504  float getVisibleEditBoxWidth();
505 
506 
508  // This function will search after which character the caret should be placed. It will not change the caret position.
510  std::size_t findCaretPosition(float posX);
511 
512 
514  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
515  // some characters selected.
517  void deleteSelectedCharacters();
518 
519 
521  // Recalculates the position of the texts.
523  void recalculateTextPositions();
524 
525 
527  // Makes a copy of the widget
529  virtual Widget::Ptr clone() const override
530  {
531  return std::make_shared<EditBox>(*this);
532  }
533 
534 
536  // This function is called every frame with the time passed since the last frame.
538  virtual void update(sf::Time elapsedTime) override;
539 
540 
542  // Draws the widget on the render target.
544  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
545 
546 
548  protected:
549 
550  // Is the caret visible or not?
551  bool m_caretVisible = true;
552 
553  // When this boolean is true then you can no longer add text when the EditBox is full.
554  // Changing it to false will allow you to scroll the text (default).
555  // You can change the boolean with the limitTextWidth(bool) function.
556  bool m_limitTextWidth = false;
557 
558  // The text inside the edit box
559  sf::String m_displayedText;
560  sf::String m_text;
561 
562  std::string m_regexString = ".*";
563  std::regex m_regex = std::regex{m_regexString};
564 
565  // This will store the size of the text ( 0 to auto size )
566  unsigned int m_textSize = 0;
567 
568  // The text alignment
569  Alignment m_textAlignment = Alignment::Left;
570 
571  // The selection
572  std::size_t m_selChars = 0;
573  std::size_t m_selStart = 0;
574  std::size_t m_selEnd = 0;
575 
576  // The password character
577  char m_passwordChar = '\0';
578 
579  // The maximum allowed characters.
580  // Zero by default, meaning no limit.
581  unsigned int m_maxChars = 0;
582 
583  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
584  unsigned int m_textCropPosition = 0;
585 
586  // The rectangle behind the selected text
587  sf::RectangleShape m_selectedTextBackground;
588 
589  // The blinking caret
590  sf::RectangleShape m_caret;
591 
592  // We need three SFML texts to draw our text, and one more for calculations.
593  sf::Text m_textBeforeSelection;
594  sf::Text m_textSelection;
595  sf::Text m_textAfterSelection;
596  sf::Text m_textFull;
597  sf::Text m_defaultText;
598 
599  // Is there a possibility that the user is going to double click?
600  bool m_possibleDoubleClick = false;
601 
602 
603  friend class EditBoxRenderer;
604 
606 
607  };
608 
609 
611 
612  class TGUI_API EditBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
613  {
614  public:
615 
622  EditBoxRenderer(EditBox* editBox) : m_editBox{editBox} {}
623 
624 
634  virtual void setProperty(std::string property, const std::string& value) override;
635 
636 
647  virtual void setProperty(std::string property, ObjectConverter&& value) override;
648 
649 
659  virtual ObjectConverter getProperty(std::string property) const override;
660 
661 
668  virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
669 
670 
683  virtual void setPadding(const Padding& padding) override;
685 
686 
693  void setCaretWidth(float width);
694 
695 
702  void setTextColor(const Color& textColor);
703 
704 
711  void setSelectedTextColor(const Color& selectedTextColor);
712 
713 
720  void setSelectedTextBackgroundColor(const Color& selectedTextBackgroundColor);
721 
722 
729  void setDefaultTextColor(const Color& defaultTextColor);
730 
731 
745  void setBackgroundColor(const Color& color);
746 
747 
756  void setBackgroundColorNormal(const Color& color);
757 
758 
767  void setBackgroundColorHover(const Color& color);
768 
769 
776  void setCaretColor(const Color& caretColor);
777 
778 
785  void setBorderColor(const Color& color);
786 
787 
797  void setNormalTexture(const Texture& texture);
798 
799 
808  void setHoverTexture(const Texture& texture);
809 
810 
819  void setFocusTexture(const Texture& texture);
820 
821 
834  void setDefaultTextStyle(sf::Uint32 style);
835 
836 
838  // Draws the widget on the render target.
840  void draw(sf::RenderTarget& target, sf::RenderStates states) const;
841 
842 
844  private:
845 
847  // Returns the padding, which is possibly scaled with the background image.
849  Padding getScaledPadding() const;
850 
851 
853  // Makes a copy of the renderer
855  virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
856 
857 
859  protected:
860 
861  EditBox* m_editBox;
862 
863  sf::Color m_textColor;
864  sf::Color m_selectedTextColor;
865  sf::Color m_selectedTextBackgroundColor;
866  sf::Color m_defaultTextColor;
867  sf::Color m_backgroundColorNormal;
868  sf::Color m_backgroundColorHover;
869  sf::Color m_caretColor;
870  sf::Color m_borderColor;
871 
872  Texture m_textureNormal;
873  Texture m_textureHover;
874  Texture m_textureFocused;
875 
876  friend class EditBox;
877 
879  };
880 
882 
883 }
885 
886 #endif // TGUI_EDIT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
bool isTextWidthLimited()
Check if the text width is limited to the size of the edit box.
Definition: EditBox.hpp:352
EditBoxRenderer(EditBox *editBox)
Constructor.
Definition: EditBox.hpp:622
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Implicit converter for colors.
Definition: Color.hpp:39
char getPasswordCharacter() const
Returns the password character.
Definition: EditBox.hpp:281
sf::String getText() const
Returns the text inside the edit box. This text is not affected by the password character.
Definition: EditBox.hpp:197
unsigned int getTextSize() const
Returns the character size of the text.
Definition: EditBox.hpp:253
Definition: Font.hpp:37
Class to store the position or size of a widget.
Definition: Layout.hpp:262
The parent class for every widget.
Definition: Widget.hpp:71
static TGUI_API std::string Int
Accept negative and positive integers.
Definition: EditBox.hpp:83
Predefined input validators.
Definition: EditBox.hpp:81
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition: EditBox.hpp:59
Parent class for every widget that has padding.
Definition: Borders.hpp:210
virtual void setPosition(const Layout2d &position)
set the position of the widget
Clickable widget.
Definition: ClickableWidget.hpp:55
float getCaretWidth()
Returns the width of the caret.
Definition: EditBox.hpp:393
std::shared_ptr< EditBoxRenderer > getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: EditBox.hpp:121
Parent class for every widget that has borders.
Definition: Borders.hpp:136
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:42
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
unsigned int getMaximumCharacters() const
Returns the character limit.
Definition: EditBox.hpp:307
static TGUI_API std::string UInt
Accept only positive integers.
Definition: EditBox.hpp:84
static TGUI_API std::string Float
Accept decimal numbers.
Definition: EditBox.hpp:85
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:58
sf::String getDefaultText() const
Returns the default text of the edit box. This is the text drawn when the edit box is empty...
Definition: EditBox.hpp:222
Definition: Texture.hpp:44
Definition: EditBox.hpp:612
Edit box widget.
Definition: EditBox.hpp:54
Definition: Borders.hpp:37
Alignment
The text alignment.
Definition: EditBox.hpp:65
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: EditBox.hpp:529
Base class for all renderer classes.
Definition: Widget.hpp:682
Alignment getAlignment()
Get the current text alignment.
Definition: EditBox.hpp:328