Unravel Engine C++ Reference
Loading...
Searching...
No Matches
seq_updater.h
Go to the documentation of this file.
1#pragma once
2#include "seq_action.h"
3#include "seq_ease.h"
4#include "seq_math.h"
5#include "seq_private.h"
6
7namespace seq
8{
9
10template<typename Object, typename TargetType, typename InitializeFunc, typename UpdateFunc, typename Getter>
11auto create_action_updater(Object* object,
12 const TargetType& end,
13 const sentinel_t& sentinel,
14 InitializeFunc&& initialize_func,
15 UpdateFunc&& update_func,
16 Getter&& getter,
17 const ease_t& ease_func = ease::linear,
19{
20 auto updater = [object,
21 begin = TargetType{},
22 prev = TargetType{},
23 end = end,
24 sentinel = sentinel,
25 initialize_func = std::forward<InitializeFunc>(initialize_func),
26 update_func = std::forward<UpdateFunc>(update_func),
27 getter = std::forward<Getter>(getter),
28 ease_func = ease_func,
29 interpolate = interpolate,
30 start_required = true,
31 finished = false](duration_t delta, seq_action& self) mutable -> state_t
32 {
33 if(finished)
34 {
35 return state_t::finished;
36 }
37
38 if(sentinel.expired())
39 {
40 finished = true;
41 return state_t::finished;
42 }
43
45 {
46 finished = true;
47 return state_t::finished;
48 }
49
51 {
52 return state_t::paused;
53 }
54
55 const auto duration = seq_private::get_duration(self);
56
57 if(duration == duration_t::zero())
58 {
59 auto old_object = getter(object, self);
60 bool request_on_step = old_object != end;
61 update_func(object, end, self);
62
63 if(request_on_step)
64 {
65 self.on_step.emit();
66 }
68 {
70 {
71 return state_t::paused;
72 }
73 }
74 finished = true;
75 return state_t::finished;
76 }
77
78 if(start_required)
79 {
80 auto old_object = getter(object, self);
81 begin = initialize_func(object, sentinel, self);
82 start_required = false;
83 auto updated_object = getter(object, self);
84 if(old_object != updated_object)
85 {
86 self.on_step.emit();
87 }
89 {
91 {
92 return state_t::paused;
93 }
94 }
95 return state_t::running;
96 }
97
98 seq_private::update_elapsed(self, delta);
99 const auto elapsed = seq_private::get_elapsed(self);
100
101 const float progress = float(elapsed.count()) / float(duration.count());
102 const auto next = interpolate(begin, end, progress, ease_func);
103
104 if(prev != next || next == end)
105 {
106 update_func(object, next, self);
107 self.on_step.emit();
108 prev = next;
109 }
110
112 {
114 {
115 return state_t::paused;
116 }
117 }
118
119 if(elapsed == duration)
120 {
121 finished = true;
122 return state_t::finished;
123 }
124
126 {
127 finished = true;
128 return state_t::finished;
129 }
130
131 return state_t::running;
132 };
133
134 return updater;
135}
136
137} // namespace seq
const btCollisionObject * object
float elapsed
float linear(float progress)
Definition seq_ease.cpp:292
Provides a sequence-based action management system for controlling and scheduling actions.
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.
std::function< float(float)> ease_t
Represents an easing function for interpolation.
Definition seq_common.h:61
auto lerp(const T &start, const T &end, float progress, const ease_t &ease_func=ease::linear) -> T
Linearly interpolates between two values based on progress.
Definition seq_math.hpp:8
std::function< T(const T &, const T &, float, const ease_t &)> interpolate_t
Represents a function for interpolating values.
Definition seq_common.h:74
auto create_action_updater(Object *object, const TargetType &end, const sentinel_t &sentinel, InitializeFunc &&initialize_func, UpdateFunc &&update_func, Getter &&getter, const ease_t &ease_func=ease::linear, const interpolate_t< TargetType > &interpolate=lerp< TargetType >)
Definition seq_updater.h:11
std::chrono::nanoseconds duration_t
Represents a duration in nanoseconds.
Definition seq_common.h:41
Represents an action within the sequence management system. Contains lifecycle events and management ...
Definition seq_action.h:18
static auto get_duration(const seq_action &self) -> duration_t
Gets the total duration of a given action.
static void update_elapsed(seq_action &self, duration_t update_time)
Updates the elapsed time of a given action.
static auto get_state(const seq_action &action) -> state_t
Gets the current state of a given action.
static auto is_stop_and_finished_requested(const seq_action &action) -> bool
Checks if the action is requested to stop and finish.
static auto get_elapsed(const seq_action &self) -> duration_t
Gets the elapsed time of a given action.