载入中...
搜索中...
未找到
AnimationCurveBindings.cpp
浏览该文件的文档.
1#include <functional>
2#include <optional>
3#include <simplesquirrel/simplesquirrel.hpp>
4#include <stdexcept>
5#include <string>
6
12#include "filesystem/FileData.h"
14
15namespace eve::animation {
16namespace {
17
18constexpr const char* kSource = "animation.bindings";
19
20} // namespace
21
22// A provider read hands over a freshly allocated FileData that this call owns.
24
25void exposeAnimCurveLibraryBindings(ssq::Table& table) {
26 const script::BindContext bind{table.getHandle(), kSource};
27 auto cls = table.addClass<AnimCurveLibrary>(
28 "AnimCurveLibrary", std::function<AnimCurveLibrary*()>([] { return new AnimCurveLibrary(); }), true);
29 script::bindMethod(cls, "loadBinary", [bind](AnimCurveLibrary* self, std::string path) {
30 try {
31 auto* fs = eve::ModuleManager::getInstance<eve::filesystem::Filesystem>("Filesystem");
32 if (!fs) throw std::runtime_error("filesystem unavailable");
33 auto* raw = fs->read(path);
34 if (!raw) throw std::runtime_error("animation curve file unavailable");
35 Owned<eve::filesystem::FileData> data(raw);
36 return script::projectResult(bind.vm(),
37 self->load({static_cast<const std::byte*>(data->getData()), data->getSize()}));
38 } catch (const std::exception& error) {
39 return bind.failInvalid(error.what());
40 }
41 });
42 script::bindMethod(cls, "contains",
43 [](AnimCurveLibrary* self, std::string source) { return self->contains(source); });
44 script::bindMethod(cls, "getSourceCount", [](AnimCurveLibrary* self) { return static_cast<int>(self->size()); });
45 script::bindMethod(cls, "sample",
46 [bind](AnimCurveLibrary* self, std::string source, std::string channel, float time, bool loop) {
47 return script::projectResult(bind.vm(), self->sample(source, channel, time, loop),
48 [](std::optional<float> sample) {
49 return Value(Value::Object{
50 {"present", Value(sample.has_value())},
51 {"value", Value(sample.value_or(0.f))},
52 });
53 });
54 });
55}
56} // namespace eve::animation
HSQOBJECT cls
Definition ECS.cpp:21
std::string error
Definition Package.cpp:60
std::string path
Definition PlayHost.cpp:110
Per-translation-unit VM + diagnostic-source helper for Squirrel bindings.
The single Squirrel projection for common Result, Status and Value.
Explicit Value/Owned/Borrowed semantics at the Squirrel boundary.
const UnitySourceAsset & source
Owned scalar animation curves indexed by stable source and channel names.
eve::Result< std::optional< float > > sample(std::string_view source, std::string_view channel, double seconds, bool loop) const
Evaluate one scalar channel with linear interpolation.
std::size_t size() const noexcept
Return the number of source records currently owned.
bool contains(std::string_view source) const
Query whether a source record exists, including records without channels.
eve::Result< void > load(std::span< const std::byte > bytes)
Atomically replace data from little-endian eve.animation-curves/1 bytes.
Pins the active VM and a stable diagnostic source for one expose() TU.
void exposeAnimCurveLibraryBindings(ssq::Table &table)
Register the owned scalar animation curve library and checked loader.
std::unique_ptr< T > Owned
C++ owning storage used by an object factory.
void bindMethod(ssq::Class &cls, const char *name, F &&fn)
Register one script method while keeping a literal name for Binding Contracts.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.