载入中...
搜索中...
未找到
UIHost.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
6#include "common/ECS.h"
7#include "ui/Theme.h"
8
9#include <cstdint>
10#include <functional>
11#include <string>
12#include <vector>
13
14namespace eve::ui {
15
16using UIHostHandle = ecs::EntityHandle;
17
19enum class NodeType : uint8_t {
20 Window = 0,
21 Text = 1,
22 Button = 2,
23 SameLine = 3,
24 Group = 4,
25 Separator = 5,
26 Checkbox = 6,
27 Slider = 7,
28 Progress = 8,
29 InputText = 9,
31 Child = 11,
32 Flex = 12, // elastic row/column container
33 Spacer = 13, // flexible empty space (default flexGrow=1)
34 Image = 14, // colored rect or engine texture (nine-patch capable)
35 ImageButton = 15, // clickable image
36 Combo = 16, // dropdown; options newline-separated in valueText, index in value
37 ScrollList = 17, // virtualized scrollable list (uniform itemHeight)
38 Viewport = 18, // embedded render target: offscreen Canvas shown + input routed
39 SearchField = 19, // compact input with a search hint/icon
40 Switch = 20, // modern boolean toggle
41 Badge = 21, // compact status/category pill
42 Card = 22, // bordered surface container
43 SectionHeader = 23, // non-collapsible editor section heading
44 MenuBar = 24, // native ImGui window menu bar
45 Menu = 25, // popup menu container
46 MenuItem = 26, // selectable menu command
47 Toolbar = 27, // horizontal editor command strip
48 Toolbox = 28, // wrapping grid of editor tools
49 Sidebar = 29, // vertical editor side panel
50 StatusBar = 30, // compact horizontal status strip
51 SplitPane = 31, // two resizable panes
52 NinePatchPanel = 32, // .9.png-backed stretchable content container
53 ColorPalette = 33, // color editor + swatch grid; RGBA lives in tint*
54 Grid = 34, // fixed-column retained grid container
55};
56
58enum class FlexDirection : uint8_t { Row = 0, Column = 1 };
59
61enum class FlexWrap : uint8_t { NoWrap = 0, Wrap = 1 };
62
64enum class OverflowMode : uint8_t { Visible = 0, Clip = 1, Scroll = 2 };
65
67enum class FlexAlign : uint8_t { Start = 0, Center = 1, End = 2, Stretch = 3 };
68
70enum class FlexJustify : uint8_t {
71 Start = 0,
72 Center = 1,
73 End = 2,
74 SpaceBetween = 3,
75 SpaceAround = 4,
76};
77
79enum class FocusMode : uint8_t { None = 0, Click = 1, All = 2 };
80
82enum class FocusDirection : uint8_t { Next = 0, Previous, Left, Right, Up, Down };
83
85enum class MouseFilter : uint8_t { Stop = 0, Pass = 1, Ignore = 2 };
86
88enum class ThemePreset : uint8_t { Inherit = 0, Dark = 1, Light = 2 };
89
91enum class WorldAnchorState : uint8_t {
92 Disabled = 0,
93 Visible,
98};
99
101enum class WorldAnchorEdgePolicy : uint8_t { Hide = 0, Clamp };
102
104enum class WorldAnchorOverlapPolicy : uint8_t { Allow = 0, Avoid };
105
107enum class AccessibilityRole : uint8_t {
108 Auto = 0,
109 Button,
110 Checkbox,
111 Slider,
112 Text,
113 TextInput,
114 List,
115 ListItem,
116 Menu,
117 MenuItem,
118 Progress,
119 Region,
120 Tab,
121 Window
122};
123
128struct UINode {
130 std::string id;
131 std::string key;
132 std::string text; // label / title
133 std::string valueText; // InputText content
134 std::string tooltip; // hover help; empty disables the tooltip
135 bool visible = true;
136 bool enabled = true;
137 bool checked = false;
138 bool open = true; // CollapsingHeader default-open hint
142 std::string styleClass;
144 bool hasResolvedStyle = false;
145 float opacity = 1.f; // transient visual state; animation never changes persistent style data
146 int tabIndex = 0;
147 std::string focusNext;
148 std::string focusPrevious;
149 std::string focusLeft;
150 std::string focusRight;
151 std::string focusUp;
152 std::string focusDown;
153 bool focusRequested = false;
154 bool focused = false;
156 std::string accessibilityName;
158 bool dragSource = false;
159 bool dropTarget = false;
160 std::string dragPayloadType;
161 std::string dragPayloadText;
162 std::string acceptedDropType;
163 float value = 0.f;
164 float minValue = 0.f;
165 float maxValue = 1.f;
166 float sizeX = 0.f; // Child / item basis width; 0 = auto
167 float sizeY = 0.f; // Child / item basis height; 0 = auto
168 // --- Layout box model (input props; 0 = unset) ---
169 float marginL = 0.f;
170 float marginT = 0.f;
171 float marginR = 0.f;
172 float marginB = 0.f;
173 float paddingL = 0.f;
174 float paddingT = 0.f;
175 float paddingR = 0.f;
176 float paddingB = 0.f;
177 float minSizeX = 0.f; // 0 = auto
178 float minSizeY = 0.f;
179 float maxSizeX = 0.f; // 0 = unlimited
180 float maxSizeY = 0.f;
181 float percentW = 0.f; // 0..1 fraction of parent content width; overrides basis
182 float percentH = 0.f; // 0..1 fraction of parent content height
183 float aspectRatio = 0.f; // width / height; 0 disables ratio-derived sizing
184 float anchorX = 0.f; // absolute placement inside Flex: anchor point in parent (0..1)
185 float anchorY = 0.f;
186 float posX = 0.f; // absolute placement offset (relative to anchor edge)
187 float posY = 0.f;
188 bool absolute = false; // skip flex flow; placed by anchor/pos
189 // --- Image / ImageButton props (NodeType::Image*) ---
190 uint64_t textureId = 0; // opaque id from UIBackend::registerTexture; 0 = solid color
191 float tintR = 1.f;
192 float tintG = 1.f;
193 float tintB = 1.f;
194 float tintA = 1.f;
195 float uv0x = 0.f; // source rect in texture (UV)
196 float uv0y = 0.f;
197 float uv1x = 1.f;
198 float uv1y = 1.f;
199 float borderL = 0.f; // nine-patch border (px); all 0 = plain image
200 float borderT = 0.f;
201 float borderR = 0.f;
202 float borderB = 0.f;
203 float cornerRadius = 0.f; // solid-color rounded rect radius
204 float wrapWidth = 0.f; // Text wrap width (0 = no wrap)
205 float itemHeight = 0.f; // ScrollList uniform row height (0 = frame height)
206 // --- Computed by measure/arrange pass (per frame) ---
207 float measuredW = 0.f;
208 float measuredH = 0.f;
209 // Flex container props (meaningful on NodeType::Flex)
213 float gap = -1.f; // <0 → theme ItemSpacing on that axis
214 float columnGap = -1.f;
215 float rowGap = -1.f;
218 int gridColumns = 1;
219 // Flex item props (any child inside Flex)
220 float flexGrow = 0.f;
221 float flexShrink = 1.f;
222 float flexBasis = -1.f; // <0 = measured/automatic basis
223 int alignSelf = -1; // <0 = inherit parent alignItems
225 float layoutOverflowX = 0.f;
226 float layoutOverflowY = 0.f;
227 int parent = -1;
228 int firstChild = -1;
229 int nextSibling = -1;
230 uint32_t handlerClick = 0; // 1-based → Tree::clickHandlers
231 uint32_t handlerToggle = 0; // 1-based → Tree::toggleHandlers
232 uint32_t handlerValue = 0; // 1-based → Tree::valueHandlers
233 uint32_t handlerText = 0; // 1-based → Tree::textHandlers
234};
235
236struct WidgetDesc;
237
243class EVENGINE_API_WORLD UIHost : public ecs::Entity {
244public:
246 ENTITY(UIHost, ecs::Entity)
247
248
249 void release() override {}
250
252 struct Meta {
253 bool visible = true;
254 int layer = 0;
255 bool modal = false;
256 bool overlay = false; // no title bar / chrome (HUD-style)
257 bool hasPos = false;
258 bool lockPos = true; // false: initial position only; user may move/persist it
260 bool animDrivePos = false;
261 float posX = 0.f;
262 float posY = 0.f;
263 float pivotX = 0.f;
264 float pivotY = 0.f;
265 bool hasSize = false; // explicit window size
266 bool lockSize = true; // false: initial size only; user may resize/persist it
268 bool animDriveSize = false;
269 float sizeX = 0.f;
270 float sizeY = 0.f;
271 float percentW = 0.f; // 0..1 of display width; overrides sizeX
272 float percentH = 0.f; // 0..1 of display height
273 float anchorX = 0.f; // anchor in display (0..1); with hasPos, posX is offset
274 float anchorY = 0.f;
275 std::string name;
276 float overlayBgAlpha = 0.4f;
277 bool overlayFlush = false; // remove outer WindowPadding for desktop chrome
278 uint32_t ownerId = 0;
279 UIHostHandle entity{};
280 bool hasThemeOverride = false;
282 };
283
284 struct Tree {
285 bool dirty = true;
286 std::vector<UINode> nodes;
287 int root = -1;
289 std::vector<std::function<void()>> clickHandlers;
291 std::vector<std::function<void(bool)>> toggleHandlers;
293 std::vector<std::function<void(float)>> valueHandlers;
295 std::vector<std::function<void(const std::string &)>> textHandlers;
296 };
297
307 struct WorldAnchor {
308 bool enabled = false;
309 float worldX = 0.f;
310 float worldY = 0.f;
311 float worldZ = 0.f;
312 float offsetX = 0.f;
313 float offsetY = 0.f;
314 float safeMargin = 8.f;
315 WorldAnchorEdgePolicy edgePolicy = WorldAnchorEdgePolicy::Hide;
316 bool hideBehindCamera = true;
317 bool distanceScale = false;
318 float referenceDistance = 10.f;
319 float minScale = 0.65f;
320 float maxScale = 1.25f;
321 WorldAnchorOverlapPolicy overlapPolicy = WorldAnchorOverlapPolicy::Allow;
322 int overlapPriority = 0;
323 float overlapPadding = 4.f;
324 float maxDisplacement = 96.f;
325
326 WorldAnchorState state = WorldAnchorState::Disabled;
327 float screenX = 0.f;
328 float screenY = 0.f;
329 float depth = 0.f;
330 float scale = 1.f;
331 float displacementX = 0.f;
332 float displacementY = 0.f;
333 };
334
335 COMPONENT(Meta, meta)
336 COMPONENT(Tree, tree)
337 COMPONENT(WorldAnchor, worldAnchor)
338
339
347 [[nodiscard]] static UIHostHandle createHost(const std::string &name = "");
348
358 [[nodiscard]] static eve::OptionalRef<UIHost> resolve(UIHostHandle handle) noexcept;
359
364 [[nodiscard]] UIHostHandle handle() const noexcept { return ecs::handle_of(this); }
365
367 void setName(const std::string &name);
368 const std::string &getName();
374 void setThemeOverride(const Theme &theme);
376 void clearThemeOverride();
378 bool hasThemeOverride();
384 [[nodiscard]] const Theme *themeOverride();
386 void setOwnerId(uint32_t id) { meta()->ownerId = id; }
387 uint32_t getOwnerId() { return meta()->ownerId; }
388
390 void setTree(WidgetDesc root);
392 bool setTreeReconcile(WidgetDesc root);
393
395 void setSimplePanel(const std::string &title, const std::string &labelText,
396 const std::string &buttonText, const std::string &labelId = "label",
397 const std::string &buttonId = "btn");
398
400 void setTextById(const std::string &id, const std::string &text);
401 void setVisibleById(const std::string &id, bool visible);
402 void setEnabledById(const std::string &id, bool enabled);
403 void setCheckedById(const std::string &id, bool checked);
404 void setValueById(const std::string &id, float value);
405 void setValueTextById(const std::string &id, const std::string &value);
407 bool setClickHandler(const std::string &id, std::function<void()> fn);
408 bool setToggleHandler(const std::string &id, std::function<void(bool)> fn);
409 bool setValueHandler(const std::string &id, std::function<void(float)> fn);
410 bool setTextHandler(const std::string &id, std::function<void(const std::string &)> fn);
419 [[nodiscard]] eve::OptionalRef<UINode> findById(const std::string &id);
428 [[nodiscard]] eve::OptionalRef<UINode> findByKey(const std::string &key);
430 bool requestFocusById(const std::string &id);
436 bool moveFocus(FocusDirection direction, bool wrap = true);
438 std::string focusedId();
440 void markDirty() { tree()->dirty = true; }
441
443 void setVisible(bool v) { meta()->visible = v; }
444 void setLayer(int layer) { meta()->layer = layer; }
445 void setModal(bool modal) { meta()->modal = modal; }
446
453 void setWorldAnchor(float x, float y, float z);
455 void clearWorldAnchor();
456};
457
458} // namespace eve::ui
double value
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
int root
Definition AnimSmr.cpp:119
Vocabulary aliases for nullable and checked borrowed references.
std::string title
Object::Ptr theme
#define EVENGINE_API_WORLD
Definition Export.h:109
std::uint32_t key
float v
std::string text
std::array< float, 3 > scale
std::string name
TileLayer * layer
bool dirty
std::string id
Definition PlayHost.cpp:108
PrimitiveHandle handle
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
RoadLaneDirection direction
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:243
void markDirty()
Marks the tree for a full rebuild on the next frame.
Definition UIHost.h:440
void setLayer(int layer)
Definition UIHost.h:444
void setOwnerId(uint32_t id)
Attaches the host to an owner id (scene/UI ownership).
Definition UIHost.h:386
void setVisible(bool v)
Host visibility / layer / modality.
Definition UIHost.h:443
void setModal(bool modal)
Definition UIHost.h:445
UIHostHandle handle() const noexcept
Returns this host's generation-qualified ECS handle.
Definition UIHost.h:364
uint32_t getOwnerId()
Definition UIHost.h:387
WorldAnchorEdgePolicy
Policy used when a projected world-space anchor leaves the viewport.
Definition UIHost.h:101
FlexWrap
Whether Flex children remain on one line or wrap onto additional lines.
Definition UIHost.h:61
FlexDirection
Main-axis direction for Flex containers.
Definition UIHost.h:58
MouseFilter
Pointer event participation and propagation policy.
Definition UIHost.h:85
NodeType
Widget node kinds understood by the UI renderer.
Definition UIHost.h:19
FocusDirection
Logical focus movement understood by keyboard, gamepad and automation.
Definition UIHost.h:82
FocusMode
Keyboard/gamepad focus policy for an interactive control.
Definition UIHost.h:79
FlexJustify
Main-axis distribution of free space in a Flex container.
Definition UIHost.h:70
ThemePreset
Built-in theme override inherited by a retained UI subtree.
Definition UIHost.h:88
OverflowMode
Behavior when arranged children exceed the retained layout box.
Definition UIHost.h:64
ecs::EntityHandle UIHostHandle
Definition UIHost.h:16
WorldAnchorState
Visibility result of projecting a world-space UI anchor.
Definition UIHost.h:91
FlexAlign
Cross-axis alignment of Flex children.
Definition UIHost.h:67
WorldAnchorOverlapPolicy
Deterministic screen-space overlap policy for projected world anchors.
Definition UIHost.h:104
AccessibilityRole
Platform-neutral accessibility role exposed by controls.
Definition UIHost.h:107
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
Sparse reusable widget style resolved once while a retained tree is built.
Definition Theme.h:131
Unified UI design tokens (colors, geometry, typography). Applied to ImGui each frame via applyThemeTo...
Definition Theme.h:39
Meta public API.
Definition UIHost.h:252
std::string name
Definition UIHost.h:275
std::vector< UINode > nodes
Definition UIHost.h:286
std::vector< std::function< void(float)> > valueHandlers
Void.
Definition UIHost.h:293
std::vector< std::function< void()> > clickHandlers
Void.
Definition UIHost.h:289
std::vector< std::function< void(const std::string &)> > textHandlers
Void.
Definition UIHost.h:295
std::vector< std::function< void(bool)> > toggleHandlers
Void.
Definition UIHost.h:291
Optional 3D-world projection for this UI host.
Definition UIHost.h:307
Retained UI widget node (arena tree). Conceptual counterpart of eve::scene::SceneNode; built declarat...
Definition UIHost.h:128
float marginR
Definition UIHost.h:171
float wrapWidth
Definition UIHost.h:204
NodeType type
Definition UIHost.h:129
uint64_t textureId
Definition UIHost.h:190
std::string focusUp
Definition UIHost.h:151
float anchorY
Definition UIHost.h:185
std::string key
Definition UIHost.h:131
FlexDirection flexDirection
Definition UIHost.h:210
float paddingB
Definition UIHost.h:176
float maxValue
Definition UIHost.h:165
uint32_t handlerValue
Definition UIHost.h:232
float anchorX
Definition UIHost.h:184
float paddingT
Definition UIHost.h:174
bool dropTarget
Definition UIHost.h:159
int gridColumnSpan
Definition UIHost.h:224
MouseFilter mouseFilter
Definition UIHost.h:140
float layoutOverflowY
Definition UIHost.h:226
std::string accessibilityName
Definition UIHost.h:156
uint32_t handlerText
Definition UIHost.h:233
ThemePreset themePreset
Definition UIHost.h:141
float percentH
Definition UIHost.h:182
float minSizeX
Definition UIHost.h:177
float borderB
Definition UIHost.h:202
float marginB
Definition UIHost.h:172
float maxSizeY
Definition UIHost.h:180
float borderL
Definition UIHost.h:199
float minSizeY
Definition UIHost.h:178
uint32_t handlerClick
Definition UIHost.h:230
float flexShrink
Definition UIHost.h:221
std::string focusNext
Definition UIHost.h:147
float borderT
Definition UIHost.h:200
float opacity
Definition UIHost.h:145
std::string dragPayloadType
Definition UIHost.h:160
float marginL
Definition UIHost.h:169
AccessibilityRole accessibilityRole
Definition UIHost.h:155
FlexWrap flexWrap
Definition UIHost.h:216
std::string acceptedDropType
Definition UIHost.h:162
std::string text
Definition UIHost.h:132
float itemHeight
Definition UIHost.h:205
float minValue
Definition UIHost.h:164
std::string id
Definition UIHost.h:130
StyleClass resolvedStyle
Definition UIHost.h:143
float columnGap
Definition UIHost.h:214
float maxSizeX
Definition UIHost.h:179
float paddingR
Definition UIHost.h:175
float aspectRatio
Definition UIHost.h:183
FocusMode focusMode
Definition UIHost.h:139
std::string focusPrevious
Definition UIHost.h:148
float paddingL
Definition UIHost.h:173
std::string focusDown
Definition UIHost.h:152
std::string tooltip
Definition UIHost.h:134
std::string styleClass
Definition UIHost.h:142
float cornerRadius
Definition UIHost.h:203
std::string focusRight
Definition UIHost.h:150
uint32_t handlerToggle
Definition UIHost.h:231
std::string accessibilityDescription
Definition UIHost.h:157
std::string dragPayloadText
Definition UIHost.h:161
float layoutOverflowX
Definition UIHost.h:225
FlexJustify justifyContent
Definition UIHost.h:212
float marginT
Definition UIHost.h:170
FlexAlign alignItems
Definition UIHost.h:211
float measuredW
Definition UIHost.h:207
bool dragSource
Definition UIHost.h:158
float measuredH
Definition UIHost.h:208
bool hasResolvedStyle
Definition UIHost.h:144
float flexBasis
Definition UIHost.h:222
std::string focusLeft
Definition UIHost.h:149
std::string valueText
Definition UIHost.h:133
float borderR
Definition UIHost.h:201
float flexGrow
Definition UIHost.h:220
bool focusRequested
Definition UIHost.h:153
float percentW
Definition UIHost.h:181
OverflowMode overflow
Definition UIHost.h:217
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:15