Spinning Topp Logo BlackTopp Studios
inc
actionhandler.cpp
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_cpp
41 #define _uiactionhandler_cpp
42 
43 #include "UI/actionhandler.h"
44 #include "UI/action.h"
45 
46 namespace Mezzanine
47 {
48  namespace UI
49  {
51  {
52 
53  }
54 
56  {
57 
58  }
59 
60  ///////////////////////////////////////////////////////////////////////////////
61  // Action Management
62 
64  {
65  ActionIterator ActIt = Actions.find(Name);
66  if( ActIt == Actions.end() )
67  {
68  Action* NewAction = new Action(Name,this);
69  Actions.insert( std::pair<String,Action*>(Name,NewAction) );
70  return NewAction;
71  }else{
72  MEZZ_EXCEPTION(ExceptionBase::II_DUPLICATE_IDENTITY_EXCEPTION,"An Action with the name \"" + Name + "\" already exists.");
73  }
74  }
75 
77  {
78  ActionIterator ActIt = Actions.find(Name);
79  if( ActIt != Actions.end() ) return (*ActIt).second;
80  else return NULL;
81  }
82 
83  void ActionHandler::DestroyAction(Action* ToBeDestroyed)
84  {
85  Unbind(ToBeDestroyed);
86  ActionIterator ActIt = Actions.find( ToBeDestroyed->GetName() );
87  if( ActIt != Actions.end() )
88  {
89  delete ToBeDestroyed;
90  Actions.erase(ActIt);
91  }
92  }
93 
95  {
96  UnbindAll();
97  for( ActionIterator ActIt = Actions.begin() ; ActIt != Actions.end() ; ++ActIt )
98  {
99  delete (*ActIt).second;
100  }
101  Actions.clear();
102  }
103 
105  {
106  return Actions.begin();
107  }
108 
110  {
111  return Actions.end();
112  }
113 
115  {
116  return Actions.begin();
117  }
118 
120  {
121  return Actions.end();
122  }
123 
124  ///////////////////////////////////////////////////////////////////////////////
125  // Binding Methods
126 
128  {
129  return Bindings.equal_range( Input::MetaCodeKey(Code) );
130  }
131 
132  void ActionHandler::Bind(const Input::MetaCode& Code, Action* ToBind, bool ForceUnique)
133  {
134  Input::MetaCodeKey Temp(Code);
135  if( ForceUnique )
136  {
137  BindingRange Range = Bindings.equal_range(Temp);
138  if( Range.first != Range.second )
139  {
140  Bindings.erase(Range.first,Range.second);
141  }
142  }
143  Bindings.insert( BindingPair(Temp,ToBind) );
144  }
145 
147  {
148  BindingRange Range = Bindings.equal_range( Input::MetaCodeKey(Code) );
149  for( BindingIterator BindIt = Range.first ; BindIt != Range.second ; ++BindIt )
150  (*BindIt).second = NULL;
151  }
152 
154  {
155  for( BindingIterator BindIt = Bindings.begin() ; BindIt != Bindings.end() ; ++BindIt )
156  {
157  if( ToUnbind == (*BindIt).second )
158  (*BindIt).second = NULL;
159  }
160  }
161 
163  {
164  for( BindingIterator BindIt = Bindings.begin() ; BindIt != Bindings.end() ; ++BindIt )
165  {
166  (*BindIt).second = NULL;
167  }
168  }
169 
171  {
172  Bindings.clear();
173  }
174 
176  {
177  return Bindings.begin();
178  }
179 
181  {
182  return Bindings.end();
183  }
184 
186  {
187  return Bindings.begin();
188  }
189 
191  {
192  return Bindings.end();
193  }
194 
195  ///////////////////////////////////////////////////////////////////////////////
196  // Internal Methods
197 
199  {
200  for( ActivatedIterator ActIt = ActivatedActions.begin() ; ActIt != ActivatedActions.end() ; ++ActIt )
201  {
202  if( BeingActivated == (*ActIt) )
203  return;
204  }
205  ActivatedActions.push_back(BeingActivated);
206  }
207 
209  {
210  Input::MetaCodeKey Temp(Code);
211  BindingRange CodeRange = Bindings.equal_range( Temp );
212  if( CodeRange.first != CodeRange.second )
213  {
214 
215  }
216  return false;
217  }
218 
220  {
221 
222  }
223  }//UI
224 }//Mezzanine
225 
226 #endif
const String & GetName() const
Gets the name of this Action.
Definition: action.cpp:67
BindingIterator BeginBinding()
Gets an iterator to the first Binding.
Thrown when duplicates of teh same identity string exist.
Definition: exception.h:95
ActionContainer::const_iterator ConstActionIterator
Const Iterator type for Action instances that are stored in this class.
Definition: actionhandler.h:62
void DestroyAllActions()
Destroys all Actions being stored by this Handler.
void Bind(const Input::MetaCode &Code, Action *ToBind, bool ForceUnique=true)
Binds a MetaCode to an action, making the action fire when this handler recieves the code...
std::pair< ConstBindingIterator, ConstBindingIterator > ConstBindingRange
A convenience type for a const iterator range of bindings.
Definition: actionhandler.h:80
void _ProcessAllActions()
Processes all active actions, and deactivates them if necessary.
#define MEZZ_EXCEPTION(num, desc)
An easy way to throw exceptions with rich information.
Definition: exception.h:3048
A key class for MetaCodes to be used in associative containers.
Definition: metacodekey.h:56
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::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
~ActionHandler()
Class destructor.
Action * CreateAction(const String &Name)
Creates a new Action that can be bound to a MetaCode.
BindingIterator EndBinding()
Gets an iterator to one-passed-the-last Binding.
This class represents an action to be taken. Can have multiple inputs bound to it.
Definition: action.h:112
bool _HandleInput(const Input::MetaCode &Code)
Handles input passed to this handler.
std::pair< BindingIterator, BindingIterator > BindingRange
A convenience type for an iterator range of bindings.
Definition: actionhandler.h:78
void DestroyAction(Action *ToBeDestroyed)
Destroy's an action.
BindingContainer Bindings
Container storing all the bindings that connect inputs to actions.
Definition: actionhandler.h:87
void RemoveAllBindings()
Completely removes all bindings from this Handler.
ActionContainer::iterator ActionIterator
Iterator type for Action instances that are stored in this class.
Definition: actionhandler.h:60
void _NotifyActionActivated(Action *BeingActivated)
Used by Actions to notify this handler it was activated.
This Determines the kind of user input.
Definition: metacode.h:93
ConstBindingRange GetActionsBoundToCode(const Input::MetaCode &Code)
Gets all Actions bound to a MetaCode.
ActionIterator EndAction()
Gets an iterator to one-passed-the-last Action.
Action * GetAction(const String &Name)
Gets an Action by name.
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
ActionHandler()
Class constructor.
ActionIterator BeginAction()
Gets an iterator to the first Action.
void UnbindAll()
Unbinds all actions in this handler.
void Unbind(const Input::MetaCode &Code)
Unbinds Actions via MetaCode.
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