MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
actionhandler.h
1 //© Copyright 2010 - 2012 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 _uiactionhandler_h
41 #define _uiactionhandler_h
42 
43 #include "Input/metacodekey.h"
44 
45 namespace Mezzanine
46 {
47  namespace UI
48  {
49  class Action;
50  ///////////////////////////////////////////////////////////////////////////////
51  /// @class ActionHandler
52  /// @headerfile actionhandler.h
53  /// @brief This class is the core class responsible for the management of actions.
54  /// @details
55  ///////////////////////////////////////
57  {
58  public:
59  typedef std::map< String,Action* > ActionContainer;
60  typedef ActionContainer::iterator ActionIterator;
61  typedef ActionContainer::const_iterator ConstActionIterator;
62  typedef std::vector< Action* > ActivatedContainer;
63  typedef ActivatedContainer::iterator ActivatedIterator;
64  typedef ActivatedContainer::const_iterator ConstActivatedIterator;
65  typedef std::multimap< Input::MetaCodeKey,Action* > BindingContainer;
66  typedef BindingContainer::iterator BindingIterator;
67  typedef BindingContainer::const_iterator ConstBindingIterator;
68  typedef std::pair< Input::MetaCodeKey,Action* > BindingPair;
69  typedef std::pair<BindingIterator,BindingIterator> BindingRange;
70  typedef std::pair<ConstBindingIterator,ConstBindingIterator> ConstBindingRange;
71  protected:
72  ActionContainer Actions;
73  BindingContainer Bindings;
74  ActivatedContainer ActivatedActions;
75  public:
76  /// @brief Class constructor.
77  ActionHandler();
78  /// @brief Class destructor.
79  ~ActionHandler();
80 
81  ///////////////////////////////////////////////////////////////////////////////
82  // Action Management
83 
84  /// @brief Creates a new Action that can be bound to a MetaCode.
85  /// @param Name The name to be given to the created Action.
86  /// @return Returns a pointer to the created Action.
87  Action* CreateAction(const String& Name);
88  /// @brief Gets an Action by name.
89  /// @param Name The name of the Action to retrieve.
90  /// @return Returns a pointer to the named Action.
91  Action* GetAction(const String& Name);
92  /// @brief Destroy's an action.
93  /// @param ToBeDestroyed The action to be destroyed.
94  void DestroyAction(Action* ToBeDestroyed);
95  /// @brief Destroys all Actions being stored by this Handler.
96  void DestroyAllActions();
97 
98  /// @brief Gets an iterator to the first Action.
99  /// @return Returns an iterator to the first Action stored by this handler.
100  ActionIterator BeginAction();
101  /// @brief Gets an iterator to one-passed-the-last Action.
102  /// @return Returns an iterator to one-passed-the-last Action stored by this handler.
103  ActionIterator EndAction();
104  /// @brief Gets a const iterator to the first Action.
105  /// @return Returns a const iterator to the first Action stored by this handler.
106  ConstActionIterator BeginAction() const;
107  /// @brief Gets a const iterator to one-passed-the-last Action.
108  /// @return Returns a const iterator to one-passed-the-last Action stored by this handler.
109  ConstActionIterator EndAction() const;
110 
111  ///////////////////////////////////////////////////////////////////////////////
112  // Binding Methods
113 
114  /// @brief Gets all Actions bound to a MetaCode.
115  /// @param Code The MetaCode to use to search for bound Actions.
116  /// @return Returns the first and one-passed-the-last iterators representing all of the Actions bound to the provided code.
117  ConstBindingRange GetActionsBoundToCode(const Input::MetaCode& Code);
118 
119  /// @brief Binds a MetaCode to an action, making the action fire when this handler recieves the code.
120  /// @note Only one Action can be bound to a given code. Binding a second Action to a code will unbind the first.
121  /// @param Code The code to trigger the Action.
122  /// @param ToBind The Action to be triggered.
123  /// @param ForceUnique If true this will clear any previous entries that are equal to the MetaCode provided in the binding multimap.
124  void Bind(const Input::MetaCode& Code, Action* ToBind, bool ForceUnique = true);
125  /// @brief Unbinds Actions via MetaCode.
126  /// @note This can unbind multiple Actions.
127  /// @param Code The MetaCode to find and remove all bindings to actions.
128  void Unbind(const Input::MetaCode& Code);
129  /// @brief Unbinds an Action via Action pointer.
130  /// @note This method will iterate over all the elements in the binding multimap in search of all bindings to the provided Action.
131  /// @param ToUnbind The Action to unbind from any and all MetaCodes.
132  void Unbind(Action* ToUnbind);
133  /// @brief Unbinds all actions in this handler.
134  /// @note This does not delete entries for MetaCodeKey's, this simply sets the Action value to NULL.
135  void UnbindAll();
136  /// @brief Completely removes all bindings from this Handler.
137  void RemoveAllBindings();
138 
139  /// @brief Gets an iterator to the first Binding.
140  /// @return Returns an iterator to the first Binding stored by this handler.
141  BindingIterator BeginBinding();
142  /// @brief Gets an iterator to one-passed-the-last Binding.
143  /// @return Returns an iterator to one-passed-the-last Binding stored by this handler.
144  BindingIterator EndBinding();
145  /// @brief Gets a const iterator to the first Binding.
146  /// @return Returns a const iterator to the first Binding stored by this handler.
147  ConstBindingIterator BeginBinding() const;
148  /// @brief Gets a const iterator to one-passed-the-last Binding.
149  /// @return Returns a const iterator to one-passed-the-last Binding stored by this handler.
150  ConstBindingIterator EndBinding() const;
151 
152  ///////////////////////////////////////////////////////////////////////////////
153  // Internal Methods
154 
155  /// @internal
156  /// @brief Used by Actions to notify this handler it was activated.
157  /// @param BeingActivated The Action calling this method and being activated.
158  void _NotifyActionActivated(Action* BeingActivated);
159  /// @internal
160  /// @brief Handles input passed to this handler.
161  /// @param Code The MetaCode to be processed.
162  /// @return Returns true if this input was consumed/handled, false otherwise.
163  bool _HandleInput(const Input::MetaCode& Code);
164  /// @internal
165  /// @brief Processes all active actions, and deactivates them if necessary.
166  void _ProcessAllActions();
167  };//ActionHandler
168  }//UI
169 }//Mezzanine
170 
171 #endif