载入中...
搜索中...
未找到
UIHost.cpp
浏览该文件的文档.
1#include "ui/UIHost.h"
2#include "ui/Widget.h"
3
4namespace eve::ui {
5namespace {
6
7uint32_t g_anonHostSeq = 0;
8
9} // namespace
10
11UIHost *UIHost::createHost(const std::string &name) {
12 UIHost *h = UIHost::create();
13 h->meta()->entity = h;
14 if (name.empty()) {
15 h->meta()->name = "host" + std::to_string(++g_anonHostSeq);
16 } else {
17 h->meta()->name = name;
18 }
19 return h;
20}
21
22void UIHost::setName(const std::string &name) { meta()->name = name; }
23
24const std::string &UIHost::getName() { return meta()->name; }
25
26void UIHost::setTree(WidgetDesc root) { applyTree(this, std::move(root)); }
27
28bool UIHost::setTreeReconcile(WidgetDesc root) { return applyTreeReconcile(this, std::move(root)); }
29
30void UIHost::setSimplePanel(const std::string &title, const std::string &labelText,
31 const std::string &buttonText, const std::string &labelId,
32 const std::string &buttonId) {
34 {
35 text(labelText, labelId),
36 button(buttonText, buttonId),
37 },
38 "root"));
39}
40
41UINode *UIHost::findById(const std::string &id) {
42 if (id.empty()) return nullptr;
43 auto t = tree();
44 for (auto &n : t->nodes) {
45 if (n.id == id) return &n;
46 }
47 return nullptr;
48}
49
50UINode *UIHost::findByKey(const std::string &key) {
51 if (key.empty()) return nullptr;
52 auto t = tree();
53 for (auto &n : t->nodes) {
54 if (n.key == key) return &n;
55 }
56 return nullptr;
57}
58
59void UIHost::setTextById(const std::string &id, const std::string &text) {
60 if (auto *n = findById(id)) n->text = text;
61}
62
63void UIHost::setVisibleById(const std::string &id, bool visible) {
64 if (auto *n = findById(id)) n->visible = visible;
65}
66
67void UIHost::setCheckedById(const std::string &id, bool checked) {
68 if (auto *n = findById(id)) n->checked = checked;
69}
70
71void UIHost::setValueById(const std::string &id, float value) {
72 if (auto *n = findById(id)) n->value = value;
73}
74
75void UIHost::setValueTextById(const std::string &id, const std::string &value) {
76 if (auto *n = findById(id)) n->valueText = value;
77}
78
79bool UIHost::setClickHandler(const std::string &id, std::function<void()> fn) {
80 auto *n = findById(id);
81 if (!n) return false;
82 auto t = tree();
83 if (n->handlerClick != 0) {
84 size_t idx = size_t(n->handlerClick - 1);
85 if (idx < t->clickHandlers.size()) {
86 t->clickHandlers[idx] = std::move(fn);
87 return true;
88 }
89 }
90 t->clickHandlers.push_back(std::move(fn));
91 n->handlerClick = static_cast<uint32_t>(t->clickHandlers.size());
92 return true;
93}
94
95bool UIHost::setToggleHandler(const std::string &id, std::function<void(bool)> fn) {
96 auto *n = findById(id);
97 if (!n) return false;
98 auto t = tree();
99 if (n->handlerToggle != 0) {
100 size_t idx = size_t(n->handlerToggle - 1);
101 if (idx < t->toggleHandlers.size()) {
102 t->toggleHandlers[idx] = std::move(fn);
103 return true;
104 }
105 }
106 t->toggleHandlers.push_back(std::move(fn));
107 n->handlerToggle = static_cast<uint32_t>(t->toggleHandlers.size());
108 return true;
109}
110
111bool UIHost::setValueHandler(const std::string &id, std::function<void(float)> fn) {
112 auto *n = findById(id);
113 if (!n) return false;
114 auto t = tree();
115 if (n->handlerValue != 0) {
116 size_t idx = size_t(n->handlerValue - 1);
117 if (idx < t->valueHandlers.size()) {
118 t->valueHandlers[idx] = std::move(fn);
119 return true;
120 }
121 }
122 t->valueHandlers.push_back(std::move(fn));
123 n->handlerValue = static_cast<uint32_t>(t->valueHandlers.size());
124 return true;
125}
126
127bool UIHost::setTextHandler(const std::string &id, std::function<void(const std::string &)> fn) {
128 auto *n = findById(id);
129 if (!n) return false;
130 auto t = tree();
131 if (n->handlerText != 0) {
132 size_t idx = size_t(n->handlerText - 1);
133 if (idx < t->textHandlers.size()) {
134 t->textHandlers[idx] = std::move(fn);
135 return true;
136 }
137 }
138 t->textHandlers.push_back(std::move(fn));
139 n->handlerText = static_cast<uint32_t>(t->textHandlers.size());
140 return true;
141}
142
143} // namespace eve::ui
std::string value
std::string title
glm::vec3 n
Definition Grass.cpp:64
int h
int idx
const char * name
Definition RockMesh.cpp:21
SettlementPipeline::Stage fn
ECS mount point for one UI panel/screen. Subclass to attach UI to game entities, e....
Definition UIHost.h:130
bool setTreeReconcile(WidgetDesc root)
Key-aware patch when structure matches; else full replace.
Definition UIHost.cpp:28
static UIHost * createHost(const std::string &name="")
Definition UIHost.cpp:11
void setTree(WidgetDesc root)
Full replace.
Definition UIHost.cpp:26
void setCheckedById(const std::string &id, bool checked)
Definition UIHost.cpp:67
void setValueById(const std::string &id, float value)
Definition UIHost.cpp:71
bool setToggleHandler(const std::string &id, std::function< void(bool)> fn)
Definition UIHost.cpp:95
const std::string & getName()
Definition UIHost.cpp:24
bool setTextHandler(const std::string &id, std::function< void(const std::string &)> fn)
Definition UIHost.cpp:127
void setName(const std::string &name)
Names the host.
Definition UIHost.cpp:22
bool setClickHandler(const std::string &id, std::function< void()> fn)
Installs a widget callback by node id (returns false if not found).
Definition UIHost.cpp:79
void setVisibleById(const std::string &id, bool visible)
Definition UIHost.cpp:63
UINode * findById(const std::string &id)
Looks up a node by id or reconciliation key.
Definition UIHost.cpp:41
void setValueTextById(const std::string &id, const std::string &value)
Definition UIHost.cpp:75
UINode * findByKey(const std::string &key)
Definition UIHost.cpp:50
void setTextById(const std::string &id, const std::string &text)
Widget state updates by node id.
Definition UIHost.cpp:59
void setSimplePanel(const std::string &title, const std::string &labelText, const std::string &buttonText, const std::string &labelId="label", const std::string &buttonId="btn")
One-shot convenience panel: window + label + button.
Definition UIHost.cpp:30
bool setValueHandler(const std::string &id, std::function< void(float)> fn)
Definition UIHost.cpp:111
void applyTree(UIHost *host, WidgetDesc root)
Full replace flatten.
Definition Widget.cpp:477
WidgetDesc text(std::string content, std::string id)
Static text label.
Definition Widget.cpp:235
WidgetDesc window(std::string title, std::vector< WidgetDesc > children, std::string id)
Top-level window widget with a title bar.
Definition Widget.cpp:225
bool applyTreeReconcile(UIHost *host, WidgetDesc root)
Patch by key/id when structure (type + child keys) matches; otherwise full replace....
Definition Widget.cpp:490
WidgetDesc button(std::string label, std::string id, std::function< void()> onClick)
Clickable button; fires onClick.
Definition Widget.cpp:244
Retained UI widget node (arena tree). Conceptual counterpart of eve::scene::SceneNode; built declarat...
Definition UIHost.h:54
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:13