载入中...
搜索中...
未找到
ScenePanel.cpp
浏览该文件的文档.
1#include "ui/ScenePanel.h"
2
3#include "common/Capability.h"
4#include "common/SceneQuery.h"
5#include "ui/UIHost.h"
6
7#include <algorithm>
8#include <cstdlib>
9
10namespace eve::ui {
11namespace {
12
13constexpr const char* kSceneHostName = "eve_scene";
14
15} // namespace
16
18 if (auto host = UIHost::resolve(host_)) host->get().setTree(window("", {}));
19}
20
22 auto host = UIHost::resolve(host_);
23 if (!host) {
24 host_ = UIHost::createHost(kSceneHostName);
25 host = UIHost::resolve(host_);
26 }
27 if (!host) return;
28 host->get().setVisible(true);
29 host->get().setLayer(80);
30 refresh();
31}
32
34 if (auto host = UIHost::resolve(host_)) host->get().setVisible(false);
35}
36
37bool ScenePanel::isOpen() const {
38 auto host = UIHost::resolve(host_);
39 return host && host->get().meta()->visible;
40}
41
43 if (auto host = UIHost::resolve(host_)) host->get().setTree(build());
44}
45
46bool ScenePanel::selectNode(const std::string& id) {
47 eve::ISceneQuery* scene = eve::cap::query<eve::ISceneQuery>();
48 if (!scene || id.empty()) return false;
49 if (!scene->getNode(id)) return false;
50 selectedId_ = id;
51 rebuildHost();
52 return true;
53}
54
55void ScenePanel::setPickHandler(std::function<void(const std::string&)> handler) {
56 pickHandler_ = std::move(handler);
57}
58
59WidgetDesc ScenePanel::nodeTree(const eve::SceneNodeInfo& node,
60 const std::vector<eve::SceneNodeInfo>& all) {
61 std::vector<WidgetDesc> children;
62 children.push_back(button("select " + node.name + "##sel_" + node.id,
63 "sel_" + node.id,
64 [this, id = node.id]() { selectNode(id); }));
65 for (const std::string& childId : node.children) {
66 const auto found =
67 std::find_if(all.begin(), all.end(),
68 [&](const eve::SceneNodeInfo& n) { return n.id == childId; });
69 if (found == all.end()) continue;
70 children.push_back(nodeTree(*found, all));
71 }
72 return collapsingHeader(
73 node.name + "##hdr_" + node.id, std::move(children), "node_" + node.id,
74 false);
75}
76
78 std::vector<WidgetDesc> children;
79 eve::ISceneQuery* scene = eve::cap::query<eve::ISceneQuery>();
80 if (!scene) {
81 children.push_back(text(
82 "Scene module unavailable. This build has no scene support.",
83 "scene_missing"));
84 return window("Scene", std::move(children), "root");
85 }
86
87 const std::vector<eve::SceneNodeInfo> nodes = scene->nodes(512);
88 const std::string hostName = scene->activeHost();
89 children.push_back(row(
90 {
91 text("Scene: " + (hostName.empty() ? std::string("(none)") : hostName),
92 "scene_host"),
93 spacer("scene_spacer"),
94 button("Refresh##scene_refresh", "scene_refresh",
95 [this]() { refresh(); }),
96 },
97 "scene_toolbar"));
98 children.push_back(separator("scene_sep"));
99
100 // Tree roots (nodes without a parent present in the snapshot).
101 for (const eve::SceneNodeInfo& node : nodes) {
102 const bool hasParent =
103 !node.parent.empty() &&
104 std::find_if(nodes.begin(), nodes.end(),
105 [&](const eve::SceneNodeInfo& n) {
106 return n.id == node.parent;
107 }) != nodes.end();
108 if (!hasParent) children.push_back(nodeTree(node, nodes));
109 }
110
111 // Selected node property sheet.
112 if (!selectedId_.empty()) {
113 auto selected = scene->getNode(selectedId_);
114 if (selected) {
115 const eve::SceneNodeInfo info = std::move(selected).value();
116 const std::string base = "node_" + info.id;
117 children.push_back(separator("scene_sel_sep"));
118 children.push_back(text("id: " + info.id, base + "_id"));
119 children.push_back(text("path: " + info.path, base + "_path"));
120 children.push_back(row(
121 {
122 text("x", base + "_lx"),
123 inputText("##" + base + "_x", std::to_string(info.x), base + "_x",
124 [id = info.id, scene](const std::string& t) {
125 auto cur = scene->getNode(id);
126 if (!cur) return;
127 eve::SceneNodeInfo node = std::move(cur).value();
128 node.x = static_cast<float>(std::atof(t.c_str()));
129 scene->setNodeTransform(id, node.x, node.y, node.z).ignore();
130 }),
131 text("y", base + "_ly"),
132 inputText("##" + base + "_y", std::to_string(info.y), base + "_y",
133 [id = info.id, scene](const std::string& t) {
134 auto cur = scene->getNode(id);
135 if (!cur) return;
136 eve::SceneNodeInfo node = std::move(cur).value();
137 node.y = static_cast<float>(std::atof(t.c_str()));
138 scene->setNodeTransform(id, node.x, node.y, node.z).ignore();
139 }),
140 text("z", base + "_lz"),
141 inputText("##" + base + "_z", std::to_string(info.z), base + "_z",
142 [id = info.id, scene](const std::string& t) {
143 auto cur = scene->getNode(id);
144 if (!cur) return;
145 eve::SceneNodeInfo node = std::move(cur).value();
146 node.z = static_cast<float>(std::atof(t.c_str()));
147 scene->setNodeTransform(id, node.x, node.y, node.z).ignore();
148 }),
149 },
150 base + "_transform"));
151 children.push_back(row(
152 {
153 checkbox("visible##" + base + "_vis", info.visible, base + "_visible",
154 [id = info.id, scene](bool v) { scene->setNodeVisible(id, v).ignore(); }),
155 spacer(base + "_vis_spacer"),
156 button("Pick##" + base + "_pick", base + "_pick",
157 [this, id = info.id]() {
158 if (pickHandler_) pickHandler_(id);
159 }),
160 },
161 base + "_actions"));
162 }
163 }
164 return window("Scene", std::move(children), "root");
165}
166
167void ScenePanel::rebuildHost() {
168 auto host = UIHost::resolve(host_);
169 if (!host || !host->get().meta()->visible) return;
170 host->get().setTree(build());
171}
172
173} // namespace eve::ui
std::string selected
glm::vec3 n
Definition Grass.cpp:63
float v
std::map< std::string, Node > nodes
std::string id
HSQOBJECT handler
bool all
std::weak_ptr< PrimitiveScene > scene
float t
const RoadNode * node
bool found
std::string text
int children
Definition TreeMesh.cpp:432
Scene graph query/mutation surface (provided by the scene module).
Definition SceneQuery.h:31
~ScenePanel()
Scene panel.
UIHostHandle host() const noexcept
Returns the mounted scene host handle, or an empty handle until open().
Definition ScenePanel.h:43
WidgetDesc build()
Declarative tree of the current scene state.
void open()
Mounts (or updates) the scene host on the UI ECS world.
void refresh()
Re-queries the scene graph and rebuilds the tree.
void close()
Hides the scene host.
bool isOpen() const
True while the scene host is mounted and visible.
bool selectNode(const std::string &id)
Selects a node by id; false when unknown or no scene module.
void setPickHandler(std::function< void(const std::string &)> handler)
Registers the pick handler for the Pick button.
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
WidgetDesc collapsingHeader(std::string label, std::vector< WidgetDesc > children, std::string id, bool defaultOpen)
Collapsible header containing child widgets.
Definition Widget.cpp:814
WidgetDesc checkbox(std::string label, bool checked, std::string id, std::function< void(bool)> onToggle)
Checkbox with a label; fires onToggle.
Definition Widget.cpp:387
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 button(std::string label, std::string id, std::function< void()> onClick)
Clickable button; fires onClick.
Definition Widget.cpp:345
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:870
WidgetDesc separator(std::string id)
Horizontal separator line.
Definition Widget.cpp:379
Serializable snapshot of one scene node (value type, no scene types).
Definition SceneQuery.h:18
std::string id
Definition SceneQuery.h:19
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:15
glm::uvec4 info