vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
ui_element.h
1#pragma once
2
3#include <glm/glm.hpp>
4
5#include "core/reflect.h"
6
7namespace Vkm::Engine {
8
12struct UIRect {
13 glm::vec2 pos = {0.0f, 0.0f};
14 glm::vec2 size = {0.0f, 0.0f};
15
16 glm::vec2 max() const { return pos + size; }
17
18 bool contains(const glm::vec2& point) const {
19 return point.x >= pos.x && point.x < pos.x + size.x
20 && point.y >= pos.y && point.y < pos.y + size.y;
21 }
22
31 UIRect intersected(const UIRect& other) const {
32 const glm::vec2 lo = glm::max(pos, other.pos);
33 const glm::vec2 hi = glm::min(max(), other.max());
34 return UIRect{lo, glm::max(hi - lo, glm::vec2(0.0f))};
35 }
36};
37
44struct UIElement {
46 glm::vec2 anchor = {0.5f, 0.5f};
47 glm::vec2 pivot = {0.5f, 0.5f};
48 glm::vec2 position = {0.0f, 0.0f};
50 glm::vec2 size = {100.0f, 100.0f};
51
58 glm::vec2 relativeSize = {0.0f, 0.0f};
59
60 bool visible = true;
61
67 bool blocksPointer = true;
68
75 bool clipChildren = false;
76
78
87 static UIElement at(const glm::vec2& anchorPivot, const glm::vec2& position, const glm::vec2& size) {
88 UIElement element;
89 element.anchor = anchorPivot;
90 element.pivot = anchorPivot;
91 element.position = position;
92 element.size = size;
93 return element;
94 }
95};
96
97} // namespace Vkm::Engine
98
99VKM_REFLECT_BEGIN(::Vkm::Engine::UIElement)
100 VKM_F(anchor)
101 VKM_F(pivot)
102 VKM_F(position)
103 VKM_F(size)
104 VKM_F(relativeSize)
105 VKM_F(visible)
106 VKM_F(blocksPointer)
107 VKM_F(clipChildren)
108VKM_REFLECT_END()
The 2D rect of a UI node - the screen-space analogue of Transform.
Definition ui_element.h:44
bool blocksPointer
Whether this element stops a pointer reaching what is behind it.
Definition ui_element.h:67
UIRect screenRect
Resolved rect in screen pixels, written each frame.
Definition ui_element.h:77
glm::vec2 relativeSize
The share of the parent's size this element takes, added to size.
Definition ui_element.h:58
static UIElement at(const glm::vec2 &anchorPivot, const glm::vec2 &position, const glm::vec2 &size)
An element pinned to one normalised point of its parent by the matching point of itself.
Definition ui_element.h:87
glm::vec2 anchor
Parent anchor point, normalised (0,0 = top-left, 1,1 = bottom-right).
Definition ui_element.h:46
glm::vec2 size
Element size, in canvas reference pixels, added to relativeSize's share.
Definition ui_element.h:50
glm::vec2 pivot
Element pivot, normalised; the point placed at the anchor.
Definition ui_element.h:47
glm::vec2 position
Definition ui_element.h:48
bool clipChildren
Whether this element's rect bounds what its descendants may draw.
Definition ui_element.h:75
bool visible
Skip this element and its whole subtree when false.
Definition ui_element.h:60
An axis-aligned screen-space rectangle (top-left origin, pixels).
Definition ui_element.h:12
glm::vec2 pos
Top-left corner.
Definition ui_element.h:13
UIRect intersected(const UIRect &other) const
The overlap of this rect and other, empty when they miss.
Definition ui_element.h:31