nomlib
Loading...
Searching...
No Matches
ActionPlayer.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_ACTIONS_ACTION_PLAYER_HPP
30#define NOMLIB_ACTIONS_ACTION_PLAYER_HPP
31
32#include <memory>
33#include <functional>
34#include <vector>
35#include <map>
36#include <deque>
37
38#include "nomlib/config.hpp"
39
40namespace nom {
41
42// Forward declarations
43class IActionObject;
44class DispatchQueue;
45
46typedef std::function<void()> action_callback_func;
47
49class ActionPlayer
50{
51 public:
52 typedef ActionPlayer self_type;
53
55 typedef std::vector<const char*> action_names;
56
58 enum State
59 {
60 RUNNING = 0,
61 PAUSED,
62 STOPPED,
63 };
64
66
68
70 ActionPlayer(const ActionPlayer& rhs) = delete;
71
73 ActionPlayer& operator =(const ActionPlayer& rhs) = delete;
74
80 bool idle() const;
81
83 nom::size_type num_actions() const;
84
89
96 void pause();
97
103 void resume();
104
112 void stop();
113
122 bool action_running(const std::string& action_id) const;
123
129 bool cancel_action(const std::string& action_id);
130
131 void cancel_actions(const action_names& actions);
132
137
146 bool run_action(const std::shared_ptr<IActionObject>& action);
147
159 bool run_action( const std::shared_ptr<IActionObject>& action,
160 const action_callback_func& completion_func );
161
172 bool update(real32 delta_time);
173
174 private:
197 bool run_action( const std::shared_ptr<IActionObject>& action,
198 std::unique_ptr<DispatchQueue> dispatch_queue,
199 const action_callback_func& completion_func );
200
201 static const char* DEBUG_CLASS_NAME;
202
203 typedef
204 std::map<std::string, std::unique_ptr<DispatchQueue>> container_type;
205
206 typedef container_type::iterator container_iterator;
207
208 ActionPlayer::State player_state_;
209
211 container_type actions_;
212
214 std::deque<container_iterator> free_list_;
215};
216
217} // namespace nom
218
219#endif // include guard defined
220
Interface for running and controlling action flow.
State
The status of the player.
bool idle() const
Get the status of the action queue.
bool run_action(const std::shared_ptr< IActionObject > &action)
Enqueue an action.
bool action_running(const std::string &action_id) const
Get the completion status of an action.
void stop()
Reset the enqueued actions back to its initial starting state.
void resume()
Resume the advancement of time for the enqueued actions.
bool run_action(const std::shared_ptr< IActionObject > &action, const action_callback_func &completion_func)
Enqueue an action with a completion callback.
nom::size_type num_actions() const
Get the number of actions enqueued.
ActionPlayer::State player_state() const
Get the control status of the queue.
void cancel_actions()
Stop executing the enqueued actions.
ActionPlayer & operator=(const ActionPlayer &rhs)=delete
Disabled copy assignment operator.
std::deque< container_iterator > free_list_
The actions pending removal.
bool cancel_action(const std::string &action_id)
Stop executing an action.
std::vector< const char * > action_names
bool update(real32 delta_time)
Run the enqueued actions' update loop.
container_type actions_
Enqueued actions.
bool run_action(const std::shared_ptr< IActionObject > &action, std::unique_ptr< DispatchQueue > dispatch_queue, const action_callback_func &completion_func)
Enqueue an action that runs on a specific dispatch queue.
void pause()
Freeze the enqueued actions from advancing forward in time.
ActionPlayer(const ActionPlayer &rhs)=delete
Disabled copy constructor.
The internal processing queue for actions.
Pure virtual base class interface for actions.