载入中...
搜索中...
未找到
UIHost.h
浏览该文件的文档.
1#pragma once
2
3#include "common/ECS.h"
4
5#include <cstdint>
6#include <functional>
7#include <string>
8#include <vector>
9
10namespace eve::ui {
11
13enum class NodeType : uint8_t {
14 Window = 0,
15 Text = 1,
16 Button = 2,
17 SameLine = 3,
18 Group = 4,
19 Separator = 5,
20 Checkbox = 6,
21 Slider = 7,
22 Progress = 8,
23 InputText = 9,
25 Child = 11,
26 Flex = 12, // elastic row/column container
27 Spacer = 13, // flexible empty space (default flexGrow=1)
28 Image = 14, // colored rect or engine texture (nine-patch capable)
29 ImageButton = 15, // clickable image
30 Combo = 16, // dropdown; options newline-separated in valueText, index in value
31 ScrollList = 17, // virtualized scrollable list (uniform itemHeight)
32 Viewport = 18, // embedded render target: offscreen Canvas shown + input routed
33};
34
36enum class FlexDirection : uint8_t { Row = 0, Column = 1 };
37
39enum class FlexAlign : uint8_t { Start = 0, Center = 1, End = 2, Stretch = 3 };
40
42enum class FlexJustify : uint8_t {
43 Start = 0,
44 Center = 1,
45 End = 2,
46 SpaceBetween = 3,
47 SpaceAround = 4,
48};
49
54struct UINode {
56 std::string id;
57 std::string key;
58 std::string text; // label / title
59 std::string valueText; // InputText content
60 bool visible = true;
61 bool checked = false;
62 bool open = true; // CollapsingHeader default-open hint
63 float value = 0.f;
64 float minValue = 0.f;
65 float maxValue = 1.f;
66 float sizeX = 0.f; // Child / item basis width; 0 = auto
67 float sizeY = 0.f; // Child / item basis height; 0 = auto
68 // --- Layout box model (input props; 0 = unset) ---
69 float marginL = 0.f;
70 float marginT = 0.f;
71 float marginR = 0.f;
72 float marginB = 0.f;
73 float paddingL = 0.f;
74 float paddingT = 0.f;
75 float paddingR = 0.f;
76 float paddingB = 0.f;
77 float minSizeX = 0.f; // 0 = auto
78 float minSizeY = 0.f;
79 float maxSizeX = 0.f; // 0 = unlimited
80 float maxSizeY = 0.f;
81 float percentW = 0.f; // 0..1 fraction of parent content width; overrides basis
82 float percentH = 0.f; // 0..1 fraction of parent content height
83 float anchorX = 0.f; // absolute placement inside Flex: anchor point in parent (0..1)
84 float anchorY = 0.f;
85 float posX = 0.f; // absolute placement offset (relative to anchor edge)
86 float posY = 0.f;
87 bool absolute = false; // skip flex flow; placed by anchor/pos
88 // --- Image / ImageButton props (NodeType::Image*) ---
89 uint64_t textureId = 0; // opaque id from UIBackend::registerTexture; 0 = solid color
90 float tintR = 1.f;
91 float tintG = 1.f;
92 float tintB = 1.f;
93 float tintA = 1.f;
94 float uv0x = 0.f; // source rect in texture (UV)
95 float uv0y = 0.f;
96 float uv1x = 1.f;
97 float uv1y = 1.f;
98 float borderL = 0.f; // nine-patch border (px); all 0 = plain image
99 float borderT = 0.f;
100 float borderR = 0.f;
101 float borderB = 0.f;
102 float cornerRadius = 0.f; // solid-color rounded rect radius
103 float wrapWidth = 0.f; // Text wrap width (0 = no wrap)
104 float itemHeight = 0.f; // ScrollList uniform row height (0 = frame height)
105 // --- Computed by measure/arrange pass (per frame) ---
106 float measuredW = 0.f;
107 float measuredH = 0.f;
108 // Flex container props (meaningful on NodeType::Flex)
112 float gap = -1.f; // <0 → theme ItemSpacing on that axis
113 // Flex item props (any child inside Flex)
114 float flexGrow = 0.f;
115 int firstChild = -1;
116 int nextSibling = -1;
117 uint32_t handlerClick = 0; // 1-based → Tree::clickHandlers
118 uint32_t handlerToggle = 0; // 1-based → Tree::toggleHandlers
119 uint32_t handlerValue = 0; // 1-based → Tree::valueHandlers
120 uint32_t handlerText = 0; // 1-based → Tree::textHandlers
121};
122
123struct WidgetDesc;
124
130class UIHost : public ecs::Entity {
131public:
132 ENTITY(UIHost, ecs::Entity)
133
134 void release() override {}
135
136 struct Meta {
137 bool visible = true;
138 int layer = 0;
139 bool modal = false;
140 bool overlay = false; // no title bar / chrome (HUD-style)
141 bool hasPos = false;
142 float posX = 0.f;
143 float posY = 0.f;
144 float pivotX = 0.f;
145 float pivotY = 0.f;
146 bool hasSize = false; // explicit window size
147 float sizeX = 0.f;
148 float sizeY = 0.f;
149 float percentW = 0.f; // 0..1 of display width; overrides sizeX
150 float percentH = 0.f; // 0..1 of display height
151 float anchorX = 0.f; // anchor in display (0..1); with hasPos, posX is offset
152 float anchorY = 0.f;
153 std::string name;
154 uint32_t ownerId = 0;
155 UIHost *entity = nullptr;
156 };
157
158 struct Tree {
159 bool dirty = true;
160 std::vector<UINode> nodes;
161 int root = -1;
162 std::vector<std::function<void()>> clickHandlers;
163 std::vector<std::function<void(bool)>> toggleHandlers;
164 std::vector<std::function<void(float)>> valueHandlers;
165 std::vector<std::function<void(const std::string &)>> textHandlers;
166 };
167
168 COMPONENT(Meta, meta)
169 COMPONENT(Tree, tree)
170
171 static UIHost *createHost(const std::string &name = "");
172
174 void setName(const std::string &name);
175 const std::string &getName();
177 void setOwnerId(uint32_t id) { meta()->ownerId = id; }
178 uint32_t getOwnerId() { return meta()->ownerId; }
179
181 void setTree(WidgetDesc root);
183 bool setTreeReconcile(WidgetDesc root);
184
186 void setSimplePanel(const std::string &title, const std::string &labelText,
187 const std::string &buttonText, const std::string &labelId = "label",
188 const std::string &buttonId = "btn");
189
191 void setTextById(const std::string &id, const std::string &text);
192 void setVisibleById(const std::string &id, bool visible);
193 void setCheckedById(const std::string &id, bool checked);
194 void setValueById(const std::string &id, float value);
195 void setValueTextById(const std::string &id, const std::string &value);
197 bool setClickHandler(const std::string &id, std::function<void()> fn);
198 bool setToggleHandler(const std::string &id, std::function<void(bool)> fn);
199 bool setValueHandler(const std::string &id, std::function<void(float)> fn);
200 bool setTextHandler(const std::string &id, std::function<void(const std::string &)> fn);
202 UINode *findById(const std::string &id);
203 UINode *findByKey(const std::string &key);
205 void markDirty() { tree()->dirty = true; }
206
208 void setVisible(bool v) { meta()->visible = v; }
209 void setLayer(int layer) { meta()->layer = layer; }
210 void setModal(bool modal) { meta()->modal = modal; }
211};
212
213} // namespace eve::ui
std::string value
std::string title
std::string id
TileLayer * layer
const char * name
Definition RockMesh.cpp:21
SettlementPipeline::Stage fn
int v
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
void markDirty()
Marks the tree for a full rebuild on the next frame.
Definition UIHost.h:205
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 setLayer(int layer)
Definition UIHost.h:209
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
void setOwnerId(uint32_t id)
Attaches the host to an owner id (scene/UI ownership).
Definition UIHost.h:177
const std::string & getName()
Definition UIHost.cpp:24
void release() override
Definition UIHost.h:134
void setVisible(bool v)
Host visibility / layer / modality.
Definition UIHost.h:208
bool setTextHandler(const std::string &id, std::function< void(const std::string &)> fn)
Definition UIHost.cpp:127
void setModal(bool modal)
Definition UIHost.h:210
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
uint32_t getOwnerId()
Definition UIHost.h:178
bool setValueHandler(const std::string &id, std::function< void(float)> fn)
Definition UIHost.cpp:111
WidgetDesc text(std::string content, std::string id)
Static text label.
Definition Widget.cpp:235
FlexDirection
Main-axis direction for Flex containers.
Definition UIHost.h:36
NodeType
Widget node kinds understood by the UI renderer.
Definition UIHost.h:13
FlexJustify
Main-axis distribution of free space in a Flex container.
Definition UIHost.h:42
FlexAlign
Cross-axis alignment of Flex children.
Definition UIHost.h:39
std::string name
Definition UIHost.h:153
std::vector< UINode > nodes
Definition UIHost.h:160
std::vector< std::function< void(float)> > valueHandlers
Definition UIHost.h:164
std::vector< std::function< void()> > clickHandlers
Definition UIHost.h:162
std::vector< std::function< void(const std::string &)> > textHandlers
Definition UIHost.h:165
std::vector< std::function< void(bool)> > toggleHandlers
Definition UIHost.h:163
Retained UI widget node (arena tree). Conceptual counterpart of eve::scene::SceneNode; built declarat...
Definition UIHost.h:54
float marginR
Definition UIHost.h:71
float wrapWidth
Definition UIHost.h:103
NodeType type
Definition UIHost.h:55
float value
Definition UIHost.h:63
uint64_t textureId
Definition UIHost.h:89
float anchorY
Definition UIHost.h:84
std::string key
Definition UIHost.h:57
FlexDirection flexDirection
Definition UIHost.h:109
float paddingB
Definition UIHost.h:76
float maxValue
Definition UIHost.h:65
uint32_t handlerValue
Definition UIHost.h:119
float anchorX
Definition UIHost.h:83
float paddingT
Definition UIHost.h:74
float sizeY
Definition UIHost.h:67
uint32_t handlerText
Definition UIHost.h:120
float percentH
Definition UIHost.h:82
float minSizeX
Definition UIHost.h:77
float borderB
Definition UIHost.h:101
float marginB
Definition UIHost.h:72
float tintA
Definition UIHost.h:93
float maxSizeY
Definition UIHost.h:80
float borderL
Definition UIHost.h:98
float minSizeY
Definition UIHost.h:78
uint32_t handlerClick
Definition UIHost.h:117
float borderT
Definition UIHost.h:99
float marginL
Definition UIHost.h:69
float tintG
Definition UIHost.h:91
bool absolute
Definition UIHost.h:87
std::string text
Definition UIHost.h:58
float itemHeight
Definition UIHost.h:104
float minValue
Definition UIHost.h:64
float tintR
Definition UIHost.h:90
std::string id
Definition UIHost.h:56
float maxSizeX
Definition UIHost.h:79
float paddingR
Definition UIHost.h:75
float paddingL
Definition UIHost.h:73
float cornerRadius
Definition UIHost.h:102
uint32_t handlerToggle
Definition UIHost.h:118
FlexJustify justifyContent
Definition UIHost.h:111
float marginT
Definition UIHost.h:70
FlexAlign alignItems
Definition UIHost.h:110
float measuredW
Definition UIHost.h:106
float tintB
Definition UIHost.h:92
float measuredH
Definition UIHost.h:107
std::string valueText
Definition UIHost.h:59
float sizeX
Definition UIHost.h:66
float borderR
Definition UIHost.h:100
float flexGrow
Definition UIHost.h:114
float percentW
Definition UIHost.h:81
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:13