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 {
57  class TGUI_API EditBox : public ClickableWidget
58  {
59  public:
60 
61  typedef std::shared_ptr<EditBox> Ptr;
62  typedef std::shared_ptr<const EditBox> ConstPtr;
63 
64 
68  enum class Alignment
69  {
71  Left,
72 
74  Center,
75 
77  Right
78  };
79 
80 
84  struct Validator
85  {
86  static TGUI_API std::string All;
87  static TGUI_API std::string Int;
88  static TGUI_API std::string UInt;
89  static TGUI_API std::string Float;
90  };
91 
92 
94  // Default constructor
96  EditBox();
97 
98 
105  static EditBox::Ptr create();
106 
107 
116  static EditBox::Ptr copy(EditBox::ConstPtr editBox);
117 
118 
126  {
127  return aurora::downcast<EditBoxRenderer*>(m_renderer.get());
128  }
129 
130 
137  void setSize(const Layout2d& size) override;
139 
140 
148  virtual void enable() override;
149 
150 
158  virtual void disable() override;
159 
160 
174  void setText(const sf::String& text);
175 
176 
183  const sf::String& getText() const;
184 
185 
194  void setDefaultText(const sf::String& text);
195 
196 
205  const sf::String& getDefaultText() const;
206 
207 
217  void selectText(std::size_t start = 0, std::size_t length = sf::String::InvalidPos);
218 
219 
226  sf::String getSelectedText() const;
227 
228 
236  void setTextSize(unsigned int textSize);
237 
238 
245  unsigned int getTextSize() const;
246 
247 
260  void setPasswordCharacter(char passwordChar);
261 
262 
270  char getPasswordCharacter() const;
271 
272 
281  void setMaximumCharacters(unsigned int maxChars);
282 
283 
293  unsigned int getMaximumCharacters() const;
294 
295 
302  void setAlignment(Alignment alignment);
303 
304 
311  Alignment getAlignment() const;
312 
313 
323  void limitTextWidth(bool limitWidth = true);
324 
325 
332  bool isTextWidthLimited() const;
333 
334 
341  void setCaretPosition(std::size_t charactersBeforeCaret);
342 
343 
350  std::size_t getCaretPosition() const;
351 
352 
367  void setInputValidator(const std::string& regex = ".*");
368 
369 
376  const std::string& getInputValidator() const;
377 
378 
382  virtual void leftMousePressed(sf::Vector2f pos) override;
383 
387  virtual void mouseMoved(sf::Vector2f pos) override;
388 
392  virtual void keyPressed(const sf::Event::KeyEvent& event) override;
393 
397  virtual void textEntered(sf::Uint32 Key) override;
398 
402  virtual void widgetFocused() override;
403 
407  virtual void widgetUnfocused() override;
408 
409 
417  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
418 
419 
421  protected:
422 
429  virtual void rendererChanged(const std::string& property) override;
430 
431 
433  // Returns the size without the borders
435  sf::Vector2f getInnerSize() const;
436 
437 
439  // Returns the width of the edit box minus the padding.
441  float getVisibleEditBoxWidth() const;
442 
443 
445  // This function will search after which character the caret should be placed. It will not change the caret position.
447  std::size_t findCaretPosition(float posX);
448 
449 
451  // Removes the selected characters. This function is called when pressing backspace, delete or a letter while there were
452  // some characters selected.
454  void deleteSelectedCharacters();
455 
456 
458  // Recalculates the position of the texts.
460  void recalculateTextPositions();
461 
462 
464  // This function is called every frame with the time passed since the last frame.
466  virtual void update(sf::Time elapsedTime) override;
467 
468 
470  // Makes a copy of the widget
472  virtual Widget::Ptr clone() const override
473  {
474  return std::make_shared<EditBox>(*this);
475  }
476 
477 
479  protected:
480 
481  // Is the caret visible or not?
482  bool m_caretVisible = true;
483 
484  // When this boolean is true then you can no longer add text when the EditBox is full.
485  // Changing it to false will allow you to scroll the text (default).
486  // You can change the boolean with the limitTextWidth(bool) function.
487  bool m_limitTextWidth = false;
488 
489  // The text inside the edit box
490  sf::String m_text;
491 
492  std::string m_regexString = ".*";
493  std::regex m_regex = std::regex{m_regexString};
494 
495  // This will store the size of the text ( 0 to auto size )
496  unsigned int m_textSize = 0;
497 
498  // The text alignment
499  Alignment m_textAlignment = Alignment::Left;
500 
501  // The selection
502  std::size_t m_selChars = 0;
503  std::size_t m_selStart = 0;
504  std::size_t m_selEnd = 0;
505 
506  // The password character
507  char m_passwordChar = '\0';
508 
509  // The maximum allowed characters.
510  // Zero by default, meaning no limit.
511  unsigned int m_maxChars = 0;
512 
513  // When the text width is not limited, you can scroll the edit box and only a part will be visible.
514  unsigned int m_textCropPosition = 0;
515 
516  // The rectangle behind the selected text
517  FloatRect m_selectedTextBackground;
518 
519  // The blinking caret
520  FloatRect m_caret;
521 
522  // Is there a possibility that the user is going to double click?
523  bool m_possibleDoubleClick = false;
524 
525  // We need three texts for drawing + one for the default text + one more for calculations.
526  Text m_textBeforeSelection;
527  Text m_textSelection;
528  Text m_textAfterSelection;
529  Text m_defaultText;
530  Text m_textFull;
531 
532  Sprite m_sprite;
533  Sprite m_spriteHover;
534  Sprite m_spriteDisabled;
535  Sprite m_spriteFocused;
536 
537  // Cached renderer properties
538  Borders m_bordersCached;
539  Padding m_paddingCached;
540  Color m_borderColorCached;
541  Color m_borderColorHoverCached;
542  Color m_borderColorDisabledCached;
543  Color m_backgroundColorCached;
544  Color m_backgroundColorHoverCached;
545  Color m_backgroundColorDisabledCached;
546  Color m_caretColorCached;
547  Color m_caretColorHoverCached;
548  Color m_caretColorDisabledCached;
549  Color m_selectedTextBackgroundColorCached;
550 
551 
553  };
554 
556 }
557 
559 
560 #endif // TGUI_EDIT_BOX_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
Definition: Sprite.hpp:40
Wrapper for colors.
Definition: Color.hpp:46
EditBoxRenderer * getRenderer() const
Returns the renderer, which gives access to functions that determine how the widget is displayed...
Definition: EditBox.hpp:125
Class to store the position or size of a widget.
Definition: Layout.hpp:263
static TGUI_API std::string Int
Accept negative and positive integers.
Definition: EditBox.hpp:87
Predefined input validators.
Definition: EditBox.hpp:84
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:62
Definition: Text.hpp:41
Clickable widget.
Definition: ClickableWidget.hpp:55
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:76
static TGUI_API std::string UInt
Accept only positive integers.
Definition: EditBox.hpp:88
static TGUI_API std::string Float
Accept decimal numbers.
Definition: EditBox.hpp:89
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:61
Definition: EditBoxRenderer.hpp:36
static TGUI_API std::string All
Accept any input.
Definition: EditBox.hpp:86
Edit box widget.
Definition: EditBox.hpp:57
Definition: Outline.hpp:37
Alignment
The text alignment.
Definition: EditBox.hpp:68
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don&#39;t know its exact type.
Definition: EditBox.hpp:472
Definition: FloatRect.hpp:36