|
| 1 | +#include "godot/performer.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | + |
| 5 | +#include "godot/engine.h" |
| 6 | +#include "godot_cpp/core/class_db.hpp" |
| 7 | + |
| 8 | +namespace barely::godot { |
| 9 | + |
| 10 | +using ::godot::Callable; |
| 11 | +using ::godot::ClassDB; |
| 12 | +using ::godot::D_METHOD; |
| 13 | +using ::godot::PropertyHint; |
| 14 | +using ::godot::PropertyInfo; |
| 15 | +using ::godot::Ref; |
| 16 | +using ::godot::TypedArray; |
| 17 | +using ::godot::Variant; |
| 18 | + |
| 19 | +void BarelyTaskResource::set_position(double position) { |
| 20 | + position_ = position; |
| 21 | + emit_changed(); |
| 22 | +} |
| 23 | + |
| 24 | +void BarelyTaskResource::set_duration(double duration) { |
| 25 | + static constexpr double kMinTaskDuration = 1e-6; |
| 26 | + duration_ = std::max(duration, kMinTaskDuration); |
| 27 | + emit_changed(); |
| 28 | +} |
| 29 | + |
| 30 | +void BarelyTaskResource::set_priority(int32_t priority) { |
| 31 | + priority_ = priority; |
| 32 | + emit_changed(); |
| 33 | +} |
| 34 | + |
| 35 | +void BarelyTaskResource::set_begin_callback(const Callable& callback) { |
| 36 | + begin_callback_ = callback; |
| 37 | + emit_changed(); |
| 38 | +} |
| 39 | + |
| 40 | +void BarelyTaskResource::set_end_callback(const Callable& callback) { |
| 41 | + end_callback_ = callback; |
| 42 | + emit_changed(); |
| 43 | +} |
| 44 | + |
| 45 | +void BarelyTaskResource::_bind_methods() { |
| 46 | + ClassDB::bind_method(D_METHOD("set_position", "position"), &BarelyTaskResource::set_position); |
| 47 | + ClassDB::bind_method(D_METHOD("get_position"), &BarelyTaskResource::get_position); |
| 48 | + |
| 49 | + ClassDB::bind_method(D_METHOD("set_duration", "duration"), &BarelyTaskResource::set_duration); |
| 50 | + ClassDB::bind_method(D_METHOD("get_duration"), &BarelyTaskResource::get_duration); |
| 51 | + |
| 52 | + ClassDB::bind_method(D_METHOD("set_priority", "priority"), &BarelyTaskResource::set_priority); |
| 53 | + ClassDB::bind_method(D_METHOD("get_priority"), &BarelyTaskResource::get_priority); |
| 54 | + |
| 55 | + ClassDB::bind_method(D_METHOD("set_begin_callback", "callback"), |
| 56 | + &BarelyTaskResource::set_begin_callback); |
| 57 | + ClassDB::bind_method(D_METHOD("get_begin_callback"), &BarelyTaskResource::get_begin_callback); |
| 58 | + |
| 59 | + ClassDB::bind_method(D_METHOD("set_end_callback", "callback"), |
| 60 | + &BarelyTaskResource::set_end_callback); |
| 61 | + ClassDB::bind_method(D_METHOD("get_end_callback"), &BarelyTaskResource::get_end_callback); |
| 62 | + |
| 63 | + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "position"), "set_position", "get_position"); |
| 64 | + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "duration"), "set_duration", "get_duration"); |
| 65 | + ADD_PROPERTY(PropertyInfo(Variant::INT, "priority"), "set_priority", "get_priority"); |
| 66 | + |
| 67 | + ADD_PROPERTY(PropertyInfo(Variant::CALLABLE, "begin_callback"), "set_begin_callback", |
| 68 | + "get_begin_callback"); |
| 69 | + ADD_PROPERTY(PropertyInfo(Variant::CALLABLE, "end_callback"), "set_end_callback", |
| 70 | + "get_end_callback"); |
| 71 | +} |
| 72 | + |
| 73 | +BarelyPerformer::BarelyPerformer() { |
| 74 | + BarelyEngine_CreatePerformer(BarelyEngine::get_singleton()->get(), &performer_id_); |
| 75 | + tasks_ = TypedArray<Ref<BarelyTaskResource>>(); |
| 76 | +} |
| 77 | + |
| 78 | +BarelyPerformer::~BarelyPerformer() { |
| 79 | + _clear_tasks(); |
| 80 | + BarelyEngine_DestroyPerformer(BarelyEngine::get_singleton()->get(), performer_id_); |
| 81 | +} |
| 82 | + |
| 83 | +void BarelyPerformer::start() { |
| 84 | + BarelyPerformer_Start(BarelyEngine::get_singleton()->get(), performer_id_); |
| 85 | +} |
| 86 | + |
| 87 | +void BarelyPerformer::stop() { |
| 88 | + BarelyPerformer_Stop(BarelyEngine::get_singleton()->get(), performer_id_); |
| 89 | +} |
| 90 | + |
| 91 | +void BarelyPerformer::set_position(double position) { |
| 92 | + BarelyPerformer_SetPosition(BarelyEngine::get_singleton()->get(), performer_id_, position); |
| 93 | +} |
| 94 | + |
| 95 | +void BarelyPerformer::set_loop_begin_position(double loop_begin_position) { |
| 96 | + loop_begin_position_ = loop_begin_position; |
| 97 | + |
| 98 | + BarelyPerformer_SetLoopBeginPosition(BarelyEngine::get_singleton()->get(), performer_id_, |
| 99 | + loop_begin_position_); |
| 100 | +} |
| 101 | + |
| 102 | +void BarelyPerformer::set_loop_length(double loop_length) { |
| 103 | + loop_length_ = loop_length; |
| 104 | + |
| 105 | + BarelyPerformer_SetLoopLength(BarelyEngine::get_singleton()->get(), performer_id_, loop_length_); |
| 106 | +} |
| 107 | + |
| 108 | +void BarelyPerformer::set_looping(bool looping) { |
| 109 | + looping_ = looping; |
| 110 | + |
| 111 | + BarelyPerformer_SetLooping(BarelyEngine::get_singleton()->get(), performer_id_, looping_); |
| 112 | +} |
| 113 | + |
| 114 | +void BarelyPerformer::set_tasks(const TypedArray<Ref<BarelyTaskResource>>& tasks) { |
| 115 | + for (int i = 0; i < tasks_.size(); ++i) { |
| 116 | + Ref<BarelyTaskResource> task = tasks_[i]; |
| 117 | + if (task.is_valid()) { |
| 118 | + task->disconnect("changed", Callable(this, "_on_task_changed")); |
| 119 | + } |
| 120 | + } |
| 121 | + tasks_ = tasks; |
| 122 | + for (int i = 0; i < tasks_.size(); ++i) { |
| 123 | + Ref<BarelyTaskResource> task = tasks_[i]; |
| 124 | + if (task.is_valid()) { |
| 125 | + task->connect("changed", Callable(this, "_on_task_changed")); |
| 126 | + } |
| 127 | + } |
| 128 | + _on_task_changed(); |
| 129 | +} |
| 130 | + |
| 131 | +double BarelyPerformer::get_position() const { |
| 132 | + double position = 0.0; |
| 133 | + BarelyPerformer_GetPosition(BarelyEngine::get_singleton()->get(), performer_id_, &position); |
| 134 | + return position; |
| 135 | +} |
| 136 | + |
| 137 | +bool BarelyPerformer::is_playing() const { |
| 138 | + bool playing = false; |
| 139 | + BarelyPerformer_IsPlaying(BarelyEngine::get_singleton()->get(), performer_id_, &playing); |
| 140 | + return playing; |
| 141 | +} |
| 142 | + |
| 143 | +void BarelyPerformer::_bind_methods() { |
| 144 | + ClassDB::bind_method(D_METHOD("start"), &BarelyPerformer::start); |
| 145 | + ClassDB::bind_method(D_METHOD("stop"), &BarelyPerformer::stop); |
| 146 | + |
| 147 | + ClassDB::bind_method(D_METHOD("set_position", "position"), &BarelyPerformer::set_position); |
| 148 | + ClassDB::bind_method(D_METHOD("get_position"), &BarelyPerformer::get_position); |
| 149 | + |
| 150 | + ClassDB::bind_method(D_METHOD("is_playing"), &BarelyPerformer::is_playing); |
| 151 | + |
| 152 | + ClassDB::bind_method(D_METHOD("set_loop_begin_position", "position"), |
| 153 | + &BarelyPerformer::set_loop_begin_position); |
| 154 | + ClassDB::bind_method(D_METHOD("get_loop_begin_position"), |
| 155 | + &BarelyPerformer::get_loop_begin_position); |
| 156 | + ClassDB::bind_method(D_METHOD("set_loop_length", "length"), &BarelyPerformer::set_loop_length); |
| 157 | + ClassDB::bind_method(D_METHOD("get_loop_length"), &BarelyPerformer::get_loop_length); |
| 158 | + ClassDB::bind_method(D_METHOD("set_looping", "looping"), &BarelyPerformer::set_looping); |
| 159 | + ClassDB::bind_method(D_METHOD("is_looping"), &BarelyPerformer::is_looping); |
| 160 | + |
| 161 | + ClassDB::bind_method(D_METHOD("set_tasks", "tasks"), &BarelyPerformer::set_tasks); |
| 162 | + ClassDB::bind_method(D_METHOD("get_tasks"), &BarelyPerformer::get_tasks); |
| 163 | + ClassDB::bind_method(D_METHOD("_on_task_changed"), &BarelyPerformer::_on_task_changed); |
| 164 | + |
| 165 | + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_begin_position"), "set_loop_begin_position", |
| 166 | + "get_loop_begin_position"); |
| 167 | + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "loop_length"), "set_loop_length", "get_loop_length"); |
| 168 | + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "looping"), "set_looping", "is_looping"); |
| 169 | + |
| 170 | + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "position"), "set_position", "get_position"); |
| 171 | + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "tasks", PropertyHint::PROPERTY_HINT_ARRAY_TYPE, |
| 172 | + "BarelyTaskResource"), |
| 173 | + "set_tasks", "get_tasks"); |
| 174 | +} |
| 175 | + |
| 176 | +void BarelyPerformer::_clear_tasks() { |
| 177 | + for (uint32_t task_id : task_ids_) { |
| 178 | + BarelyEngine_DestroyTask(BarelyEngine::get_singleton()->get(), task_id); |
| 179 | + } |
| 180 | + task_ids_.clear(); |
| 181 | +} |
| 182 | + |
| 183 | +void BarelyPerformer::_on_task_changed() { |
| 184 | + _clear_tasks(); |
| 185 | + |
| 186 | + for (int i = 0; i < tasks_.size(); ++i) { |
| 187 | + Ref<BarelyTaskResource> task = tasks_[i]; |
| 188 | + if (task.is_null()) { |
| 189 | + continue; |
| 190 | + } |
| 191 | + |
| 192 | + uint32_t task_id = 0; |
| 193 | + BarelyEngine_CreateTask( |
| 194 | + BarelyEngine::get_singleton()->get(), performer_id_, task->get_position(), |
| 195 | + task->get_duration(), task->get_priority(), |
| 196 | + [](BarelyEventType type, void* user_data) { |
| 197 | + BarelyTaskResource* task = static_cast<BarelyTaskResource*>(user_data); |
| 198 | + |
| 199 | + if (!task) return; |
| 200 | + |
| 201 | + if (type == BarelyEventType_kBegin) { |
| 202 | + const auto& cb = task->get_begin_callback(); |
| 203 | + if (cb.is_valid()) cb.call(); |
| 204 | + } else if (type == BarelyEventType_kEnd) { |
| 205 | + const auto& cb = task->get_end_callback(); |
| 206 | + if (cb.is_valid()) cb.call(); |
| 207 | + } |
| 208 | + }, |
| 209 | + task.ptr(), &task_id); |
| 210 | + task_ids_.push_back(task_id); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +} // namespace barely::godot |
0 commit comments