载入中...
搜索中...
未找到
Widget.h
浏览该文件的文档.
1#pragma once
2
3#include "ui/UIHost.h"
4
5#include <functional>
6#include <string>
7#include <utility>
8#include <vector>
9
10namespace eve::ui {
11
13struct WidgetDesc {
15 std::string id;
17 std::string key;
18 std::string text;
19 std::string valueText;
20 bool visible = true;
21 bool checked = false;
22 bool open = true;
23 float value = 0.f;
24 float minValue = 0.f;
25 float maxValue = 1.f;
26 float sizeX = 0.f;
27 float sizeY = 0.f;
28 // --- Layout box model (mirrors UINode) ---
29 float marginL = 0.f;
30 float marginT = 0.f;
31 float marginR = 0.f;
32 float marginB = 0.f;
33 float paddingL = 0.f;
34 float paddingT = 0.f;
35 float paddingR = 0.f;
36 float paddingB = 0.f;
37 float minSizeX = 0.f;
38 float minSizeY = 0.f;
39 float maxSizeX = 0.f;
40 float maxSizeY = 0.f;
41 float percentW = 0.f;
42 float percentH = 0.f;
43 float anchorX = 0.f;
44 float anchorY = 0.f;
45 float posX = 0.f;
46 float posY = 0.f;
47 bool absolute = false;
48 // --- Image / ImageButton props ---
49 uint64_t textureId = 0;
50 float tintR = 1.f;
51 float tintG = 1.f;
52 float tintB = 1.f;
53 float tintA = 1.f;
54 float uv0x = 0.f;
55 float uv0y = 0.f;
56 float uv1x = 1.f;
57 float uv1y = 1.f;
58 float borderL = 0.f;
59 float borderT = 0.f;
60 float borderR = 0.f;
61 float borderB = 0.f;
62 float cornerRadius = 0.f;
63 float wrapWidth = 0.f;
64 float itemHeight = 0.f; // ScrollList uniform row height
68 float gap = -1.f;
69 float flexGrow = 0.f;
70 std::function<void()> onClick;
71 std::function<void(bool)> onToggle;
72 std::function<void(float)> onValue;
73 std::function<void(const std::string &)> onTextChange;
74 std::vector<WidgetDesc> children;
75
76 WidgetDesc &withId(std::string v) {
77 id = std::move(v);
78 return *this;
79 }
80 WidgetDesc &withKey(std::string v) {
81 key = std::move(v);
82 return *this;
83 }
84 WidgetDesc &withText(std::string v) {
85 text = std::move(v);
86 return *this;
87 }
88 WidgetDesc &withValueText(std::string v) {
89 valueText = std::move(v);
90 return *this;
91 }
93 visible = v;
94 return *this;
95 }
97 checked = v;
98 return *this;
99 }
101 open = v;
102 return *this;
103 }
105 value = v;
106 return *this;
107 }
108 WidgetDesc &withRange(float lo, float hi) {
109 minValue = lo;
110 maxValue = hi;
111 return *this;
112 }
113 WidgetDesc &withSize(float w, float h) {
114 sizeX = w;
115 sizeY = h;
116 return *this;
117 }
118 WidgetDesc &withMargin(float l, float t, float r, float b) {
119 marginL = l;
120 marginT = t;
121 marginR = r;
122 marginB = b;
123 return *this;
124 }
125 WidgetDesc &withPadding(float l, float t, float r, float b) {
126 paddingL = l;
127 paddingT = t;
128 paddingR = r;
129 paddingB = b;
130 return *this;
131 }
132 WidgetDesc &withMinSize(float w, float h) {
133 minSizeX = w;
134 minSizeY = h;
135 return *this;
136 }
137 WidgetDesc &withMaxSize(float w, float h) {
138 maxSizeX = w;
139 maxSizeY = h;
140 return *this;
141 }
142 WidgetDesc &withPercent(float w, float h) {
143 percentW = w;
144 percentH = h;
145 return *this;
146 }
148 WidgetDesc &withAbsolute(float ax, float ay, float x = 0.f, float y = 0.f) {
149 absolute = true;
150 anchorX = ax;
151 anchorY = ay;
152 posX = x;
153 posY = y;
154 return *this;
155 }
156 WidgetDesc &withTint(float r, float g, float b, float a = 1.f) {
157 tintR = r;
158 tintG = g;
159 tintB = b;
160 tintA = a;
161 return *this;
162 }
163 WidgetDesc &withUv(float u0, float v0, float u1, float v1) {
164 uv0x = u0;
165 uv0y = v0;
166 uv1x = u1;
167 uv1y = v1;
168 return *this;
169 }
171 WidgetDesc &withNinePatch(float l, float t, float r, float b) {
172 borderL = l;
173 borderT = t;
174 borderR = r;
175 borderB = b;
176 return *this;
177 }
179 cornerRadius = radius;
180 return *this;
181 }
184 return *this;
185 }
188 return *this;
189 }
190 WidgetDesc &withGap(float g) {
191 gap = g;
192 return *this;
193 }
196 return *this;
197 }
199 alignItems = a;
200 return *this;
201 }
203 justifyContent = j;
204 return *this;
205 }
207 flexGrow = g;
208 return *this;
209 }
210 WidgetDesc &withClick(std::function<void()> fn) {
211 onClick = std::move(fn);
212 return *this;
213 }
214 WidgetDesc &withToggle(std::function<void(bool)> fn) {
215 onToggle = std::move(fn);
216 return *this;
217 }
218 WidgetDesc &withValueFn(std::function<void(float)> fn) {
219 onValue = std::move(fn);
220 return *this;
221 }
222 WidgetDesc &withTextChange(std::function<void(const std::string &)> fn) {
223 onTextChange = std::move(fn);
224 return *this;
225 }
227 children.push_back(std::move(c));
228 return *this;
229 }
230
231 const std::string &reconcileKey() const { return key.empty() ? id : key; }
232};
233
235WidgetDesc window(std::string title, std::vector<WidgetDesc> children = {}, std::string id = "root");
237WidgetDesc text(std::string content, std::string id = "");
239WidgetDesc button(std::string label, std::string id = "", std::function<void()> onClick = {});
241WidgetDesc group(std::vector<WidgetDesc> children = {}, std::string id = "");
243WidgetDesc sameLine(std::string id = "");
245WidgetDesc separator(std::string id = "");
247WidgetDesc checkbox(std::string label, bool checked = false, std::string id = "",
248 std::function<void(bool)> onToggle = {});
250WidgetDesc slider(std::string label, float value, float minV, float maxV, std::string id = "",
251 std::function<void(float)> onValue = {});
253WidgetDesc progress(float fraction, std::string id = "", std::string overlay = "");
255WidgetDesc combo(std::string label, std::vector<std::string> options, int selected,
256 std::string id = "", std::function<void(int)> onValue = {});
258WidgetDesc image(std::string id = "", float width = 0.f, float height = 0.f,
259 std::function<void()> onClick = {});
261WidgetDesc imageButton(std::string id, float width, float height, std::function<void()> onClick = {});
263WidgetDesc viewport(std::string id = "", float width = 0.f, float height = 0.f);
265WidgetDesc inputText(std::string label, std::string value, std::string id = "",
266 std::function<void(const std::string &)> onChange = {});
268WidgetDesc collapsingHeader(std::string label, std::vector<WidgetDesc> children = {},
269 std::string id = "", bool defaultOpen = true);
271WidgetDesc child(std::string id, std::vector<WidgetDesc> children = {}, float width = 0.f,
272 float height = 120.f);
278WidgetDesc scrollList(std::string id, std::vector<WidgetDesc> children = {}, float height = 0.f,
279 float itemHeight = 0.f);
281WidgetDesc virtualList(std::string listId, const std::vector<std::string> &items,
282 float height = 200.f, float itemHeight = 0.f);
283
285WidgetDesc flex(FlexDirection direction, std::vector<WidgetDesc> children = {},
286 std::string id = "");
288WidgetDesc row(std::vector<WidgetDesc> children = {}, std::string id = "");
290WidgetDesc column(std::vector<WidgetDesc> children = {}, std::string id = "");
292WidgetDesc spacer(std::string id = "", float grow = 1.f);
293
295WidgetDesc when(bool cond, WidgetDesc child);
296WidgetDesc whenElse(bool cond, WidgetDesc ifTrue, WidgetDesc ifFalse);
297
302WidgetDesc list(std::string listId, const std::vector<std::string> &items,
303 const std::function<WidgetDesc(const std::string &, int)> &itemFn);
304
306WidgetDesc listButtons(std::string listId, const std::vector<std::string> &items);
307
309void applyTree(UIHost *host, WidgetDesc root);
310
315bool applyTreeReconcile(UIHost *host, WidgetDesc root);
316
317} // namespace eve::ui
std::string value
std::string title
int y
Definition Grass.cpp:135
float height
Definition Grass.cpp:235
int x
Definition Grass.cpp:135
int h
int w
const FusedGroup & group
uint32_t a
uint32_t b
uint32_t c
int width
SettlementPipeline::Stage fn
int d
int v
std::string image
int children
Definition TreeMesh.cpp:177
WidgetDesc spacer(std::string id, float grow)
Flexible empty space; default flexGrow=1 so it absorbs free space in a Flex parent.
Definition Widget.cpp:439
WidgetDesc slider(std::string label, float value, float minV, float maxV, std::string id, std::function< void(float)> onValue)
Horizontal slider; fires onValue.
Definition Widget.cpp:291
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 scrollList(std::string id, std::vector< WidgetDesc > children, float height, float itemHeight)
Definition Widget.cpp:398
WidgetDesc progress(float fraction, std::string id, std::string overlay)
Progress bar; fraction is clamped to [0,1].
Definition Widget.cpp:305
FlexDirection
Main-axis direction for Flex containers.
Definition UIHost.h:36
WidgetDesc checkbox(std::string label, bool checked, std::string id, std::function< void(bool)> onToggle)
Checkbox with a label; fires onToggle.
Definition Widget.cpp:279
WidgetDesc separator(std::string id)
Horizontal separator line.
Definition Widget.cpp:271
WidgetDesc whenElse(bool cond, WidgetDesc ifTrue, WidgetDesc ifFalse)
Definition Widget.cpp:453
WidgetDesc when(bool cond, WidgetDesc child)
Conditional: include child only when cond is true (empty group otherwise).
Definition Widget.cpp:448
NodeType
Widget node kinds understood by the UI renderer.
Definition UIHost.h:13
WidgetDesc combo(std::string label, std::vector< std::string > options, int selected, std::string id, std::function< void(int)> onValue)
Definition Widget.cpp:315
WidgetDesc inputText(std::string label, std::string value, std::string id, std::function< void(const std::string &)> onChange)
Editable text field; fires onTextChange.
Definition Widget.cpp:363
WidgetDesc listButtons(std::string listId, const std::vector< std::string > &items)
Default list: one Button per item, id = listId + "/" + index.
Definition Widget.cpp:470
FlexJustify
Main-axis distribution of free space in a Flex container.
Definition UIHost.h:42
WidgetDesc flex(FlexDirection direction, std::vector< WidgetDesc > children, std::string id)
Elastic layout container (row/column). Prefer row / column shorthands.
Definition Widget.cpp:421
WidgetDesc imageButton(std::string id, float width, float height, std::function< void()> onClick)
Definition Widget.cpp:342
WidgetDesc virtualList(std::string listId, const std::vector< std::string > &items, float height, float itemHeight)
Definition Widget.cpp:410
WidgetDesc collapsingHeader(std::string label, std::vector< WidgetDesc > children, std::string id, bool defaultOpen)
Collapsible header containing child widgets.
Definition Widget.cpp:375
bool applyTreeReconcile(UIHost *host, WidgetDesc root)
Patch by key/id when structure (type + child keys) matches; otherwise full replace....
Definition Widget.cpp:490
WidgetDesc sameLine(std::string id)
Holds the next widget on the same line as the previous one.
Definition Widget.cpp:263
WidgetDesc button(std::string label, std::string id, std::function< void()> onClick)
Clickable button; fires onClick.
Definition Widget.cpp:244
WidgetDesc column(std::vector< WidgetDesc > children, std::string id)
Vertical elastic layout column.
Definition Widget.cpp:435
FlexAlign
Cross-axis alignment of Flex children.
Definition UIHost.h:39
WidgetDesc viewport(std::string id, float width, float height)
Definition Widget.cpp:353
WidgetDesc child(std::string id, std::vector< WidgetDesc > children, float width, float height)
Scrollable child region with an explicit size.
Definition Widget.cpp:387
WidgetDesc list(std::string listId, const std::vector< std::string > &items, const std::function< WidgetDesc(const std::string &, int)> &itemFn)
Expand a string list into a Group of item widgets. itemFn(label, index) builds each row; keys default...
Definition Widget.cpp:457
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:431
Declarative widget description (build once / on dirty → flatten into UIHost::Tree).
Definition Widget.h:13
std::vector< WidgetDesc > children
Definition Widget.h:74
uint64_t textureId
Definition Widget.h:49
WidgetDesc & withNinePatch(float l, float t, float r, float b)
Definition Widget.h:171
NodeType type
Definition Widget.h:14
WidgetDesc & withFlexGrow(float g)
Definition Widget.h:206
std::string key
Reconciliation key; defaults to id when empty.
Definition Widget.h:17
WidgetDesc & withVisible(bool v)
Definition Widget.h:92
std::string valueText
Definition Widget.h:19
WidgetDesc & withItemHeight(float height)
Definition Widget.h:186
WidgetDesc & withText(std::string v)
Definition Widget.h:84
WidgetDesc & withOpen(bool v)
Definition Widget.h:100
WidgetDesc & withPercent(float w, float h)
Definition Widget.h:142
WidgetDesc & withAbsolute(float ax, float ay, float x=0.f, float y=0.f)
Definition Widget.h:148
WidgetDesc & withJustify(FlexJustify j)
Definition Widget.h:202
WidgetDesc & withChecked(bool v)
Definition Widget.h:96
WidgetDesc & withTextChange(std::function< void(const std::string &)> fn)
Definition Widget.h:222
WidgetDesc & withRange(float lo, float hi)
Definition Widget.h:108
WidgetDesc & withCornerRadius(float radius)
Definition Widget.h:178
WidgetDesc & withPadding(float l, float t, float r, float b)
Definition Widget.h:125
WidgetDesc & withMaxSize(float w, float h)
Definition Widget.h:137
WidgetDesc & withTint(float r, float g, float b, float a=1.f)
Definition Widget.h:156
WidgetDesc & child(WidgetDesc c)
Definition Widget.h:226
WidgetDesc & withMargin(float l, float t, float r, float b)
Definition Widget.h:118
WidgetDesc & withMinSize(float w, float h)
Definition Widget.h:132
std::function< void()> onClick
Definition Widget.h:70
WidgetDesc & withSize(float w, float h)
Definition Widget.h:113
std::string text
Definition Widget.h:18
WidgetDesc & withFlexDirection(FlexDirection d)
Definition Widget.h:194
std::function< void(const std::string &)> onTextChange
Definition Widget.h:73
WidgetDesc & withAlign(FlexAlign a)
Definition Widget.h:198
std::function< void(bool)> onToggle
Definition Widget.h:71
WidgetDesc & withUv(float u0, float v0, float u1, float v1)
Definition Widget.h:163
WidgetDesc & withKey(std::string v)
Definition Widget.h:80
WidgetDesc & withToggle(std::function< void(bool)> fn)
Definition Widget.h:214
WidgetDesc & withGap(float g)
Definition Widget.h:190
WidgetDesc & withValueText(std::string v)
Definition Widget.h:88
WidgetDesc & withValue(float v)
Definition Widget.h:104
WidgetDesc & withClick(std::function< void()> fn)
Definition Widget.h:210
FlexDirection flexDirection
Definition Widget.h:65
FlexJustify justifyContent
Definition Widget.h:67
WidgetDesc & withWrap(float width)
Definition Widget.h:182
WidgetDesc & withId(std::string v)
Definition Widget.h:76
std::string id
Definition Widget.h:15
WidgetDesc & withValueFn(std::function< void(float)> fn)
Definition Widget.h:218
FlexAlign alignItems
Definition Widget.h:66
const std::string & reconcileKey() const
Definition Widget.h:231
std::function< void(float)> onValue
Definition Widget.h:72