Spinning Topp Logo BlackTopp Studios
inc
button.h
1 // © Copyright 2010 - 2016 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  /// @n @n
64  /// This class does not use a LayoutStrategy.
65  ///////////////////////////////////////
66  class MEZZ_LIB Button : public Widget
67  {
68  public:
69  /// @brief Basic container type for Activation Code storage by this class.
70  typedef std::set< Input::MetaCodeKey > ActivationCodeContainer;
71  /// @brief Iterator type for Activation Codes stored by this class.
72  typedef ActivationCodeContainer::iterator ActivationCodeIterator;
73  /// @brief Const Iterator type for Activation Codes stored by this class.
74  typedef ActivationCodeContainer::const_iterator ConstActivationCodeIterator;
75 
76  /// @brief This enum describes the different possible states for the activation of a button.
77  /// @details The first two states should be self explanitory. Button is made but not interacted with, it's deactivated.
78  /// If the button is pressed it becomes activated for the duration of the press. The third state is a special state that
79  /// occurs when one of the criteria for being activated is removed in a way that can promptly return without changing the
80  /// other criteria. Specifically this can happen if a mouse click occurs over the button and is held like that while moving
81  /// the mouse cursor around. If it is moved such that it is no longer hovering over the button, it will go in standby. The
82  /// normal activation will be restored once the cursor is hovered over the button again.
84  {
85  AS_Deactivated = 0, ///< The normal default state.
86  AS_Activated = 1, ///< This button has been activated via an input device.
87  AS_Activation_Standby = 2 ///< This button was activated, but an event occured that made it suspend it's activation.
88  };
89 
90  /// @brief String containing the type name for this class: "Button".
91  static const String TypeName;
92  /// @brief Event name for when this activatable widget is activated.
93  static const String EventActivated;
94  /// @brief Event name for when this activatable widget is put into standby.
95  static const String EventStandby;
96  /// @brief Event name for when this activatable widget is deactivated.
97  static const String EventDeactivated;
98  protected:
99  /// @internal
100  /// @brief A container of codes that stores the inputs that will trigger this button to be activated.
101  ActivationCodeContainer ActivationCodes;
102  /// @internal
103  /// @brief Stores the current state of this button's activation. See Button::ActivationState enum for more details.
105  /// @internal
106  /// @brief The timer that will be used when a button is locked from activating a second time within a certain period.
108  /// @internal
109  /// @brief The amount of time to lock the activation of a button after it has already been activated.
111  /// @internal
112  /// @brief Stores whether or not the current activation of this button was triggered by a mouse.
114 
115  /// @copydoc Widget::HandleInputImpl(const Input::MetaCode& Code)
116  virtual Boole HandleInputImpl(const Input::MetaCode& Code);
117  /// @internal
118  /// @brief Contains all the common necessary startup initializations for this class.
119  virtual void ConstructButton();
120  /// @internal
121  /// @brief Verifies the provided to code is valid for this button.
122  /// @param Code The code to check.
123  /// @return Returns true if this code is valid, false otherwise.
124  virtual Boole VertifyActivationCode(const Input::InputCode Code);
125  /// @internal
126  /// @brief Attempts to activate this button.
127  /// @return Returns true if this button was successfully activated.
128  virtual Boole Activate();
129  /// @internal
130  /// @brief Attempts to deactivate this button.
131  /// @return Returns true if this button was successfully deactivated.
132  virtual Boole Deactivate();
133  /// @internal
134  /// @brief Attempts to put this button into standby.
135  /// @return Returns true if this button was successfully put into standby.
136  virtual Boole Standby();
137  public:
138  /// @brief Blank constructor.
139  /// @param Parent The parent Screen that created this widget.
140  Button(Screen* Parent);
141  /// @brief Standard initialization constructor.
142  /// @param RendName The name to be given to this renderable.
143  /// @param Parent The parent Screen that created this widget.
144  Button(const String& RendName, Screen* Parent);
145  /// @brief Rect constructor.
146  /// @param RendName The name to be given to this renderable.
147  /// @param RendRect The rect describing this widget's transform relative to it's parent.
148  /// @param Parent The parent screen that created this renderable.
149  Button(const String& RendName, const UnifiedRect& RendRect, Screen* Parent);
150  /// @brief XML constructor.
151  /// @param XMLNode The node of the xml document to construct from.
152  /// @param Parent The screen the created Button will belong to.
153  Button(const XML::Node& XMLNode, Screen* Parent);
154  /// @brief Standard destructor.
155  virtual ~Button();
156  //public:
157  ///////////////////////////////////////////////////////////////////////////////
158  // Utility Methods
159 
160  /// @brief Sets a timer preventing multiple activations for a period of time.
161  /// @param Milliseconds The amount of time to lock out additional activations, in milliseconds.
162  void SetLockoutTime(const UInt32& Milliseconds);
163  /// @brief Gets this activatables lockout timer.
164  /// @return Returns a pointer to the Lockout timer for this button, or NULL if one hasn't been set.
165  const Timer& GetLockoutTimer() const;
166 
167  /// @brief Gets whether or not this button can be activated again.
168  /// @return Returns true if this button is not ready to be activated again.
169  Boole IsActivationLocked() const;
170  /// @brief Gets whether or not this button is currently activated.
171  /// @return Returns true if this button is currently activated, false otherwise.
172  Boole IsActivated() const;
173  /// @brief Gets whether or not this button is currently on standby.
174  /// @return Returns true if this button is currently on activation standby, false otherwise.
175  Boole IsOnStandby() const;
176  /// @brief Gets whether or not this button is currently deactivated.
177  /// @return Retruns true if this button is currently deactivated, false otherwise.
178  Boole IsDeactivated() const;
179 
180  /// @copydoc Widget::GetTypeName() const
181  virtual const String& GetTypeName() const;
182 
183  ///////////////////////////////////////////////////////////////////////////////
184  // Binding Methods
185 
186  /// @brief Registers a keyboard key or mouse button that can activate this button.
187  /// @details In the case of a mouse button, the hover check has to return true to activate the button.
188  /// @param Code The input code to register that will trigger activation.
189  virtual void BindActivationKeyOrButton(const Input::MetaCode& Code);
190  /// @brief Removes a previously registered activation code.
191  /// @param Code The input code to remove.
192  virtual void UnbindActivationKeyOrButton(const Input::MetaCode& Code);
193  /// @brief Clears all keyboard input codes from the set of activation codes.
194  virtual void UnbindAllKeyboardActivationKeys();
195  /// @brief Clears all mouse input codes from the set of activation codes.
196  virtual void UnbindAllMouseActivationButtons();
197  /// @brief Clears all controller input codes from the set of activation codes.
198  virtual void UnbindAllControllerActivationButtons();
199  /// @brief Clears all keyboard and mouse input codes from the list of activators.
200  virtual void UnbindAllActivationKeysAndButtons();
201 
202  ///////////////////////////////////////////////////////////////////////////////
203  // Fetch Methods
204 
205  /// @brief Gets a set with all the activation codes used to activate this button.
206  /// @return Returns a pointer to an std::set containing all the activation codes that will activate this button.
207  const ActivationCodeContainer& GetActivationCodes() const;
208 
209  ///////////////////////////////////////////////////////////////////////////////
210  // Serialization
211 
212  /// @copydoc Renderable::ProtoSerializeProperties(XML::Node&) const
213  virtual void ProtoSerializeProperties(XML::Node& SelfRoot) const;
214  /// @copydoc Renderable::ProtoDeSerializeProperties(const XML::Node&)
215  virtual void ProtoDeSerializeProperties(const XML::Node& SelfRoot);
216 
217  /// @copydoc Renderable::GetSerializableName()
218  static String GetSerializableName();
219 
220  ///////////////////////////////////////////////////////////////////////////////
221  // Internal Event Methods
222 
223  /// @copydoc Widget::_OnMouseEnter()
224  virtual void _OnMouseEnter();
225  /// @copydoc Widget::_OnMouseExit()
226  virtual void _OnMouseExit();
227  /// @brief Self logic to be executed when this button is activated.
228  virtual void _OnActivate();
229  /// @brief Self logic to be executed when this button is put into standby.
230  virtual void _OnStandby();
231  /// @brief Self logic to be executed when this button is deactivated.
232  virtual void _OnDeactivate();
233  };//Button
234 
235  ///////////////////////////////////////////////////////////////////////////////
236  /// @brief This is the factory implementation for Button widgets.
237  /// @details
238  ///////////////////////////////////////
240  {
241  public:
242  /// @brief Class constructor.
244  /// @brief Class destructor.
245  virtual ~ButtonFactory() { }
246 
247  /// @copydoc WidgetFactory::GetWidgetTypeName() const
248  virtual String GetWidgetTypeName() const;
249 
250  /// @brief Creates a new Button.
251  /// @param RendName The name to be given to the created Button.
252  /// @param Parent The screen the created Button will belong to.
253  /// @return Returns a pointer to the created Button.
254  virtual Button* CreateButton(const String& RendName, Screen* Parent);
255  /// @brief Creates a new Button.
256  /// @param RendName The name to be given to the created Button.
257  /// @param RendRect The dimensions that will be assigned to the created Button.
258  /// @param Parent The screen the created Button will belong to.
259  /// @return Returns a pointer to the created Button.
260  virtual Button* CreateButton(const String& RendName, const UnifiedRect& RendRect, Screen* Parent);
261  /// @brief Creates a new Button.
262  /// @param XMLNode The node of the xml document to construct from.
263  /// @param Parent The screen the created Button will belong to.
264  /// @return Returns a pointer to the created Button.
265  virtual Button* CreateButton(const XML::Node& XMLNode, Screen* Parent);
266 
267  /// @copydoc WidgetFactory::CreateWidget(Screen*)
268  virtual Widget* CreateWidget(Screen* Parent);
269  /// @copydoc WidgetFactory::CreateWidget(const String&, const NameValuePairMap&, Screen*)
270  virtual Widget* CreateWidget(const String& RendName, const NameValuePairMap& Params, Screen* Parent);
271  /// @copydoc WidgetFactory::CreateWidget(const String&, const UnifiedRect&, const NameValuePairMap&, Screen*)
272  virtual Widget* CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent);
273  /// @copydoc WidgetFactory::CreateWidget(const XML::Node&, Screen*)
274  virtual Widget* CreateWidget(const XML::Node& XMLNode, Screen* Parent);
275  /// @copydoc WidgetFactory::DestroyWidget(Widget*)
276  virtual void DestroyWidget(Widget* ToBeDestroyed);
277  };//ButtonFactory
278  }//UI
279 }//Mezzanine
280 
281 #endif
std::set< Input::MetaCodeKey > ActivationCodeContainer
Basic container type for Activation Code storage by this class.
Definition: button.h:70
InputCode
The InputCode enum defines all the posible types of inputs.
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
static const String EventActivated
Event name for when this activatable widget is activated.
Definition: button.h:93
static const String EventDeactivated
Event name for when this activatable widget is deactivated.
Definition: button.h:97
Whole LockoutTime
The amount of time to lock the activation of a button after it has already been activated.
Definition: button.h:110
virtual ~ButtonFactory()
Class destructor.
Definition: button.h:245
Boole MouseActivated
Stores whether or not the current activation of this button was triggered by a mouse.
Definition: button.h:113
Timer LockoutTimer
The timer that will be used when a button is locked from activating a second time within a certain pe...
Definition: button.h:107
This class represents a 2D rect which can express the size and position of a renderable on screen...
Definition: unifieddim.h:661
ActivationState Activation
Stores the current state of this button's activation. See Button::ActivationState enum for more detai...
Definition: button.h:104
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
uint32_t UInt32
An 32-bit unsigned integer.
Definition: datatypes.h:126
static const String EventStandby
Event name for when this activatable widget is put into standby.
Definition: button.h:95
ActivationCodeContainer ActivationCodes
A container of codes that stores the inputs that will trigger this button to be activated.
Definition: button.h:101
This is the base class for all widgets.
Definition: widget.h:126
static const String TypeName
String containing the type name for this class: "Button".
Definition: button.h:91
This Determines the kind of user input.
Definition: metacode.h:93
A basic timer class to assist in timed operations.
Definition: timer.h:67
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
ActivationState
This enum describes the different possible states for the activation of a button. ...
Definition: button.h:83
ActivationCodeContainer::iterator ActivationCodeIterator
Iterator type for Activation Codes stored by this class.
Definition: button.h:72
This class is a helper class, specifically for use as a button.
Definition: button.h:66
ActivationCodeContainer::const_iterator ConstActivationCodeIterator
Const Iterator type for Activation Codes stored by this class.
Definition: button.h:74
std::map< String, String > NameValuePairMap
This is a datatype mostly used for describing settings or parameters that can't be declared in advanc...
Definition: datatypes.h:209
This is the factory implementation for Button widgets.
Definition: button.h:239
This class is a helper class for creating UI's. It is responsible for storing and keeping track of al...
Definition: screen.h:142
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
ButtonFactory()
Class constructor.
Definition: button.h:243
This is a base class for factories that construct the widgets available to the UI subsystem...
Definition: widgetfactory.h:61