vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
ui_system.h
1#pragma once
2
3#include <string_view>
4#include <vector>
5
6#include <glm/glm.hpp>
7
8#include "core/system.h"
9#include "ecs/entity.h"
10#include "ecs/component/ui/ui_element.h"
11#include "ecs/component/ui/ui_shape.h"
12#include "system/ui/ui_draw_data.h"
13
14namespace Vkm::Engine {
15
24class UISystem : public System {
25 public:
26 UISystem() = default;
27 ~UISystem() override = default;
28
29 UISystem(const UISystem& other) = delete;
30 UISystem& operator=(const UISystem& other) = delete;
31
32 UISystem(UISystem && other) = delete;
33 UISystem& operator=(UISystem && other) = delete;
34
35 public:
36 void update(FrameContext& ctx) override;
37
38 private:
42 struct CanvasRef {
43 int32_t sortOrder;
44 EntityId entity;
45 };
46
50 struct ButtonHit {
51 EntityId entity;
52 uint32_t firstVertex;
53 float opacity = 1.0f;
54 };
55
59 struct ScrollTarget {
60 EntityId entity;
61 size_t blockersBefore;
62 };
63
64 private:
66 void layOutCanvases(FrameContext& ctx, const UIRect& viewport);
67
83 glm::vec2 resolveElement(
84 FrameContext& ctx,
85 EntityId entity,
86 const UIRect& parentRect,
87 const UIRect& clip,
88 float scale,
89 uint32_t depth
90 );
91
104 void appendCommand(
105 uint32_t first,
106 uint32_t count,
107 FontHandle font,
108 TextureHandle image,
109 const UIRect& clip
110 );
111
122 bool emitImage(
123 FrameContext& ctx,
124 EntityId entity,
125 const UIElement& element,
126 const UIRect& clip,
127 float canvasScale
128 );
129
140 bool emitButton(
141 FrameContext& ctx,
142 EntityId entity,
143 const UIElement& element,
144 const UIRect& clip,
145 float canvasScale
146 );
147
159 glm::vec2 emitText(
160 FrameContext& ctx,
161 EntityId entity,
162 const UIElement& element,
163 const UIRect& clip,
164 float canvasScale
165 );
166
179 void breakLines(std::string_view text, const FontAsset& font, float scale, float maxWidth, bool wrap);
180
192 bool resolveInteraction(FrameContext& ctx);
193
208 void appendQuad(
209 const glm::vec2& p0,
210 const glm::vec2& p1,
211 const glm::vec2& uv0,
212 const glm::vec2& uv1,
213 const glm::vec4& color,
214 const glm::vec4& bottom,
215 const glm::vec2& shape,
216 const glm::vec4& border,
217 bool image
218 );
219
229 void recolourQuad(uint32_t first, const glm::vec4& top, const glm::vec4& bottom);
230
240 void appendShaped(
241 const UIRect& rect,
242 const glm::vec4& color,
243 const UIShape& shape,
244 float scale,
245 bool image
246 );
247
248 private:
249 UIDrawData m_drawData;
250
251 std::vector<CanvasRef> m_canvases;
253 std::vector<ButtonHit> m_buttonHits;
254
260 std::vector<ScrollTarget> m_scrollTargets;
261
263 std::vector<std::string_view> m_lines;
264
266 std::vector<EntityId> m_pointerBlockers;
267 float m_opacity = 1.0f;
268
269 EntityId m_pressedButton{};
270 glm::vec2 m_pointer{0.0f};
271 float m_wheel = 0.0f;
272 bool m_mouseDown = false;
273 bool m_mouseDownEdge = false;
274 bool m_mouseUpEdge = false;
275 bool m_cursorFree = false;
276 bool m_pointerIsOurs = false;
277};
278
279} // namespace Vkm::Engine
void update(FrameContext &ctx) override
Execute this system for the current frame.
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
Per-frame state bundle passed to every system.
Definition system.h:57