载入中...
搜索中...
未找到
PixelWorldCatalogPanel.cpp
浏览该文件的文档.
2
4#include "ui/UIHost.h"
5
6#include <algorithm>
7#include <array>
8#include <charconv>
9#include <iomanip>
10#include <limits>
11#include <sstream>
12#include <utility>
13
15namespace {
16
17using namespace eve::pixelworld;
18
19constexpr const char* kCatalogPanelHostName = "eve_pixelworld_catalog";
20
21std::vector<MaterialDefinition> definitions(const MaterialCatalog& catalog) {
22 return {catalog.definitions().begin(), catalog.definitions().end()};
23}
24
25std::vector<MaterialReactionRule> reactions(const MaterialCatalog& catalog) {
26 return {catalog.reactions().begin(), catalog.reactions().end()};
27}
28
29std::vector<MaterialPhaseRule> phases(const MaterialCatalog& catalog) {
30 return {catalog.phaseRules().begin(), catalog.phaseRules().end()};
31}
32
33std::string stateName(MaterialState state) {
34 constexpr std::array names{"Empty", "Solid", "Powder", "Liquid", "Gas", "Energy"};
35 const auto index = static_cast<std::size_t>(state);
36 return index < names.size() ? names[index] : "Unknown";
37}
38
39template <class Integer>
40std::optional<Integer> parseInteger(std::string_view text) {
41 Integer value{};
42 const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value);
43 if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size()) return std::nullopt;
44 return value;
45}
46
47std::optional<std::uint32_t> parseRgba(std::string_view text) {
48 if (!text.empty() && text.front() == '#') text.remove_prefix(1);
49 if (text.size() != 8) return std::nullopt;
50 std::uint32_t value = 0;
51 const auto parsed = std::from_chars(text.data(), text.data() + text.size(), value, 16);
52 if (parsed.ec != std::errc{} || parsed.ptr != text.data() + text.size()) return std::nullopt;
53 return value;
54}
55
56std::string rgbaText(std::uint32_t rgba) {
57 std::ostringstream output;
58 output << '#' << std::uppercase << std::hex << std::setw(8) << std::setfill('0') << rgba;
59 return output.str();
60}
61
62std::vector<std::string> splitTags(std::string_view value) {
63 std::vector<std::string> result;
64 while (!value.empty()) {
65 const auto comma = value.find(',');
66 std::string tag(value.substr(0, comma));
67 const auto first = tag.find_first_not_of(" \t");
68 const auto last = tag.find_last_not_of(" \t");
69 if (first != std::string::npos) result.push_back(tag.substr(first, last - first + 1));
70 if (comma == std::string_view::npos) break;
71 value.remove_prefix(comma + 1);
72 }
73 std::sort(result.begin(), result.end());
74 result.erase(std::unique(result.begin(), result.end()), result.end());
75 return result;
76}
77
78std::string joinTags(const std::vector<std::string>& tags) {
79 std::string result;
80 for (const auto& tag : tags) {
81 if (!result.empty()) result += ", ";
82 result += tag;
83 }
84 return result;
85}
86
87template <class Integer, class Setter>
88eve::ui::WidgetDesc integerField(std::string label, std::string id, Integer value,
89 Setter setter) {
90 return eve::ui::inputText(std::move(label), std::to_string(value), std::move(id),
91 [setter = std::move(setter)](const std::string& text) mutable {
92 if (const auto parsed = parseInteger<Integer>(text)) setter(*parsed);
93 });
94}
95
96} // namespace
97
99 : draft_(std::move(catalog)) {
100 if (!draft_.reactions().empty()) selectedReaction_ = 0;
101 if (!draft_.phaseRules().empty()) selectedPhase_ = 0;
102}
103
105 if (auto host = eve::ui::UIHost::resolve(host_)) {
106 host->get().setTree(eve::ui::window("", {}));
107 host->get().setVisible(false);
108 }
109}
110
112 : draft_(std::move(other.draft_)),
113 revision_(other.revision_),
114 selectedMaterial_(other.selectedMaterial_),
115 selectedReaction_(other.selectedReaction_),
116 selectedPhase_(other.selectedPhase_),
117 statusText_(std::move(other.statusText_)),
118 host_(other.host_) {
119 other.host_ = {};
120 if (host_.table) {
121 auto refreshed = refresh();
122 if (!refreshed.ok()) statusText_ = refreshed.status().describe();
123 }
124}
125
127 if (this == &other) return *this;
128 if (auto host = eve::ui::UIHost::resolve(host_)) {
129 host->get().setTree(eve::ui::window("", {}));
130 host->get().setVisible(false);
131 }
132 draft_ = std::move(other.draft_);
133 revision_ = other.revision_;
134 selectedMaterial_ = other.selectedMaterial_;
135 selectedReaction_ = other.selectedReaction_;
136 selectedPhase_ = other.selectedPhase_;
137 statusText_ = std::move(other.statusText_);
138 host_ = other.host_;
139 other.host_ = {};
140 if (host_.table) {
141 auto refreshed = refresh();
142 if (!refreshed.ok()) statusText_ = refreshed.status().describe();
143 }
144 return *this;
145}
146
148 auto decoded = decodeMaterialCatalogJson(catalogJson);
149 if (!decoded.ok())
150 return eve::Result<PixelWorldCatalogPanel>::failure(decoded.status());
152 PixelWorldCatalogPanel(std::move(decoded).takeValue()));
153}
154
158
159std::vector<MaterialCard> PixelWorldCatalogPanel::materialCards() const {
160 std::vector<MaterialCard> result;
161 result.reserve(draft_.definitions().size());
162 for (std::size_t index = 0; index < draft_.definitions().size(); ++index) {
163 const auto& definition = draft_.definitions()[index];
164 result.push_back({definition.id, definition.name, definition.state,
165 definition.displayRgba, index == selectedMaterial_,
166 draft_.canReact(definition.id),
167 draft_.canPhaseTransition(definition.id)});
168 }
169 return result;
170}
171
173 if (index >= draft_.definitions().size())
175 "material selection is outside the Catalog",
176 "materialIndex", {}, "pixelworld.catalog-panel"));
177 selectedMaterial_ = index;
179}
180
182 if (index >= draft_.reactions().size())
184 "reaction selection is outside the Catalog",
185 "reactionIndex", {}, "pixelworld.catalog-panel"));
186 selectedReaction_ = index;
188}
189
191 if (index >= draft_.phaseRules().size())
193 "phase selection is outside the Catalog", "phaseIndex",
194 {}, "pixelworld.catalog-panel"));
195 selectedPhase_ = index;
197}
198
199eve::Result<CatalogDraftReceipt> PixelWorldCatalogPanel::commitCandidate(
200 std::vector<MaterialDefinition> candidateDefinitions,
201 std::vector<MaterialReactionRule> candidateReactions,
202 std::vector<MaterialPhaseRule> candidatePhases) {
203 auto candidate = MaterialCatalog::create(std::move(candidateDefinitions),
204 std::move(candidateReactions),
205 std::move(candidatePhases));
206 if (!candidate.ok())
207 return eve::Result<CatalogDraftReceipt>::failure(candidate.status());
208 CatalogDraftReceipt receipt{revision_, revision_ + 1, candidate.value().fingerprint()};
209 draft_ = std::move(candidate).takeValue();
210 revision_ = receipt.revisionAfter;
211 if (selectedMaterial_ >= draft_.definitions().size()) selectedMaterial_ = 0;
212 if (draft_.reactions().empty()) selectedReaction_.reset();
213 else if (!selectedReaction_ || *selectedReaction_ >= draft_.reactions().size())
214 selectedReaction_ = draft_.reactions().size() - 1;
215 if (draft_.phaseRules().empty()) selectedPhase_.reset();
216 else if (!selectedPhase_ || *selectedPhase_ >= draft_.phaseRules().size())
217 selectedPhase_ = draft_.phaseRules().size() - 1;
219}
220
222 MaterialDefinition definition) {
223 const auto index = static_cast<std::size_t>(definition.id);
224 if (index >= draft_.definitions().size())
226 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "material id is outside the Catalog",
227 "material.id", {}, "pixelworld.catalog-panel"));
228 auto candidateDefinitions = definitions(draft_);
229 candidateDefinitions[index] = std::move(definition);
230 return commitCandidate(std::move(candidateDefinitions), reactions(draft_), phases(draft_));
231}
232
234 std::size_t index, MaterialReactionRule rule) {
235 if (index >= draft_.reactions().size())
237 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "reaction index is outside the Catalog",
238 "reactionIndex", {}, "pixelworld.catalog-panel"));
239 auto candidate = reactions(draft_);
240 candidate[index] = std::move(rule);
241 return commitCandidate(definitions(draft_), std::move(candidate), phases(draft_));
242}
243
246 const std::string selectedId = rule.id;
247 auto candidate = reactions(draft_);
248 candidate.push_back(std::move(rule));
249 auto committed = commitCandidate(definitions(draft_), std::move(candidate), phases(draft_));
250 if (committed.ok()) {
251 for (std::size_t index = 0; index < draft_.reactions().size(); ++index)
252 if (draft_.reactions()[index].id == selectedId) selectedReaction_ = index;
253 }
254 return committed;
255}
256
258 if (index >= draft_.reactions().size())
260 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "reaction index is outside the Catalog",
261 "reactionIndex", {}, "pixelworld.catalog-panel"));
262 auto candidate = reactions(draft_);
263 candidate.erase(candidate.begin() + static_cast<std::ptrdiff_t>(index));
264 return commitCandidate(definitions(draft_), std::move(candidate), phases(draft_));
265}
266
268 std::size_t index, MaterialPhaseRule rule) {
269 if (index >= draft_.phaseRules().size())
271 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "phase index is outside the Catalog",
272 "phaseIndex", {}, "pixelworld.catalog-panel"));
273 auto candidate = phases(draft_);
274 candidate[index] = std::move(rule);
275 return commitCandidate(definitions(draft_), reactions(draft_), std::move(candidate));
276}
277
279 const std::string selectedId = rule.id;
280 auto candidate = phases(draft_);
281 candidate.push_back(std::move(rule));
282 auto committed = commitCandidate(definitions(draft_), reactions(draft_), std::move(candidate));
283 if (committed.ok()) {
284 for (std::size_t index = 0; index < draft_.phaseRules().size(); ++index)
285 if (draft_.phaseRules()[index].id == selectedId) selectedPhase_ = index;
286 }
287 return committed;
288}
289
291 if (index >= draft_.phaseRules().size())
293 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "phase index is outside the Catalog",
294 "phaseIndex", {}, "pixelworld.catalog-panel"));
295 auto candidate = phases(draft_);
296 candidate.erase(candidate.begin() + static_cast<std::ptrdiff_t>(index));
297 return commitCandidate(definitions(draft_), reactions(draft_), std::move(candidate));
298}
299
303
305 std::string_view catalogJson) {
306 auto decoded = decodeMaterialCatalogJson(catalogJson);
307 if (!decoded.ok())
308 return eve::Result<CatalogDraftReceipt>::failure(decoded.status());
309 return commitCandidate(definitions(decoded.value()), reactions(decoded.value()),
310 phases(decoded.value()));
311}
312
314 PixelWorldControlService& control, std::uint64_t worldId,
315 std::uint64_t expectedFingerprint) const {
316 auto encoded = document();
317 if (!encoded.ok())
319 return control.reloadMaterialCatalog(worldId, encoded.value(), expectedFingerprint);
320}
321
323 auto host = eve::ui::UIHost::resolve(host_);
324 if (!host) {
325 host_ = eve::ui::UIHost::createHost(kCatalogPanelHostName);
327 }
328 if (!host) {
330 eve::DiagnosticCode::PreconditionViolation, "pixelworld catalog panel requires an active UI ECS world",
331 "uiHost", {}, "pixelworld.catalog-panel"));
332 }
333 host->get().setVisible(true);
334 host->get().setLayer(90);
335 auto meta = host->get().meta();
336 meta->hasPos = true;
337 meta->posX = 12.0F;
338 meta->posY = 12.0F;
339 meta->hasSize = true;
340 meta->sizeX = 820.0F;
341 meta->sizeY = 540.0F;
342 auto refreshed = refresh();
343 if (!refreshed) {
344 return eve::Result<eve::ui::UIHostHandle>::failure(refreshed.status());
345 }
347}
348
350 if (auto host = eve::ui::UIHost::resolve(host_)) host->get().setVisible(false);
351}
352
354 auto host = eve::ui::UIHost::resolve(host_);
355 return host && host->get().meta()->visible;
356}
357
359 auto host = eve::ui::UIHost::resolve(host_);
360 if (!host)
362 "pixelworld catalog panel is not mounted", "uiHost",
363 {}, "pixelworld.catalog-panel"));
364 host->get().setTree(buildWidgetTree());
366}
367
368void PixelWorldCatalogPanel::observe(eve::Result<CatalogDraftReceipt> result) {
369 if (result.ok()) statusText_ = "Catalog draft validated at revision " +
370 std::to_string(result.value().revisionAfter);
371 else statusText_ = result.status().describe();
372 if (isOpen()) {
373 auto refreshed = refresh();
374 if (!refreshed.ok()) statusText_ = refreshed.status().describe();
375 }
376}
377
378void PixelWorldCatalogPanel::observe(eve::Result<void> result) {
379 if (result.ok()) statusText_ = "Selection updated";
380 else statusText_ = result.status().describe();
381 if (isOpen()) {
382 auto refreshed = refresh();
383 if (!refreshed.ok()) statusText_ = refreshed.status().describe();
384 }
385}
386
388 using namespace eve::ui;
389 std::vector<WidgetDesc> materialRows;
390 for (std::size_t index = 0; index < draft_.definitions().size(); ++index) {
391 const auto& material = draft_.definitions()[index];
392 const float red = float((material.displayRgba >> 24U) & 0xFFU) / 255.0f;
393 const float green = float((material.displayRgba >> 16U) & 0xFFU) / 255.0f;
394 const float blue = float((material.displayRgba >> 8U) & 0xFFU) / 255.0f;
395 WidgetDesc swatch = image("material-swatch/" + std::to_string(index), 18.0f, 18.0f)
396 .withTint(red, green, blue, 1.0f)
397 .withCornerRadius(3.0f);
398 WidgetDesc choose = button(material.name + " · " + stateName(material.state),
399 "material/" + std::to_string(index), [this, index] {
400 observe(selectMaterial(index));
401 });
402 choose.checked = index == selectedMaterial_;
403 materialRows.push_back(row({std::move(swatch), std::move(choose)},
404 "material-row/" + std::to_string(index)));
405 }
406
407 const MaterialDefinition selected = draft_.definitions()[selectedMaterial_];
408 auto mutateMaterial = [this, id = selected.id](auto mutation) {
410 mutation(value);
411 observe(replaceMaterial(std::move(value)));
412 };
413 std::vector<std::string> stateOptions{"Empty", "Solid", "Powder", "Liquid", "Gas", "Energy"};
414 const float red = float((selected.displayRgba >> 24U) & 0xFFU) / 255.0f;
415 const float green = float((selected.displayRgba >> 16U) & 0xFFU) / 255.0f;
416 const float blue = float((selected.displayRgba >> 8U) & 0xFFU) / 255.0f;
417 const float alpha = float(selected.displayRgba & 0xFFU) / 255.0f;
418
419 std::vector<WidgetDesc> materialInspector{
420 sectionHeader("Material · " + selected.name, "material-heading"),
421 image("material-preview", 96.0f, 56.0f).withTint(red, green, blue, alpha).withCornerRadius(6.0f),
422 inputText("Name", selected.name, "material-name", [mutateMaterial](const std::string& value) mutable {
423 mutateMaterial([&](auto& material) { material.name = value; });
424 }),
425 combo("State", stateOptions, static_cast<int>(selected.state), "material-state",
426 [mutateMaterial](float value) mutable {
427 mutateMaterial([&](auto& material) {
428 material.state = static_cast<MaterialState>(std::clamp(int(value), 0, 5));
429 });
430 }),
431 inputText("RGBA", rgbaText(selected.displayRgba), "material-rgba",
432 [mutateMaterial](const std::string& value) mutable {
433 if (const auto parsed = parseRgba(value))
434 mutateMaterial([&](auto& material) { material.displayRgba = *parsed; });
435 }),
436 checkbox("Flammable", selected.flammable, "material-flammable",
437 [mutateMaterial](bool value) mutable {
438 mutateMaterial([&](auto& material) { material.flammable = value; });
439 }),
440 inputText("Tags", joinTags(selected.tags), "material-tags",
441 [mutateMaterial](const std::string& value) mutable {
442 mutateMaterial([&](auto& material) { material.tags = splitTags(value); });
443 }),
444 integerField("Density", "material-density", selected.density,
445 [mutateMaterial](std::uint16_t value) mutable {
446 mutateMaterial([&](auto& material) { material.density = value; });
447 }),
448 integerField("Viscosity", "material-viscosity", selected.viscosity,
449 [mutateMaterial](std::uint8_t value) mutable {
450 mutateMaterial([&](auto& material) { material.viscosity = value; });
451 }),
452 integerField("Thermal conductivity", "material-conductivity", selected.thermalConductivity,
453 [mutateMaterial](std::uint8_t value) mutable {
454 mutateMaterial([&](auto& material) { material.thermalConductivity = value; });
455 }),
456 integerField("Heat capacity", "material-capacity", selected.heatCapacity,
457 [mutateMaterial](std::uint16_t value) mutable {
458 mutateMaterial([&](auto& material) { material.heatCapacity = value; });
459 }),
460 integerField("Ignition temperature", "material-ignition", selected.ignitionTemperature,
461 [mutateMaterial](std::int16_t value) mutable {
462 mutateMaterial([&](auto& material) { material.ignitionTemperature = value; });
463 }),
464 integerField("Melting temperature", "material-melting", selected.meltingTemperature,
465 [mutateMaterial](std::int16_t value) mutable {
466 mutateMaterial([&](auto& material) { material.meltingTemperature = value; });
467 }),
468 integerField("Boiling temperature", "material-boiling", selected.boilingTemperature,
469 [mutateMaterial](std::int16_t value) mutable {
470 mutateMaterial([&](auto& material) { material.boilingTemperature = value; });
471 }),
472 integerField("Default temperature", "material-default-temperature", selected.defaultTemperature,
473 [mutateMaterial](std::int16_t value) mutable {
474 mutateMaterial([&](auto& material) { material.defaultTemperature = value; });
475 }),
476 integerField("Default lifetime", "material-lifetime", selected.defaultLifetime,
477 [mutateMaterial](std::uint16_t value) mutable {
478 mutateMaterial([&](auto& material) { material.defaultLifetime = value; });
479 }),
480 integerField("Blast resistance", "material-blast", selected.blastResistance,
481 [mutateMaterial](std::uint16_t value) mutable {
482 mutateMaterial([&](auto& material) { material.blastResistance = value; });
483 })};
484
485 std::vector<std::string> materialNames;
486 for (const auto& material : draft_.definitions()) materialNames.push_back(material.name);
487 std::vector<std::string> reactionNames;
488 for (const auto& reaction : draft_.reactions()) reactionNames.push_back(reaction.id);
489 std::vector<WidgetDesc> reactionEditor{sectionHeader("Binary reactions", "reaction-heading")};
490 if (!reactionNames.empty()) {
491 const auto reactionIndex = selectedReaction_.value_or(0);
492 const MaterialReactionRule selectedRule = draft_.reactions()[reactionIndex];
493 auto mutateReaction = [this, reactionIndex](auto mutation) {
494 MaterialReactionRule value = draft_.reactions()[reactionIndex];
495 mutation(value);
496 observe(replaceReaction(reactionIndex, std::move(value)));
497 };
498 reactionEditor.push_back(combo("Rule", reactionNames, int(reactionIndex), "reaction-select",
499 [this](float value) {
500 observe(selectReaction(std::size_t(std::max(0, int(value)))));
501 }));
502 reactionEditor.push_back(inputText("ID", selectedRule.id, "reaction-id",
503 [mutateReaction](const std::string& value) mutable {
504 mutateReaction([&](auto& rule) { rule.id = value; });
505 }));
506 auto materialCombo = [&](std::string label, std::string id, MaterialId value, auto assign) {
507 return combo(std::move(label), materialNames, int(value), std::move(id),
508 [mutateReaction, assign](float index) mutable {
509 mutateReaction([&](auto& rule) {
510 assign(rule, static_cast<MaterialId>(std::max(0, int(index))));
511 });
512 });
513 };
514 reactionEditor.push_back(materialCombo("First", "reaction-first", selectedRule.first,
515 [](auto& rule, auto value) { rule.first = value; }));
516 reactionEditor.push_back(materialCombo("Second", "reaction-second", selectedRule.second,
517 [](auto& rule, auto value) { rule.second = value; }));
518 reactionEditor.push_back(materialCombo("First result", "reaction-first-result",
519 selectedRule.firstResult,
520 [](auto& rule, auto value) { rule.firstResult = value; }));
521 reactionEditor.push_back(materialCombo("Second result", "reaction-second-result",
522 selectedRule.secondResult,
523 [](auto& rule, auto value) { rule.secondResult = value; }));
524 reactionEditor.push_back(integerField("Minimum temperature", "reaction-min-temperature",
525 selectedRule.minimumTemperature,
526 [mutateReaction](std::int16_t value) mutable {
527 mutateReaction([&](auto& rule) {
528 rule.minimumTemperature = value;
529 });
530 }));
531 reactionEditor.push_back(integerField("Heat delta", "reaction-heat", selectedRule.heatDelta,
532 [mutateReaction](std::int16_t value) mutable {
533 mutateReaction([&](auto& rule) { rule.heatDelta = value; });
534 }));
535 reactionEditor.push_back(integerField("Priority", "reaction-priority", selectedRule.priority,
536 [mutateReaction](std::int32_t value) mutable {
537 mutateReaction([&](auto& rule) { rule.priority = value; });
538 }));
539 reactionEditor.push_back(button("Delete reaction", "reaction-delete", [this, reactionIndex] {
540 observe(removeReaction(reactionIndex));
541 }));
542 }
543 reactionEditor.push_back(button("Add reaction", "reaction-add", [this] {
545 rule.id = "reaction-" + std::to_string(revision_ + 1);
546 observe(addReaction(std::move(rule)));
547 }));
548
549 std::vector<std::string> phaseNames;
550 for (const auto& phase : draft_.phaseRules()) phaseNames.push_back(phase.id);
551 std::vector<WidgetDesc> phaseEditor{sectionHeader("Phase transitions", "phase-heading")};
552 if (!phaseNames.empty()) {
553 const auto phaseIndex = selectedPhase_.value_or(0);
554 const MaterialPhaseRule selectedRule = draft_.phaseRules()[phaseIndex];
555 auto mutatePhase = [this, phaseIndex](auto mutation) {
556 MaterialPhaseRule value = draft_.phaseRules()[phaseIndex];
557 mutation(value);
558 observe(replacePhase(phaseIndex, std::move(value)));
559 };
560 phaseEditor.push_back(combo("Rule", phaseNames, int(phaseIndex), "phase-select",
561 [this](float value) {
562 observe(selectPhase(std::size_t(std::max(0, int(value)))));
563 }));
564 phaseEditor.push_back(inputText("ID", selectedRule.id, "phase-id",
565 [mutatePhase](const std::string& value) mutable {
566 mutatePhase([&](auto& rule) { rule.id = value; });
567 }));
568 phaseEditor.push_back(combo("Source", materialNames, int(selectedRule.source), "phase-source",
569 [mutatePhase](float value) mutable {
570 mutatePhase([&](auto& rule) {
571 rule.source = static_cast<MaterialId>(std::max(0, int(value)));
572 });
573 }));
574 phaseEditor.push_back(combo("Result", materialNames, int(selectedRule.result), "phase-result",
575 [mutatePhase](float value) mutable {
576 mutatePhase([&](auto& rule) {
577 rule.result = static_cast<MaterialId>(std::max(0, int(value)));
578 });
579 }));
580 phaseEditor.push_back(combo("Direction", {"At or below", "At or above"},
581 int(selectedRule.direction), "phase-direction",
582 [mutatePhase](float value) mutable {
583 mutatePhase([&](auto& rule) {
584 rule.direction = value < 0.5f ? TemperatureDirection::AtOrBelow
585 : TemperatureDirection::AtOrAbove;
586 });
587 }));
588 phaseEditor.push_back(integerField("Threshold", "phase-threshold", selectedRule.threshold,
589 [mutatePhase](std::int16_t value) mutable {
590 mutatePhase([&](auto& rule) { rule.threshold = value; });
591 }));
592 phaseEditor.push_back(integerField("Temperature delta", "phase-temperature-delta",
593 selectedRule.temperatureDelta,
594 [mutatePhase](std::int16_t value) mutable {
595 mutatePhase([&](auto& rule) {
596 rule.temperatureDelta = value;
597 });
598 }));
599 phaseEditor.push_back(integerField("Priority", "phase-priority", selectedRule.priority,
600 [mutatePhase](std::int32_t value) mutable {
601 mutatePhase([&](auto& rule) { rule.priority = value; });
602 }));
603 phaseEditor.push_back(button("Delete transition", "phase-delete", [this, phaseIndex] {
604 observe(removePhase(phaseIndex));
605 }));
606 }
607 phaseEditor.push_back(button("Add transition", "phase-add", [this] {
609 rule.id = "phase-" + std::to_string(revision_ + 1);
610 observe(addPhase(std::move(rule)));
611 }));
612
613 WidgetDesc browser = sidebar({searchField("Filter materials", {}, "material-filter"),
614 scrollList("materials", std::move(materialRows), 0.0f, 28.0f)},
615 "catalog-browser", 260.0f);
616 WidgetDesc inspector = child(
617 "catalog-inspector",
618 {collapsingHeader("Material properties", std::move(materialInspector), "material-card", true),
619 collapsingHeader("Reactions", std::move(reactionEditor), "reaction-section", false),
620 collapsingHeader("Phase transitions", std::move(phaseEditor), "phase-section", false)});
621 WidgetDesc content = splitPane(FlexDirection::Row, std::move(browser), std::move(inspector),
622 0.28f, "catalog-split");
623 return window("PixelWorld Material & Reaction Catalog",
624 {toolbar({badge("Validated", "catalog-valid"), spacer(),
625 text("Fingerprint " + std::to_string(draft_.fingerprint()),
626 "catalog-fingerprint")},
627 "catalog-toolbar"),
628 std::move(content), statusBar({text(statusText_, "catalog-status")},
629 "catalog-statusbar")},
630 "pixelworld-catalog-editor");
631}
632
633} // namespace eve::pixelworld_editor
std::string selected
std::string output
bool committed
std::string name
Definition ECS.cpp:154
vk::UniqueImage image
std::int32_t first
std::map< unsigned, Colour > definitions
std::string label
std::string id
Poco::JSON::Object::Ptr result
std::unordered_set< std::string > tags
const std::string * tag
std::array< uint32_t, 16 > material
Object::Ptr value
std::string text
float phase
Battle::Reactions reactions
float size
Definition TreeMesh.cpp:158
uint32_t index
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
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
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Owning immutable material and reaction table used by one PixelWorld.
const MaterialDefinition & definition(MaterialId id) const noexcept
Resolve a validated id; invalid ids safely project to air.
bool canReact(MaterialId id) const noexcept
Whether this material participates in at least one binary reaction.
static MaterialCatalog builtIn()
Construct the engine's canonical built-in catalog.
std::span< const MaterialReactionRule > reactions() const noexcept
Borrow immutable canonical-priority reaction rules.
std::span< const MaterialDefinition > definitions() const noexcept
Borrow immutable definitions until this catalog is destroyed.
std::span< const MaterialPhaseRule > phaseRules() const noexcept
Borrow immutable canonical-priority phase rules.
static eve::Result< MaterialCatalog > create(std::vector< MaterialDefinition > definitions, std::vector< MaterialReactionRule > reactions={}, std::vector< MaterialPhaseRule > phaseRules={})
Validate and own a complete catalog candidate transactionally.
bool canPhaseTransition(MaterialId id) const noexcept
Whether this material owns at least one phase transition rule.
Main-thread-affine control surface shared by editor, CLI and MCP adapters.
eve::Result< PixelCatalogReloadReceipt > reloadMaterialCatalog(std::uint64_t worldId, std::string_view catalogJson, std::uint64_t expectedFingerprint)
Validate JSON and transactionally hot-reload a compatible paused world Catalog.
Dedicated visual material/reaction Catalog editor with transactional draft admission.
eve::ui::WidgetDesc buildWidgetTree()
Build a dedicated material browser, property inspector and rule editor visual tree.
eve::Result< CatalogDraftReceipt > addReaction(eve::pixelworld::MaterialReactionRule rule)
Add a binary rule and select its canonical post-validation position.
eve::Result< CatalogDraftReceipt > removePhase(std::size_t index)
Remove one phase rule transactionally.
eve::Result< CatalogDraftReceipt > removeReaction(std::size_t index)
Remove one binary rule transactionally.
static eve::Result< PixelWorldCatalogPanel > create(std::string_view catalogJson)
Decode and validate a versioned Catalog document into a fresh panel draft.
eve::Result< CatalogDraftReceipt > replaceReaction(std::size_t index, eve::pixelworld::MaterialReactionRule rule)
Replace one binary rule after validating the entire reconstructed Catalog.
PixelWorldCatalogPanel(const PixelWorldCatalogPanel &)=delete
std::vector< MaterialCard > materialCards() const
Project deterministic cards for the material browser.
eve::Result< void > selectPhase(std::size_t index)
Select a phase-transition rule by canonical index.
eve::Result< void > selectMaterial(std::size_t index)
Select a material by canonical index without mutating the Catalog.
eve::Result< CatalogDraftReceipt > loadDocument(std::string_view catalogJson)
Replace the draft from a validated document; failure leaves all panel state intact.
eve::Result< void > selectReaction(std::size_t index)
Select a binary reaction rule by canonical index.
void close()
Hide the mounted panel without discarding its validated draft.
eve::Result< CatalogDraftReceipt > replaceMaterial(eve::pixelworld::MaterialDefinition definition)
Replace one material definition after validating the entire reconstructed Catalog.
eve::Result< eve::pixelworld::PixelCatalogReloadReceipt > apply(eve::pixelworld::PixelWorldControlService &control, std::uint64_t worldId, std::uint64_t expectedFingerprint) const
Apply the draft through the canonical paused-world authority path.
eve::Result< CatalogDraftReceipt > replacePhase(std::size_t index, eve::pixelworld::MaterialPhaseRule rule)
Replace one phase rule after validating the entire reconstructed Catalog.
eve::Result< CatalogDraftReceipt > addPhase(eve::pixelworld::MaterialPhaseRule rule)
Add a phase rule and select its canonical post-validation position.
PixelWorldCatalogPanel & operator=(const PixelWorldCatalogPanel &)=delete
bool isOpen() const
True while the panel has a live, visible UI host.
eve::ui::UIHostHandle host() const noexcept
Mounted UI host handle, or an empty handle before open().
eve::Result< eve::ui::UIHostHandle > open()
Mount or reveal the panel in the UI ECS world and publish its current tree.
static PixelWorldCatalogPanel builtIn()
Create a panel over the canonical built-in Catalog.
eve::Result< void > refresh()
Reconcile the mounted host with the latest draft and selection state.
eve::Result< std::string > document() const
Encode the current validated draft using the canonical versioned JSON codec.
static UIHostHandle createHost(const std::string &name="")
Creates an ECS-owned UI host.
Definition UIHost.cpp:49
static eve::OptionalRef< UIHost > resolve(UIHostHandle handle) noexcept
Resolves a UI host handle with ECS generation checking.
Definition UIHost.cpp:62
ShapeState * child(const JPH::Body &body, const JPH::SubShapeID &id)
Resolves a native child record for a synchronous callback or query. @ownership Borrowed from the worl...
eve::Result< MaterialCatalog > decodeMaterialCatalogJson(std::string_view json)
Decode and validate schema eve.pixelworld.material-catalog version 1.
MaterialId
Compact stable material identifier stored in each authoritative cell.
MaterialState
Broad movement class for one pixel material.
eve::Result< std::string > encodeMaterialCatalogJson(const MaterialCatalog &catalog)
Encode schema eve.pixelworld.material-catalog version 1 canonically.
ControlEdit button(const char *label, float width=0.f, float height=0.f)
Shared immediate renderer for retained and EditorHost buttons.
ControlEdit combo(const char *label, int &selected, const std::vector< const char * > &items)
Shared immediate renderer for indexed combo boxes.
ControlEdit inputText(const char *label, std::string &value, bool multiline=false, ImVec2 multilineSize=ImVec2(0.f, 96.f), bool readOnly=false, const char *hint="")
UTF-8 owning text editor with automatic buffer growth.
WidgetDesc collapsingHeader(std::string label, std::vector< WidgetDesc > children, std::string id, bool defaultOpen)
Collapsible header containing child widgets.
Definition Widget.cpp:814
WidgetDesc badge(std::string label, std::string id)
Compact status/category pill. Tint controls its background color.
Definition Widget.cpp:581
WidgetDesc statusBar(std::vector< WidgetDesc > children, std::string id)
Compact horizontal status strip; Spacer children absorb free width.
Definition Widget.cpp:688
WidgetDesc inputText(std::string label, std::string value, std::string id, std::function< void(const std::string &)> onChange)
Editable text field; fires onTextChange.
Definition Widget.cpp:533
WidgetDesc window(std::string title, std::vector< WidgetDesc > children, std::string id)
Top-level window widget with a title bar.
Definition Widget.cpp:326
WidgetDesc spacer(std::string id, float grow)
Flexible empty space; default flexGrow=1 so it absorbs free space in a Flex parent.
Definition Widget.cpp:888
WidgetDesc toolbar(std::vector< WidgetDesc > children, std::string id)
Horizontal editor command strip; Spacer children absorb free width.
Definition Widget.cpp:655
WidgetDesc searchField(std::string hint, std::string value, std::string id, std::function< void(const std::string &)> onChange)
Compact search field with placeholder text and a bundled search icon.
Definition Widget.cpp:557
WidgetDesc sidebar(std::vector< WidgetDesc > children, std::string id, float width)
Vertical editor side panel. Width defaults to 240 logical pixels.
Definition Widget.cpp:678
WidgetDesc scrollList(std::string id, std::vector< WidgetDesc > children, float height, float itemHeight)
Scroll list.
Definition Widget.cpp:837
WidgetDesc splitPane(FlexDirection direction, WidgetDesc first, WidgetDesc second, float ratio, std::string id, std::function< void(float)> onResize)
Two resizable panes separated by a drag handle.
Definition Widget.cpp:699
WidgetDesc sectionHeader(std::string label, std::string id)
Non-collapsible editor section heading with an accent marker.
Definition Widget.cpp:614
enum EVENGINE_API_FOUNDATION Integer
OT_INTEGER.
Definition Runtime.h:117
Immutable validated properties for one catalog material.
Data-driven phase transition evaluated after heat diffusion.
Deterministically ordered binary material reaction.
Draft mutation receipt used for UI reconciliation and optimistic publication.
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:15