KeyField.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 KEYFIELD_H_
19 #define KEYFIELD_H_
20 
21 #include <SFML/UI/Focusable.hpp>
22 #include <SFML/UI/IText.hpp>
23 #include <set>
24 
25 namespace sf
26 {
27 namespace ui
28 {
29 
30 class KeyField : public Focusable, public IText
31 {
32 public:
33  //CONSTRUCTORS/DESTRUCTORS -------------------------------------------
42  KeyField(sf::Keyboard::Key key);
43 
54  KeyField(sf::Texture const &texture, sf::Texture const &textureFocused, sf::Font const &font, sf::Keyboard::Key key);
55 
56 
57  virtual ~KeyField();
58  //--------------------------------------------------------------------
59 
60  //METHODS ------------------------------------------------------------
61 private:
67  void setKeys();
68 
69 protected:
75  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
76 
83  virtual void updateCoord();
84 
85 public:
93  virtual void updateEvent(sf::Event const &event);
94 
103  virtual void updateFixed(sf::Time delta);
104  //--------------------------------------------------------------------
105 
106  //GETTERS/SETTERS ----------------------------------------------------
112  const std::set<sf::Keyboard::Key>& getAllowedKeys() const;
113 
121  void addKey(sf::Keyboard::Key key);
122 
130  bool isKeyAllowed(sf::Keyboard::Key key) const;
131 
137  void removeKey(sf::Keyboard::Key key);
138 
144  virtual const sf::String& getText() const;
145 
146 private:
155  virtual void setText(sf::String const &text);
156 
157 public:
163  virtual const sf::Font* getFont() const;
164 
170  virtual void setFont(sf::Font const &font);
171 
177  virtual unsigned int getFontSize() const;
178 
186  virtual void setFontSize(unsigned int size);
187 
193  virtual sf::Color getFontColor() const;
194 
202  virtual void setFontColor(sf::Color color);
203  //--------------------------------------------------------------------
204 
205  //FIELDS -------------------------------------------------------------
206 protected:
207  sf::Text m_text;
208  sf::Keyboard::Key m_key;
209  std::set<sf::Keyboard::Key> m_keysAllowed;
210  //--------------------------------------------------------------------
211 };
212 
213 } /* namespace ui */
214 } /* namespace sf */
215 #endif /* KEYFIELD_H_ */
216 
virtual unsigned int getFontSize() const
virtual void setFontColor(sf::Color color)
Sets the color's font of the text.
The base class for all UI components that can be focused.
Definition: Focusable.hpp:28
bool isKeyAllowed(sf::Keyboard::Key key) const
const std::set< sf::Keyboard::Key > & getAllowedKeys() const
virtual void updateEvent(sf::Event const &event)
Update the component each time an event has been polled.
virtual const sf::Font * getFont() const
void addKey(sf::Keyboard::Key key)
Add a key to the allowed keys set.
virtual void setFont(sf::Font const &font)
Sets the font of the component's text.
virtual void updateFixed(sf::Time delta)
Update the component with the main loop's frequency.
void removeKey(sf::Keyboard::Key key)
Remove the key from the allowed keys set.
An interface that allow component to have a text.
Definition: IText.hpp:28
KeyField(sf::Keyboard::Key key)
Creates a key field without textures, which store a (keyboard's) key Do not forget to set the texture...
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const
Draw the component to the render target.
virtual void setFontSize(unsigned int size)
Set the font's size of the component.
virtual void updateCoord()
Called when the component need update its geometry Inherit when you need to update some sprite...
virtual sf::Color getFontColor() const
virtual const sf::String & getText() const
A field that stores a keyboard's key Not supported on mobile platforms.
Definition: KeyField.hpp:30