载入中...
搜索中...
未找到
ProcgenScriptEditorScriptBindings.cpp
浏览该文件的文档.
2
5#include "procgen/PointSet.h"
9
10#include <simplesquirrel/simplesquirrel.hpp>
11
12#include <cstdint>
13#include <string>
15
16namespace eve::procgen_editor {
17namespace {
18
19constexpr const char* kBindingSource = "editor.procgen.script.squirrel";
20
21class ScriptProcgenScriptEditor {
22public:
23 explicit ScriptProcgenScriptEditor(std::string targetId) : editor_(std::move(targetId)) {}
24
25 ProcgenScriptEditor& editor() noexcept { return editor_; }
26 const ProcgenScriptEditor& editor() const noexcept { return editor_; }
27
28private:
29 ProcgenScriptEditor editor_;
30};
31
32ssq::Table loadModuleFromScript(const editor::ScriptBind& bind, ScriptProcgenScriptEditor* self, const std::string& uri,
33 const std::string& id, const std::string& displayName, const std::string& kind,
34 const ssq::Object& schema) {
35 if (!self) return bind.fail(DiagnosticCode::InvalidArgument, "procgen editor must not be null");
36 auto converted = script::valueFromSquirrel(schema);
37 if (!converted.ok()) return script::projectStatusResult(bind.vm(), converted.status());
38 return editor::project(
39 bind.vm(), self->editor().loadModule(uri, id, displayName, kind, editor::toEditorValue(converted.value())));
40}
41
42} // namespace
43
44void exposeProcgenScriptEditorScriptBindings(ssq::Table& table, ssq::Class& moduleClass) {
45 const editor::ScriptBind bind{table.getHandle(), kBindingSource};
46 auto procgenEditor = editor::addScriptClass<ScriptProcgenScriptEditor>(table, "ProcgenScriptEditor");
47 auto meshEditor = editor::addScriptClass<MeshModifierEditor>(table, "MeshModifierEditor");
48 editor::registerDirectEditorWorkspace<MeshModifierEditor>(meshEditor, bind,
49 "mesh modifier editor and workspace required");
50 meshEditor.addFunc("activateTool",
51 [bind](MeshModifierEditor* self, editor::EditorWorkspace* workspace, const std::string& tool) {
52 return bind.checked(self && workspace, "mesh modifier editor and workspace required",
53 [&] { return self->activateTool(*workspace, tool); });
54 });
55 meshEditor.addFunc("observeRevision", [bind](MeshModifierEditor* self, const std::string& document, int revision) {
56 return bind.checked(self && revision >= 0, "valid editor and revision required",
57 [&] { return self->observeRevision(document, static_cast<std::uint64_t>(revision)); });
58 });
59 meshEditor.addFunc("getTargetId", [](MeshModifierEditor* self) { return self ? self->targetId() : std::string{}; });
60 meshEditor.addFunc("getActiveTool", [](MeshModifierEditor* self) { return self ? self->activeTool() : std::string{}; });
61 editor::registerEditorWorkspace<ScriptProcgenScriptEditor>(procgenEditor, bind,
62 "procgen editor and workspace must not be null");
63 procgenEditor.addFunc("loadModule",
64 [bind](ScriptProcgenScriptEditor* self, const std::string& uri, const std::string& id,
65 const std::string& displayName, const std::string& kind, const ssq::Object& schema) {
66 return loadModuleFromScript(bind, self, uri, id, displayName, kind, schema);
67 });
68 procgenEditor.addFunc("setInt", [bind](ScriptProcgenScriptEditor* self, const std::string& key, int value) {
69 return bind.checked(self, "procgen editor must not be null", [&] { return self->editor().setInt(key, value); });
70 });
71 procgenEditor.addFunc("setFloat", [bind](ScriptProcgenScriptEditor* self, const std::string& key, float value) {
72 return bind.checked(self, "procgen editor must not be null",
73 [&] { return self->editor().setFloat(key, static_cast<double>(value)); });
74 });
75 procgenEditor.addFunc("setBool", [bind](ScriptProcgenScriptEditor* self, const std::string& key, bool value) {
76 return bind.checked(self, "procgen editor must not be null",
77 [&] { return self->editor().setBool(key, value); });
78 });
79 procgenEditor.addFunc("setString",
80 [bind](ScriptProcgenScriptEditor* self, const std::string& key, const std::string& value) {
81 return bind.checked(self, "procgen editor must not be null",
82 [&] { return self->editor().setString(key, value); });
83 });
84 procgenEditor.addFunc("publishPreview", [bind](ScriptProcgenScriptEditor* self, procgen::PointSet* points,
85 const std::string& stage, int expectedRevision) {
86 return bind.checked(self, "procgen editor must not be null", [&] {
87 return self->editor().publishPreview(points, stage, static_cast<std::uint64_t>(expectedRevision));
88 });
89 });
90 procgenEditor.addFunc("publishStage",
91 [bind](ScriptProcgenScriptEditor* self, procgen::PointSet* points, const std::string& stage) {
92 return bind.checked(self, "procgen editor must not be null",
93 [&] { return self->editor().publishStage(points, stage); });
94 });
95 procgenEditor.addFunc(
96 "failPreview", [bind](ScriptProcgenScriptEditor* self, const std::string& message, int expectedRevision) {
97 return bind.checked(self, "procgen editor must not be null", [&] {
98 return self->editor().failPreview(message, static_cast<std::uint64_t>(expectedRevision));
99 });
100 });
101 procgenEditor.addFunc("selectStage", [bind](ScriptProcgenScriptEditor* self, const std::string& stage) {
102 return bind.checked(self, "procgen editor must not be null", [&] { return self->editor().selectStage(stage); });
103 });
104 procgenEditor.addFunc("setPointBudget", [bind](ScriptProcgenScriptEditor* self, int budget) {
105 return bind.checked(self, "procgen editor must not be null",
106 [&] { return self->editor().setPointBudget(budget); });
107 });
108 procgenEditor.addFunc("setLive", [bind](ScriptProcgenScriptEditor* self, bool enabled) {
109 return bind.checked(self, "procgen editor must not be null", [&] { return self->editor().setLive(enabled); });
110 });
111 editor::registerEditorHistory<ScriptProcgenScriptEditor>(procgenEditor, bind, "procgen editor must not be null");
112 procgenEditor.addFunc("isDirty", [](ScriptProcgenScriptEditor* self) { return self && self->editor().isDirty(); });
113 procgenEditor.addFunc("isLive", [](ScriptProcgenScriptEditor* self) {
114 return self && self->editor().isContinuousRebuild();
115 });
116 procgenEditor.addFunc("getPreviewRevision", [](ScriptProcgenScriptEditor* self) {
117 return self ? static_cast<int>(self->editor().previewRevision()) : 0;
118 });
119 procgenEditor.addFunc("getModuleId", [](ScriptProcgenScriptEditor* self) {
120 return self ? self->editor().moduleId() : std::string{};
121 });
122 procgenEditor.addFunc("getModuleUri", [](ScriptProcgenScriptEditor* self) {
123 return self ? self->editor().moduleUri() : std::string{};
124 });
125 procgenEditor.addFunc("getDisplayName", [](ScriptProcgenScriptEditor* self) {
126 return self ? self->editor().displayName() : std::string{};
127 });
128 procgenEditor.addFunc("getKind", [](ScriptProcgenScriptEditor* self) {
129 return self ? self->editor().kind() : std::string{};
130 });
131 procgenEditor.addFunc("getSelectedStage", [](ScriptProcgenScriptEditor* self) {
132 return self ? self->editor().selectedStage() : std::string{};
133 });
134 procgenEditor.addFunc("getPreviewFailureSummary", [](ScriptProcgenScriptEditor* self) {
135 return self ? self->editor().previewFailureSummary() : std::string{};
136 });
137 procgenEditor.addFunc("getParamCount", [](ScriptProcgenScriptEditor* self) {
138 return self ? self->editor().paramCount() : 0;
139 });
140 procgenEditor.addFunc("getParamKey", [](ScriptProcgenScriptEditor* self, int index) {
141 return self ? self->editor().paramKey(index) : std::string{};
142 });
143 procgenEditor.addFunc("getParamLabel", [](ScriptProcgenScriptEditor* self, int index) {
144 return self ? self->editor().paramLabel(index) : std::string{};
145 });
146 procgenEditor.addFunc("getParamKind", [](ScriptProcgenScriptEditor* self, int index) {
147 return self ? self->editor().paramKind(index) : std::string{};
148 });
149 procgenEditor.addFunc("getParamMinimum", [](ScriptProcgenScriptEditor* self, int index) {
150 return self ? self->editor().paramMinimum(index) : 0.0f;
151 });
152 procgenEditor.addFunc("getParamMaximum", [](ScriptProcgenScriptEditor* self, int index) {
153 return self ? self->editor().paramMaximum(index) : 0.0f;
154 });
155 procgenEditor.addFunc("getParamStep", [](ScriptProcgenScriptEditor* self, int index) {
156 return self ? self->editor().paramStep(index) : 0.0f;
157 });
158 procgenEditor.addFunc("getParamChoiceCount", [](ScriptProcgenScriptEditor* self, int index) {
159 return self ? self->editor().paramChoiceCount(index) : 0;
160 });
161 procgenEditor.addFunc("getParamChoice", [](ScriptProcgenScriptEditor* self, int paramIndex, int choiceIndex) {
162 return self ? self->editor().paramChoice(paramIndex, choiceIndex) : std::string{};
163 });
164 procgenEditor.addFunc("getInt", [](ScriptProcgenScriptEditor* self, const std::string& key) {
165 return self ? self->editor().getInt(key) : 0;
166 });
167 procgenEditor.addFunc("getFloat", [](ScriptProcgenScriptEditor* self, const std::string& key) {
168 return self ? self->editor().getFloat(key) : 0.0f;
169 });
170 procgenEditor.addFunc("getBool", [](ScriptProcgenScriptEditor* self, const std::string& key) {
171 return self && self->editor().getBool(key);
172 });
173 procgenEditor.addFunc("getString", [](ScriptProcgenScriptEditor* self, const std::string& key) {
174 return self ? self->editor().getString(key) : std::string{};
175 });
176 procgenEditor.addFunc("getStageCount", [](ScriptProcgenScriptEditor* self) {
177 return self ? self->editor().stageCount() : 0;
178 });
179 procgenEditor.addFunc("getStageName", [](ScriptProcgenScriptEditor* self, int index) {
180 return self ? self->editor().stageName(index) : std::string{};
181 });
182 procgenEditor.addFunc("getPointCount", [](ScriptProcgenScriptEditor* self) {
183 return self ? self->editor().pointCount() : 0;
184 });
185 procgenEditor.addFunc("getPointX", [](ScriptProcgenScriptEditor* self, int index) {
186 return self ? self->editor().pointX(index) : 0.0f;
187 });
188 procgenEditor.addFunc("getPointZ", [](ScriptProcgenScriptEditor* self, int index) {
189 return self ? self->editor().pointZ(index) : 0.0f;
190 });
191 procgenEditor.addFunc("getPointSeed", [](ScriptProcgenScriptEditor* self, int index) {
192 return self ? static_cast<int>(self->editor().pointSeed(index)) : 0;
193 });
194
195 editor::registerEditorOwnedCreate<ScriptProcgenScriptEditor, ProcgenEditorModule>(
196 moduleClass, bind, "procgen target id must not be empty");
197 moduleClass.addFunc("createMeshModifier", [bind](ProcgenEditorModule*, const std::string& targetId) {
198 return bind.ownedCreate<MeshModifierEditor>("mesh target id must not be empty", targetId);
199 });
200}
201
202} // namespace eve::procgen_editor
double value
std::string message
std::uint32_t key
TokenKind kind
std::uint64_t revision
std::string uri
std::uint32_t targetId
std::shared_ptr< const std::vector< glm::vec2 > > points
Squirrel adapter for the procgen script editor.
UI-neutral host for a script generator: schema params, undo, PointSet preview.
uint32_t index
Composition model shared by project-specific developer and runtime editors.
Editor facade binder: common script::BindContext plus history/owned-create helpers.
Script-friendly collection of attributed 3D samples.
Definition PointSet.h:59
UI-neutral composition controller for modifier graph, spline, sculpt, UV-paint, merge,...
const std::string & activeTool() const noexcept
Active tool.
const std::string & targetId() const noexcept
procgen_editing::Result< void > observeRevision(std::string document, std::uint64_t revision)
Observe an externally owned document revision without copying its state.
procgen_editing::Result< void > activateTool(editor::EditorWorkspace &workspace, std::string tool)
Activate graph, spline, sculpt, uvPaint, merge, or adhere and update semantic focus.
Composition adapter for the script-hosted procgen generator editor.
EditorValue toEditorValue(const eve::Value &value)
Compatibility spelling for conversion into the authoring value tree.
ssq::Table project(HSQUIRRELVM vm, const eve::Result< void > &result)
Project a completed editing result that has no payload.
void exposeProcgenScriptEditorScriptBindings(ssq::Table &table, ssq::Class &moduleClass)
Expose the script-owned ProcgenScriptEditor factory on ProcgenEditorModule.
ssq::Table projectStatusResult(HSQUIRRELVM vm, const Status &status)
Project a checked native status that carries no payload.
Result< Value > valueFromSquirrel(HSQUIRRELVM vm, SQInteger index, const SquirrelValueOptions &options)
Convert one Squirrel value into the canonical owning Value tree.
bool enabled