载入中...
搜索中...
未找到
ControlRigBindings.cpp
浏览该文件的文档.
2#if defined(EVE_ANIMATION_EDITOR_RUNTIME)
3#include <algorithm>
11
12namespace eve::animation_editor {
13namespace {
14Value trsValue(const animation::TransformTRS& t) {
15 return Value::object({{"px", t.px},
16 {"py", t.py},
17 {"pz", t.pz},
18 {"qx", t.qx},
19 {"qy", t.qy},
20 {"qz", t.qz},
21 {"qw", t.qw},
22 {"sx", t.sx},
23 {"sy", t.sy},
24 {"sz", t.sz}});
25}
26template <class F>
27Result<void> edit(ControlRigEditor* self, F function) {
28 auto value = self->snapshot();
29 if (!value.ok()) return Result<void>::failure(value.status());
30 auto definition = animation::decodeControlRig(value.value());
31 if (!definition.ok()) return Result<void>::failure(definition.status());
32 auto changed = function(definition.value());
33 if (!changed.ok()) return changed;
34 return self->replace(definition.value());
35}
36Result<void> invalid(std::string message) {
38}
39} // namespace
40void exposeControlRigBindings(ssq::Table& table, ssq::Class& module) {
41 const editor::ScriptBind bind{table.getHandle(), "animation.controlRig"};
42 auto cls = editor::addScriptClass<ControlRigEditor>(table, "ControlRigEditor");
43 module.addFunc("createRig", [bind](AnimationEditorModule*, const std::string& id) {
44 return bind.ownedCreate<ControlRigEditor>("rig target ID must not be empty", id);
45 });
46 cls.addFunc("initialize", [bind](ControlRigEditor* self, animation::AnimSkeleton* skeleton) {
47 return bind.checked(self && skeleton, "editor and skeleton required",
48 [&] { return self->initialize(*skeleton); });
49 });
50 cls.addFunc("getRevision", [](ControlRigEditor* self) { return std::to_string(self->revision()); });
51 cls.addFunc("enableAutomation", [bind](ControlRigEditor* self) {
52 return bind.checked(self, "editor required", [&] { return self->enableAutomation(); });
53 });
54 cls.addFunc("disableAutomation", [bind](ControlRigEditor* self) {
55 return bind.checked(self, "editor required", [&] { return self->disableAutomation(); });
56 });
57 cls.addFunc("seek", [bind](ControlRigEditor* self, float time) {
58 return bind.checked(self, "editor required", [&] { return self->seek(time); });
59 });
60 cls.addFunc("undo", [bind](ControlRigEditor* self) {
61 return bind.checked(self, "editor required", [&] { return self->undo(); });
62 });
63 cls.addFunc("redo", [bind](ControlRigEditor* self) {
64 return bind.checked(self, "editor required", [&] { return self->redo(); });
65 });
66 cls.addFunc("copyPose", [bind](ControlRigEditor* self, animation::AnimPose* pose) {
67 return bind.checked(self && pose, "editor and pose required", [&] { return self->copyPose(*pose); });
68 });
69 cls.addFunc("keyFromPose", [bind](ControlRigEditor* self, animation::AnimPose* pose) {
70 return bind.checked(self && pose, "editor and pose required", [&] { return self->keyFromPose(*pose); });
71 });
72 cls.addFunc("bakeInto", [bind](ControlRigEditor* self, animation::AnimClip* clip, float rate) {
73 return bind.checked(self && clip, "editor and clip required", [&] { return self->bakeInto(*clip, rate); });
74 });
75 cls.addFunc("setControl", [bind](ControlRigEditor* self, const std::string& id, float x, float y, float z, float qx,
76 float qy, float qz, float qw, float scale, bool key) {
77 return bind.checked(self, "editor required", [&] {
78 animation::TransformTRS t{x, y, z, qx, qy, qz, qw, scale, scale, scale};
79 return self->setControl(id, t, key);
80 });
81 });
82 cls.addFunc("deleteKey", [bind](ControlRigEditor* self, const std::string& id) {
83 return bind.checked(self, "editor required", [&] { return self->deleteKey(id); });
84 });
85 cls.addFunc("moveControl", [bind](ControlRigEditor* self, int index, float x, float y, float z, bool key) {
86 return bind.checked(self, "editor required", [&] { return self->moveControl(index, x, y, z, key); });
87 });
88 cls.addFunc("previewMoveControl", [bind](ControlRigEditor* self, int index, float x, float y, float z) {
89 return bind.checked(self, "editor required", [&] { return self->previewMoveControl(index, x, y, z); });
90 });
91 cls.addFunc("reparent", [bind](ControlRigEditor* self, const std::string& id, const std::string& parent) {
92 return bind.checked(self, "editor required", [&] { return self->reparent(id, parent); });
93 });
94 cls.addFunc("getControlWorld", [bind](ControlRigEditor* self, int index) {
95 if (!self) return bind.fail(DiagnosticCode::InvalidArgument, "editor required");
96 return script::projectResult(bind.vm(), self->controlWorld(index), trsValue);
97 });
98 cls.addFunc("getControlLocal", [bind](ControlRigEditor* self, int index) {
99 if (!self) return bind.fail(DiagnosticCode::InvalidArgument, "editor required");
100 return script::projectResult(bind.vm(), self->controlLocal(index), trsValue);
101 });
102 cls.addFunc("getControlCount", &ControlRigEditor::controlCount);
103 cls.addFunc("getDocument", [bind](ControlRigEditor* self) {
104 if (!self) return bind.fail(DiagnosticCode::InvalidArgument, "editor required");
105 return script::projectResult(bind.vm(), self->snapshot(), [](Value value) { return value; });
106 });
107 cls.addFunc("getControlId", &ControlRigEditor::controlId);
108 cls.addFunc("getTime", [](ControlRigEditor* self) { return static_cast<float>(self->time()); });
109 cls.addFunc("getDuration", [](ControlRigEditor* self) { return static_cast<float>(self->duration()); });
110 cls.addFunc("exportJson", [bind](ControlRigEditor* self) {
111 if (!self) return bind.fail(DiagnosticCode::InvalidArgument, "editor required");
112 auto snapshot = self->snapshot();
113 if (!snapshot.ok()) return script::projectStatusResult(bind.vm(), snapshot.status());
114 return script::projectResult(bind.vm(), snapshot.value().toJson(),
115 [](std::string value) { return Value(std::move(value)); });
116 });
117 cls.addFunc("importJson", [bind](ControlRigEditor* self, const std::string& text) {
118 return bind.checked(self, "editor required", [&] {
119 auto parsed = Value::fromJson(text);
120 if (!parsed.ok()) return Result<void>::failure(parsed.status());
121 return self->restore(parsed.value());
122 });
123 });
124 cls.addFunc("setLimitsJson", [bind](ControlRigEditor* self, const std::string& id, const std::string& text) {
125 return bind.checked(self, "editor required", [&] {
127 if (!limits.ok()) return Result<void>::failure(limits.status());
128 auto document = self->snapshot();
129 if (!document.ok()) return Result<void>::failure(document.status());
130 for (int i = 0; i < self->controlCount(); ++i) {
131 if (self->controlId(i) != id) continue;
132 document.value().find("controls")->at(static_cast<size_t>(i)).set("limits", limits.value());
133 return self->restore(document.value());
134 }
135 return invalid("unknown control ID");
136 });
137 });
138 cls.addFunc("addControl", [bind](ControlRigEditor* self, const std::string& id, const std::string& parent, float x,
139 float y, float z) {
140 return bind.checked(self, "editor required", [&] {
141 return edit(self, [&](auto& d) {
142 animation::RigControl c;
143 c.id = id;
144 c.parent = parent;
145 c.initial.px = x;
146 c.initial.py = y;
147 c.initial.pz = z;
148 c.limits.reference = c.initial;
149 d.controls.push_back(c);
150 return Result<void>::success();
151 });
152 });
153 });
154 cls.addFunc("removeControl", [bind](ControlRigEditor* self, const std::string& id) {
155 return bind.checked(self, "editor required", [&] {
156 return edit(self, [&](auto& d) {
157 auto found =
158 std::find_if(d.controls.begin(), d.controls.end(), [&](const auto& c) { return c.id == id; });
159 if (found == d.controls.end()) return invalid("unknown control");
160 d.controls.erase(found);
161 return Result<void>::success();
162 });
163 });
164 });
165 cls.addFunc("setNode", [bind](ControlRigEditor* self, const std::string& id, const std::string& kind,
166 const std::string& control, const std::string& bone, const std::string& middle,
167 const std::string& tip, const std::string& after, float weight) {
168 return bind.checked(self, "editor required", [&] {
169 return edit(self, [&](auto& d) {
170 animation::RigNode n;
171 n.id = id;
172 n.control = control;
173 n.bone = bone;
174 n.middle = middle;
175 n.tip = tip;
176 n.weight = weight;
177 if (!after.empty()) n.after.push_back(after);
178 if (kind == "fk")
179 n.operation = animation::RigOperation::FK;
180 else if (kind == "aim")
182 else if (kind == "twoBoneIK")
184 else
185 return invalid("unknown node operation");
186 auto found =
187 std::find_if(d.nodes.begin(), d.nodes.end(), [&](const auto& node) { return node.id == id; });
188 if (found == d.nodes.end())
189 d.nodes.push_back(n);
190 else
191 *found = n;
192 return Result<void>::success();
193 });
194 });
195 });
196 cls.addFunc("removeNode", [bind](ControlRigEditor* self, const std::string& id) {
197 return bind.checked(self, "editor required", [&] {
198 return edit(self, [&](auto& d) {
199 auto found = std::find_if(d.nodes.begin(), d.nodes.end(), [&](const auto& n) { return n.id == id; });
200 if (found == d.nodes.end()) return invalid("unknown node");
201 d.nodes.erase(found);
202 return Result<void>::success();
203 });
204 });
205 });
206}
207} // namespace eve::animation_editor
208#else
209namespace eve::animation_editor {
210void exposeControlRigBindings(ssq::Table&, ssq::Class&) {}
211} // namespace eve::animation_editor
212#endif
double z
double y
double x
int tip
Definition AnimSmr.cpp:121
eve::EntitySpatialPose pose
bool changed
HSQOBJECT cls
Definition ECS.cpp:39
SQObjectPtr key
Definition ECS.cpp:155
std::string message
glm::vec4 clip
glm::vec3 n
Definition Grass.cpp:63
std::int32_t c
const ColourScope * parent
std::array< float, 3 > scale
std::uint32_t bone
std::string id
Snapshot snapshot
Object::Ptr value
float d
float t
const RoadNode * node
bool found
MigrationFunction function
std::string text
std::string kind
const AssetImportLimits & limits
double weight
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
static Value object(Object value)
Compatibility factory for an object value.
Definition Value.cpp:129
int controlCount() const
Number of authored controls.
std::string controlId(int index) const
Control ID at index, empty if invalid.
void exposeControlRigBindings(ssq::Table &, ssq::Class &)
Register owned rig authoring facade when animation runtime is available.
EVENGINE_API_WORLD Result< RigDefinition > decodeControlRig(const Value &value)
Decode atomically; rejects unknown versions/fields. Pure, no retained input references.
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
Status invalid(const char *message)
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:73
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.