载入中...
搜索中...
未找到
MotionScriptBindings.cpp
浏览该文件的文档.
2
8#include "common/Exception.h"
10#include "common/Value.h"
11
12#include <simplesquirrel/simplesquirrel.hpp>
13
14#include <cstdint>
15#include <memory>
16#include <string>
17#include <utility>
18#include <vector>
19
20namespace eve::animation {
21namespace {
22
23[[noreturn]] void throwMotionError(const eve::Diagnostic *diag) {
24 throw Exception("%s", diag ? diag->message().c_str() : "Motion error");
25}
26
27MotionLoopMode parseLoopMode(const std::string &mode) {
28 if (mode.empty() || mode == "restart") return MotionLoopMode::Restart;
29 if (mode == "yoyo") return MotionLoopMode::Yoyo;
30 throw Exception("Motion.loops: unknown mode '%s' (use restart|yoyo)", mode.c_str());
31}
32
33Animation *requireAnimation(Animation *animation) {
34 if (!animation) throw Exception("MotionSequence: Animation is null");
35 return animation;
36}
37
46class ScriptMotionFloatSink final : public IMotionFloatSink {
47public:
48 explicit ScriptMotionFloatSink(ScriptProviderHandle handle) : handle_(std::move(handle)) {}
49
51 [[nodiscard]] const ScriptProviderHandle& handle() const noexcept { return handle_; }
52
53 [[nodiscard]] eve::Result<void> write(float value) override {
54 const eve::ScriptArg args[] = {eve::ScriptArg::fromNumber(double(value))};
55 return eve::ScriptProviderRegistry::instance().call(handle_, "write", args);
56 }
57
58private:
59 ScriptProviderHandle handle_;
60};
61
66double readMotionNumber(const ssq::Object& table, const char* field, const char* what) {
67 if (table.getType() != ssq::Type::TABLE) throw Exception("%s must be a table (x/y/z or r/g/b/a)", what);
68 const ssq::Object value = table.find(field);
69 const ssq::Type type = value.getType();
70 if (type != ssq::Type::FLOAT && type != ssq::Type::INTEGER)
71 throw Exception("%s requires a numeric '%s'", what, field);
72 return value.toFloat();
73}
74
82struct MotionVec2Traits {
83 using Value = MotionVec2;
84 using Desc = MotionRuntime::Vec2Desc;
85 using Sink = IMotionVec2Sink;
86
87 static constexpr bool kHasStyle = true;
88 static constexpr const char* kType = "MotionVec2";
89
90 static Value read(const ssq::Object& table, const char* what) {
91 return MotionVec2{float(readMotionNumber(table, "x", what)), float(readMotionNumber(table, "y", what))};
92 }
93 static std::vector<eve::ScriptArg> scriptArgs(const Value& value) {
95 }
96 static void configure(Desc& desc, Value from, Value to, float duration) {
97 desc.from = from;
98 desc.to = to;
99 desc.duration = duration;
100 }
101 static eve::Result<MotionHandle> spawn(MotionRuntime& runtime, const Desc& desc) { return runtime.spawnVec2(desc); }
102};
103
104struct MotionVec3Traits {
105 using Value = MotionVec3;
106 using Desc = MotionRuntime::Vec3Desc;
107 using Sink = IMotionVec3Sink;
108
109 static constexpr bool kHasStyle = true;
110 static constexpr const char* kType = "MotionVec3";
111
112 static Value read(const ssq::Object& table, const char* what) {
113 return MotionVec3{float(readMotionNumber(table, "x", what)), float(readMotionNumber(table, "y", what)),
114 float(readMotionNumber(table, "z", what))};
115 }
116 static std::vector<eve::ScriptArg> scriptArgs(const Value& value) {
119 }
120 static void configure(Desc& desc, Value from, Value to, float duration) {
121 desc.from = from;
122 desc.to = to;
123 desc.duration = duration;
124 }
125 static eve::Result<MotionHandle> spawn(MotionRuntime& runtime, const Desc& desc) { return runtime.spawnVec3(desc); }
126};
127
128struct MotionColorTraits {
129 using Value = MotionColor;
130 using Desc = MotionRuntime::ColorDesc;
131 using Sink = IMotionColorSink;
132
133 static constexpr bool kHasStyle = false;
134 static constexpr const char* kType = "MotionColor";
135
136 static Value read(const ssq::Object& table, const char* what) {
137 return MotionColor{float(readMotionNumber(table, "r", what)), float(readMotionNumber(table, "g", what)),
138 float(readMotionNumber(table, "b", what)), float(readMotionNumber(table, "a", what))};
139 }
140 static std::vector<eve::ScriptArg> scriptArgs(const Value& value) {
143 }
144 static void configure(Desc& desc, Value from, Value to, float duration) {
145 desc.from = from;
146 desc.to = to;
147 desc.duration = duration;
148 }
149 static eve::Result<MotionHandle> spawn(MotionRuntime& runtime, const Desc& desc) {
150 return runtime.spawnColor(desc);
151 }
152};
153
154struct MotionQuatTraits {
155 using Value = MotionQuat;
156 using Desc = MotionRuntime::QuatDesc;
157 using Sink = IMotionQuatSink;
158
159 static constexpr bool kHasStyle = false;
160 static constexpr const char* kType = "MotionQuat";
161
162 static Value read(const ssq::Object& table, const char* what) {
163 return MotionQuat{float(readMotionNumber(table, "x", what)), float(readMotionNumber(table, "y", what)),
164 float(readMotionNumber(table, "z", what)), float(readMotionNumber(table, "w", what))};
165 }
166 static std::vector<eve::ScriptArg> scriptArgs(const Value& value) {
169 }
170 static void configure(Desc& desc, Value from, Value to, float duration) {
171 desc.from = from;
172 desc.to = to;
173 desc.duration = duration;
174 }
175 static eve::Result<MotionHandle> spawn(MotionRuntime& runtime, const Desc& desc) { return runtime.spawnQuat(desc); }
176};
177
184template <class Traits>
185class ScriptMotionSink final : public Traits::Sink {
186public:
187 explicit ScriptMotionSink(ScriptProviderHandle handle) : handle_(std::move(handle)) {}
188
190 [[nodiscard]] const ScriptProviderHandle& handle() const noexcept { return handle_; }
191
192 [[nodiscard]] eve::Result<void> write(typename Traits::Value value) override {
193 const std::vector<eve::ScriptArg> args = Traits::scriptArgs(value);
194 return eve::ScriptProviderRegistry::instance().call(handle_, "write", args);
195 }
196
197private:
198 ScriptProviderHandle handle_;
199};
200
208template <class Adapter>
209struct ScriptMotionSinkStore {
210 std::vector<std::string> providerNames;
211 std::vector<std::unique_ptr<Adapter>> adapters;
212};
213
214struct ScriptMotionSinkStores {
215 ScriptMotionSinkStore<ScriptMotionFloatSink> floats;
216 ScriptMotionSinkStore<ScriptMotionSink<MotionVec2Traits>> vec2s;
217 ScriptMotionSinkStore<ScriptMotionSink<MotionVec3Traits>> vec3s;
218 ScriptMotionSinkStore<ScriptMotionSink<MotionColorTraits>> colors;
219 ScriptMotionSinkStore<ScriptMotionSink<MotionQuatTraits>> quats;
220};
221
222ScriptMotionSinkStores& scriptMotionSinkStores() {
223 static ScriptMotionSinkStores stores;
224 return stores;
225}
226
227std::uint64_t& scriptMotionSinkSequence() {
228 static std::uint64_t sequence = 0;
229 return sequence;
230}
231
236template <class Adapter>
237Adapter* adoptScriptMotionSink(ScriptMotionSinkStore<Adapter>& store, const ssq::Object& sink) {
238 const std::string name = "animation.motion-sink." + std::to_string(++scriptMotionSinkSequence());
239 auto published = eve::ScriptProviderRegistry::instance().publish(name, sink);
240 if (!published.ok()) throwMotionError(published.error());
241 auto adapter = std::make_unique<Adapter>(published.value());
242 Adapter* raw = adapter.get();
243 store.providerNames.push_back(name);
244 store.adapters.push_back(std::move(adapter));
245 return raw;
246}
247
249template <class Adapter>
250void clearScriptMotionSinkStore(ScriptMotionSinkStore<Adapter>& store) {
252 for (const std::string& name : store.providerNames) {
253 registry.unload(eve::ScriptProviderHandle{name, registry.generationOf(name)})
254 .ignore("module teardown may already have dropped this motion sink provider");
255 }
256 store.providerNames.clear();
257 store.adapters.clear();
258}
259
260} // namespace
261
263 ScriptMotionSinkStores& stores = scriptMotionSinkStores();
264 clearScriptMotionSinkStore(stores.floats);
265 clearScriptMotionSinkStore(stores.vec2s);
266 clearScriptMotionSinkStore(stores.vec3s);
267 clearScriptMotionSinkStore(stores.colors);
268 clearScriptMotionSinkStore(stores.quats);
269}
270
271namespace {
272
273class ScriptMotionHandle {
274public:
275 ScriptMotionHandle(MotionRuntime *runtime, MotionHandle handle)
276 : runtime_(runtime), handle_(handle) {}
277
278 bool isActive() const { return runtime_ && runtime_->isActive(handle_); }
279
280 float value() const {
281 if (!runtime_) throw Exception("Motion.value: no runtime");
282 auto result = runtime_->floatValue(handle_);
283 if (!result.ok()) throwMotionError(result.error());
284 return result.value();
285 }
286
287 void complete() {
288 if (!runtime_) throw Exception("Motion.complete: no runtime");
289 auto result = runtime_->complete(handle_);
290 if (!result.ok()) throwMotionError(result.error());
291 }
292
293 void cancel() {
294 if (!runtime_) throw Exception("Motion.cancel: no runtime");
295 auto result = runtime_->cancel(handle_);
296 if (!result.ok()) throwMotionError(result.error());
297 }
298
299private:
300 MotionRuntime *runtime_ = nullptr;
301 MotionHandle handle_{};
302};
303
304class ScriptMotionBuilder {
305public:
306 ScriptMotionBuilder(Animation *animation, float from, float to, float duration)
307 : animation_(animation) {
308 if (!animation_) throw Exception("MotionBuilder: Animation is null");
309 desc_.from = from;
310 desc_.to = to;
311 desc_.duration = duration;
312 }
313
314 void ease(std::string kind) { desc_.ease = std::move(kind); }
315 void delay(float seconds) { desc_.delay = seconds; }
316 void loops(int count, std::string mode = "restart") {
317 desc_.loops = count;
318 desc_.loopMode = parseLoopMode(mode);
319 }
320 void cancelOnError(bool enabled) { desc_.cancelOnError = enabled; }
321 void frequency(int count) { desc_.frequency = count; }
322 void dampingRatio(float ratio) { desc_.dampingRatio = ratio; }
323 void seed(int value) { desc_.seed = static_cast<std::uint32_t>(value); }
324 void stylePunch() { desc_.style = MotionStyle::Punch; }
325 void styleShake() { desc_.style = MotionStyle::Shake; }
326
334 void to(const ssq::Object& sink) {
335 ensureLive();
336 if (sink.getType() != ssq::Type::INSTANCE)
337 throw Exception("MotionBuilder.to: sink must be an instance implementing write(value)");
338 desc_.sink = adoptScriptMotionSink(scriptMotionSinkStores().floats, sink);
339 }
340
342 ScriptMotionHandle* bind(const ssq::Object& sink) {
343 to(sink);
344 return run();
345 }
346
347 ScriptMotionHandle* run() {
348 ensureLive();
349 consumed_ = true;
350 auto spawned = animation_->motions().spawnFloat(desc_);
351 if (!spawned.ok()) throwMotionError(spawned.error());
352 return new ScriptMotionHandle(&animation_->motions(), spawned.value());
353 }
354
355 [[nodiscard]] MotionRuntime &runtime() {
356 ensureLive();
357 return animation_->motions();
358 }
359
360 [[nodiscard]] eve::Result<MotionRuntime::FloatDesc> takeDesc() {
361 ensureLive();
362 consumed_ = true;
363 return eve::Result<MotionRuntime::FloatDesc>::success(std::move(desc_));
364 }
365
366private:
367 void ensureLive() {
368 if (!animation_) throw Exception("MotionBuilder: Animation is null");
369 if (consumed_) throw Exception("MotionBuilder: already consumed (run/append)");
370 }
371
372 Animation *animation_ = nullptr;
373 MotionRuntime::FloatDesc desc_{};
374 bool consumed_ = false;
375};
376
377class ScriptMotionSequenceHandle {
378public:
379 explicit ScriptMotionSequenceHandle(MotionSequenceHandle handle) : handle_(std::move(handle)) {}
380
381 bool isActive() const { return handle_.isActive(); }
382 int childCount() const { return handle_.childCount(); }
383
384 void complete() {
385 auto result = handle_.complete();
386 if (!result.ok()) throwMotionError(result.error());
387 }
388
389 void cancel() {
390 auto result = handle_.cancel();
391 if (!result.ok()) throwMotionError(result.error());
392 }
393
394private:
395 MotionSequenceHandle handle_;
396};
397
398class ScriptMotionSequence {
399public:
400 explicit ScriptMotionSequence(Animation *animation)
401 : animation_(animation), sequence_(requireAnimation(animation)->motions()) {}
402
403 void append(ScriptMotionBuilder *builder) {
404 if (!builder) throw Exception("MotionSequence.append: builder is null");
405 auto taken = builder->takeDesc();
406 if (!taken.ok()) throwMotionError(taken.error());
407 auto desc = std::move(taken).value();
408 MotionBuilder bridge(animation_->motions(), desc.from, desc.to, desc.duration);
409 bridge.ease(desc.ease).delay(desc.delay).loops(desc.loops, desc.loopMode).cancelOnError(desc.cancelOnError)
410 .style(desc.style).frequency(desc.frequency).dampingRatio(desc.dampingRatio).seed(desc.seed);
411 if (desc.sink) bridge.to(*desc.sink);
412 auto scheduled = sequence_.append(std::move(bridge));
413 if (!scheduled.ok()) throwMotionError(scheduled.error());
414 }
415
416 void join(ScriptMotionBuilder *builder) {
417 if (!builder) throw Exception("MotionSequence.join: builder is null");
418 auto taken = builder->takeDesc();
419 if (!taken.ok()) throwMotionError(taken.error());
420 auto desc = std::move(taken).value();
421 MotionBuilder bridge(animation_->motions(), desc.from, desc.to, desc.duration);
422 bridge.ease(desc.ease).delay(desc.delay).loops(desc.loops, desc.loopMode).cancelOnError(desc.cancelOnError)
423 .style(desc.style).frequency(desc.frequency).dampingRatio(desc.dampingRatio).seed(desc.seed);
424 if (desc.sink) bridge.to(*desc.sink);
425 auto scheduled = sequence_.join(std::move(bridge));
426 if (!scheduled.ok()) throwMotionError(scheduled.error());
427 }
428
429 void insert(float atSeconds, ScriptMotionBuilder *builder) {
430 if (!builder) throw Exception("MotionSequence.insert: builder is null");
431 auto taken = builder->takeDesc();
432 if (!taken.ok()) throwMotionError(taken.error());
433 auto desc = std::move(taken).value();
434 MotionBuilder bridge(animation_->motions(), desc.from, desc.to, desc.duration);
435 bridge.ease(desc.ease).delay(desc.delay).loops(desc.loops, desc.loopMode).cancelOnError(desc.cancelOnError)
436 .style(desc.style).frequency(desc.frequency).dampingRatio(desc.dampingRatio).seed(desc.seed);
437 if (desc.sink) bridge.to(*desc.sink);
438 auto scheduled = sequence_.insert(atSeconds, std::move(bridge));
439 if (!scheduled.ok()) throwMotionError(scheduled.error());
440 }
441
442 void appendInterval(float seconds) {
443 auto scheduled = sequence_.appendInterval(seconds);
444 if (!scheduled.ok()) throwMotionError(scheduled.error());
445 }
446
447 float cursor() const { return sequence_.cursor(); }
448 float duration() const { return sequence_.duration(); }
449 int itemCount() const { return sequence_.itemCount(); }
450
451 ScriptMotionSequenceHandle *run() {
452 auto started = sequence_.run();
453 if (!started.ok()) throwMotionError(started.error());
454 return new ScriptMotionSequenceHandle(std::move(started).value());
455 }
456
457private:
458 Animation *animation_ = nullptr;
459 MotionSequence sequence_;
460};
461
470template <class Traits>
471class ScriptMotionBuilderT {
472public:
473 using Desc = typename Traits::Desc;
474 using Value = typename Traits::Value;
475
476 ScriptMotionBuilderT(Animation* animation, Value from, Value to, float duration) : animation_(animation) {
477 if (!animation_) throw Exception("%s: Animation is null", Traits::kType);
478 Traits::configure(desc_, from, to, duration);
479 }
480
481 void ease(std::string kind) { desc_.ease = std::move(kind); }
482 void delay(float seconds) { desc_.delay = seconds; }
483 void loops(int count, std::string mode = "restart") {
484 desc_.loops = count;
485 desc_.loopMode = parseLoopMode(mode);
486 }
487 void cancelOnError(bool enabled) { desc_.cancelOnError = enabled; }
488 void frequency(int count) {
489 if constexpr (Traits::kHasStyle) desc_.frequency = count;
490 }
491 void dampingRatio(float ratio) {
492 if constexpr (Traits::kHasStyle) desc_.dampingRatio = ratio;
493 }
494 void seed(int value) {
495 if constexpr (Traits::kHasStyle) desc_.seed = static_cast<std::uint32_t>(value);
496 }
497 void stylePunch() {
498 if constexpr (Traits::kHasStyle) desc_.style = MotionStyle::Punch;
499 }
500 void styleShake() {
501 if constexpr (Traits::kHasStyle) desc_.style = MotionStyle::Shake;
502 }
503
504 void to(const ssq::Object& sink) {
505 ensureLive();
506 if (sink.getType() != ssq::Type::INSTANCE)
507 throw Exception("%s.to: sink must be an instance implementing write(...)", Traits::kType);
508 desc_.sink = adoptTraitsSink(sink);
509 }
510
511 ScriptMotionHandle* bind(const ssq::Object& sink) {
512 to(sink);
513 return run();
514 }
515
516 ScriptMotionHandle* run() {
517 ensureLive();
518 consumed_ = true;
519 auto spawned = Traits::spawn(animation_->motions(), desc_);
520 if (!spawned.ok()) throwMotionError(spawned.error());
521 return new ScriptMotionHandle(&animation_->motions(), spawned.value());
522 }
523
524 [[nodiscard]] eve::Result<Desc> takeDesc() {
525 ensureLive();
526 consumed_ = true;
527 return eve::Result<Desc>::success(std::move(desc_));
528 }
529
530private:
531 using Adapter = ScriptMotionSink<Traits>;
532
534 static ScriptMotionSinkStore<Adapter>& store() {
535 if constexpr (std::is_same_v<Traits, MotionVec2Traits>) {
536 return scriptMotionSinkStores().vec2s;
537 } else if constexpr (std::is_same_v<Traits, MotionVec3Traits>) {
538 return scriptMotionSinkStores().vec3s;
539 } else if constexpr (std::is_same_v<Traits, MotionColorTraits>) {
540 return scriptMotionSinkStores().colors;
541 } else {
542 return scriptMotionSinkStores().quats;
543 }
544 }
545
546 static typename Traits::Sink* adoptTraitsSink(const ssq::Object& sink) {
547 return adoptScriptMotionSink(store(), sink);
548 }
549
550 void ensureLive() {
551 if (!animation_) throw Exception("%s: Animation is null", Traits::kType);
552 if (consumed_) throw Exception("%s: builder already consumed (run/bind)", Traits::kType);
553 }
554
555 Animation* animation_ = nullptr;
556 Desc desc_{};
557 bool consumed_ = false;
558};
559
561template <class Traits>
562void exposeMotionTraitsBuilder(ssq::Table& table, const char* className) {
563 using Builder = ScriptMotionBuilderT<Traits>;
564 auto builder =
565 table.addClass<Builder>(className, std::function<Builder*()>([]() -> Builder* { return nullptr; }), true);
566 // These are lambdas rather than `&Builder::ease` (the shape the float builder uses)
567 // because `Builder` is a template-dependent name: the binding-contract generator indexes
568 // members by concrete class, and the same method name also exists on the concrete float
569 // builder with a different parameter type, so `&Builder::ease` resolved to two shapes and
570 // was reported as an unresolved binding. A lambda states the parameters directly, which is
571 // also the shape `to`/`bind` below already use.
572 builder.addFunc("ease", [](Builder* self, std::string kind) {
573 if (!self) throw Exception("%s.ease: builder is null", Traits::kType);
574 self->ease(std::move(kind));
575 });
576 builder.addFunc("delay", [](Builder* self, float seconds) {
577 if (!self) throw Exception("%s.delay: builder is null", Traits::kType);
578 self->delay(seconds);
579 });
580 builder.addFunc("loops", [](Builder* self, int count, std::string mode) {
581 if (!self) throw Exception("%s.loops: builder is null", Traits::kType);
582 self->loops(count, std::move(mode));
583 });
584 builder.addFunc("cancelOnError", [](Builder* self, bool enabled) {
585 if (!self) throw Exception("%s.cancelOnError: builder is null", Traits::kType);
586 self->cancelOnError(enabled);
587 });
588 if constexpr (Traits::kHasStyle) {
589 builder.addFunc("stylePunch", &Builder::stylePunch);
590 builder.addFunc("styleShake", &Builder::styleShake);
591 builder.addFunc("frequency", [](Builder* self, int count) {
592 if (!self) throw Exception("%s.frequency: builder is null", Traits::kType);
593 self->frequency(count);
594 });
595 builder.addFunc("dampingRatio", [](Builder* self, float ratio) {
596 if (!self) throw Exception("%s.dampingRatio: builder is null", Traits::kType);
597 self->dampingRatio(ratio);
598 });
599 builder.addFunc("seed", [](Builder* self, int value) {
600 if (!self) throw Exception("%s.seed: builder is null", Traits::kType);
601 self->seed(value);
602 });
603 }
604 builder.addFunc("to", [](Builder* self, const ssq::Object& sink) {
605 if (!self) throw Exception("%s.to: builder is null", Traits::kType);
606 self->to(sink);
607 });
608 builder.addFunc("bind", [](Builder* self, const ssq::Object& sink) -> ScriptMotionHandle* {
609 if (!self) throw Exception("%s.bind: builder is null", Traits::kType);
610 return self->bind(sink);
611 });
612 builder.addFunc("run", [](Builder* self) -> ScriptMotionHandle* {
613 if (!self) throw Exception("%s.run: builder is null", Traits::kType);
614 return self->run();
615 });
616}
617
619void exposeNonFloatMotionBuilders(ssq::Table& table, ssq::Class& animationClass) {
620 exposeMotionTraitsBuilder<MotionVec2Traits>(table, "MotionVec2Builder");
621 exposeMotionTraitsBuilder<MotionVec3Traits>(table, "MotionVec3Builder");
622 exposeMotionTraitsBuilder<MotionColorTraits>(table, "MotionColorBuilder");
623 exposeMotionTraitsBuilder<MotionQuatTraits>(table, "MotionQuatBuilder");
624
625 animationClass.addFunc("newMotionVec2",
626 [](Animation* self, const ssq::Object& from, const ssq::Object& to,
627 float duration) -> ScriptMotionBuilderT<MotionVec2Traits>* {
628 if (!self) throw Exception("newMotionVec2: Animation is null");
629 return new ScriptMotionBuilderT<MotionVec2Traits>(
630 self, MotionVec2Traits::read(from, "newMotionVec2 'from'"),
631 MotionVec2Traits::read(to, "newMotionVec2 'to'"), duration);
632 });
633 animationClass.addFunc("newMotionVec3",
634 [](Animation* self, const ssq::Object& from, const ssq::Object& to,
635 float duration) -> ScriptMotionBuilderT<MotionVec3Traits>* {
636 if (!self) throw Exception("newMotionVec3: Animation is null");
637 return new ScriptMotionBuilderT<MotionVec3Traits>(
638 self, MotionVec3Traits::read(from, "newMotionVec3 'from'"),
639 MotionVec3Traits::read(to, "newMotionVec3 'to'"), duration);
640 });
641 animationClass.addFunc("newMotionColor",
642 [](Animation* self, const ssq::Object& from, const ssq::Object& to,
643 float duration) -> ScriptMotionBuilderT<MotionColorTraits>* {
644 if (!self) throw Exception("newMotionColor: Animation is null");
645 return new ScriptMotionBuilderT<MotionColorTraits>(
646 self, MotionColorTraits::read(from, "newMotionColor 'from'"),
647 MotionColorTraits::read(to, "newMotionColor 'to'"), duration);
648 });
649 animationClass.addFunc("newMotionQuat",
650 [](Animation* self, const ssq::Object& from, const ssq::Object& to,
651 float duration) -> ScriptMotionBuilderT<MotionQuatTraits>* {
652 if (!self) throw Exception("newMotionQuat: Animation is null");
653 return new ScriptMotionBuilderT<MotionQuatTraits>(
654 self, MotionQuatTraits::read(from, "newMotionQuat 'from'"),
655 MotionQuatTraits::read(to, "newMotionQuat 'to'"), duration);
656 });
657}
658
659} // namespace
660
661EVENGINE_API_WORLD void exposeMotionScriptBindings(ssq::Table &table, ssq::Class &animationClass) {
662 auto motion = table.addClass<ScriptMotionHandle>(
663 "Motion", std::function<ScriptMotionHandle *()>([]() -> ScriptMotionHandle * { return nullptr; }),
664 true);
665 motion.addFunc("isActive", &ScriptMotionHandle::isActive);
666 motion.addFunc("value", &ScriptMotionHandle::value);
667 motion.addFunc("complete", &ScriptMotionHandle::complete);
668 motion.addFunc("cancel", &ScriptMotionHandle::cancel);
669
670 auto builder = table.addClass<ScriptMotionBuilder>(
671 "MotionBuilder",
672 std::function<ScriptMotionBuilder *()>([]() -> ScriptMotionBuilder * { return nullptr; }), true);
673 builder.addFunc("ease", &ScriptMotionBuilder::ease);
674 builder.addFunc("delay", &ScriptMotionBuilder::delay);
675 builder.addFunc("loops", &ScriptMotionBuilder::loops);
676 builder.addFunc("cancelOnError", &ScriptMotionBuilder::cancelOnError);
677 builder.addFunc("frequency", &ScriptMotionBuilder::frequency);
678 builder.addFunc("dampingRatio", &ScriptMotionBuilder::dampingRatio);
679 builder.addFunc("seed", &ScriptMotionBuilder::seed);
680 builder.addFunc("to", [](ScriptMotionBuilder* self, const ssq::Object& sink) {
681 if (!self) throw Exception("MotionBuilder.to: builder is null");
682 self->to(sink);
683 });
684 builder.addFunc("bind", [](ScriptMotionBuilder* self, const ssq::Object& sink) -> ScriptMotionHandle* {
685 if (!self) throw Exception("MotionBuilder.bind: builder is null");
686 return self->bind(sink);
687 });
688 builder.addFunc("run", &ScriptMotionBuilder::run);
689
690 auto seqHandle = table.addClass<ScriptMotionSequenceHandle>(
691 "MotionSequenceHandle",
692 std::function<ScriptMotionSequenceHandle *()>([]() -> ScriptMotionSequenceHandle * { return nullptr; }),
693 true);
694 seqHandle.addFunc("isActive", &ScriptMotionSequenceHandle::isActive);
695 seqHandle.addFunc("childCount", &ScriptMotionSequenceHandle::childCount);
696 seqHandle.addFunc("complete", &ScriptMotionSequenceHandle::complete);
697 seqHandle.addFunc("cancel", &ScriptMotionSequenceHandle::cancel);
698
699 auto seq = table.addClass<ScriptMotionSequence>(
700 "MotionSequence",
701 std::function<ScriptMotionSequence *()>([]() -> ScriptMotionSequence * { return nullptr; }), true);
702 seq.addFunc("append", &ScriptMotionSequence::append);
703 seq.addFunc("join", &ScriptMotionSequence::join);
704 seq.addFunc("insert", &ScriptMotionSequence::insert);
705 seq.addFunc("appendInterval", &ScriptMotionSequence::appendInterval);
706 seq.addFunc("cursor", &ScriptMotionSequence::cursor);
707 seq.addFunc("duration", &ScriptMotionSequence::duration);
708 seq.addFunc("itemCount", &ScriptMotionSequence::itemCount);
709 seq.addFunc("run", &ScriptMotionSequence::run);
710
711 animationClass.addFunc(
712 "newMotion",
713 std::function<ScriptMotionBuilder *(Animation *, float, float, float)>(
714 [](Animation *self, float from, float to, float duration) -> ScriptMotionBuilder * {
715 return new ScriptMotionBuilder(self, from, to, duration);
716 }));
717 animationClass.addFunc(
718 "newMotionPunch",
719 std::function<ScriptMotionBuilder *(Animation *, float, float, float)>(
720 [](Animation *self, float from, float strength, float duration) -> ScriptMotionBuilder * {
721 auto *b = new ScriptMotionBuilder(self, from, strength, duration);
722 b->stylePunch();
723 return b;
724 }));
725 animationClass.addFunc(
726 "newMotionShake",
727 std::function<ScriptMotionBuilder *(Animation *, float, float, float)>(
728 [](Animation *self, float from, float strength, float duration) -> ScriptMotionBuilder * {
729 auto *b = new ScriptMotionBuilder(self, from, strength, duration);
730 b->styleShake();
731 return b;
732 }));
733 animationClass.addFunc(
734 "newMotionSequence",
735 std::function<ScriptMotionSequence *(Animation *)>(
736 [](Animation *self) -> ScriptMotionSequence * { return new ScriptMotionSequence(self); }));
737 animationClass.addFunc(
738 "ensureMotionCapacity",
739 std::function<void(Animation *, int)>(
740 [](Animation *self, int count) {
741 if (!self) throw Exception("ensureMotionCapacity: Animation is null");
742 if (count < 0) throw Exception("ensureMotionCapacity: count must be >= 0");
743 self->ensureMotionCapacity(static_cast<std::size_t>(count));
744 }));
745 animationClass.addFunc(
746 "getMotionFloatCapacity",
747 std::function<int(Animation *)>(
748 [](Animation *self) -> int {
749 if (!self) throw Exception("getMotionFloatCapacity: Animation is null");
750 return static_cast<int>(self->getMotionFloatCapacity());
751 }));
752 animationClass.addFunc(
753 "getMotionFloatFreeCount",
754 std::function<int(Animation *)>(
755 [](Animation *self) -> int {
756 if (!self) throw Exception("getMotionFloatFreeCount: Animation is null");
757 return static_cast<int>(self->getMotionFloatFreeCount());
758 }));
759
760 // 非 float 形状(vec2/vec3/color/quat):builder + 脚本 sink,与 float 那套同形。
761 exposeNonFloatMotionBuilders(table, animationClass);
762}
763
764} // namespace eve::animation
float duration
std::size_t cursor
std::string name
Definition ECS.cpp:154
double raw
scene::NodeDesc desc
#define EVENGINE_API_WORLD
Definition Export.h:109
float strength
std::uint32_t spawned
bool scheduled
float ratio
float dampingRatio
std::function< void(b3JointId, const Numbers &)> write
std::function< Numbers(b3JointId)> read
std::string to
std::string from
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
Fluent LitMotion-style builder that commits into MotionRuntime.
Dense motion storage advanced by Animation::advance(SimulationStep).
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
std::vector< std::string > providerNames
ScriptMotionSinkStore< ScriptMotionSink< MotionQuatTraits > > quats
ScriptMotionSinkStore< ScriptMotionFloatSink > floats
ScriptMotionSinkStore< ScriptMotionSink< MotionVec3Traits > > vec3s
ScriptMotionSinkStore< ScriptMotionSink< MotionVec2Traits > > vec2s
std::vector< std::unique_ptr< Adapter > > adapters
Squirrel GC wrappers for Motion builder / handle / sequence.
Lean LitMotion-style sequence: Append / Join / Insert / AppendInterval.
LitMotion-style typed motion handles, values, and sink interfaces.
std::weak_ptr< Run > run
Definition OnnxGpgpu.cpp:25
std::unique_ptr< gpgpu::Sequence > sequence
Definition OnnxGpgpu.cpp:43
PrimitiveHandle handle
const eve::script::BindingContractRegistry & registry
Object::Ptr value
std::string kind
int count
std::string_view adapter
uint32_t seed
Definition TreeMesh.cpp:276
Owning, renderer-independent dynamic values.
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
const std::string & message() const noexcept
Human-readable explanation.
Definition Diagnostic.h:147
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
Named script objects that native systems drive through named methods.
static ScriptProviderRegistry & instance()
Process-lifetime registry; clearAll is the only reset.
Result< ScriptProviderHandle > publish(std::string name, const ssq::Object &instance)
Publish one live script instance under a stable name.
Result< void > call(const ScriptProviderHandle &handle, std::string_view method) const
Call method() on a published provider.
Animation module — tween factory + 2D sprite-sheet / Spine + 3D skeletal playback (player / state mac...
Definition Animation.h:80
EVENGINE_API_WORLD void clearScriptMotionSinkAdapters()
释放所有**脚本 motion sink** 适配器,并撤回它们在 provider 注册表里的登记。
EVENGINE_API_WORLD void exposeMotionScriptBindings(ssq::Table &table, ssq::Class &animationClass)
Register Motion / MotionSequence script types and Animation factories.
MotionLoopMode
Loop behaviour after a cycle completes.
Definition MotionTypes.h:68
@ Restart
Jump back to start each cycle.
@ Yoyo
Alternate direction each cycle.
@ Punch
Finite damped sine punch about from.
@ Shake
Punch with per-axis deterministic random sign.
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:73
bool enabled
One argument passed to a script provider method.
static ScriptArg fromNumber(double value)
Generation-qualified identity of one published script provider.
bool started
Definition Graphics.cpp:200