TextField.hpp
1 /*
2  * Copyright © 2013 mathdu07
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef TEXTFIELD_H_
19 #define TEXTFIELD_H_
20 
21 #include <SFML/UI/Focusable.hpp>
22 #include <SFML/UI/IText.hpp>
23 #include <SFML/UI/Model/TextFieldModel.hpp>
24 
25 namespace sf
26 {
27 namespace ui
28 {
29 
30 class TextField: public Focusable, public IText
31 {
32 public:
33  //CONSTRUCTORS/DESTRUCTORS -------------------------------------------
40  TextField();
41 
52  TextField(sf::Texture const &texture, sf::Texture const &textureFocused, sf::String const &text = "");
53 
64  TextField(sf::Texture const &texture, sf::Texture const &textureFocused, sf::Font const &font, sf::String text = "");
65 
66  virtual ~TextField();
67  //--------------------------------------------------------------------
68 
69  //METHODS ------------------------------------------------------------
70 protected:
76  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
77 
84  virtual void updateCoord();
85 
97  virtual bool insertText(sf::Uint32 text, unsigned int index);
98 
109  virtual bool deleteText(unsigned int index);
110 
111 public:
119  virtual void updateEvent(sf::Event const &event);
120 
129  virtual void updateFixed(sf::Time delta);
130  //--------------------------------------------------------------------
131 
132  //GETTERS/SETTERS ----------------------------------------------------
133 public:
139  const TextFieldModel* getModel() const;
140 
148  void setModel(TextFieldModel* model);
149 
155  virtual const sf::String& getText() const;
156 
165  virtual void setText(sf::String const &text);
166 
172  virtual const sf::Font* getFont() const;
173 
179  virtual void setFont(sf::Font const &font);
180 
186  virtual unsigned int getFontSize() const;
187 
195  virtual void setFontSize(unsigned int size);
196 
202  virtual sf::Color getFontColor() const;
203 
211  virtual void setFontColor(sf::Color color);
212 
218  unsigned int getCursor() const;
219 
227  void setCursor(unsigned int cursor);
228 
235  int getMaxLength() const;
236 
243  void setMaxLength(int maxLength);
244 
250  bool canBeEmpty() const;
251 
257  void setCanBeEmpty(bool canBeEmpty);
258  //--------------------------------------------------------------------
259 
260  //FIELDS -------------------------------------------------------------
261 protected:
262  sf::Text m_text;
263  TextFieldModel *m_model;
264  unsigned int m_cursor;
265  sf::RectangleShape m_cursorShape;
266  int m_maxLength;
267  bool m_canBeEmpty;
268  //--------------------------------------------------------------------
269 };
270 
271 } /* namespace ui */
272 } /* namespace sf */
273 #endif /* TEXTFIELD_H_ */
274 
void setCursor(unsigned int cursor)
Sets the cursor's position.
void setModel(TextFieldModel *model)
Sets the model of the text field.
The base class for all UI components that can be focused.
Definition: Focusable.hpp:28
virtual void setFont(sf::Font const &font)
Sets the font of the component's text.
void setMaxLength(int maxLength)
Sets the max length that the string can have -1 means the string has no length limit.
int getMaxLength() const
void setCanBeEmpty(bool canBeEmpty)
Sets if the string can be empty.
virtual unsigned int getFontSize() const
TextField()
Creates an empty text field without texture, font ...
A text field which stores a text.
Definition: TextField.hpp:30
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const
Draw the component to the render target.
virtual void updateFixed(sf::Time delta)
Update the component with the main loop's frequency Can be useful for animation, or time's needed stu...
const TextFieldModel * getModel() const
virtual sf::Color getFontColor() const
virtual void updateCoord()
Called when the component need update its geometry Inherit when you need to update some sprite...
virtual void setFontColor(sf::Color color)
Sets the color's font of the text.
virtual void updateEvent(sf::Event const &event)
Update the component each time an event has been polled.
unsigned int getCursor() const
virtual const sf::Font * getFont() const
virtual const sf::String & getText() const
The abstract class that defines what can be typed into the text field.
bool canBeEmpty() const
An interface that allow component to have a text.
Definition: IText.hpp:28
virtual void setText(sf::String const &text)
Sets the text's string of the component Don't forget to set the font.
virtual bool insertText(sf::Uint32 text, unsigned int index)
Insert a char at the given index Trigger a TextEnteredEvent.
virtual bool deleteText(unsigned int index)
Delete the char at the given index Trigger a TextDeletedEvent.
virtual void setFontSize(unsigned int size)
Set the font's size of the component.