Unravel Engine C++ Reference
Loading...
Searching...
No Matches
seq_action.cpp
Go to the documentation of this file.
1#include "seq_action.h"
2#include "seq_inspector.h"
3
4namespace seq
5{
7
9 : id_(unique_id++)
10 , creator_(std::move(creator))
11 , duration_(duration)
12 , sentinel_(std::move(sentinel))
13{
14 if(duration_ < duration_t::zero())
15 {
16 duration_ = duration_t::zero();
17 }
18}
19
21{
22 return id_;
23}
24
25seq_action::operator seq_id_t() const
26{
27 return get_id();
28}
29
30auto seq_action::is_valid() const noexcept -> bool
31{
32 return get_id() > 0;
33}
34
35void seq_action::update_elapsed(duration_t update_time)
36{
37 elapsed_ += update_time;
38
39 elapsed_not_clamped_ += update_time;
40
41 clamp_elapsed();
42}
43
44void seq_action::set_elapsed(duration_t elapsed)
45{
46 elapsed_ = elapsed;
47 elapsed_not_clamped_ = elapsed;
48
49 clamp_elapsed();
50}
51
52void seq_action::clamp_elapsed()
53{
54 elapsed_ = std::max(elapsed_, duration_t::zero());
55 elapsed_ = std::min(elapsed_, duration_);
56
57 elapsed_not_clamped_ = std::max(elapsed_not_clamped_, duration_t::zero());
58}
59
60void seq_action::start()
61{
62 if(creator_ == nullptr)
63 {
64 return;
65 }
66
67 state_ = state_t::running;
68 elapsed_ = duration_t::zero();
69 elapsed_not_clamped_ = duration_t::zero();
70 on_begin.emit();
71 updater_ = creator_();
72 state_ = updater_(0ms, *this);
73 inspector::update_action_state(*this, state_);
74
75 if(state_ == state_t::finished)
76 {
77 if(!sentinel_.expired())
78 {
79 on_end.emit();
80 }
81 }
82}
83
84void seq_action::stop()
85{
86 state_ = state_t::finished;
87 inspector::update_action_state(*this, state_);
88}
89
90void seq_action::resume(const std::string& key, bool force)
91{
92 if(state_ == state_t::paused)
93 {
94 if(force || pause_key_ == key)
95 {
96 pause_key_ = {};
97 state_ = state_t::running;
98 inspector::update_action_state(*this, state_);
99 }
100 }
101}
102
103void seq_action::pause(const std::string& key)
104{
105 if(state_ == state_t::running)
106 {
107 pause_forced(key);
108 }
109}
110
111void seq_action::pause_forced(const std::string& key)
112{
113 pause_key_ = key;
114
115 pause_forced();
116}
117
118void seq_action::pause_forced()
119{
120 state_ = state_t::paused;
121 inspector::update_action_state(*this, state_);
122}
123
124auto seq_action::update(duration_t delta) -> state_t
125{
126 if(state_ == state_t::finished || state_ == state_t::paused)
127 {
128 return state_;
129 }
130
131 auto update_time = (delta.count() * int64_t(speed_multiplier_ * 1000.0f)) / int64_t(1000);
132 if(updater_)
133 {
134 state_ = updater_(duration_t(update_time), *this);
135 }
136 else
137 {
138 stop();
139 }
140
141 if(state_ == state_t::finished)
142 {
143 inspector::update_action_state(*this, state_);
144 if(!sentinel_.expired())
145 {
146 on_end.emit();
147 }
148 }
149 else if(state_ == state_t::running)
150 {
151 on_update.emit();
152 }
153 return state_;
154}
155
156}
float elapsed
void update_action_state(seq_action &action, state_t state)
Provides a sequence-based action management system for controlling and scheduling actions.
size_t seq_id_t
Represents a unique identifier for sequence actions.
Definition seq_common.h:53
hpp::sentinel sentinel_t
Alias for a sentinel object used for lifecycle management.
Definition seq_common.h:47
state_t
Represents the state of a sequence action.
Definition seq_common.h:81
@ running
The action is running.
@ finished
The action has finished.
@ paused
The action is paused.
seq::seq_action creator(const std::string &type, values_t< T > &values, T &begin, const T &end, seq::duration_t duration)
Definition tests.cpp:195
seq_id_t unique_id
Definition seq_action.cpp:6
void stop(seq_id_t id)
Stops the action associated with the given ID.
Definition seq.cpp:14
std::chrono::nanoseconds duration_t
Represents a duration in nanoseconds.
Definition seq_common.h:41
std::function< updater_t()> creator_t
Type alias for a creator function that generates an updater function for the action.
Definition seq_action.h:33
auto is_valid() const noexcept -> bool
Checks if the action is valid.
auto get_id() const -> seq_id_t
Gets the unique ID of the action.
seq_action()=default
hpp::event< void()> on_end
Event emitted when the action is finished. Not emitted if the action is stopped prematurely.
Definition seq_action.h:70
hpp::event< void()> on_begin
Event emitted when the action is started.
Definition seq_action.h:55