ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
CoreTypes.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include "Common.hpp"
12#include "SmallVector.hpp"
13
14#include <algorithm>
15#include <atomic>
16
17namespace ZenitUI
18{
22
24 struct Vec2
25 {
26 float x{0.0f}, y{0.0f};
27 };
28 inline Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
29 inline Vec2 operator-(Vec2 a, Vec2 b) { return {a.x - b.x, a.y - b.y}; }
30 inline Vec2 operator*(Vec2 a, float k) { return {a.x * k, a.y * k}; }
31
33 struct Vec3
34 {
35 float x{0.0f}, y{0.0f}, z{0.0f};
36 };
37
39 struct Vec4
40 {
41 float x{0.0f}, y{0.0f}, z{0.0f}, w{1.0f};
42 };
43
45 struct Rect
46 {
47 float x{0.0f}, y{0.0f}, width{0.0f}, height{0.0f};
48
50 bool contains(Vec2 p) const
51 {
52 return p.x >= x && p.x <= x + width && p.y >= y && p.y <= y + height;
53 }
54
55 Vec2 center() const { return {x + width * 0.5f, y + height * 0.5f}; }
57 Vec2 size() const { return {width, height}; }
58 };
59
62 {
63 float top{0.0f}, right{0.0f}, bottom{0.0f}, left{0.0f};
64
65 bool empty() const
66 {
67 return top == 0.0f && right == 0.0f && bottom == 0.0f && left == 0.0f;
68 }
69 bool operator==(const EdgeInsets &o) const
70 {
71 return top == o.top && right == o.right && bottom == o.bottom && left == o.left;
72 }
73 bool operator!=(const EdgeInsets &o) const { return !(*this == o); }
74 };
75
77 struct Color
78 {
79 uint8_t r{255}, g{255}, b{255}, a{255};
80
85 Color withAlpha(float k) const
86 {
87 float f = std::clamp(k, 0.0f, 1.0f);
88 return {r, g, b, static_cast<uint8_t>(std::lround(a * f))};
89 }
90 bool operator==(const Color &o) const { return r == o.r && g == o.g && b == o.b && a == o.a; }
91 bool operator!=(const Color &o) const { return !(*this == o); }
92 };
93
95 namespace Colors
96 {
97 inline constexpr Color White{255, 255, 255, 255};
98 inline constexpr Color Black{0, 0, 0, 255};
99 inline constexpr Color Blank{0, 0, 0, 0};
100 inline constexpr Color Red{230, 41, 55, 255};
101 inline constexpr Color Maroon{190, 33, 55, 255};
102 inline constexpr Color Green{0, 228, 48, 255};
103 inline constexpr Color DarkGreen{0, 117, 44, 255};
104 inline constexpr Color Blue{0, 121, 241, 255};
105 inline constexpr Color Yellow{253, 249, 0, 255};
106 inline constexpr Color Gray{130, 130, 130, 255};
107 inline constexpr Color DarkGray{80, 80, 80, 255};
108 inline constexpr Color LightGray{200, 200, 200, 255};
109 }
110
117 struct Vertex2D
118 {
119 Vec2 pos{};
120 Vec2 uv{0.5f, 0.5f};
121 Color color{255, 255, 255, 255};
122 };
123 static_assert(sizeof(Vertex2D) == 20, "Vertex2D must stay tightly packed (20 bytes)");
124
127 {
128 uint32_t id{0};
129 int width{0}, height{0};
130 bool valid() const { return id != 0; }
131 };
132
135 {
136 uint32_t id{0};
137 bool valid() const noexcept { return id != 0; }
138 };
139
142 {
143 uint32_t id{0};
144 bool valid() const { return id != 0; }
145 };
146
155 {
156 uint32_t id{0};
157 int width{0}, height{0};
158 bool valid() const { return id != 0; }
159 };
160
163 {
164 int left{0}, top{0}, right{0}, bottom{0};
165 };
166
178 {
179 Vec2 pivot;
180 Vec2 translate;
181 float rotationDeg{0.0f};
182 float scale{1.0f};
183 };
184
193 class FontStack
194 {
195 public:
196 using Vec = std::vector<std::string>;
197 using value_type = std::string;
198 using iterator = Vec::iterator;
199 using const_iterator = Vec::const_iterator;
200
201 private:
202 struct Data
203 {
204 mutable std::atomic<uint32_t> refCount{1};
205 Vec vec;
206 };
207
208 Data *data_{nullptr};
209
210 static const Vec &emptyVec() noexcept
211 {
212 static const Vec kEmpty{};
213 return kEmpty;
214 }
215
216 static Vec &emptyMutVec() noexcept
217 {
218 static Vec kEmpty{};
219 return kEmpty;
220 }
221
222 static void release(Data *p) noexcept
223 {
224 if (p && p->refCount.fetch_sub(1u, std::memory_order_acq_rel) == 1u)
225 delete p;
226 }
227
228 Vec &detach()
229 {
230 if (!data_)
231 {
232 data_ = new Data();
233 return data_->vec;
234 }
235 if (data_->refCount.load(std::memory_order_acquire) > 1u)
236 {
237 auto *copy = new Data();
238 copy->vec = data_->vec;
239 release(data_);
240 data_ = copy;
241 }
242 return data_->vec;
243 }
244
245 public:
246 FontStack() noexcept = default;
247 FontStack(const char *s)
248 {
249 if (s && *s)
250 {
251 data_ = new Data();
252 data_->vec.emplace_back(s);
253 }
254 }
255 FontStack(std::string s)
256 {
257 if (!s.empty())
258 {
259 data_ = new Data();
260 data_->vec.push_back(std::move(s));
261 }
262 }
263 FontStack(std::string_view s)
264 {
265 if (!s.empty())
266 {
267 data_ = new Data();
268 data_->vec.emplace_back(s);
269 }
270 }
271 FontStack(std::initializer_list<std::string> init)
272 {
273 if (init.size() > 0)
274 {
275 data_ = new Data();
276 data_->vec.assign(init.begin(), init.end());
277 }
278 }
279 FontStack(const FontStack &o) noexcept : data_(o.data_)
280 {
281 if (data_)
282 data_->refCount.fetch_add(1u, std::memory_order_relaxed);
283 }
284 FontStack(FontStack &&o) noexcept : data_(o.data_)
285 {
286 o.data_ = nullptr;
287 }
288 FontStack &operator=(const FontStack &o) noexcept
289 {
290 if (this == &o)
291 return *this;
292 Data *old = data_;
293 data_ = o.data_;
294 if (data_)
295 data_->refCount.fetch_add(1u, std::memory_order_relaxed);
296 release(old);
297 return *this;
298 }
299 FontStack &operator=(FontStack &&o) noexcept
300 {
301 if (this == &o)
302 return *this;
303 release(data_);
304 data_ = o.data_;
305 o.data_ = nullptr;
306 return *this;
307 }
308 ~FontStack() noexcept { release(data_); }
309
310 bool empty() const noexcept { return !data_ || data_->vec.empty(); }
311 size_t size() const noexcept { return data_ ? data_->vec.size() : 0u; }
312 bool sharesStorageWith(const FontStack &o) const noexcept { return data_ == o.data_; }
313
314 const Vec &asVector() const noexcept { return data_ ? data_->vec : emptyVec(); }
315
316 const_iterator begin() const noexcept { return asVector().begin(); }
317 const_iterator end() const noexcept { return asVector().end(); }
318 const_iterator cbegin() const noexcept { return asVector().cbegin(); }
319 const_iterator cend() const noexcept { return asVector().cend(); }
320 iterator begin() { return empty() ? emptyMutVec().begin() : detach().begin(); }
321 iterator end() { return empty() ? emptyMutVec().end() : detach().end(); }
322
323 const std::string &operator[](size_t i) const { return asVector()[i]; }
324 std::string &operator[](size_t i) { return detach()[i]; }
325 const std::string &front() const { return asVector().front(); }
326 std::string &front() { return detach().front(); }
327 const std::string &back() const { return asVector().back(); }
328 std::string &back() { return detach().back(); }
329
330 void push_back(const std::string &s) { detach().push_back(s); }
331 void push_back(std::string &&s) { detach().push_back(std::move(s)); }
332 template <typename... Args>
333 std::string &emplace_back(Args &&...args) { return detach().emplace_back(std::forward<Args>(args)...); }
334
335 void reserve(size_t n)
336 {
337 if (n > 0)
338 detach().reserve(n);
339 }
340
341 void clear() noexcept
342 {
343 release(data_);
344 data_ = nullptr;
345 }
346
347 bool operator==(const FontStack &o) const
348 {
349 if (data_ == o.data_)
350 return true;
351 if (empty() && o.empty())
352 return true;
353 return asVector() == o.asVector();
354 }
355 bool operator!=(const FontStack &o) const { return !(*this == o); }
356 };
357 static_assert(sizeof(FontStack) == sizeof(void *), "FontStack must remain pointer-sized");
358
360 enum class MouseButton : uint8_t
361 {
362 Left,
363 Right,
364 Middle,
365 Count
366 };
367
370 {
371 bool down{false};
372 bool pressed{false};
373 bool released{false};
374 };
375
378 {
379 Vec2 pos;
380 float wheelY{0.0f};
381
382 std::array<ButtonState, (size_t)MouseButton::Count> buttons{};
383
384 ButtonState &operator[](MouseButton b) { return buttons[(size_t)b]; }
385 const ButtonState &operator[](MouseButton b) const { return buttons[(size_t)b]; }
386 };
387
390 {
391 bool shift{false};
392 bool ctrl{false};
393 bool alt{false};
394 bool super{false};
395 };
396
398 /*struct Metrics
399 {
400 static inline Vec2 viewport{1280.0f, 720.0f};
401 };*/
402
410 namespace Key
411 {
412 constexpr int Backspace = 1;
413 constexpr int Delete = 2;
414 constexpr int Enter = 3;
415 constexpr int Escape = 4;
416 constexpr int Left = 5;
417 constexpr int Right = 6;
418 constexpr int Home = 7;
419 constexpr int End = 8;
420 constexpr int Tab = 9;
421 constexpr int Space = 10;
422
423 constexpr int Up = 11;
424 constexpr int Down = 12;
425
426 constexpr int A = 65;
427 constexpr int C = 67;
428 constexpr int V = 86;
429 constexpr int X = 88;
430
431 constexpr int Y = 89;
432 constexpr int Z = 90;
433 }
434
437 {
438 std::vector<int> chars;
439 std::vector<int> keys;
440 std::vector<int> held;
441
444 std::string compositionText;
448 };
449
451 enum class Align: uint8_t
452 {
453 Auto,
458 };
459
461 enum class Justify: uint8_t
462 {
467 };
468
476 {
477 float dx = p.x - t.pivot.x;
478 float dy = p.y - t.pivot.y;
479 dx *= t.scale;
480 dy *= t.scale;
481
482 float rad = t.rotationDeg * 3.14159265358979323846f / 180.0f;
483 float c = std::cos(rad), s = std::sin(rad);
484 float rx = dx * c - dy * s;
485 float ry = dx * s + dy * c;
486
487 return {rx + t.pivot.x + t.translate.x, ry + t.pivot.y + t.translate.y};
488 }
489
499 {
500 p.x -= t.translate.x;
501 p.y -= t.translate.y;
502 float dx = p.x - t.pivot.x, dy = p.y - t.pivot.y;
503 float rad = -t.rotationDeg * 3.14159265358979323846f / 180.0f;
504 float c = std::cos(rad), s = std::sin(rad);
505 float rx = dx * c - dy * s;
506 float ry = dx * s + dy * c;
507 rx /= t.scale;
508 ry /= t.scale;
509 return {rx + t.pivot.x, ry + t.pivot.y};
510 }
511
513}
Centralized standard library includes used throughout ZenitUI.
Fixed-capacity inline-storage vector with heap fallback.
Ordered list of font family names to try, first match wins.
Definition CoreTypes.hpp:194
Justify
Main-axis distribution for flex containers.
Definition CoreTypes.hpp:462
Align
Cross-axis alignment for flex items and content.
Definition CoreTypes.hpp:452
MouseButton
Mouse buttons tracked by the input system.
Definition CoreTypes.hpp:361
Vec2 applyInverseTransform(const Transform2D &t, Vec2 p)
Invert a Transform2D and apply it to a point.
Definition CoreTypes.hpp:498
Vec2 applyTransform(const Transform2D &t, Vec2 p)
Apply a Transform2D to a point.
Definition CoreTypes.hpp:475
@ SpaceBetween
Distribute leftover space between items.
Definition CoreTypes.hpp:466
@ Center
Center within the available space.
Definition CoreTypes.hpp:455
@ End
Pack at the trailing edge.
Definition CoreTypes.hpp:456
@ Start
Pack at the leading edge.
Definition CoreTypes.hpp:454
@ Stretch
Stretch to fill the cross axis.
Definition CoreTypes.hpp:457
Named color constants.
Definition CoreTypes.hpp:96
Global viewport metrics, updated once per frame from the platform.
Definition CoreTypes.hpp:411
Per-button state snapshot for the current frame.
Definition CoreTypes.hpp:370
bool down
Button is currently held.
Definition CoreTypes.hpp:371
bool released
Button transitioned from down to up this frame.
Definition CoreTypes.hpp:373
bool pressed
Button transitioned from up to down this frame.
Definition CoreTypes.hpp:372
8-bit-per-channel RGBA color, non-premultiplied.
Definition CoreTypes.hpp:78
Color withAlpha(float k) const
Return a copy with the alpha channel scaled by k.
Definition CoreTypes.hpp:85
Four-sided edge inset (used for padding, margin, borders).
Definition CoreTypes.hpp:62
Opaque handle to a shader/effect owned by the asset provider.
Definition CoreTypes.hpp:142
Opaque handle to a font loaded by TextEngine.
Definition CoreTypes.hpp:135
One frame's worth of keyboard input.
Definition CoreTypes.hpp:437
int compositionSelectionLen
Highlighted clause length in codepoints inside compositionText.
Definition CoreTypes.hpp:446
std::string compositionText
UTF-8 pre-edit text (empty when cancelled/ended).
Definition CoreTypes.hpp:444
bool hasCompositionUpdate
IME pre-edit (composition) state update for this frame (ยง6.3).
Definition CoreTypes.hpp:443
bool compositionCommitted
true when active composition was committed into chars.
Definition CoreTypes.hpp:447
int compositionCursor
Caret offset in codepoints inside compositionText (or -1).
Definition CoreTypes.hpp:445
Keyboard modifier snapshot.
Definition CoreTypes.hpp:390
Nine-slice inset values for scalable textures.
Definition CoreTypes.hpp:163
Pointer position, wheel delta, and per-button state for one frame.
Definition CoreTypes.hpp:378
Axis-aligned rectangle in screen coordinates.
Definition CoreTypes.hpp:46
Vec2 center() const
Center point of the rectangle.
Definition CoreTypes.hpp:55
Vec2 size() const
Same data as a Vec2.
Definition CoreTypes.hpp:57
bool contains(Vec2 p) const
Inclusive point-in-rect test.
Definition CoreTypes.hpp:50
Opaque handle to an off-screen render target.
Definition CoreTypes.hpp:155
Opaque handle to a GPU/software texture owned by the asset provider.
Definition CoreTypes.hpp:127
2D affine transform stored as pivot + translate + rotation + scale.
Definition CoreTypes.hpp:178
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25
3D vector.
Definition CoreTypes.hpp:34
4D vector with an implicit w = 1 default.
Definition CoreTypes.hpp:40
Interleaved 2D vertex passed to IRenderer::drawGeometry.
Definition CoreTypes.hpp:118