MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
widget.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 _uiwidget_h
41 #define _uiwidget_h
42 
43 #include "UI/quadrenderable.h"
44 #include "UI/widgetfactory.h"
45 #include "Input/metacode.h"
46 #include "eventpublisher.h"
47 #include "eventsubscriber.h"
48 
49 namespace Mezzanine
50 {
51  namespace UI
52  {
53  class Button;
54  class RenderableContainer;
55  class GenericWidgetFactory;
56  ///////////////////////////////////////////////////////////////////////////////
57  /// @class WidgetEventArguments
58  /// @headerfile widget.h
59  /// @brief This is the base class for widget specific event arguments.
60  /// @details
61  ///////////////////////////////////////
63  {
64  public:
65  /// @brief The identification of the source firing this event.
67 
68  /// @brief Class constructor.
69  /// @param Name The name of the event being fired.
70  /// @param Source The identification of the widget firing this event.
71  WidgetEventArguments(const String& Name, const String& Source) :
72  EventArguments(Name), WidgetName(Source) { }
73  /// @brief Class destructor.
74  virtual ~WidgetEventArguments() { }
75  };//WidgetEventArguments
76 
77  ///////////////////////////////////////////////////////////////////////////////
78  /// @class Widget
79  /// @headerfile widget.h
80  /// @brief This is the base class for all widgets.
81  /// @details A widget is really a mechanism for implementing not-so-generic UI behaviors. They
82  /// are control stuctures allowing UI elements to interact with each other and potentially classes
83  /// outside the UI system(through the event system). @n @n
84  /// This class automatically creates a "Normal" and "Hovered" RenderLayerGroup that are used with
85  /// the events of this class, these groups should be used and additional groups only made for special
86  /// cases.
87  ///////////////////////////////////////
89  {
90  public:
91  /// @enum WidgetState
92  /// @brief Enum describing the current state of the widget.
94  {
95  WS_Untouched = 0,
96  WS_Hovered = 1,
97  WS_Focused = 2,
98  WS_Dragged = 4
99  };
100 
101  /// @brief Container class for storing @ref RenderLayerGroup instances in relation to widget states.
102  typedef std::map<UInt32,RenderLayerGroup*> StateLayerGroupMap;
103  /// @brief Iterator type for @ref RenderLayerGroup instances stored in relation to widget states.
104  typedef StateLayerGroupMap::iterator StateLayerGroupIterator;
105  /// @brief Const Iterator type for @ref RenderLayerGroup instances stored in relation to widget states.
106  typedef StateLayerGroupMap::const_iterator ConstStateLayerGroupIterator;
107 
108  /// @brief String containing the type name for this class: "GenericWidget".
109  static const String TypeName;
110  /// @brief Event name for when the mouse enters this widget.
111  static const String EventMouseEnter;
112  /// @brief Event name for when the mouse leaves this widget.
113  static const String EventMouseExit;
114  /// @brief Event name for when the mouse starts dragging this widget.
116  /// @brief Event name for when a mouse activation button is pressed, and held while moving.
118  /// @brief Event name for when the mouse stops dragging this widget.
119  static const String EventMouseDragEnd;
120  /// @brief Event name for when this widget gains focus.
121  static const String EventFocusGained;
122  /// @brief Event name for when this widget loses focus.
123  static const String EventFocusLost;
124  /// @brief Event name for when the system locks focus on this widget.
125  static const String EventFocusLocked;
126  /// @brief Event name fow when the system removes the focus lock from this widget.
128  /// @brief Event name for when this widget is switched from being hidden to being shown.
130  /// @brief Event name for when this widget is switched from being shown to being hidden.
132  protected:
133  friend class GenericWidgetFactory;
134  /// @internal
135  /// @brief Map containing all the RenderLayerGroups bound to specific widget states.
137  /// @internal
138  /// @brief The child widget of this widget the mouse is over, if any.
140  /// @internal
141  /// @brief UInt32 describing the current state of this widget.
143 
144  /// @copydoc Renderable::ProtoSerializeImpl(XML::Node&) const
145  virtual void ProtoSerializeImpl(XML::Node& SelfRoot) const;
146  /// @copydoc Renderable::ProtoDeSerializeImpl(const XML::Node&)
147  virtual void ProtoDeSerializeImpl(const XML::Node& SelfRoot);
148  /// @internal
149  /// @brief Consumes input for this widget's use.
150  /// @param Code The input to be processed.
151  /// @return Returns true if the input was handled, false otherwise.
152  virtual Boolean HandleInputImpl(const Input::MetaCode& Code);
153  /// @internal
154  /// @brief Contains all the common necessary startup initializations for this class.
155  void ConstructWidget();
156  //public:
157  /// @brief Blank constructor.
158  /// @param Parent The parent Screen that created this widget.
159  Widget(Screen* Parent);
160  /// @brief Standard initialization constructor.
161  /// @param RendName The name to be given to this widget.
162  /// @param Parent The parent Screen that created this widget.
163  Widget(const String& RendName, Screen* Parent);
164  /// @brief Rect constructor.
165  /// @param RendName The name to be given to this widget.
166  /// @param RendRect The rect describing this widget's transform relative to it's parent.
167  /// @param Parent The parent screen that created this widget.
168  Widget(const String& RendName, const UnifiedRect& RendRect, Screen* Parent);
169  /// @brief XML constructor.
170  /// @param XMLNode The node of the xml document to construct from.
171  /// @param Parent The screen the created Widget will belong to.
172  Widget(const XML::Node& XMLNode, Screen* Parent);
173  /// @brief Standard destructor.
174  virtual ~Widget();
175  public:
176  ///////////////////////////////////////////////////////////////////////////////
177  // Utility Methods
178 
179  /// @copydoc Renderable::GetRenderableType() const
180  RenderableType GetRenderableType() const;
181  /// @brief Gets the type of widget this is.
182  /// @return Returns a const String reference representing the type of widget this is.
183  virtual const String& GetTypeName() const;
184  /// @brief Gets the result of the last mouse hover check.
185  /// @return Returns whether or not the mouse was hovering over this widget during the last check.
186  virtual Boolean IsHovered() const;
187  /// @brief Gets whether or not this widget currently has focus.
188  /// @return True if this widget has focus, false otherwise.
189  virtual Boolean HasFocus() const;
190  /// @brief Gets whether or not this widget is being dragged.
191  /// @return Returns true if this widget is being dragged, false otherwise.
192  virtual Boolean IsBeingDragged() const;
193 
194  ///////////////////////////////////////////////////////////////////////////////
195  // State-LayerGroup Binding Methods
196 
197  /// @brief Binds a RenderLayerGroup to a WidgetState.
198  /// @param BindState The WidgetState to be bound to.
199  /// @param ToBind The RenderLayerGroup to be bound to the specified state.
200  virtual void BindGroupToState(const UInt32 BindState, RenderLayerGroup* ToBind);
201  /// @brief Gets the current RenderLayerGroup bound to a specified WidgetState.
202  /// @param BindState The WidgetState bound to the RenderLayerGroup being retrieved.
203  /// @return Returns a pointer to the RenderLayerGroup bound to the specified WidgetState, or NULL if none are bound.
204  virtual RenderLayerGroup* GetGroupBoundToState(const UInt32 BindState) const;
205  /// @brief Sets the group bound to the specified WidgetState as active.
206  /// @param BindState The WidgetState bound to the RenderLayerGroup being set.
207  /// @return Returns true if the active RenderLayerGroup has changed, false otherwise.
208  virtual bool SetGroupFromState(const UInt32 BindState);
209 
210  ///////////////////////////////////////////////////////////////////////////////
211  // Fetch Methods
212 
213  /// @brief Gets the hovered sub-widget within this widget, if any.
214  /// @return Returns a pointer to the sub-widget within this widget the mouse is hovering over, or NULL if none.
215  Widget* GetHoveredSubWidget() const;
216  /// @brief Gets a pointer to the Widget at the bottom of the hovered SubWidget chain.
217  /// @return Returns a pointer to the hovered sub-widget of the hovered sub-widget, etc., until you reach the end of the chain.
218  Widget* GetBottomMostHoveredWidget();
219 
220  ///////////////////////////////////////////////////////////////////////////////
221  // Visibility and Priority Methods
222 
223  /// @copydoc Renderable::SetVisible(Bool)
224  virtual void SetVisible(Boolean CanSee);
225  /// @copydoc Renderable::GetVisible() const
226  virtual Boolean GetVisible() const;
227  /// @copydoc Renderable::IsVisible() const
228  virtual Boolean IsVisible() const;
229  /// @copydoc Renderable::Show()
230  virtual void Show();
231  /// @copydoc Renderable::Hide()
232  virtual void Hide();
233 
234  ///////////////////////////////////////////////////////////////////////////////
235  // Serialization
236 
237  /// @copydoc Renderable::ProtoSerializeProperties(XML::Node&) const
238  virtual void ProtoSerializeProperties(XML::Node& SelfRoot) const;
239  /// @brief Convert the state-group bindings of this class to an XML::Node ready for serialization.
240  /// @param SelfRoot The root node containing all the serialized data for this instance.
241  virtual void ProtoSerializeStateGroupBindings(XML::Node& SelfRoot) const;
242  /// @brief Convert the Events of this class to an XML::Node ready for serialization.
243  /// @param SelfRoot The root node containing all the serialized data for this instance.
244  virtual void ProtoSerializeEvents(XML::Node& SelfRoot) const;
245 
246  /// @copydoc Renderable::ProtoDeSerializeProperties(const XML::Node&)
247  virtual void ProtoDeSerializeProperties(const XML::Node& SelfRoot);
248  /// @brief Take the data stored in an XML Node and overwrite the state-group bindings of this object with it.
249  /// @param SelfRoot An XML::Node containing the data to populate this class with.
250  virtual void ProtoDeSerializeStateGroupBindings(const XML::Node& SelfRoot);
251  /// @brief Take the data stored in an XML Node and overwrite the Events of this object with it.
252  /// @param SelfRoot An XML::Node containing the data to populate this class with.
253  virtual void ProtoDeSerializeEvents(const XML::Node& SelfRoot);
254 
255  /// @copydoc Renderable::GetDerivedSerializableName() const
256  virtual String GetDerivedSerializableName() const;
257  /// @copydoc Renderable::GetSerializableName()
258  static String GetSerializableName();
259 
260  ///////////////////////////////////////////////////////////////////////////////
261  // Internal Event Methods
262 
263  /// @brief Self logic to be executed when the mouse cursor enters the bounds of this widget.
264  /// @details This method should be exclusively called by the UIManager.
265  virtual void _OnMouseEnter();
266  /// @brief Self logic to be executed when the mouse cursor leaves the bounds of thiw widget.
267  /// @details This method should be exclusively called by the UIManager.
268  virtual void _OnMouseExit();
269  /// @brief Self logic to be executed when the mouse cursor starts dragging across the bounds of this widget.
270  /// @details This method should be exclusively called by the UIManager.
271  virtual void _OnMouseDragStart();
272  /// @brief Self logic to be executed when the mouse cursor is dragging across the bounds of this widget.
273  /// @details This method should be exclusively called by the UIManager.
274  virtual void _OnMouseDragging();
275  /// @brief Self logic to be executed when the mouse cursor stops dragging across the bounds of this widget.
276  /// @details This method should be exclusively called by the UIManager.
277  virtual void _OnMouseDragEnd();
278  /// @brief Self logic to be executed when focus is given to this widget.
279  /// @details This method should be exclusively called by the UIManager.
280  virtual void _OnFocusGained();
281  /// @brief Self logic to be executed when focus is removed from this widget.
282  /// @details This method should be exclusively called by the UIManager.
283  virtual void _OnFocusLost();
284  /// @brief Self logic to be executed when focus is locked to this widget.
285  /// @details This method should be exclusively called by the UIManager.
286  virtual void _OnFocusLocked();
287  /// @brief Self logic to be executed when focus is no longer locked to this widget.
288  /// @details This method should be exclusively called by the UIManager.
289  virtual void _OnFocusUnlocked();
290  /// @brief Self logic to be executed when this widget becomes visible.
291  virtual void _OnVisibilityShown();
292  /// @brief Self logic to be executed when this widget becomes invisible.
293  virtual void _OnVisibilityHidden();
294 
295  ///////////////////////////////////////////////////////////////////////////////
296  // Internal Methods
297 
298  /// @internal
299  /// @brief Handles input passed to this widget.
300  /// @note This input will be passed up if not consumed by this widget.
301  /// @param Code The MetaCode to be processed.
302  /// @return Returns true if this input was consumed/handled, false otherwise.
303  virtual bool _HandleInput(const Input::MetaCode& Code);
304  /// @copydoc EventSubscriber::_NotifyEvent(const EventArguments& Args)
305  virtual void _NotifyEvent(const EventArguments& Args);
306  };//Widget
307 
308  ///////////////////////////////////////////////////////////////////////////////
309  /// @brief This is the factory implementation for generic widgets.
310  /// @details
311  ///////////////////////////////////////
313  {
314  public:
315  /// @brief Class constructor.
317  /// @brief Class destructor.
318  virtual ~GenericWidgetFactory() { }
319 
320  /// @copydoc WidgetFactory::GetWidgetTypeName() const
321  virtual String GetWidgetTypeName() const;
322 
323  /// @brief Creates a new Widget.
324  /// @param RendName The name to be given to the created Widget.
325  /// @param Parent The screen the created Widget will belong to.
326  /// @return Returns a pointer to the created Widget.
327  virtual Widget* CreateWidget(const String& RendName, Screen* Parent);
328  /// @brief Creates a new Widget.
329  /// @param RendName The name to be given to the created Widget.
330  /// @param RendRect The dimensions that will be assigned to the created Widget.
331  /// @param Parent The screen the created Widget will belong to.
332  /// @return Returns a pointer to the created Widget.
333  virtual Widget* CreateWidget(const String& RendName, const UnifiedRect& RendRect, Screen* Parent);
334 
335  /// @copydoc WidgetFactory::CreateWidget(Screen*)
336  virtual Widget* CreateWidget(Screen* Parent);
337  /// @copydoc WidgetFactory::CreateWidget(const String&, const NameValuePairMap&, Screen*)
338  virtual Widget* CreateWidget(const String& RendName, const NameValuePairMap& Params, Screen* Parent);
339  /// @copydoc WidgetFactory::CreateWidget(const String&, const UnifiedRect&, const NameValuePairMap&, Screen*)
340  virtual Widget* CreateWidget(const String& RendName, const UnifiedRect& RendRect, const NameValuePairMap& Params, Screen* Parent);
341  /// @copydoc WidgetFactory::CreateWidget(const XML::Node&, Screen*)
342  virtual Widget* CreateWidget(const XML::Node& XMLNode, Screen* Parent);
343  /// @copydoc WidgetFactory::DestroyWidget(Widget*)
344  virtual void DestroyWidget(Widget* ToBeDestroyed);
345  };//GenericWidgetFactory
346  }//UI
347 }//Mezzanine
348 
349 #endif