Spinning Topp Logo BlackTopp Studios
inc
actionhandler.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 _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  /// @brief This class is the core class responsible for the management of actions.
52  /// @details
53  ///////////////////////////////////////
55  {
56  public:
57  /// @brief Container type for Action instances that are stored by this class.
58  typedef std::map< String,Action* > ActionContainer;
59  /// @brief Iterator type for Action instances that are stored in this class.
60  typedef ActionContainer::iterator ActionIterator;
61  /// @brief Const Iterator type for Action instances that are stored in this class.
62  typedef ActionContainer::const_iterator ConstActionIterator;
63  /// @brief Container type for in-progress Action instances that are stored by this class.
64  typedef std::vector< Action* > ActivatedContainer;
65  /// @brief Iterator type for in-progress Action instances that are stored in this class.
66  typedef ActivatedContainer::iterator ActivatedIterator;
67  /// @brief Const Iterator type for in-progress Action instances that are stored in this class.
68  typedef ActivatedContainer::const_iterator ConstActivatedIterator;
69  /// @brief Container for bindings that connect to input codes to actions.
70  typedef std::multimap< Input::MetaCodeKey,Action* > BindingContainer;
71  /// @brief Iterator type for input-action bindings stored in this class.
72  typedef BindingContainer::iterator BindingIterator;
73  /// @brief Const Iterator type for input-action bindings stored in this class.
74  typedef BindingContainer::const_iterator ConstBindingIterator;
75  /// @brief A convenience type for the actual input-action binding.
76  typedef std::pair< Input::MetaCodeKey,Action* > BindingPair;
77  /// @brief A convenience type for an iterator range of bindings.
78  typedef std::pair<BindingIterator,BindingIterator> BindingRange;
79  /// @brief A convenience type for a const iterator range of bindings.
80  typedef std::pair<ConstBindingIterator,ConstBindingIterator> ConstBindingRange;
81  protected:
82  /// @internal
83  /// @brief Container storing all registered actions.
84  ActionContainer Actions;
85  /// @internal
86  /// @brief Container storing all the bindings that connect inputs to actions.
87  BindingContainer Bindings;
88  /// @internal
89  /// @brief Container storing all the actions that are in-progress.
90  ActivatedContainer ActivatedActions;
91  public:
92  /// @brief Class constructor.
93  ActionHandler();
94  /// @brief Class destructor.
95  ~ActionHandler();
96 
97  ///////////////////////////////////////////////////////////////////////////////
98  // Action Management
99 
100  /// @brief Creates a new Action that can be bound to a MetaCode.
101  /// @param Name The name to be given to the created Action.
102  /// @return Returns a pointer to the created Action.
103  Action* CreateAction(const String& Name);
104  /// @brief Gets an Action by name.
105  /// @param Name The name of the Action to retrieve.
106  /// @return Returns a pointer to the named Action.
107  Action* GetAction(const String& Name);
108  /// @brief Destroy's an action.
109  /// @param ToBeDestroyed The action to be destroyed.
110  void DestroyAction(Action* ToBeDestroyed);
111  /// @brief Destroys all Actions being stored by this Handler.
112  void DestroyAllActions();
113 
114  /// @brief Gets an iterator to the first Action.
115  /// @return Returns an iterator to the first Action stored by this handler.
116  ActionIterator BeginAction();
117  /// @brief Gets an iterator to one-passed-the-last Action.
118  /// @return Returns an iterator to one-passed-the-last Action stored by this handler.
119  ActionIterator EndAction();
120  /// @brief Gets a const iterator to the first Action.
121  /// @return Returns a const iterator to the first Action stored by this handler.
122  ConstActionIterator BeginAction() const;
123  /// @brief Gets a const iterator to one-passed-the-last Action.
124  /// @return Returns a const iterator to one-passed-the-last Action stored by this handler.
125  ConstActionIterator EndAction() const;
126 
127  ///////////////////////////////////////////////////////////////////////////////
128  // Binding Methods
129 
130  /// @brief Gets all Actions bound to a MetaCode.
131  /// @param Code The MetaCode to use to search for bound Actions.
132  /// @return Returns the first and one-passed-the-last iterators representing all of the Actions bound to the provided code.
133  ConstBindingRange GetActionsBoundToCode(const Input::MetaCode& Code);
134 
135  /// @brief Binds a MetaCode to an action, making the action fire when this handler recieves the code.
136  /// @note Only one Action can be bound to a given code. Binding a second Action to a code will unbind the first.
137  /// @param Code The code to trigger the Action.
138  /// @param ToBind The Action to be triggered.
139  /// @param ForceUnique If true this will clear any previous entries that are equal to the MetaCode provided in the binding multimap.
140  void Bind(const Input::MetaCode& Code, Action* ToBind, bool ForceUnique = true);
141  /// @brief Unbinds Actions via MetaCode.
142  /// @note This can unbind multiple Actions.
143  /// @param Code The MetaCode to find and remove all bindings to actions.
144  void Unbind(const Input::MetaCode& Code);
145  /// @brief Unbinds an Action via Action pointer.
146  /// @note This method will iterate over all the elements in the binding multimap in search of all bindings to the provided Action.
147  /// @param ToUnbind The Action to unbind from any and all MetaCodes.
148  void Unbind(Action* ToUnbind);
149  /// @brief Unbinds all actions in this handler.
150  /// @note This does not delete entries for MetaCodeKey's, this simply sets the Action value to NULL.
151  void UnbindAll();
152  /// @brief Completely removes all bindings from this Handler.
153  void RemoveAllBindings();
154 
155  /// @brief Gets an iterator to the first Binding.
156  /// @return Returns an iterator to the first Binding stored by this handler.
157  BindingIterator BeginBinding();
158  /// @brief Gets an iterator to one-passed-the-last Binding.
159  /// @return Returns an iterator to one-passed-the-last Binding stored by this handler.
160  BindingIterator EndBinding();
161  /// @brief Gets a const iterator to the first Binding.
162  /// @return Returns a const iterator to the first Binding stored by this handler.
163  ConstBindingIterator BeginBinding() const;
164  /// @brief Gets a const iterator to one-passed-the-last Binding.
165  /// @return Returns a const iterator to one-passed-the-last Binding stored by this handler.
166  ConstBindingIterator EndBinding() const;
167 
168  ///////////////////////////////////////////////////////////////////////////////
169  // Internal Methods
170 
171  /// @internal
172  /// @brief Used by Actions to notify this handler it was activated.
173  /// @param BeingActivated The Action calling this method and being activated.
174  void _NotifyActionActivated(Action* BeingActivated);
175  /// @internal
176  /// @brief Handles input passed to this handler.
177  /// @param Code The MetaCode to be processed.
178  /// @return Returns true if this input was consumed/handled, false otherwise.
179  bool _HandleInput(const Input::MetaCode& Code);
180  /// @internal
181  /// @brief Processes all active actions, and deactivates them if necessary.
182  void _ProcessAllActions();
183  };//ActionHandler
184  }//UI
185 }//Mezzanine
186 
187 #endif
ActionContainer::const_iterator ConstActionIterator
Const Iterator type for Action instances that are stored in this class.
Definition: actionhandler.h:62
std::pair< ConstBindingIterator, ConstBindingIterator > ConstBindingRange
A convenience type for a const iterator range of bindings.
Definition: actionhandler.h:80
ActivatedContainer ActivatedActions
Container storing all the actions that are in-progress.
Definition: actionhandler.h:90
BindingContainer::const_iterator ConstBindingIterator
Const Iterator type for input-action bindings stored in this class.
Definition: actionhandler.h:74
std::multimap< Input::MetaCodeKey, Action * > BindingContainer
Container for bindings that connect to input codes to actions.
Definition: actionhandler.h:70
std::pair< Input::MetaCodeKey, Action * > BindingPair
A convenience type for the actual input-action binding.
Definition: actionhandler.h:76
ActionContainer Actions
Container storing all registered actions.
Definition: actionhandler.h:84
This class is the core class responsible for the management of actions.
Definition: actionhandler.h:54
std::map< String, Action * > ActionContainer
Container type for Action instances that are stored by this class.
Definition: actionhandler.h:58
This class represents an action to be taken. Can have multiple inputs bound to it.
Definition: action.h:112
std::pair< BindingIterator, BindingIterator > BindingRange
A convenience type for an iterator range of bindings.
Definition: actionhandler.h:78
BindingContainer Bindings
Container storing all the bindings that connect inputs to actions.
Definition: actionhandler.h:87
std::vector< Action * > ActivatedContainer
Container type for in-progress Action instances that are stored by this class.
Definition: actionhandler.h:64
ActionContainer::iterator ActionIterator
Iterator type for Action instances that are stored in this class.
Definition: actionhandler.h:60
This Determines the kind of user input.
Definition: metacode.h:93
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
ActivatedContainer::const_iterator ConstActivatedIterator
Const Iterator type for in-progress Action instances that are stored in this class.
Definition: actionhandler.h:68
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
BindingContainer::iterator BindingIterator
Iterator type for input-action bindings stored in this class.
Definition: actionhandler.h:72
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
ActivatedContainer::iterator ActivatedIterator
Iterator type for in-progress Action instances that are stored in this class.
Definition: actionhandler.h:66