TGUI  0.7.8
EditBox.hpp
1
2//
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
35namespace 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
102
103
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
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
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 // Updates the internal texts after SelStart or SelEnd changed.
529 void updateSelection();
530
531
533 // Makes a copy of the widget
535 virtual Widget::Ptr clone() const override
536 {
537 return std::make_shared<EditBox>(*this);
538 }
539
540
542 // This function is called every frame with the time passed since the last frame.
544 virtual void update(sf::Time elapsedTime) override;
545
546
548 // Draws the widget on the render target.
550 virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
551
552
554 protected:
555
556 // Is the caret visible or not?
557 bool m_caretVisible = true;
558
559 // When this boolean is true then you can no longer add text when the EditBox is full.
560 // Changing it to false will allow you to scroll the text (default).
561 // You can change the boolean with the limitTextWidth(bool) function.
562 bool m_limitTextWidth = false;
563
564 // The text inside the edit box
565 sf::String m_displayedText;
566 sf::String m_text;
567
568 std::string m_regexString = ".*";
569 std::regex m_regex = std::regex{m_regexString};
570
571 // This will store the size of the text ( 0 to auto size )
572 unsigned int m_textSize = 0;
573
574 // The text alignment
575 Alignment m_textAlignment = Alignment::Left;
576
577 // The selection
578 std::size_t m_selChars = 0;
579 std::size_t m_selStart = 0;
580 std::size_t m_selEnd = 0;
581
582 // The password character
583 char m_passwordChar = '\0';
584
585 // The maximum allowed characters.
586 // Zero by default, meaning no limit.
587 unsigned int m_maxChars = 0;
588
589 // When the text width is not limited, you can scroll the edit box and only a part will be visible.
590 unsigned int m_textCropPosition = 0;
591
592 // The rectangle behind the selected text
593 sf::RectangleShape m_selectedTextBackground;
594
595 // The blinking caret
596 sf::RectangleShape m_caret;
597
598 // We need three SFML texts to draw our text, and one more for calculations.
599 sf::Text m_textBeforeSelection;
600 sf::Text m_textSelection;
601 sf::Text m_textAfterSelection;
602 sf::Text m_textFull;
603 sf::Text m_defaultText;
604
605 // Is there a possibility that the user is going to double click?
606 bool m_possibleDoubleClick = false;
607
608
609 friend class EditBoxRenderer;
610
612
613 };
614
615
617
618 class TGUI_API EditBoxRenderer : public WidgetRenderer, public WidgetBorders, public WidgetPadding
619 {
620 public:
621
628 EditBoxRenderer(EditBox* editBox) : m_editBox{editBox} {}
629
630
640 virtual void setProperty(std::string property, const std::string& value) override;
641
642
653 virtual void setProperty(std::string property, ObjectConverter&& value) override;
654
655
665 virtual ObjectConverter getProperty(std::string property) const override;
666
667
674 virtual std::map<std::string, ObjectConverter> getPropertyValuePairs() const override;
675
676
689 virtual void setPadding(const Padding& padding) override;
691
692
699 void setCaretWidth(float width);
700
701
708 void setTextColor(const Color& textColor);
709
710
717 void setSelectedTextColor(const Color& selectedTextColor);
718
719
726 void setSelectedTextBackgroundColor(const Color& selectedTextBackgroundColor);
727
728
735 void setDefaultTextColor(const Color& defaultTextColor);
736
737
751 void setBackgroundColor(const Color& color);
752
753
762 void setBackgroundColorNormal(const Color& color);
763
764
773 void setBackgroundColorHover(const Color& color);
774
775
782 void setCaretColor(const Color& caretColor);
783
784
791 void setBorderColor(const Color& color);
792
793
803 void setNormalTexture(const Texture& texture);
804
805
814 void setHoverTexture(const Texture& texture);
815
816
825 void setFocusTexture(const Texture& texture);
826
827
840 void setDefaultTextStyle(sf::Uint32 style);
841
842
844 // Draws the widget on the render target.
846 void draw(sf::RenderTarget& target, sf::RenderStates states) const;
847
848
850 private:
851
853 // Returns the padding, which is possibly scaled with the background image.
855 Padding getScaledPadding() const;
856
857
859 // Makes a copy of the renderer
861 virtual std::shared_ptr<WidgetRenderer> clone(Widget* widget) override;
862
863
865 protected:
866
867 EditBox* m_editBox;
868
869 sf::Color m_textColor;
870 sf::Color m_selectedTextColor;
871 sf::Color m_selectedTextBackgroundColor;
872 sf::Color m_defaultTextColor;
873 sf::Color m_backgroundColorNormal;
874 sf::Color m_backgroundColorHover;
875 sf::Color m_caretColor;
876 sf::Color m_borderColor;
877
878 Texture m_textureNormal;
879 Texture m_textureHover;
880 Texture m_textureFocused;
881
882 friend class EditBox;
883
885 };
886
888
889}
891
892#endif // TGUI_EDIT_BOX_HPP
Definition: Borders.hpp:38
Clickable widget.
Definition: ClickableWidget.hpp:56
Implicit converter for colors.
Definition: Color.hpp:40
Definition: EditBox.hpp:619
void setTextColor(const Color &textColor)
Set the text color that will be used inside the edit box.
void setDefaultTextStyle(sf::Uint32 style)
Changes the text style of the default text (the text drawn when the edit box is empty)
void setHoverTexture(const Texture &texture)
Change the image that is displayed when the mouse is located on top of the edit box.
virtual void setProperty(std::string property, const std::string &value) override
Change a property of the renderer.
void setFocusTexture(const Texture &texture)
Change the image that is drawn on top of the edit box image when the edit box is focused.
void setSelectedTextColor(const Color &selectedTextColor)
Set the text color of the selected text that will be used inside the edit box.
virtual void setPadding(const Padding &padding) override
Changes the padding of the edit box.
void setBackgroundColor(const Color &color)
Changes the color of the background.
virtual void setProperty(std::string property, ObjectConverter &&value) override
Change a property of the renderer.
void setBackgroundColorNormal(const Color &color)
Changes the color of the background in the normal state (mouse not on edit box).
virtual std::map< std::string, ObjectConverter > getPropertyValuePairs() const override
Get a map with all properties and their values.
void setCaretColor(const Color &caretColor)
Set the color that will be used inside the edit box for the blinking caret.
void setCaretWidth(float width)
This will change the width of the caret.
void setBackgroundColorHover(const Color &color)
Changes the color of the background in the hover state (mouse on edit box, but not pressed).
void setDefaultTextColor(const Color &defaultTextColor)
Set the color of the default text that can optionally be displayed when the edit box is empty.
void setNormalTexture(const Texture &texture)
Change the image that is displayed when the mouse is not on the edit box.
void setSelectedTextBackgroundColor(const Color &selectedTextBackgroundColor)
Set the background color of the selected text that will be used inside the edit box.
void setBorderColor(const Color &color)
Changes the color of the borders.
EditBoxRenderer(EditBox *editBox)
Constructor.
Definition: EditBox.hpp:628
virtual ObjectConverter getProperty(std::string property) const override
Retrieve the value of a certain property.
Edit box widget.
Definition: EditBox.hpp:55
static EditBox::Ptr create()
Creates a new edit box widget.
void setPasswordCharacter(char passwordChar)
Sets a password character.
sf::String getText() const
Returns the text inside the edit box. This text is not affected by the password character.
Definition: EditBox.hpp:197
virtual sf::Vector2f getFullSize() const override
Returns the full size of the edit box.
bool isTextWidthLimited()
Check if the text width is limited to the size of the edit box.
Definition: EditBox.hpp:352
void setCaretPosition(std::size_t charactersBeforeCaret)
Sets the blinking caret to after a specific character.
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
Alignment
The text alignment.
Definition: EditBox.hpp:66
Alignment getAlignment()
Get the current text alignment.
Definition: EditBox.hpp:328
void setMaximumCharacters(unsigned int maxChars)
Change the character limit.
const std::string & getInputValidator()
Returns the regex to which the text is matched.
virtual sf::Vector2f getWidgetOffset() const override
Returns the distance between the position where the widget is drawn and where the widget is placed.
void setInputValidator(const std::string &regex=".*")
Define how the text input should look like.
void selectText()
Selects the entire text in the edit box.
void limitTextWidth(bool limitWidth=true)
Should the text width be limited or should you be able to type even if the edit box is full?
std::size_t getCaretPosition() const
Returns after which character the blinking cursor is currently located.
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
sf::String getSelectedText() const
Returns the text that you currently have selected. This text is not affected by the password characte...
void setText(const sf::String &text)
Changes the text of the editbox.
virtual void setFont(const Font &font) override
Changes the font of the text in the widget.
void setAlignment(Alignment alignment)
Change the text alignment.
unsigned int getMaximumCharacters() const
Returns the character limit.
Definition: EditBox.hpp:307
std::shared_ptr< const EditBox > ConstPtr
Shared constant widget pointer.
Definition: EditBox.hpp:59
static EditBox::Ptr copy(EditBox::ConstPtr editBox)
Makes a copy of another edit box.
virtual void setPosition(const Layout2d &position) override
Set the position of the widget.
virtual void setOpacity(float opacity) override
Changes the opacity of the widget.
void setDefaultText(const sf::String &text)
Changes the default text of the editbox. This is the text drawn when the edit box is empty.
virtual void reload(const std::string &primary="", const std::string &secondary="", bool force=false) override
Reload the widget.
virtual Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
Definition: EditBox.hpp:535
void setSize(const Layout2d &size) override
Changes the size of the edit box.
std::shared_ptr< EditBox > Ptr
Shared widget pointer.
Definition: EditBox.hpp:58
void setCaretWidth(float width)
This will change the width of the caret.
float getCaretWidth()
Returns the width of the caret.
Definition: EditBox.hpp:393
void setTextSize(unsigned int textSize)
Changes the character size of the text.
unsigned int getTextSize() const
Returns the character size of the text.
Definition: EditBox.hpp:253
char getPasswordCharacter() const
Returns the password character.
Definition: EditBox.hpp:281
Definition: Font.hpp:38
Class to store the position or size of a widget.
Definition: Layout.hpp:255
Implicit converter for settable properties.
Definition: ObjectConverter.hpp:43
Definition: Texture.hpp:45
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
virtual void setPosition(const Layout2d &position)
set the position of the widget
Parent class for every widget that has borders.
Definition: Borders.hpp:137
Parent class for every widget that has padding.
Definition: Borders.hpp:211
virtual void setPadding(const Padding &padding)
Changes the size of the padding.
Definition: Borders.hpp:223
Base class for all renderer classes.
Definition: Widget.hpp:683
The parent class for every widget.
Definition: Widget.hpp:72
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition: Widget.hpp:75
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:34
Predefined input validators.
Definition: EditBox.hpp:82
static TGUI_API std::string Float
Accept decimal numbers.
Definition: EditBox.hpp:85
static TGUI_API std::string UInt
Accept only positive integers.
Definition: EditBox.hpp:84
static TGUI_API std::string Int
Accept negative and positive integers.
Definition: EditBox.hpp:83