MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
uimanager.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 _uiuimanager_h
41 #define _uiuimanager_h
42 
43 #include "managerbase.h"
44 #include "managerfactory.h"
45 #include "singleton.h"
46 #include "UI/rect.h"
47 #include "Input/metacode.h"
48 #include "Threading/workunit.h"
49 
50 namespace Mezzanine
51 {
52  namespace Graphics
53  {
54  class Viewport;
55  }
56  namespace UI
57  {
58  class Screen;
59  class Button;
60  class Widget;
61  class TextureAtlas;
62  class TextureAtlasHandler;
63  class HotKeyHandler;
64  class ActionHandler;
65  class QuadRenderable;
66  class MarkupParser;
67  class UIManager;
68 
69  ///////////////////////////////////////////////////////////////////////////////
70  /// @brief This is a Mezzanine::Threading::iWorkUnit for the updating of widgets in the UI system.
71  /// @details
72  ///////////////////////////////////////
74  {
75  protected:
76  /// @internal
77  /// @brief A pointer to the manager this work unit is processing.
79  /// @internal
80  /// @brief Protected copy constructor. THIS IS NOT ALLOWED.
81  /// @param Other The other work unit being copied from. WHICH WILL NEVER HAPPEN.
83  /// @internal
84  /// @brief Protected assignment operator. THIS IS NOT ALLOWED.
85  /// @param Other The other work unit being copied from. WHICH WILL NEVER HAPPEN.
86  WidgetUpdateWorkUnit& operator=(const WidgetUpdateWorkUnit& Other);
87  public:
88  /// @brief Class constructor.
89  /// @param Target The UIManager this work unit will process during the frame.
91  /// @brief Class destructor.
92  virtual ~WidgetUpdateWorkUnit();
93 
94  ///////////////////////////////////////////////////////////////////////////////
95  // Utility
96 
97  /// @brief This does any required update of the Graphical Scene graph and REnders one frame
98  /// @param CurrentThreadStorage The storage class for all resources owned by this work unit during it's execution.
99  virtual void DoWork(Threading::DefaultThreadSpecificStorage::Type& CurrentThreadStorage);
100  };//WidgetUpdateWorkUnit
101 
102  // Used by the scripting language binder to help create bindgings for this class. SWIG does know to creation template instances
103  #ifdef SWIG
104  %template(SingletonUIManager) Singleton<UIManager>;
105  #endif
106 
107  ///////////////////////////////////////////////////////////////////////////////
108  /// @class UIManager
109  /// @headerfile uimanager.h
110  /// @brief This class is responsible for any and all user interactions with the User interface/HUD.
111  /// @details Currently, you have to create the UI/HUD in code. Font and sprite data is loaded through a premade mta file(*.mta).
112  ///////////////////////////////////////
114  {
115  public:
116  /// @brief Basic container type for @ref MarkupParser storage by this class.
117  typedef std::map<String,MarkupParser*> MarkupParserContainer;
118  /// @brief Iterator type for @ref MarkupParser instances stored by this class.
119  typedef MarkupParserContainer::iterator MarkupParserIterator;
120  /// @brief Const Iterator type for @ref MarkupParser instances stored by this class.
121  typedef MarkupParserContainer::const_iterator ConstMarkupParserIterator;
122  /// @brief Basic container type for @ref Screen storage by this class.
123  typedef std::vector< UI::Screen* > ScreenContainer;
124  /// @brief Iterator type for @ref Screen instances stored by this class.
125  typedef ScreenContainer::iterator ScreenIterator;
126  /// @brief Const Iterator type for @ref Screen instances stored by this class.
127  typedef ScreenContainer::const_iterator ConstScreenIterator;
128  /// @brief Basic container type for @ref Input::MetaCode storage by this class.
129  typedef std::vector< Input::MetaCode > InputContainer;
130  /// @brief Iterator type for @ref Input::MetaCode instances stored by this class.
131  typedef InputContainer::iterator InputIterator;
132  /// @brief Const Iterator type for @ref Input::MetaCode instances stored by this class.
133  typedef InputContainer::const_iterator ConstInputIterator;
134  /// @brief An std::pair type for returning the result of a font suggestion.
135  typedef std::pair< String, Real > FontResult;
136  protected:
137  /// @internal
138  /// @brief A container storing all the registered/known markup parsers.
139  MarkupParserContainer MarkupParsers;
140  /// @internal
141  /// @brief A container storing all the Screens created in the UI system.
142  ScreenContainer Screens;
143  /// @internal
144  /// @brief A container storing all of the Inputs buttons will be set to activate in response to.
145  InputContainer AutoRegisterCodes;
146  /// @internal
147  /// @brief A container storing all of the Inputs generated/tracked during the last UI update.
148  InputContainer InjectedInputs;
149 
150  /// @internal
151  /// @brief Stores the Input that locked the current focus.
153  /// @internal
154  /// @brief A pointer to the current widget in the UI system the mouse is hovered over.
156  /// @internal
157  /// @brief A pointer to the widget that is currently holding the focus.
159  /// @internal
160  /// @brief A pointer to the class responsible for Atlas tasks in the UI system.
162  /// @internal
163  /// @brief A pointer to the class responsible for HotKey tasks in the UI system.
165  /// @internal
166  /// @brief The work unit that updates all of the widgets in the UI system.
168  /// @internal
169  /// @brief Can be used for thread safe logging and other thread specific resources.
171  /// @internal
172  /// @brief Stores whether or not newly created buttons will have activation codes binded to them automatically.
174  /// @internal
175  /// @brief Stores whether or not the mouse moved since the last time the UI was updated.
177 
178  /// @internal
179  /// @brief Handles UI processing prior to the update of the main focus.
180  /// @param Code The MetaCode containing the input to be processed.
181  void HandlePreFocusInput(const Input::MetaCode& Code);
182  /// @internal
183  /// @brief Handles UI processing after the update of the main focus.
184  /// @param Code The MetaCode containing the input to be processed.
185  void HandlePostFocusInput(const Input::MetaCode& Code);
186  /// @internal
187  /// @brief Handles keyboard processes prior to the update of the main focus.
188  /// @param Code The MetaCode containing the input to be processed.
189  void HandlePreFocusKeyboardInput(const Input::MetaCode& Code);
190  /// @internal
191  /// @brief Handles mouse processes prior to the update of the main focus.
192  /// @param Code The MetaCode containing the input to be processed.
193  void HandlePreFocusMouseInput(const Input::MetaCode& Code);
194  /// @internal
195  /// @brief Handles controller processes prior to the update of the main focus.
196  /// @param Code The MetaCode containing the input to be processed.
197  void HandlePreFocusControllerInput(const Input::MetaCode& Code);
198  /// @internal
199  /// @brief Handles keyboard processes after the update of the main focus.
200  /// @param Code The MetaCode containing the input to be processed.
201  void HandlePostFocusKeyboardInput(const Input::MetaCode& Code);
202  /// @internal
203  /// @brief Handles mouse processes after the update of the main focus.
204  /// @param Code The MetaCode containing the input to be processed.
205  void HandlePostFocusMouseInput(const Input::MetaCode& Code);
206  /// @internal
207  /// @brief Handles controller processes after the update of the main focus.
208  /// @param Code The MetaCode containing the input to be processed.
209  void HandlePostFocusControllerInput(const Input::MetaCode& Code);
210 
211  /// @internal
212  /// @brief Updates the current focus to a new target.
213  /// @param NewFocus The new widget obtaining the UI focus.
214  /// @return Returns true if the target widget successfully gained focus, false otherwise.
215  bool SwitchFocus(Widget* NewFocus);
216  /// @internal
217  /// @brief Gets whether or not the systems current focus is locked.
218  /// @return Returns true if the focus is currently locked, false otherwise.
219  bool FocusIsLocked() const;
220  public:
221  /// @brief Class Constructor.
222  UIManager();
223  /// @brief XML constructor.
224  /// @param XMLNode The node of the xml document to construct from.
225  UIManager(XML::Node& XMLNode);
226  /// @brief Class Destructor.
227  virtual ~UIManager();
228 
229  ///////////////////////////////////////////////////////////////////////////////
230  // Texture Atlas Management
231 
232  /// @brief Loads a Mezzanine Texture Atlas file for use with Screens.
233  /// @param Name The name of the file to be loaded.
234  /// @param Group The resource group where the MTA file can be found.
235  void LoadMTA(const String& Name, const String& Group = "UI");
236  /// @brief Gets a loaded Atlas being stored in this manager.
237  /// @param AtlasName The name of the Atlas, which is usually the name of the file without the extension.
238  /// @return Returns a pointer to the requested Atlas, or NULL if it doesn't exist.
239  TextureAtlas* GetAtlas(const String& AtlasName);
240 
241  ///////////////////////////////////////////////////////////////////////////////
242  // Screen Management
243 
244  /// @brief Creates an internal HUD screen.
245  /// @details Screens are the base set of renderable UI you can use, allowing you to switch entire sets of UI's
246  /// on the fly if needed. For performance reasons you should always keep the number of screens you create to a minimum.
247  /// @param ScreenName The name to be given to the screen.
248  /// @param Atlas The name of a previously loaded mta file to be used with this screen.
249  /// @param WindowViewport The viewport to create this screen in.
250  Screen* CreateScreen(const String& ScreenName, const String& Atlas, Graphics::Viewport* WindowViewport);
251  /// @brief Gets an already created screen by name.
252  /// @return Returns a pointer to the screen of the specified name.
253  Screen* GetScreen(const String& Name);
254  /// @brief Gets an already created screen by index.
255  /// @return Returns a pointer to the screen at the specified index.
256  Screen* GetScreen(const Whole& Index);
257  /// @brief Gets the number of screens created and stored in this manager.
258  /// @return Returns the number of screens this manager is storing.
259  Whole GetNumScreens();
260  /// @brief Deletes a screen and removes all trace of it from the manager.
261  /// @details Destroying a screen will also destroy all of it's layers, and everything contained in those layers.
262  /// @param Screen The screen to be destroyed.
263  void DestroyScreen(Screen* Screen);
264  /// @brief Deletes all screens stored in this manager.
265  void DestroyAllScreens();
266 
267  /// @brief Shows all screens bound to a specific viewport.
268  /// @param WindowViewport The viewport bound to all screens to be shown.
269  void ShowScreensOnViewport(Graphics::Viewport* WindowViewport);
270  /// @brief Shows all screens owned by this manager.
271  void ShowAllScreens();
272  /// @brief Hides all screens bound to a specific viewport.
273  /// @param WindowViewport The viewport bound to all screens to be hidden.
274  /// @param Exclude Optional parameter that will make a single screen be excluded from this operation.
275  void HideScreensOnViewport(Graphics::Viewport* WindowViewport, Screen* Exclude = NULL);
276  /// @brief Hides all screens owned by this manager.
277  void HideAllScreens();
278  /// @brief Gets the currently visible screen on the provided viewport.
279  /// @note If there are multiple screens visible on a single viewport then the first screen (in construction order) will be returned.
280  /// @param WindowViewport The viewport to check for a visible screen.
281  /// @return Returns a pointer to the screen currently being shown on the requested viewport.
282  Screen* GetVisibleScreenOnViewport(Graphics::Viewport* WindowViewport);
283 
284  ///////////////////////////////////////////////////////////////////////////////
285  // HotKey Management
286 
287  /// @brief Binds a key to a button.
288  /// @details This function allows buttons to behave like they are pressed without mouse input.
289  /// @param HotKey The key or button (on the input device) to activate the button.
290  /// @param BoundButton The button being bound to the hotkey.
291  void BindHotKey(const Input::MetaCode& HotKey, Button* BoundButton);
292  /// @brief Removes a previously set hotkey binding.
293  /// @param HotKey The key or button (on the input device) to activate the button.
294  /// @param BoundButton The button currently bound to the hotkey.
295  void UnbindHotKey(const Input::MetaCode& HotKey, Button* BoundButton);
296 
297  ///////////////////////////////////////////////////////////////////////////////
298  // Activation Management
299 
300  /// @brief Enables whether or not to automatically set the activation key or button for UI buttons.
301  /// @details If true, this will cause every UI button to have keyboard keys or mouse buttons added with
302  /// AddButtonAutoRegister() to be added as activation keys or buttons immediately after they are created. @n
303  /// Default: False.
304  /// @param Enable Whether or not to enable this feature.
305  void EnableButtonAutoRegister(bool Enable);
306  /// @brief Gets whether or not the ButtonAutoRegister feature is enabled.
307  /// @return Returns a bool indicating whether or not the ButtonAutoRegister feature is enabled.
308  bool ButtonAutoRegisterEnabled();
309  /// @brief Adds a key or button that will be auto-registered with every created UI button.
310  /// @param Code The input code for the keyboard key or mouse button to be added to the list of codes to be auto-registered.
311  void AddAutoRegisterCode(const Input::MetaCode& Code);
312  /// @brief Removes a previously set auto-registering input code.
313  /// @param Code The input code to be removed from the list of auto-registering codes.
314  void RemoveAutoRegisterCode(const Input::MetaCode& Code);
315  /// @brief Removes all auto-registering input codes.
316  void RemoveAllAutoRegisterCodes();
317  /// @brief Gets the list of codes that will be auto-registered with each UI button.
318  /// @return Returns a pointer to the vector containing all the codes to be auto-registered with every UI button.
319  InputContainer* GetAutoRegisteredCodes();
320 
321  ///////////////////////////////////////////////////////////////////////////////
322  // MarkupParser Management
323 
324  /// @brief Registers a new MarkupParser with this Manager.
325  /// @exception This method will thrown a "II_DUPLICATE_IDENTITY_EXCEPTION" if the provided name is already registered.
326  /// @param ParserName The name to be given to the registered MarkupParser.
327  /// @param ToAdd A pointer to the MarkupParser to be registered with the given name.
328  void RegisterMarkupParser(const String& ParserName, MarkupParser* ToAdd);
329  /// @brief Checks to see if a MarkupParser has already been registsered under a specific name.
330  /// @param ParserName The name of the MarkupParser to check for.
331  /// @return Returns true if a MarkupParser is registered under the specified name.
332  bool IsMarkupParserRegistered(const String& ParserName) const;
333  /// @brief Gets a MarkupParser by it's registered name.
334  /// @param ParserName The name of the MarkupParser to retrieve.
335  /// @return Returns a pointer to the requested MarkupParser, or NULL if none are registered under the specified name.
336  MarkupParser* GetMarkupParser(const String& ParserName) const;
337  /// @brief Removes a MarkupParser from this manager.
338  /// @param ParserName The name of the MarkupParser to be removed.
339  void UnregisterMarkupParser(const String& ParserName);
340  /// @brief Removes all MarkupParsers from this manager.
341  void UnregisterAllMarkupParsers();
342  /// @brief Removes and destroys a MarkupParser from this manager.
343  /// @param ParserName The name of the MarkupParser to be destroyed.
344  void DestroyMarkupParser(const String& ParserName);
345  /// @brief Removes and destroys all MarkupParsers from this manager.
346  void DestroyAllMarkupParsers();
347 
348  ///////////////////////////////////////////////////////////////////////////////
349  // Fetch Methods
350 
351  /// @brief Gets the Widget the mouse is hovering over.
352  /// @return Returns a pointer to the widget, or NULL if it's not over any visable buttons.
353  Widget* GetHoveredWidget() const;
354  /// @brief Gets the current widget being controlled.
355  /// @details The widget control is used mostly for manipulating widgets while the mouse is not
356  /// currently hovering over them, such as the click and drag action of scrollbars and resizing windows.
357  /// @return Returns a pointer to the currently controlled widget, or NULL if none are being controlled this frame.
358  Widget* GetWidgetFocus() const;
359  /// @brief Gets this managers Atlas Handler.
360  /// @return Returns a pointer to this managers Atlas Handler.
361  TextureAtlasHandler* GetAtlasHandler() const;
362 
363  ///////////////////////////////////////////////////////////////////////////////
364  // Utility Methods
365 
366  /// @brief Injects a metacode into this manager, allowing the UI system to be aware of it.
367  /// @param Code The MetaCode to be injected and later processed.
368  void InjectInput(const Input::MetaCode& Code);
369  /// @brief Searches all visable screens and layers to see if a Widget is under the given point.
370  /// @details This is called automatically once every frame. Should only be called on manually if
371  /// you need more then one check per frame.
372  /// @param VP A pointer to the viewport to check.
373  /// @param Point The point on screen(in viewport space) to use when checking Widgets.
374  /// @return Returns the hovered Widget if there is one, NULL if not.
375  Widget* CheckWidgetUnderPoint(Graphics::Viewport* VP, const Vector2& Point);
376  /// @brief Checks to see if the mouse is over a UI element.
377  /// @details This should only be called on after this manager does it's main loop items for best results.
378  /// @return Returns true if the mouse is over a visable UI element, false if not.
379  Boolean MouseIsInUISystem() const;
380  /// @brief Suggests a glyph index based on the desired actual height.
381  /// @param Height The desired Height of the glyph wanted in pixels.
382  /// @param Atlas The altas to search.
383  /// @return Returns a std::pair, First is the name of the font and second is a Real for the scaling that should be provided to it.
384  FontResult SuggestGlyphIndex(const Whole& Height, const String& Atlas);
385 
386  /// @brief Updates the UI system based on the most recent inputs.
387  virtual void UpdateScreens();
388 
389  /// @copydoc ManagerBase::Initialize()
390  virtual void Initialize();
391  /// @copydoc ManagerBase::Deinitialize()
392  virtual void Deinitialize();
393 
394  /// @brief Gets the work unit responsible for updating the widgets in this manager.
395  /// @return Returns a pointer to the WidgetUpdateWorkUnit used by this manager.
396  WidgetUpdateWorkUnit* GetWidgetUpdateWork();
397 
398  ///////////////////////////////////////////////////////////////////////////////
399  // Type Identifier Methods
400 
401  /// @copydoc ManagerBase::GetInterfaceType()
402  virtual ManagerType GetInterfaceType() const;
403  /// @copydoc ManagerBase::GetImplementationTypeName()
404  virtual String GetImplementationTypeName() const;
405  };//UIManager
406 
407  ///////////////////////////////////////////////////////////////////////////////
408  /// @class DefaultUIManagerFactory
409  /// @headerfile uimanager.h
410  /// @brief A factory responsible for the creation and destruction of the default uimanager.
411  ///////////////////////////////////////
413  {
414  public:
415  /// @brief Class constructor.
417  /// @brief Class destructor.
418  virtual ~DefaultUIManagerFactory();
419 
420  /// @copydoc ManagerFactory::GetManagerTypeName()
421  String GetManagerTypeName() const;
422 
423  /// @copydoc ManagerFactory::CreateManager(NameValuePairList&)
424  ManagerBase* CreateManager(NameValuePairList& Params);
425  /// @copydoc ManagerFactory::CreateManager(XML::Node&)
426  ManagerBase* CreateManager(XML::Node& XMLNode);
427  /// @copydoc ManagerFactory::DestroyManager(ManagerBase*)
428  void DestroyManager(ManagerBase* ToBeDestroyed);
429  };//DefaultUIManagerFactory
430  }//UI
431 }//Mezzanine
432 
433 #endif