MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
button.h
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU 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  The Mezzanine Engine 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 General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uibutton_h
41 #define _uibutton_h
42 
43 #include "colourvalue.h"
44 #include "timer.h"
45 #include "Input/metacodekey.h"
46 #include "UI/uienumerations.h"
47 #include "UI/widget.h"
48 #include "UI/widgetfactory.h"
49 
50 namespace Mezzanine
51 {
52  class UIManager;
53  namespace UI
54  {
55  class Screen;
56  class ButtonCallback;
57  ///////////////////////////////////////////////////////////////////////////////
58  /// @class Button
59  /// @headerfile button.h
60  /// @brief This class is a helper class, specifically for use as a button.
61  /// @details Unlike rectangles and captions, this class can be interacted with by clicking.
62  /// It is important to understand what you want your space to do when selecting the class to use.
63  ///////////////////////////////////////
64  class MEZZ_LIB Button : public Widget
65  {
66  public:
67  /// @brief Basic container type for Activation Code storage by this class.
68  typedef std::set< Input::MetaCodeKey > ActivationCodeContainer;
69  /// @brief Iterator type for Activation Codes stored by this class.
70  typedef ActivationCodeContainer::iterator ActivationCodeIterator;
71  /// @brief Const Iterator type for Activation Codes stored by this class.
72  typedef ActivationCodeContainer::const_iterator ConstActivationCodeIterator;
73 
74  /// @enum ActivationState
75  /// @brief This enum describes the different possible states for the activation of a button.
76  /// @details The first two states should be self explanitory. Button is made but not interacted with, it's deactivated.
77  /// If the button is pressed it becomes activated for the duration of the press. The third state is a special state that
78  /// occurs when one of the criteria for being activated is removed in a way that can promptly return without changing the
79  /// other criteria. Specifically this can happen if a mouse click occurs over the button and is held like that while moving
80  /// the mouse cursor around. If it is moved such that it is no longer hovering over the button, it will go in standby. The
81  /// normal activation will be restored once the cursor is hovered over the button again.
83  {
84  AS_Deactivated = 0, ///< The normal default state.
85  AS_Activated = 1, ///< This button has been activated via an input device.
86  AS_Activation_Standby = 2 ///< This button was activated, but an event occured that made it suspend it's activation.
87  };
88 
89  /// @brief String containing the type name for this class: "Button".
90  static const String TypeName;
91  /// @brief Event name for when this activatable widget is activated.
92  static const String EventActivated;
93  /// @brief Event name for when this activatable widget is put into standby.
94  static const String EventStandby;
95  /// @brief Event name for when this activatable widget is deactivated.
96  static const String EventDeactivated;
97  protected:
98  friend class ButtonFactory;
99  /// @internal
100  /// @brief The timer that will be used when a button is locked from activating a second time within a certain period.
102  /// @internal
103  /// @brief A container of codes that stores the inputs that will trigger this button to be activated.
105  /// @internal
106  /// @brief Stores the current state of this button's activation. See Button::ActivationState enum for more details.
108  /// @internal
109  /// @brief Stores whether or not the current activation of this button was triggered by a mouse.
110  Boolean MouseActivated;
111 
112  /// @copydoc Widget::HandleInputImpl(const Input::MetaCode& Code)
113  virtual Boolean HandleInputImpl(const Input::MetaCode& Code);
114  /// @internal
115  /// @brief Contains all the common necessary startup initializations for this class.
116  virtual void ConstructButton();
117  /// @internal
118  /// @brief Verifies the provided to code is valid for this button.
119  /// @param Code The code to check.
120  /// @return Returns true if this code is valid, false otherwise.
121  virtual Boolean VertifyActivationCode(const Input::InputCode Code);
122  /// @internal
123  /// @brief Attempts to activate this button.
124  /// @return Returns true if this button was successfully activated.
125  virtual Boolean Activate();
126  /// @internal
127  /// @brief Attempts to deactivate this button.
128  /// @return Returns true if this button was successfully deactivated.
129  virtual Boolean Deactivate();
130  /// @internal
131  /// @brief Attempts to put this button into standby.
132  /// @return Returns true if this button was successfully put into standby.
133  virtual Boolean Standby();
134  //public:
135  /// @brief Blank constructor.
136  /// @param Parent The parent Screen that created this widget.
137  Button(Screen* Parent);
138  /// @brief Standard initialization constructor.
139  /// @param RendName The name to be given to this renderable.
140  /// @param Parent The parent Screen that created this widget.
141  Button(const String& RendName, Screen* Parent);
142  /// @brief Rect constructor.
143  /// @param RendName The name to be given to this renderable.
144  /// @param RendRect The rect describing this widget's transform relative to it's parent.
145  /// @param Parent The parent screen that created this renderable.
146  Button(const String& RendName, const UnifiedRect& RendRect, Screen* Parent);
147  /// @brief XML constructor.
148  /// @param XMLNode The node of the xml document to construct from.
149  /// @param Parent The screen the created Button will belong to.
150  Button(const XML::Node& XMLNode, Screen* Parent);
151  /// @brief Standard destructor.
152  virtual ~Button();
153  public:
154  ///////////////////////////////////////////////////////////////////////////////
155  // Utility Methods
156 
157  /// @brief Sets a timer preventing multiple activations for a period of time.
158  /// @param Milliseconds The amount of time to lock out additional activations, in milliseconds.
159  void SetLockoutTime(const UInt32& Milliseconds);
160  /// @brief Gets this activatables lockout timer.
161  /// @return Returns a pointer to the Lockout timer for this button, or NULL if one hasn't been set.
162  const StopWatchTimer& GetLockoutTimer() const;
163 
164  /// @brief Gets whether or not this button can be activated again.
165  /// @return Returns true if this button is not ready to be activated again.
166  Boolean IsActivationLocked() const;
167  /// @brief Gets whether or not this button is currently activated.
168  /// @return Returns true if this button is currently activated, false otherwise.
169  Boolean IsActivated() const;
170  /// @brief Gets whether or not this button is currently on standby.
171  /// @return Returns true if this button is currently on activation standby, false otherwise.
172  Boolean IsOnStandby() const;
173  /// @brief Gets whether or not this button is currently deactivated.
174  /// @return Retruns true if this button is currently deactivated, false otherwise.
175  Boolean IsDeactivated() const;
176 
177  /// @copydoc Widget::GetTypeName() const
178  virtual const String& GetTypeName() const;
179 
180  ///////////////////////////////////////////////////////////////////////////////
181  // Binding Methods
182 
183  /// @brief Registers a keyboard key or mouse button that can activate this button.
184  /// @details In the case of a mouse button, the hover check has to return true to activate the button.
185  /// @param Code The input code to register that will trigger activation.
186  virtual void BindActivationKeyOrButton(const Input::MetaCode& Code);
187  /// @brief Removes a previously registered activation code.
188  /// @param Code The input code to remove.
189  virtual void UnbindActivationKeyOrButton(const Input::MetaCode& Code);
190  /// @brief Clears all keyboard input codes from the set of activation codes.
191  virtual void UnbindAllKeyboardActivationKeys();
192  /// @brief Clears all mouse input codes from the set of activation codes.
193  virtual void UnbindAllMouseActivationButtons();
194  /// @brief Clears all controller input codes from the set of activation codes.
195  virtual void UnbindAllControllerActivationButtons();
196  /// @brief Clears all keyboard and mouse input codes from the list of activators.
197  virtual void UnbindAllActivationKeysAndButtons();
198 
199  ///////////////////////////////////////////////////////////////////////////////
200  // Fetch Methods
201 
202  /// @brief Gets a set with all the activation codes used to activate this button.
203  /// @return Returns a pointer to an std::set containing all the activation codes that will activate this button.
204  const ActivationCodeContainer& GetActivationCodes() const;
205 
206  ///////////////////////////////////////////////////////////////////////////////
207  // Serialization
208 
209  /// @copydoc Renderable::ProtoSerializeProperties(XML::Node&) const
210  virtual void ProtoSerializeProperties(XML::Node& SelfRoot) const;
211  /// @copydoc Renderable::ProtoDeSerializeProperties(const XML::Node&)
212  virtual void ProtoDeSerializeProperties(const XML::Node& SelfRoot);
213 
214  /// @copydoc Renderable::GetSerializableName()
215  static String GetSerializableName();
216 
217  ///////////////////////////////////////////////////////////////////////////////
218  // Internal Event Methods
219 
220  /// @copydoc Widget::_OnMouseEnter()
221  virtual void _OnMouseEnter();
222  /// @copydoc Widget::_OnMouseExit()
223  virtual void _OnMouseExit();
224  /// @brief Self logic to be executed when this button is activated.
225  virtual void _OnActivate();
226  /// @brief Self logic to be executed when this button is put into standby.
227  virtual void _OnStandby();
228  /// @brief Self logic to be executed when this button is deactivated.
229  virtual void _OnDeactivate();
230  };//Button
231 
232  ///////////////////////////////////////////////////////////////////////////////
233  /// @brief This is the factory implementation for Button widgets.
234  /// @details
235  ///////////////////////////////////////
237  {
238  public:
239  /// @brief Class constructor.
241  /// @brief Class destructor.
242  virtual ~ButtonFactory() { }
243 
244  /// @copydoc WidgetFactory::GetWidgetTypeName() const
245  virtual String GetWidgetTypeName() const;
246 
247  /// @brief Creates a new Button.
248  /// @param RendName The name to be given to the created Button.
249  /// @param Parent The screen the created Button will belong to.
250  /// @return Returns a pointer to the created Button.
251  virtual Button* CreateButton(const String& RendName, Screen* Parent);
252  /// @brief Creates a new Button.
253  /// @param RendName The name to be given to the created Button.
254  /// @param RendRect The dimensions that will be assigned to the created Button.
255  /// @param Parent The screen the created Button will belong to.
256  /// @return Returns a pointer to the created Button.
257  virtual Button* CreateButton(const String& RendName, const UnifiedRect& RendRect, Screen* Parent);
258  /// @brief Creates a new Button.
259  /// @param XMLNode The node of the xml document to construct from.
260  /// @param Parent The screen the created Button will belong to.
261  /// @return Returns a pointer to the created Button.
262  virtual Button* CreateButton(const XML::Node& XMLNode, Screen* Parent);
263 
264  /// @copydoc WidgetFactory::CreateWidget(Screen*)
265  virtual Widget* CreateWidget(Screen* Parent);
266  /// @copydoc WidgetFactory::CreateWidget(const String&, const NameValuePairMap&, Screen*)
267  virtual Widget* CreateWidget(const String& RendName, const NameValuePairMap& Params, Screen* Parent);
268  /// @copydoc WidgetFactory::CreateWidget(const String&, const UnifiedRect&, const NameValuePairMap&, Screen*)
269  virtual Widget* CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent);
270  /// @copydoc WidgetFactory::CreateWidget(const XML::Node&, Screen*)
271  virtual Widget* CreateWidget(const XML::Node& XMLNode, Screen* Parent);
272  /// @copydoc WidgetFactory::DestroyWidget(Widget*)
273  virtual void DestroyWidget(Widget* ToBeDestroyed);
274  };//ButtonFactory
275  }//UI
276 }//Mezzanine
277 
278 #endif