载入中...
搜索中...
未找到
UIHost.cpp
浏览该文件的文档.
1#include "ui/UIHost.h"
2#include "ui/Widget.h"
3
4#include <algorithm>
5
6namespace eve::ui {
7namespace {
8
9uint32_t g_anonHostSeq = 0;
10
11bool focusableType(NodeType type) {
12 switch (type) {
13 case NodeType::Link:
19 case NodeType::Combo:
24 case NodeType::MenuItem: return true;
25 default: return false;
26 }
27}
28
29bool eligible(const UINode &node, bool sequential) {
30 return focusableType(node.type) && !node.id.empty() && node.visible && node.enabled &&
31 (sequential ? node.focusMode == FocusMode::All
32 : node.focusMode != FocusMode::None);
33}
34
35const std::string &neighbor(const UINode &node, FocusDirection direction) {
36 switch (direction) {
37 case FocusDirection::Next: return node.focusNext;
38 case FocusDirection::Previous: return node.focusPrevious;
39 case FocusDirection::Left: return node.focusLeft;
40 case FocusDirection::Right: return node.focusRight;
41 case FocusDirection::Up: return node.focusUp;
42 case FocusDirection::Down: return node.focusDown;
43 }
44 return node.focusNext;
45}
46
47} // namespace
48
50 UIHost *h = UIHost::create();
51 if (!h) return {};
52 const UIHostHandle handle = ecs::handle_of(h);
53 h->meta()->entity = handle;
54 if (name.empty()) {
55 h->meta()->name = "host" + std::to_string(++g_anonHostSeq);
56 } else {
57 h->meta()->name = name;
58 }
59 return handle;
60}
61
63 auto *entity = ecs::try_get(handle);
64 if (auto *host = entity ? dynamic_cast<UIHost *>(entity) : nullptr) return std::ref(*host);
65 return std::nullopt;
66}
67
68void UIHost::setName(const std::string &name) { meta()->name = name; }
69
70const std::string &UIHost::getName() { return meta()->name; }
71
73 meta()->hasThemeOverride = true;
74 meta()->themeOverride = theme;
75}
76
77void UIHost::clearThemeOverride() { meta()->hasThemeOverride = false; }
78
79bool UIHost::hasThemeOverride() { return meta()->hasThemeOverride; }
80
82 return meta()->hasThemeOverride ? &meta()->themeOverride : nullptr;
83}
84
85void UIHost::setWorldAnchor(float x, float y, float z) {
86 auto anchor = worldAnchor();
87 anchor->enabled = true;
88 anchor->worldX = x;
89 anchor->worldY = y;
90 anchor->worldZ = z;
91}
92
94 auto anchor = worldAnchor();
95 anchor->enabled = false;
96 anchor->state = WorldAnchorState::Disabled;
97 anchor->scale = 1.f;
98}
99
100void UIHost::setTree(WidgetDesc root) { applyTree(this, std::move(root)); }
101
103
104void UIHost::setSimplePanel(const std::string &title, const std::string &labelText,
105 const std::string &buttonText, const std::string &labelId,
106 const std::string &buttonId) {
108 {
109 text(labelText, labelId),
110 button(buttonText, buttonId),
111 },
112 "root"));
113}
114
115std::optional<UINodeHandle> UIHost::nodeHandle(const std::string &id) {
116 if (id.empty()) return std::nullopt;
117 auto t = tree();
118 for (std::size_t i = 0; i < t->nodes.size(); ++i) {
119 if (t->nodes[i].id == id) return UINodeHandle{handle(), t->generation, i};
120 }
121 return std::nullopt;
122}
123
125 auto host = resolve(handle.host);
126 if (!host) return std::nullopt;
127 auto t = host->get().tree();
128 if (t->generation != handle.treeGeneration || handle.index >= t->nodes.size()) return std::nullopt;
129 return std::ref(t->nodes[handle.index]);
130}
131
133 if (id.empty()) return std::nullopt;
134 auto t = tree();
135 for (auto &n : t->nodes) {
136 if (n.id == id) return std::ref(n);
137 }
138 return std::nullopt;
139}
140
142 if (key.empty()) return std::nullopt;
143 auto t = tree();
144 for (auto &n : t->nodes) {
145 if (n.key == key) return std::ref(n);
146 }
147 return std::nullopt;
148}
149
150void UIHost::setTextById(const std::string &id, const std::string &text) {
151 if (auto n = findById(id); n && n->get().text != text) n->get().text = text;
152}
153
154void UIHost::setVisibleById(const std::string &id, bool visible) {
155 if (auto n = findById(id)) n->get().visible = visible;
156}
157
158void UIHost::setEnabledById(const std::string &id, bool enabled) {
159 if (auto n = findById(id)) {
160 n->get().enabled = enabled;
161 if (!enabled) {
162 n->get().focusRequested = false;
163 n->get().focused = false;
164 }
165 }
166}
167
168void UIHost::setCheckedById(const std::string &id, bool checked) {
169 if (auto n = findById(id)) n->get().checked = checked;
170}
171
172void UIHost::setValueById(const std::string &id, float value) {
173 if (auto n = findById(id)) n->get().value = value;
174}
175
176void UIHost::setValueTextById(const std::string &id, const std::string &value) {
177 if (auto n = findById(id)) n->get().valueText = value;
178}
179
180bool UIHost::requestFocusById(const std::string &id) {
181 auto target = findById(id);
182 if (!target || !eligible(target->get(), false)) return false;
183 auto t = tree();
184 for (auto &node : t->nodes) node.focusRequested = false;
185 target->get().focusRequested = true;
186 return true;
187}
188
190 auto t = tree();
191
192 UINode *current = nullptr;
193 for (UINode &node : t->nodes) {
194 if (node.focused) {
195 current = &node;
196 break;
197 }
198 if (!current && node.focusRequested) current = &node;
199 }
200
201 if (current) {
202 const std::string &explicitId = neighbor(*current, direction);
203 if (!explicitId.empty()) {
204 auto explicitTarget = findById(explicitId);
205 if (explicitTarget && eligible(explicitTarget->get(), true)) return requestFocusById(explicitId);
206 }
207 }
208
209 std::vector<std::pair<int, std::size_t>> order;
210 order.reserve(t->nodes.size());
211 for (std::size_t index = 0; index < t->nodes.size(); ++index) {
212 const UINode &node = t->nodes[index];
213 if (eligible(node, true) && node.tabIndex >= 0)
214 order.emplace_back(node.tabIndex, index);
215 }
216 if (order.empty()) return false;
217 std::stable_sort(order.begin(), order.end());
218
219 const bool backwards = direction == FocusDirection::Previous ||
222 std::size_t targetOrder = backwards ? order.size() - 1 : 0;
223 if (current) {
224 const auto found = std::find_if(order.begin(), order.end(),
225 [current, &t](const auto &entry) {
226 return &t->nodes[entry.second] == current;
227 });
228 if (found != order.end()) {
229 const std::size_t position = static_cast<std::size_t>(found - order.begin());
230 if (backwards) {
231 if (position == 0 && !wrap) return false;
232 targetOrder = position == 0 ? order.size() - 1 : position - 1;
233 } else {
234 if (position + 1 == order.size() && !wrap) return false;
235 targetOrder = position + 1 == order.size() ? 0 : position + 1;
236 }
237 }
238 }
239 return requestFocusById(t->nodes[order[targetOrder].second].id);
240}
241
242std::string UIHost::focusedId() {
243 auto t = tree();
244 for (const auto &node : t->nodes) {
245 if (node.focused) return node.id;
246 }
247 return {};
248}
249
250bool UIHost::setClickHandler(const std::string &id, std::function<void()> fn) {
251 auto n = findById(id);
252 if (!n) return false;
253 auto t = tree();
254 if (n->get().handlerClick != 0) {
255 size_t idx = size_t(n->get().handlerClick - 1);
256 if (idx < t->clickHandlers.size()) {
257 t->clickHandlers[idx] = std::move(fn);
258 return true;
259 }
260 }
261 t->clickHandlers.push_back(std::move(fn));
262 n->get().handlerClick = static_cast<uint32_t>(t->clickHandlers.size());
263 return true;
264}
265
266bool UIHost::setToggleHandler(const std::string &id, std::function<void(bool)> fn) {
267 auto n = findById(id);
268 if (!n) return false;
269 auto t = tree();
270 if (n->get().handlerToggle != 0) {
271 size_t idx = size_t(n->get().handlerToggle - 1);
272 if (idx < t->toggleHandlers.size()) {
273 t->toggleHandlers[idx] = std::move(fn);
274 return true;
275 }
276 }
277 t->toggleHandlers.push_back(std::move(fn));
278 n->get().handlerToggle = static_cast<uint32_t>(t->toggleHandlers.size());
279 return true;
280}
281
282bool UIHost::setValueHandler(const std::string &id, std::function<void(float)> fn) {
283 auto n = findById(id);
284 if (!n) return false;
285 auto t = tree();
286 if (n->get().handlerValue != 0) {
287 size_t idx = size_t(n->get().handlerValue - 1);
288 if (idx < t->valueHandlers.size()) {
289 t->valueHandlers[idx] = std::move(fn);
290 return true;
291 }
292 }
293 t->valueHandlers.push_back(std::move(fn));
294 n->get().handlerValue = static_cast<uint32_t>(t->valueHandlers.size());
295 return true;
296}
297
298bool UIHost::setTextHandler(const std::string &id, std::function<void(const std::string &)> fn) {
299 auto n = findById(id);
300 if (!n) return false;
301 auto t = tree();
302 if (n->get().handlerText != 0) {
303 size_t idx = size_t(n->get().handlerText - 1);
304 if (idx < t->textHandlers.size()) {
305 t->textHandlers[idx] = std::move(fn);
306 return true;
307 }
308 }
309 t->textHandlers.push_back(std::move(fn));
310 n->get().handlerText = static_cast<uint32_t>(t->textHandlers.size());
311 return true;
312}
313
314} // namespace eve::ui
double z
double y
double x
LogicalId target
int root
Definition AnimSmr.cpp:119
std::string title
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
glm::vec3 n
Definition Grass.cpp:63
int h
std::array< float, 3 > position
std::vector< std::int32_t > order
int idx
PrimitiveHandle handle
Object::Ptr value
float t
const EntityFact * entity
const RoadNode * node
RoadLaneDirection direction
bool found
std::vector< bool > eligible
std::string text
double current
bool visible
UIHostHandle host
uint32_t index
int neighbor
ECS mount point for one UI panel/screen. Subclass to attach UI to game entities, e....
Definition UIHost.h:284
bool setTreeReconcile(WidgetDesc root)
Key-aware patch when structure matches; else full replace.
Definition UIHost.cpp:102
void setTree(WidgetDesc root)
Full replace.
Definition UIHost.cpp:100
void setCheckedById(const std::string &id, bool checked)
Definition UIHost.cpp:168
eve::OptionalRef< UINode > findById(const std::string &id)
Looks up a node by stable id without transferring ownership.
Definition UIHost.cpp:132
std::string focusedId()
Return the currently focused control id, or an empty string.
Definition UIHost.cpp:242
void setValueById(const std::string &id, float value)
Definition UIHost.cpp:172
bool setToggleHandler(const std::string &id, std::function< void(bool)> fn)
Definition UIHost.cpp:266
const std::string & getName()
Definition UIHost.cpp:70
void setThemeOverride(const Theme &theme)
Apply a host-local Theme without mutating ui::globalTheme().
Definition UIHost.cpp:72
const Theme * themeOverride()
Borrow the host-local Theme when one is installed.
Definition UIHost.cpp:81
void clearThemeOverride()
Stop using a host-local Theme and inherit the process global again.
Definition UIHost.cpp:77
bool moveFocus(FocusDirection direction, bool wrap=true)
Move through explicit neighbors or deterministic tab order.
Definition UIHost.cpp:189
bool setTextHandler(const std::string &id, std::function< void(const std::string &)> fn)
Definition UIHost.cpp:298
void setName(const std::string &name)
Names the host.
Definition UIHost.cpp:68
static eve::OptionalRef< UINode > resolveNode(UINodeHandle handle)
Resolve a node token in constant time without transferring ownership.
Definition UIHost.cpp:124
bool hasThemeOverride()
True when a host-local Theme is installed.
Definition UIHost.cpp:79
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:250
static UIHostHandle createHost(const std::string &name="")
Creates an ECS-owned UI host.
Definition UIHost.cpp:49
void setVisibleById(const std::string &id, bool visible)
Definition UIHost.cpp:154
UIHostHandle handle() const noexcept
Returns this host's generation-qualified ECS handle.
Definition UIHost.h:408
void setValueTextById(const std::string &id, const std::string &value)
Definition UIHost.cpp:176
void clearWorldAnchor()
Disables 3D projection and returns the host to normal screen layout.
Definition UIHost.cpp:93
void setTextById(const std::string &id, const std::string &text)
Widget state updates by node id.
Definition UIHost.cpp:150
void setWorldAnchor(float x, float y, float z)
Enables projection of this host from a 3D world point.
Definition UIHost.cpp:85
void setEnabledById(const std::string &id, bool enabled)
Definition UIHost.cpp:158
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:104
static eve::OptionalRef< UIHost > resolve(UIHostHandle handle) noexcept
Resolves a UI host handle with ECS generation checking.
Definition UIHost.cpp:62
bool setValueHandler(const std::string &id, std::function< void(float)> fn)
Definition UIHost.cpp:282
eve::OptionalRef< UINode > findByKey(const std::string &key)
Looks up a node by reconciliation key without transferring ownership.
Definition UIHost.cpp:141
bool requestFocusById(const std::string &id)
Request keyboard/gamepad focus for a control on the next frame.
Definition UIHost.cpp:180
std::optional< UINodeHandle > nodeHandle(const std::string &id)
Resolve an id once into a non-owning, generation-checked node token.
Definition UIHost.cpp:115
void applyTree(UIHost *host, WidgetDesc root)
Full replace flatten.
Definition Widget.cpp:926
WidgetDesc window(std::string title, std::vector< WidgetDesc > children, std::string id)
Top-level window widget with a title bar.
Definition Widget.cpp:326
NodeType
Widget node kinds understood by the UI renderer.
Definition UIHost.h:32
FocusDirection
Logical focus movement understood by keyboard, gamepad and automation.
Definition UIHost.h:104
WidgetDesc button(std::string label, std::string id, std::function< void()> onClick)
Clickable button; fires onClick.
Definition Widget.cpp:345
ecs::EntityHandle UIHostHandle
Definition UIHost.h:20
bool applyTreeReconcile(UIHost *host, WidgetDesc root)
Patch by key/id when structure (type + child keys) matches; otherwise full replace....
Definition Widget.cpp:940
std::optional< std::reference_wrapper< T > > OptionalRef
Optional borrowed reference; it does not extend T's lifetime.
Definition BorrowedRef.h:24
SettlementPipeline::Stage fn
bool enabled
Unified UI design tokens (colors, geometry, typography). Applied to ImGui each frame via applyThemeTo...
Definition Theme.h:41
Non-owning node token; valid across property reconciliation, invalid after tree replacement....
Definition UIHost.h:25
Retained UI widget node (arena tree). Conceptual counterpart of eve::scene::SceneNode; built declarat...
Definition UIHost.h:151
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:15
Object::Ptr theme