Button.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 BUTTON_H_
19 #define BUTTON_H_
20 
21 #include <SFML/UI/AbstractButton.hpp>
22 #include <SFML/UI/IText.hpp>
23 
24 namespace sf
25 {
26 namespace ui
27 {
28 
29 class Button : public AbstractButton, public IText
30 {
31 public:
32 
33  //CONSTRUCTORS/DESTRUCTORS -------------------------------------------
34 
41  : AbstractButton(),
42  m_textureFired(0), m_text(), m_clicked(false),
43  m_task(0)
44  {
45 
46  }
47 
58  Button(sf::Texture const &texture, sf::Texture const &textureFocused, sf::Texture const &textureFired)
59  : AbstractButton(texture, textureFocused),
60  m_textureFired(&textureFired), m_text(), m_clicked(false),
61  m_task(0)
62  {
63 
64  }
65 
75  template<typename C>
76  Button(C *object, void (C::*function)())
77  : AbstractButton(),
78  m_textureFired(0), m_text(), m_clicked(false),
79  m_task(new sf::priv::ThreadMemberFunc<C>(function, object))
80  {
81 
82  }
83 
96  template<typename C>
97  Button(C *object, void (C::*function)(), sf::Texture const &texture, sf::Texture const &textureFocused, sf::Texture const &textureFired)
98  : AbstractButton(texture, textureFocused),
99  m_textureFired(&textureFired), m_text(), m_clicked(false),
100  m_task(new sf::priv::ThreadMemberFunc<C>(function, object))
101  {
102 
103  }
104 
113  template<typename F>
114  Button(F function)
115  : AbstractButton(),
116  m_textureFired(0), m_text(), m_clicked(false),
117  m_task(new sf::priv::ThreadFunctor<F>(function))
118  {
119 
120  }
121 
133  template<typename F>
134  Button(F function, sf::Texture const &texture, sf::Texture const &textureFocused, sf::Texture const &textureFired)
135  : AbstractButton(texture, textureFocused),
136  m_textureFired(&textureFired), m_text(), m_clicked(false),
137  m_task(new sf::priv::ThreadFunctor<F>(function))
138  {
139 
140  }
141 
142 
143  virtual ~Button();
144  //--------------------------------------------------------------------
145 
146  //METHODS ------------------------------------------------------------
147 protected:
154  virtual void onClick();
155 
161  virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
162 
169  virtual void updateCoord();
170 
171 public:
177  void doClick();
178 
186  virtual void updateEvent(sf::Event const &event);
187 
196  virtual void updateFixed(sf::Time delta);
197  //--------------------------------------------------------------------
198 
199  //GETTERS/SETTERS ----------------------------------------------------
205  bool isFired() const;
206 
212  sf::Texture const *getTextureFired() const;
213 
221  void setTextureFired(sf::Texture const &textureFired);
222 
228  virtual const sf::String& getText() const;
229 
238  virtual void setText(sf::String const &text);
239 
245  virtual const sf::Font* getFont() const;
246 
252  virtual void setFont(sf::Font const &font);
253 
259  virtual unsigned int getFontSize() const;
260 
268  virtual void setFontSize(unsigned int size);
269 
275  virtual sf::Color getFontColor() const;
276 
284  virtual void setFontColor(sf::Color color);
285  //--------------------------------------------------------------------
286 
287  //FIELDS -------------------------------------------------------------
288 protected:
289  sf::Texture const *m_textureFired;
290  sf::Text m_text;
291  bool m_clicked;
292  sf::priv::ThreadFunc *m_task; //The task executed each times the button is fired
293  //--------------------------------------------------------------------
294 };
295 
296 } /* namespace ui */
297 } /* namespace sf */
298 #endif /* BUTTON_H_ */
299 
virtual void setFontSize(unsigned int size)
Set the font's size of the component.
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...
virtual const sf::String & getText() const
virtual void updateEvent(sf::Event const &event)
Update the component each time an event has been polled.
Button(F function, sf::Texture const &texture, sf::Texture const &textureFocused, sf::Texture const &textureFired)
Creates a button with textures that call a function or a functor (struct with operator() ) Do not for...
Definition: Button.hpp:134
void setTextureFired(sf::Texture const &textureFired)
Sets the texture fired.
Button(C *object, void(C::*function)(), sf::Texture const &texture, sf::Texture const &textureFocused, sf::Texture const &textureFired)
Creates a button with textures that call a function in a object Do not forget to set the font ! ...
Definition: Button.hpp:97
virtual unsigned int getFontSize() const
virtual void updateCoord()
Called when the component need update its geometry Inherit when you need to update some sprite...
void doClick()
Perform a click on the button.
Button(F function)
Creates a button without textures that call a function or a functor (struct with operator() ) Do not ...
Definition: Button.hpp:114
A button which does a simply task when clicked.
Definition: Button.hpp:29
virtual void setFont(sf::Font const &font)
Sets the font of the component's text.
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const
Draw the component to the render target.
Button()
Creates a button without textures that does nothing.
Definition: Button.hpp:40
virtual sf::Color getFontColor() const
virtual void setFontColor(sf::Color color)
Sets the color's font of the text.
Abstract class for all component that are button-like.
Button(sf::Texture const &texture, sf::Texture const &textureFocused, sf::Texture const &textureFired)
Creates a button with textures that does nothing Do not forget to set the font !
Definition: Button.hpp:58
virtual void setText(sf::String const &text)
Sets the text's string of the component Don't forget to set the font.
An interface that allow component to have a text.
Definition: IText.hpp:28
sf::Texture const * getTextureFired() const
virtual const sf::Font * getFont() const
bool isFired() const
Button(C *object, void(C::*function)())
Creates a button without textures that call a function in a object Do not forget to set textures and ...
Definition: Button.hpp:76
virtual void onClick()
Called when the button is clicked Execute the task.