CUGL 1.3
Cornell University Game Library
CUActionManager.h
1 //
2 // CUActionManager.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module provides the manager to provide tweened animations. Actions
6 // themselves are not animations; they are only templates for an animation.
7 // The action manager attaches an action to a scene graph node and performs
8 // the (timed) animation.
9 //
10 // 1. The constructor does not perform any initialization; it just sets all
11 // attributes to their defaults.
12 //
13 // 2. All initialization takes place via init methods, which can fail if an
14 // object is initialized more than once.
15 //
16 // 3. All allocation takes place via static constructors which return a shared
17 // pointer.
18 //
19 // CUGL MIT License:
20 // This software is provided 'as-is', without any express or implied
21 // warranty. In no event will the authors be held liable for any damages
22 // arising from the use of this software.
23 //
24 // Permission is granted to anyone to use this software for any purpose,
25 // including commercial applications, and to alter it and redistribute it
26 // freely, subject to the following restrictions:
27 //
28 // 1. The origin of this software must not be misrepresented; you must not
29 // claim that you wrote the original software. If you use this software
30 // in a product, an acknowledgment in the product documentation would be
31 // appreciated but is not required.
32 //
33 // 2. Altered source versions must be plainly marked as such, and must not
34 // be misrepresented as being the original software.
35 //
36 // 3. This notice may not be removed or altered from any source distribution.
37 //
38 // Author: Sophie Huang and Walker White
39 // Version: 3/12/17
40 //
41 #ifndef __CU_ACTION_MANAGER_H__
42 #define __CU_ACTION_MANAGER_H__
43 
44 #include "CUAction.h"
45 #include <SDL/SDL.h>
46 #include <unordered_map>
47 #include <unordered_set>
48 #include <algorithm>
49 
50 namespace cugl {
51 
64 #pragma mark ActionInstance
65 
76  class ActionInstance {
77  public:
79  std::shared_ptr<Node> target;
80 
82  std::shared_ptr<Action> action;
83 
85  std::function<float(float)> interpolant;
86 
88  Uint64 state;
89 
90  /* The desired completion time of the action */
91  float duration;
92 
94  float elapsed;
95 
97  bool paused;
98 
99  public:
106  ActionInstance() : state(0), duration(0.0f), elapsed(0.0f), paused(false) {}
107 
111  ~ActionInstance();
112  };
113 
114 #pragma mark Values
115 protected:
117  std::unordered_map<Node*, std::unordered_set<std::string>> _keys;
118 
120  std::unordered_map<std::string, ActionInstance*> _actions;
121 
122 
123 public:
124 #pragma mark Constructors
125 
132 
137 
144  void dispose();
145 
151  bool init() { return true; }
152 
153 #pragma mark Static Constructors
154 
159  static std::shared_ptr<ActionManager> alloc() {
160  std::shared_ptr<ActionManager> result = std::make_shared<ActionManager>();
161  return (result->init() ? result : nullptr);
162  }
163 
164 #pragma mark -
165 #pragma mark Action Management
166 
173  bool isActive(std::string key) const;
174 
186  bool activate(std::string key,
187  const std::shared_ptr<Action>& action,
188  const std::shared_ptr<Node>& target) {
189  return activate(key,action,target, nullptr);
190  };
191 
192 
209  bool activate(std::string key,
210  const std::shared_ptr<Action>& action,
211  const std::shared_ptr<Node>& target,
212  std::function<float(float)> easing);
213 
227  bool remove(std::string key);
228 
238  void update(float dt);
239 
240 #pragma mark -
241 #pragma mark Pausing
242 
252  bool isPaused(std::string key);
253 
262  void pause(std::string key);
263 
272  void unpause(std::string key);
273 
274 #pragma mark -
275 #pragma mark Node Management
276 
283  void clearAllActions(const std::shared_ptr<Node>& target);
284 
293  void pauseAllActions(const std::shared_ptr<Node>& target);
294 
303  void unpauseAllActions(const std::shared_ptr<Node>& target);
304 
315  std::vector<std::string> getAllActions(const std::shared_ptr<Node>& target) const;
316 
317 };
318 
319 }
320 #endif /* __CU_ACTION_MANAGER_H__ */
cugl::ActionManager::_actions
std::unordered_map< std::string, ActionInstance * > _actions
Definition: CUActionManager.h:120
cugl::ActionManager::unpauseAllActions
void unpauseAllActions(const std::shared_ptr< Node > &target)
cugl::ActionManager::_keys
std::unordered_map< Node *, std::unordered_set< std::string > > _keys
Definition: CUActionManager.h:117
cugl::ActionManager::update
void update(float dt)
cugl::ActionManager::~ActionManager
~ActionManager()
Definition: CUActionManager.h:136
cugl::ActionManager::dispose
void dispose()
cugl::ActionManager::alloc
static std::shared_ptr< ActionManager > alloc()
Definition: CUActionManager.h:159
cugl::ActionManager::pause
void pause(std::string key)
cugl::ActionManager::unpause
void unpause(std::string key)
cugl::ActionManager::getAllActions
std::vector< std::string > getAllActions(const std::shared_ptr< Node > &target) const
cugl::ActionManager::ActionManager
ActionManager()
Definition: CUActionManager.h:131
cugl::ActionManager::activate
bool activate(std::string key, const std::shared_ptr< Action > &action, const std::shared_ptr< Node > &target)
Definition: CUActionManager.h:186
cugl::ActionManager::init
bool init()
Definition: CUActionManager.h:151
cugl::ActionManager::isPaused
bool isPaused(std::string key)
cugl::ActionManager
Definition: CUActionManager.h:63
cugl::ActionManager::pauseAllActions
void pauseAllActions(const std::shared_ptr< Node > &target)
cugl::ActionManager::remove
bool remove(std::string key)
cugl::ActionManager::isActive
bool isActive(std::string key) const
cugl::ActionManager::clearAllActions
void clearAllActions(const std::shared_ptr< Node > &target)