5#include <simplesquirrel/simplesquirrel.hpp>
19template <
class T =
void>
26BiomeRulesEditor::SpatialResolver::SpatialResolver(procgen::SpatialData* forest, procgen::SpatialData* clearing)
27 : forest_(forest), clearing_(clearing) {}
29biome_editing::Result<procgen::SpatialData*> BiomeRulesEditor::SpatialResolver::resolve(
30 const std::string& asset)
const {
31 if (asset ==
"asset://preview/forest.spatial")
return eve::editing::applied<procgen::SpatialData*>(forest_);
32 if (asset ==
"asset://preview/clearing.spatial")
return eve::editing::applied<procgen::SpatialData*>(clearing_);
33 return editorError<procgen::SpatialData*>(biome_editing::EditorStatus::NotFound,
"editor.biome.spatial",
34 "Unknown biome spatial asset");
37BiomeRulesEditor::BiomeRulesEditor(std::string
targetId)
40 transactions_(&authority_),
41 forestDomain_(procgen::SpatialData::box(0.0f, 0.0f, 0.0f, 4.0f, 0.0f, 4.0f)),
42 clearingDomain_(procgen::SpatialData::box(0.0f, 0.0f, 0.0f, 1.5f, 0.0f, 1.5f)),
43 resolver_(&forestDomain_, &clearingDomain_) {
44 seedPreviewDocument();
45 auto previewed = refreshPreview();
46 if (!previewed.ok()) previewed.ignore(
"biome editor keeps an empty PointSet when the seeded preview is rejected");
49void BiomeRulesEditor::seedPreviewDocument() {
52 {
"asset", std::string(
"models/oak.prefab")},
56 {
"randomYaw",
false}});
59 {
"name", std::string(
"forest")},
60 {
"spatial", std::string(
"asset://preview/forest.spatial")},
61 {
"priority", std::int64_t{1}},
63 {
"assets", std::move(assets)}});
65 content[
"layers"] = std::move(layers);
68 root[
"schemaVersion"] = std::int64_t{1};
71 if (!
loaded.ok())
loaded.
ignore(
"biome editor keeps defaults when the preview snapshot is rejected");
83 constexpr Panel panels[] = {
84 {
"biome.layers",
"Layers",
"left",
"list", 100},
85 {
"biome.preview",
"World Preview",
"center",
"preview", 100},
86 {
"biome.inspector",
"Biome Inspector",
"right",
"inspector", 100},
87 {
"biome.assets",
"Layer Assets",
"bottom",
"assets", 100},
89 for (
const auto& panel : panels) {
90 if (!candidate.
registerPanel(panel.id, panel.title, panel.region, panel.order) ||
93 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.workspace-conflict",
94 "Could not install the biome workspace composition");
97 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.workspace-activate",
98 "Could not activate the biome preview panel");
99 workspace = std::move(candidate);
100 return eve::editing::applied<void>();
107 item.
domain = editor::SelectionDomain::Asset;
110 item.
type = selectedType_;
124 auto begun = transactions_.
begin(std::move(spec));
125 if (!begun.ok())
return editorError(begun.code(),
"editor.biome.begin",
"Could not begin the biome transaction");
126 auto appended = transactions_.
append(std::move(
operation).takeValue());
127 if (!appended.ok()) {
128 auto discarded = transactions_.
discard();
129 if (!discarded.ok()) discarded.
ignore(
"pending biome transaction already inactive");
134 return refreshPreview();
137biome_editing::Result<void> BiomeRulesEditor::refreshPreview() {
138 auto published = runtime_.
publish(target_, resolver_);
140 auto generated = runtime_.
preview(&forestDomain_, spacing_, seed_, 0.0f, runtime_.
revision());
142 std::vector<PreviewPoint>
next;
143 const auto&
points = generated.value();
145 next.reserve(
static_cast<std::size_t
>(
points->getCount()));
146 for (
int i = 0; i <
points->getCount(); ++i) {
150 point.asset =
points->getStringAttribute(i,
"asset", {});
154 points_ = std::move(next);
155 previewRevision_ = target_.
revision();
156 return eve::editing::applied<void>();
164 return editorError(biome_editing::EditorStatus::NotFound,
"editor.biome.layer",
"Biome layer was not found");
165 selectedId_ = std::move(
id);
166 selectedType_ =
"biome.layer";
167 return eve::editing::applied<void>();
173 for (
const auto& asset :
layer.assets)
174 if (asset.id.value() ==
id)
found =
true;
176 return editorError(biome_editing::EditorStatus::NotFound,
"editor.biome.asset",
"Biome asset was not found");
177 selectedId_ = std::move(
id);
178 selectedType_ =
"biome.asset";
179 return eve::editing::applied<void>();
183 if (selectedType_ !=
"biome.layer")
184 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.selection",
185 "A layer must be selected to change density");
187 const auto revision = previewRevision_;
191 "Set layer density");
200 if (selectedType_ !=
"biome.layer")
201 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.selection",
202 "A layer must be selected to change priority");
206 "Set layer priority");
210 if (selectedType_ !=
"biome.asset")
211 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.selection",
212 "An asset must be selected to change weight");
222 layer.spatialAsset =
"asset://preview/forest.spatial";
223 layer.density = 1.0f;
230 if (selectedType_ !=
"biome.layer")
231 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.selection",
232 "A layer must be selected to delete it");
234 if (!deleted.ok())
return deleted;
237 return eve::editing::applied<void>();
241 const auto*
layer = selectedLayer();
243 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.selection",
244 "A layer must be selected to add an asset");
247 value.asset = std::move(asset);
254 if (selectedType_ !=
"biome.asset")
255 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.selection",
256 "An asset must be selected to delete it");
262 next.push_back(std::move(spatialAsset));
264 const auto revision = previewRevision_;
275 std::erase(next, spatialAsset);
281 return refreshPreview();
286 return editorError(biome_editing::EditorStatus::Rejected,
"editor.biome.spacing",
287 "Biome preview spacing must be positive");
289 return refreshPreview();
295 auto previewed = refreshPreview();
303 auto previewed = refreshPreview();
309 if (selectedType_ ==
"biome.layer") {
310 for (
const auto&
layer : target_.layers())
313 if (selectedType_ ==
"biome.asset") {
314 for (
const auto&
layer : target_.layers())
315 for (const auto& asset :
layer.assets)
318 if (!target_.
layers().empty())
return &target_.
layers().front();
322const biome_editing::BiomeAssetValue* BiomeRulesEditor::assetAt(
int index)
const {
323 const auto*
layer = selectedLayer();
324 if (!
layer ||
index < 0 ||
static_cast<std::size_t
>(
index) >=
layer->assets.size())
return nullptr;
325 return &
layer->assets[
static_cast<std::size_t
>(
index)];
329 if (
index < 0 ||
static_cast<std::size_t
>(
index) >= target_.
layers().size())
return {};
333 if (
index < 0 ||
static_cast<std::size_t
>(
index) >= target_.
layers().size())
return {};
337 if (
index < 0 ||
static_cast<std::size_t
>(
index) >= target_.
layers().size())
return 0.0f;
341 if (
index < 0 ||
static_cast<std::size_t
>(
index) >= target_.
layers().size())
return 0;
345 return selectedType_ ==
"biome.layer" &&
layerId(
index) == selectedId_;
349 const auto*
layer = selectedLayer();
350 return layer ?
static_cast<int>(
layer->assets.size()) : 0;
353 const auto* asset = assetAt(
index);
354 return asset ? asset->id.value() : std::string{};
357 const auto* asset = assetAt(
index);
358 return asset ? asset->asset : std::string{};
361 const auto* asset = assetAt(
index);
362 return asset ? asset->weight : 0.0f;
365 const auto* asset = assetAt(
index);
366 return asset && selectedType_ ==
"biome.asset" && asset->id.value() == selectedId_;
375 if (
index < 0 ||
static_cast<std::size_t
>(
index) >= points_.size())
return 0.0f;
376 return points_[
static_cast<std::size_t
>(
index)].
x;
379 if (
index < 0 ||
static_cast<std::size_t
>(
index) >= points_.size())
return 0.0f;
380 return points_[
static_cast<std::size_t
>(
index)].
z;
383 if (
index < 0 ||
static_cast<std::size_t
>(
index) >= points_.size())
return {};
384 return points_[
static_cast<std::size_t
>(
index)].asset;
393constexpr const char* kBindingSource =
"editor.biome.rules.squirrel";
395class ScriptBiomeRulesEditor {
399 BiomeRulesEditor& editor() noexcept {
return editor_; }
400 const BiomeRulesEditor& editor() const noexcept {
return editor_; }
403 BiomeRulesEditor editor_;
410 auto biomeEditor = editor::addScriptClass<ScriptBiomeRulesEditor>(table,
"BiomeRulesEditor");
411 editor::registerEditorWorkspace<ScriptBiomeRulesEditor>(biomeEditor, bind,
412 "biome editor and workspace must not be null");
413 biomeEditor.addFunc(
"selectLayer", [bind](ScriptBiomeRulesEditor* self,
const std::string&
id) {
414 return bind.checked(self,
"biome editor must not be null", [&] {
return self->editor().selectLayer(
id); });
416 biomeEditor.addFunc(
"selectAsset", [bind](ScriptBiomeRulesEditor* self,
const std::string&
id) {
417 return bind.checked(self,
"biome editor must not be null", [&] {
return self->editor().selectAsset(
id); });
419 biomeEditor.addFunc(
"setLayerDensity", [bind](ScriptBiomeRulesEditor* self,
float density) {
420 return bind.checked(self,
"biome editor must not be null",
421 [&] {
return self->editor().setLayerDensity(
static_cast<double>(
density)); });
423 biomeEditor.addFunc(
"setLayerPriority", [bind](ScriptBiomeRulesEditor* self,
int priority) {
424 return bind.checked(self,
"biome editor must not be null",
425 [&] {
return self->editor().setLayerPriority(
priority); });
427 biomeEditor.addFunc(
"setAssetWeight", [bind](ScriptBiomeRulesEditor* self,
float weight) {
428 return bind.checked(self,
"biome editor must not be null",
429 [&] {
return self->editor().setAssetWeight(
static_cast<double>(
weight)); });
431 biomeEditor.addFunc(
"createLayer",
432 [bind](ScriptBiomeRulesEditor* self,
const std::string&
id,
const std::string&
name) {
433 return bind.checked(self,
"biome editor must not be null",
434 [&] {
return self->editor().createLayer(
id,
name); });
436 biomeEditor.addFunc(
"deleteSelectedLayer", [bind](ScriptBiomeRulesEditor* self) {
437 return bind.checked(self,
"biome editor must not be null",
438 [&] {
return self->editor().deleteSelectedLayer(); });
440 biomeEditor.addFunc(
"createAsset",
441 [bind](ScriptBiomeRulesEditor* self,
const std::string&
id,
const std::string& asset) {
442 return bind.checked(self,
"biome editor must not be null",
443 [&] {
return self->editor().createAsset(
id, asset); });
445 biomeEditor.addFunc(
"deleteSelectedAsset", [bind](ScriptBiomeRulesEditor* self) {
446 return bind.checked(self,
"biome editor must not be null",
447 [&] {
return self->editor().deleteSelectedAsset(); });
449 biomeEditor.addFunc(
"addExclusion", [bind](ScriptBiomeRulesEditor* self,
const std::string& spatialAsset) {
450 return bind.checked(self,
"biome editor must not be null",
451 [&] {
return self->editor().addExclusion(spatialAsset); });
453 biomeEditor.addFunc(
"removeExclusion", [bind](ScriptBiomeRulesEditor* self,
const std::string& spatialAsset) {
454 return bind.checked(self,
"biome editor must not be null",
455 [&] {
return self->editor().removeExclusion(spatialAsset); });
457 biomeEditor.addFunc(
"setSeed", [bind](ScriptBiomeRulesEditor* self,
int seed) {
458 return bind.checked(self,
"biome editor must not be null",
459 [&] {
return self->editor().setSeed(
static_cast<std::uint32_t
>(
seed)); });
461 biomeEditor.addFunc(
"setSpacing", [bind](ScriptBiomeRulesEditor* self,
float spacing) {
462 return bind.checked(self,
"biome editor must not be null", [&] {
return self->editor().setSpacing(
spacing); });
464 editor::registerEditorHistory<ScriptBiomeRulesEditor>(biomeEditor, bind,
"biome editor must not be null");
465 biomeEditor.addFunc(
"getPreviewRevision", [](ScriptBiomeRulesEditor* self) {
466 return self ?
static_cast<int>(self->editor().previewRevision()) : 0;
469 "getSeed", [](ScriptBiomeRulesEditor* self) {
return self ?
static_cast<int>(self->editor().seed()) : 0; });
470 biomeEditor.addFunc(
"getSpacing",
471 [](ScriptBiomeRulesEditor* self) {
return self ? self->editor().spacing() : 0.0f; });
472 biomeEditor.addFunc(
"getSelectedId", [](ScriptBiomeRulesEditor* self) {
473 return self ? self->editor().selectedId() : std::string{};
475 biomeEditor.addFunc(
"getSelectedType", [](ScriptBiomeRulesEditor* self) {
476 return self ? self->editor().selectedType() : std::string{};
478 biomeEditor.addFunc(
"getLayerCount",
479 [](ScriptBiomeRulesEditor* self) {
return self ? self->editor().layerCount() : 0; });
480 biomeEditor.addFunc(
"getLayerId", [](ScriptBiomeRulesEditor* self,
int index) {
481 return self ? self->editor().layerId(
index) : std::string{};
483 biomeEditor.addFunc(
"getLayerName", [](ScriptBiomeRulesEditor* self,
int index) {
484 return self ? self->editor().layerName(
index) : std::string{};
486 biomeEditor.addFunc(
"getLayerDensity", [](ScriptBiomeRulesEditor* self,
int index) {
487 return self ? self->editor().layerDensity(
index) : 0.0f;
489 biomeEditor.addFunc(
"getLayerPriority", [](ScriptBiomeRulesEditor* self,
int index) {
490 return self ? self->editor().layerPriority(
index) : 0;
492 biomeEditor.addFunc(
"getLayerSelected", [](ScriptBiomeRulesEditor* self,
int index) {
493 return self && self->editor().isLayerSelected(
index);
495 biomeEditor.addFunc(
"getAssetCount",
496 [](ScriptBiomeRulesEditor* self) {
return self ? self->editor().assetCount() : 0; });
497 biomeEditor.addFunc(
"getAssetId", [](ScriptBiomeRulesEditor* self,
int index) {
498 return self ? self->editor().assetId(
index) : std::string{};
500 biomeEditor.addFunc(
"getAssetRef", [](ScriptBiomeRulesEditor* self,
int index) {
501 return self ? self->editor().assetRef(
index) : std::string{};
503 biomeEditor.addFunc(
"getAssetWeight", [](ScriptBiomeRulesEditor* self,
int index) {
504 return self ? self->editor().assetWeight(
index) : 0.0f;
506 biomeEditor.addFunc(
"getAssetSelected", [](ScriptBiomeRulesEditor* self,
int index) {
507 return self && self->editor().isAssetSelected(
index);
509 biomeEditor.addFunc(
"getExclusionCount",
510 [](ScriptBiomeRulesEditor* self) {
return self ? self->editor().exclusionCount() : 0; });
511 biomeEditor.addFunc(
"getExclusionAsset", [](ScriptBiomeRulesEditor* self,
int index) {
512 return self ? self->editor().exclusionAsset(
index) : std::string{};
514 biomeEditor.addFunc(
"getPointCount",
515 [](ScriptBiomeRulesEditor* self) {
return self ? self->editor().pointCount() : 0; });
516 biomeEditor.addFunc(
"getPointX", [](ScriptBiomeRulesEditor* self,
int index) {
517 return self ? self->editor().pointX(
index) : 0.0f;
519 biomeEditor.addFunc(
"getPointZ", [](ScriptBiomeRulesEditor* self,
int index) {
520 return self ? self->editor().pointZ(
index) : 0.0f;
522 biomeEditor.addFunc(
"getPointAsset", [](ScriptBiomeRulesEditor* self,
int index) {
523 return self ? self->editor().pointAsset(
index) : std::string{};
526 editor::registerEditorOwnedCreate<ScriptBiomeRulesEditor, BiomeEditorModule>(moduleClass, bind,
527 "biome target id must not be empty");
ActionParameterOperation operation
Squirrel adapter for the biome rules editor.
UI-neutral biome editor: workspace, PointSet preview, undo.
wgpu::PopErrorScopeStatus status
std::shared_ptr< const OceanSprayFrame > previous
std::unordered_set< std::string > loaded
std::shared_ptr< const std::vector< glm::vec2 > > points
const VegetationPresetContext & context
void ignore(std::string_view reason={}) const noexcept
Explicitly discard this result after documenting the reason.
Move-only operation result carrying either a value or Status.
static Result failure(Status status)
Construct a failed result from a structured status.
UUID-backed identifier adapter for legacy textual boundaries.
Result< void > publish(const BiomeDocumentTarget &, const IBiomeSpatialResolver &)
Publish.
Revision revision() const
Revision.
Result< std::unique_ptr< procgen::PointSet > > preview(procgen::SpatialData *domain, float spacing, std::uint32_t seed, float jitter, Revision expectedRevision)
Generate a PointSet from a previously published candidate.
Result< DomainOperation > makeCreateLayer(const BiomeLayerValue &) const
Make create layer.
const std::vector< std::string > & exclusions() const
Exclusions.
const std::vector< BiomeLayerValue > & layers() const
Layers.
Result< void > loadSnapshot(const EditorValue &)
Loads snapshot.
Result< DomainOperation > makeSet(const SelectionSnapshot &, const PropertyPath &, const EditorValue &, PropertySetMode) const override
Make set.
Result< DomainOperation > makeDeleteLayer(const ObjectId &) const
Make delete layer.
Result< DomainOperation > makeDeleteAsset(const ObjectId &) const
Make delete asset.
Result< DomainOperation > makeSetExclusions(std::vector< std::string >) const
Make set exclusions.
TargetId targetId() const override
Target id.
Result< DomainOperation > makeCreateAsset(const ObjectId &layer, const BiomeAssetValue &) const
Make create asset.
std::string assetId(int index) const
Asset id.
int layerPriority(int index) const
Layer priority.
bool isLayerSelected(int index) const
True when layer selected.
int assetCount() const
Asset count.
biome_editing::Result< void > removeExclusion(std::string spatialAsset)
biome_editing::Result< void > setSeed(std::uint32_t seed)
std::string layerName(int index) const
Layer name.
biome_editing::Result< void > createAsset(std::string id, std::string asset)
std::uint32_t seed() const noexcept
Seed.
biome_editing::Result< void > addExclusion(std::string spatialAsset)
std::string exclusionAsset(int index) const
Exclusion asset.
float pointX(int index) const
Point x.
float pointZ(int index) const
Point z.
biome_editing::Result< void > createLayer(std::string id, std::string name)
biome_editing::Result< void > configureWorkspace(editor::EditorWorkspace &workspace) const
Install Layers / Preview / Inspector / Assets panels.
biome_editing::Result< void > selectLayer(std::string id)
float spacing() const noexcept
Spacing.
float assetWeight(int index) const
Asset weight.
biome_editing::Result< editor::TransactionReceipt > undo()
float layerDensity(int index) const
Layer density.
std::uint64_t revision() const noexcept
Revision.
std::string pointAsset(int index) const
Point asset.
biome_editing::Result< void > setAssetWeight(double weight)
bool isAssetSelected(int index) const
True when asset selected.
std::string layerId(int index) const
Layer id.
std::string assetRef(int index) const
Asset ref.
biome_editing::Result< void > setSpacing(float spacing)
biome_editing::Result< void > selectAsset(std::string id)
biome_editing::Result< void > deleteSelectedAsset()
biome_editing::Result< editor::TransactionReceipt > redo()
biome_editing::Result< void > setLayerDensity(double density)
biome_editing::Result< void > deleteSelectedLayer()
biome_editing::Result< void > setLayerPriority(int priority)
std::uint64_t revision() const override
Return the current revision.
Dotted property path (layout.size) compared and persisted as text.
Deterministic owning value tree shared by authoring hosts.
std::vector< Value > Array
std::map< std::string, Value > Object
Composition model shared by project-specific developer and runtime editors.
bool setPanelContext(const std::string &id, const std::string &context)
Attach a semantic input context such as scene, graph, or timeline.
Result< WorkspacePanelDescriptor > registerPanel(WorkspacePanelDescriptor descriptor)
Register a checked owning panel descriptor.
Result< WorkspacePanelDescriptor > activatePanel(const StableId &id)
Activate a visible panel with structured not-found/rejected results.
bool setPanelCapability(const std::string &id, const std::string &capability)
Attach a capability requirement used by the host when filtering panels.
Result< TransactionReceipt > commit() override
Preflight and commit all active operations through the authority.
Result< TransactionId > begin(TransactionSpec specification) override
Begin a transaction with a stable id and base revision.
Result< TransactionReceipt > undo() override
Compensate the most recent committed transaction.
Result< void > discard() override
Discard the active, not-yet-committed operation list.
Result< TransactionReceipt > redo() override
Reapply the most recently compensated transaction.
Result< void > append(DomainOperation operation) override
Append an operation to the active transaction without applying it.
Editor facade binder: common script::BindContext plus history/owned-create helpers.
editing::Status EditorStatus
editing::ObjectId ObjectId
editing::Value EditorValue
void exposeBiomeRulesEditorScriptBindings(ssq::Table &table, ssq::Class &moduleClass)
Expose the script-owned BiomeRulesEditor factory on BiomeEditorModule.
eve::editing::StableId StableId
eve::editing::TargetId TargetId
eve::editing::TransactionId TransactionId
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Stable weighted asset entry in one biome layer.
Stable authored biome layer referencing a spatial asset.
SelectionItem public API.
SelectionSnapshot public API.
TransactionSpec public API.