载入中...
搜索中...
未找到
UIHost.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
6#include "common/ECS.h"
7#include "ui/HudCanvas.h"
8#include "ui/HudGeometry.h"
9#include "ui/Theme.h"
10
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <optional>
15#include <string>
16#include <vector>
17
18namespace eve::ui {
19
20using UIHostHandle = ecs::EntityHandle;
21
27 std::uint64_t treeGeneration = 0;
28 std::size_t index = 0;
29};
30
32enum class NodeType : uint8_t {
33 Window = 0,
34 Text = 1,
35 Button = 2,
36 SameLine = 3,
37 Group = 4,
38 Separator = 5,
39 Checkbox = 6,
40 Slider = 7,
41 Progress = 8,
42 InputText = 9,
44 Child = 11,
45 Flex = 12, // elastic row/column container
46 Spacer = 13, // flexible empty space (default flexGrow=1)
47 Image = 14, // colored rect or engine texture (nine-patch capable)
48 ImageButton = 15, // clickable image
49 Combo = 16, // dropdown; options newline-separated in valueText, index in value
50 ScrollList = 17, // virtualized scrollable list (uniform itemHeight)
51 Viewport = 18, // embedded render target: offscreen Canvas shown + input routed
52 SearchField = 19, // compact input with a search hint/icon
53 Switch = 20, // modern boolean toggle
54 Badge = 21, // compact status/category pill
55 Card = 22, // bordered surface container
56 SectionHeader = 23, // non-collapsible editor section heading
57 MenuBar = 24, // native ImGui window menu bar
58 Menu = 25, // popup menu container
59 MenuItem = 26, // selectable menu command
60 Toolbar = 27, // horizontal editor command strip
61 Toolbox = 28, // wrapping grid of editor tools
62 Sidebar = 29, // vertical editor side panel
63 StatusBar = 30, // compact horizontal status strip
64 SplitPane = 31, // two resizable panes
65 NinePatchPanel = 32, // .9.png-backed stretchable content container
66 ColorPalette = 33, // color editor + swatch grid; RGBA lives in tint*
67 Grid = 34, // fixed-column retained grid container
68 Tab = 35, // selectable tab label (page id in valueText)
69 TabBar = 36, // horizontal strip of Tab children
70 TabContainer = 37, // TabBar + pages; active page id in valueText
71 PanelFrame = 38, // titled fixed chrome (not a free-floating Window host)
72 DockArea = 39, // named fill slot for a dock region (region in valueText)
73 AppFrame = 40, // fixed editor frame: toolbar + body + status bar
74 Link = 41, // clickable text; target URI in valueText
75 FilledImage = 42, // cropped image or solid fill
76 RadialFill = 43, // solid sector/ring for cooldown overlays
77};
78
80enum class FlexDirection : uint8_t { Row = 0, Column = 1 };
81
83enum class FlexWrap : uint8_t { NoWrap = 0, Wrap = 1 };
84
86enum class OverflowMode : uint8_t { Visible = 0, Clip = 1, Scroll = 2 };
87
89enum class FlexAlign : uint8_t { Start = 0, Center = 1, End = 2, Stretch = 3 };
90
92enum class FlexJustify : uint8_t {
93 Start = 0,
94 Center = 1,
95 End = 2,
96 SpaceBetween = 3,
97 SpaceAround = 4,
98};
99
101enum class FocusMode : uint8_t { None = 0, Click = 1, All = 2 };
102
104enum class FocusDirection : uint8_t { Next = 0, Previous, Left, Right, Up, Down };
105
107enum class MouseFilter : uint8_t { Stop = 0, Pass = 1, Ignore = 2 };
108
110enum class ThemePreset : uint8_t { Inherit = 0, Dark = 1, Light = 2 };
111
113enum class WorldAnchorState : uint8_t {
114 Disabled = 0,
115 Visible,
118 NoCamera,
119 Crowded
120};
121
123enum class WorldAnchorEdgePolicy : uint8_t { Hide = 0, Clamp };
124
126enum class WorldAnchorOverlapPolicy : uint8_t { Allow = 0, Avoid };
127
129enum class AccessibilityRole : uint8_t {
130 Auto = 0,
131 Button,
132 Checkbox,
133 Slider,
134 Text,
135 TextInput,
136 List,
137 ListItem,
138 Menu,
139 MenuItem,
140 Progress,
141 Region,
142 Tab,
143 Window,
144 Link
145};
146
151struct UINode {
153 std::string id;
154 std::string key;
155 std::string text; // label / title
156 std::string valueText; // InputText content
157 std::string tooltip; // hover help; empty disables the tooltip
159 std::string helpText;
161 std::string disabledReason;
163 std::string placeholder;
165 bool multiline = false;
167 bool readOnly = false;
168 bool visible = true;
169 bool enabled = true;
170 bool checked = false;
171 bool open = true; // CollapsingHeader default-open hint
175 std::string styleClass;
177 bool hasResolvedStyle = false;
178 float opacity = 1.f; // transient visual state; animation never changes persistent style data
179 int tabIndex = 0;
180 std::string focusNext;
181 std::string focusPrevious;
182 std::string focusLeft;
183 std::string focusRight;
184 std::string focusUp;
185 std::string focusDown;
186 bool focusRequested = false;
187 bool focused = false;
189 std::string accessibilityName;
191 bool dragSource = false;
192 bool dropTarget = false;
193 std::string dragPayloadType;
194 std::string dragPayloadText;
195 std::string acceptedDropType;
196 float value = 0.f;
197 float minValue = 0.f;
198 float maxValue = 1.f;
199 float sizeX = 0.f; // Child / item basis width; 0 = auto
200 float sizeY = 0.f; // Child / item basis height; 0 = auto
201 // --- Layout box model (input props; 0 = unset) ---
202 float marginL = 0.f;
203 float marginT = 0.f;
204 float marginR = 0.f;
205 float marginB = 0.f;
206 float paddingL = 0.f;
207 float paddingT = 0.f;
208 float paddingR = 0.f;
209 float paddingB = 0.f;
210 float minSizeX = 0.f; // 0 = auto
211 float minSizeY = 0.f;
212 float maxSizeX = 0.f; // 0 = unlimited
213 float maxSizeY = 0.f;
214 float percentW = 0.f; // 0..1 fraction of parent content width; overrides basis
215 float percentH = 0.f; // 0..1 fraction of parent content height
216 float aspectRatio = 0.f; // width / height; 0 disables ratio-derived sizing
217 float anchorX = 0.f; // absolute placement inside Flex: anchor point in parent (0..1)
218 float anchorY = 0.f;
219 float posX = 0.f; // absolute placement offset (relative to anchor edge)
220 float posY = 0.f;
221 bool absolute = false; // skip flex flow; placed by anchor/pos
222 // --- Image / ImageButton props (NodeType::Image*) ---
223 uint64_t textureId = 0; // opaque id from UIBackend::registerTexture; 0 = solid color
224 float tintR = 1.f;
225 float tintG = 1.f;
226 float tintB = 1.f;
227 float tintA = 1.f;
228 float uv0x = 0.f; // source rect in texture (UV)
229 float uv0y = 0.f;
230 float uv1x = 1.f;
231 float uv1y = 1.f;
232 float borderL = 0.f; // nine-patch border (px); all 0 = plain image
233 float borderT = 0.f;
234 float borderR = 0.f;
235 float borderB = 0.f;
237 bool fillStretch = false;
238 float fillStartDegrees = -90.f;
239 bool fillClockwise = true;
240 float fillInnerRadius = 0.f; // fraction of outer radius, [0,1]
241 float cornerRadius = 0.f; // solid-color rounded rect radius
242 float fontSize = 0.f; // zero inherits host font; positive values are logical UI units
243 float textR = 1.f, textG = 1.f, textB = 1.f, textA = 1.f;
244 float textAlignX = 0.f, textAlignY = 0.f;
245 float wrapWidth = 0.f; // Text wrap width (0 = no wrap)
246 float itemHeight = 0.f; // ScrollList uniform row height (0 = frame height)
247 // --- Computed by measure/arrange pass (per frame) ---
248 float measuredW = 0.f;
249 float measuredH = 0.f;
250 // Flex container props (meaningful on NodeType::Flex)
254 float gap = -1.f; // <0 → theme ItemSpacing on that axis
255 float columnGap = -1.f;
256 float rowGap = -1.f;
259 int gridColumns = 1;
260 // Flex item props (any child inside Flex)
261 float flexGrow = 0.f;
262 float flexShrink = 1.f;
263 float flexBasis = -1.f; // <0 = measured/automatic basis
264 int alignSelf = -1; // <0 = inherit parent alignItems
266 float layoutOverflowX = 0.f;
267 float layoutOverflowY = 0.f;
268 int parent = -1;
269 int firstChild = -1;
270 int nextSibling = -1;
271 uint32_t handlerClick = 0; // 1-based → Tree::clickHandlers
272 uint32_t handlerToggle = 0; // 1-based → Tree::toggleHandlers
273 uint32_t handlerValue = 0; // 1-based → Tree::valueHandlers
274 uint32_t handlerText = 0; // 1-based → Tree::textHandlers
275};
276
277struct WidgetDesc;
278
284class EVENGINE_API_WORLD UIHost : public ecs::Entity {
285public:
287 ENTITY(UIHost, ecs::Entity)
288
289
290 void release() override {}
291
293 struct Meta {
294 bool visible = true;
295 int layer = 0;
296 bool modal = false;
298 bool inputTransparent = false; // explicit display-only host; all pointer/key input passes through
299 bool overlay = false; // no title bar / chrome (HUD-style)
300 bool hasPos = false;
301 bool lockPos = true; // false: initial position only; user may move/persist it
303 bool animDrivePos = false;
304 float posX = 0.f;
305 float posY = 0.f;
306 float pivotX = 0.f;
307 float pivotY = 0.f;
308 bool hasSize = false; // explicit window size
309 bool lockSize = true; // false: initial size only; user may resize/persist it
311 bool animDriveSize = false;
312 float sizeX = 0.f;
313 float sizeY = 0.f;
314 float percentW = 0.f; // 0..1 of display width; overrides sizeX
315 float percentH = 0.f; // 0..1 of display height
316 float anchorX = 0.f; // anchor in display (0..1); with hasPos, posX is offset
317 float anchorY = 0.f;
318 std::string name;
319 float overlayBgAlpha = 0.4f;
320 bool overlayFlush = false; // remove outer WindowPadding for desktop chrome
321 uint32_t ownerId = 0;
323 bool hasThemeOverride = false;
325 };
326
327 struct Tree {
328 std::uint64_t generation = 0;
329 bool dirty = true;
330 std::vector<UINode> nodes;
331 int root = -1;
333 std::vector<std::function<void()>> clickHandlers;
335 std::vector<std::function<void(bool)>> toggleHandlers;
337 std::vector<std::function<void(float)>> valueHandlers;
339 std::vector<std::function<void(const std::string &)>> textHandlers;
340 };
341
351 struct WorldAnchor {
352 bool enabled = false;
353 float worldX = 0.f;
354 float worldY = 0.f;
355 float worldZ = 0.f;
356 float offsetX = 0.f;
357 float offsetY = 0.f;
358 float safeMargin = 8.f;
359 WorldAnchorEdgePolicy edgePolicy = WorldAnchorEdgePolicy::Hide;
360 bool hideBehindCamera = true;
361 bool distanceScale = false;
362 float referenceDistance = 10.f;
363 float minScale = 0.65f;
364 float maxScale = 1.25f;
365 WorldAnchorOverlapPolicy overlapPolicy = WorldAnchorOverlapPolicy::Allow;
366 int overlapPriority = 0;
367 float overlapPadding = 4.f;
368 float maxDisplacement = 96.f;
369
370 WorldAnchorState state = WorldAnchorState::Disabled;
371 float screenX = 0.f;
372 float screenY = 0.f;
373 float depth = 0.f;
374 float scale = 1.f;
375 float displacementX = 0.f;
376 float displacementY = 0.f;
377 };
378
379 COMPONENT(Meta, meta)
380 COMPONENT(Tree, tree)
381 COMPONENT(WorldAnchor, worldAnchor)
382
383
391 [[nodiscard]] static UIHostHandle createHost(const std::string &name = "");
392
402 [[nodiscard]] static eve::OptionalRef<UIHost> resolve(UIHostHandle handle) noexcept;
403
408 [[nodiscard]] UIHostHandle handle() const noexcept { return ecs::handle_of(this); }
409
411 void setName(const std::string &name);
412 const std::string &getName();
418 void setThemeOverride(const Theme &theme);
420 void clearThemeOverride();
422 bool hasThemeOverride();
428 [[nodiscard]] const Theme *themeOverride();
430 void setOwnerId(uint32_t id) { meta()->ownerId = id; }
431 uint32_t getOwnerId() { return meta()->ownerId; }
432
434 void setTree(WidgetDesc root);
436 bool setTreeReconcile(WidgetDesc root);
437
439 void setSimplePanel(const std::string &title, const std::string &labelText,
440 const std::string &buttonText, const std::string &labelId = "label",
441 const std::string &buttonId = "btn");
442
444 void setTextById(const std::string &id, const std::string &text);
445 void setVisibleById(const std::string &id, bool visible);
446 void setEnabledById(const std::string &id, bool enabled);
447 void setCheckedById(const std::string &id, bool checked);
448 void setValueById(const std::string &id, float value);
449 void setValueTextById(const std::string &id, const std::string &value);
451 bool setClickHandler(const std::string &id, std::function<void()> fn);
452 bool setToggleHandler(const std::string &id, std::function<void(bool)> fn);
453 bool setValueHandler(const std::string &id, std::function<void(float)> fn);
454 bool setTextHandler(const std::string &id, std::function<void(const std::string &)> fn);
459 [[nodiscard]] std::optional<UINodeHandle> nodeHandle(const std::string &id);
465 [[nodiscard]] static eve::OptionalRef<UINode> resolveNode(UINodeHandle handle);
474 [[nodiscard]] eve::OptionalRef<UINode> findById(const std::string &id);
483 [[nodiscard]] eve::OptionalRef<UINode> findByKey(const std::string &key);
485 bool requestFocusById(const std::string &id);
491 bool moveFocus(FocusDirection direction, bool wrap = true);
493 std::string focusedId();
495 void markDirty() { tree()->dirty = true; }
496
498 void setVisible(bool v) { meta()->visible = v; }
499 void setLayer(int layer) { meta()->layer = layer; }
500 void setModal(bool modal) { meta()->modal = modal; }
501
508 void setWorldAnchor(float x, float y, float z);
510 void clearWorldAnchor();
511};
512
513} // namespace eve::ui
double z
double y
double x
int root
Definition AnimSmr.cpp:119
Vocabulary aliases for nullable and checked borrowed references.
std::string title
SQObjectPtr key
Definition ECS.cpp:155
Generation generation
Definition ECS.cpp:40
std::string name
Definition ECS.cpp:154
#define EVENGINE_API_WORLD
Definition Export.h:109
float v
std::array< float, 3 > scale
std::string id
TileLayer * layer
bool dirty
PrimitiveHandle handle
Object::Ptr value
const EntityFact * entity
RoadLaneDirection direction
std::string text
float offsetX
float offsetY
bool visible
std::uint32_t depth
ECS mount point for one UI panel/screen. Subclass to attach UI to game entities, e....
Definition UIHost.h:284
void markDirty()
Marks the tree for a full rebuild on the next frame.
Definition UIHost.h:495
void setLayer(int layer)
Definition UIHost.h:499
void setOwnerId(uint32_t id)
Attaches the host to an owner id (scene/UI ownership).
Definition UIHost.h:430
void setVisible(bool v)
Host visibility / layer / modality.
Definition UIHost.h:498
void setModal(bool modal)
Definition UIHost.h:500
UIHostHandle handle() const noexcept
Returns this host's generation-qualified ECS handle.
Definition UIHost.h:408
uint32_t getOwnerId()
Definition UIHost.h:431
WorldAnchorEdgePolicy
Policy used when a projected world-space anchor leaves the viewport.
Definition UIHost.h:123
FlexWrap
Whether Flex children remain on one line or wrap onto additional lines.
Definition UIHost.h:83
FlexDirection
Main-axis direction for Flex containers.
Definition UIHost.h:80
MouseFilter
Pointer event participation and propagation policy.
Definition UIHost.h:107
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
FocusMode
Keyboard/gamepad focus policy for an interactive control.
Definition UIHost.h:101
FlexJustify
Main-axis distribution of free space in a Flex container.
Definition UIHost.h:92
ThemePreset
Built-in theme override inherited by a retained UI subtree.
Definition UIHost.h:110
OverflowMode
Behavior when arranged children exceed the retained layout box.
Definition UIHost.h:86
ecs::EntityHandle UIHostHandle
Definition UIHost.h:20
FillDirection
Direction of a linear image fill; geometry stays inside the original layout box.
Definition HudGeometry.h:11
WorldAnchorState
Visibility result of projecting a world-space UI anchor.
Definition UIHost.h:113
FlexAlign
Cross-axis alignment of Flex children.
Definition UIHost.h:89
WorldAnchorOverlapPolicy
Deterministic screen-space overlap policy for projected world anchors.
Definition UIHost.h:126
AccessibilityRole
Platform-neutral accessibility role exposed by controls.
Definition UIHost.h:129
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
Per-host logical design canvas. Margins are display logical pixels, independent of DPI.
Definition HudCanvas.h:8
Sparse reusable widget style resolved once while a retained tree is built.
Definition Theme.h:133
Unified UI design tokens (colors, geometry, typography). Applied to ImGui each frame via applyThemeTo...
Definition Theme.h:41
Meta public API.
Definition UIHost.h:293
std::string name
Definition UIHost.h:318
std::vector< UINode > nodes
Definition UIHost.h:330
std::vector< std::function< void(float)> > valueHandlers
Void.
Definition UIHost.h:337
std::vector< std::function< void()> > clickHandlers
Void.
Definition UIHost.h:333
std::vector< std::function< void(const std::string &)> > textHandlers
Void.
Definition UIHost.h:339
std::vector< std::function< void(bool)> > toggleHandlers
Void.
Definition UIHost.h:335
Optional 3D-world projection for this UI host.
Definition UIHost.h:351
Non-owning node token; valid across property reconciliation, invalid after tree replacement....
Definition UIHost.h:25
std::uint64_t treeGeneration
Definition UIHost.h:27
std::size_t index
Definition UIHost.h:28
UIHostHandle host
Definition UIHost.h:26
Retained UI widget node (arena tree). Conceptual counterpart of eve::scene::SceneNode; built declarat...
Definition UIHost.h:151
float fontSize
Definition UIHost.h:242
float marginR
Definition UIHost.h:204
float fillStartDegrees
Definition UIHost.h:238
float wrapWidth
Definition UIHost.h:245
NodeType type
Definition UIHost.h:152
uint64_t textureId
Definition UIHost.h:223
FillDirection fillDirection
Definition UIHost.h:236
std::string placeholder
Empty-input hint; applies to single-line text inputs.
Definition UIHost.h:163
std::string focusUp
Definition UIHost.h:184
float anchorY
Definition UIHost.h:218
std::string key
Definition UIHost.h:154
FlexDirection flexDirection
Definition UIHost.h:251
float paddingB
Definition UIHost.h:209
float maxValue
Definition UIHost.h:198
uint32_t handlerValue
Definition UIHost.h:273
float anchorX
Definition UIHost.h:217
bool fillStretch
Definition UIHost.h:237
float textAlignX
Definition UIHost.h:244
float paddingT
Definition UIHost.h:207
bool dropTarget
Definition UIHost.h:192
int gridColumnSpan
Definition UIHost.h:265
MouseFilter mouseFilter
Definition UIHost.h:173
float layoutOverflowY
Definition UIHost.h:267
std::string accessibilityName
Definition UIHost.h:189
uint32_t handlerText
Definition UIHost.h:274
ThemePreset themePreset
Definition UIHost.h:174
std::string helpText
Plain help shown beneath the control.
Definition UIHost.h:159
float percentH
Definition UIHost.h:215
float minSizeX
Definition UIHost.h:210
float borderB
Definition UIHost.h:235
float marginB
Definition UIHost.h:205
float maxSizeY
Definition UIHost.h:213
float borderL
Definition UIHost.h:232
std::string disabledReason
Explanation shown when interaction is disabled.
Definition UIHost.h:161
float minSizeY
Definition UIHost.h:211
uint32_t handlerClick
Definition UIHost.h:271
float flexShrink
Definition UIHost.h:262
std::string focusNext
Definition UIHost.h:180
float borderT
Definition UIHost.h:233
float opacity
Definition UIHost.h:178
std::string dragPayloadType
Definition UIHost.h:193
float marginL
Definition UIHost.h:202
AccessibilityRole accessibilityRole
Definition UIHost.h:188
bool readOnly
Allow selection and copying while preventing text edits.
Definition UIHost.h:167
FlexWrap flexWrap
Definition UIHost.h:257
std::string acceptedDropType
Definition UIHost.h:195
std::string text
Definition UIHost.h:155
float itemHeight
Definition UIHost.h:246
float minValue
Definition UIHost.h:197
std::string id
Definition UIHost.h:153
StyleClass resolvedStyle
Definition UIHost.h:176
bool fillClockwise
Definition UIHost.h:239
float columnGap
Definition UIHost.h:255
float fillInnerRadius
Definition UIHost.h:240
float maxSizeX
Definition UIHost.h:212
float paddingR
Definition UIHost.h:208
float aspectRatio
Definition UIHost.h:216
FocusMode focusMode
Definition UIHost.h:172
std::string focusPrevious
Definition UIHost.h:181
float paddingL
Definition UIHost.h:206
std::string focusDown
Definition UIHost.h:185
std::string tooltip
Definition UIHost.h:157
std::string styleClass
Definition UIHost.h:175
float cornerRadius
Definition UIHost.h:241
std::string focusRight
Definition UIHost.h:183
uint32_t handlerToggle
Definition UIHost.h:272
std::string accessibilityDescription
Definition UIHost.h:190
std::string dragPayloadText
Definition UIHost.h:194
float layoutOverflowX
Definition UIHost.h:266
FlexJustify justifyContent
Definition UIHost.h:253
float marginT
Definition UIHost.h:203
FlexAlign alignItems
Definition UIHost.h:252
float measuredW
Definition UIHost.h:248
bool multiline
Use a multiline plain-text editor.
Definition UIHost.h:165
bool dragSource
Definition UIHost.h:191
float measuredH
Definition UIHost.h:249
bool hasResolvedStyle
Definition UIHost.h:177
float flexBasis
Definition UIHost.h:263
std::string focusLeft
Definition UIHost.h:182
std::string valueText
Definition UIHost.h:156
float textAlignY
Definition UIHost.h:244
float borderR
Definition UIHost.h:234
float flexGrow
Definition UIHost.h:261
bool focusRequested
Definition UIHost.h:186
float percentW
Definition UIHost.h:214
OverflowMode overflow
Definition UIHost.h:258
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:15
Object::Ptr theme