vkmEngine 1.0.15
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
69 float opacity = 1.0f;
70
76 bool blocksPointer = true;
77
84 bool clipChildren = false;
85
87
96 static UIElement at(const glm::vec2& anchorPivot, const glm::vec2& position, const glm::vec2& size) {
97 UIElement element;
98 element.anchor = anchorPivot;
99 element.pivot = anchorPivot;
100 element.position = position;
101 element.size = size;
102 return element;
103 }
104};
105
106} // namespace Vkm::Engine
107
108VKM_REFLECT_BEGIN(::Vkm::Engine::UIElement)
109 VKM_F(anchor)
110 VKM_F(pivot)
111 VKM_F(position)
112 VKM_F(size)
113 VKM_F(relativeSize)
114 VKM_F(visible)
115 VKM_F(opacity)
116 VKM_F(blocksPointer)
117 VKM_F(clipChildren)
118VKM_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:76
UIRect screenRect
Resolved rect in screen pixels, written each frame.
Definition ui_element.h:86
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:96
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:84
bool visible
Skip this element and its whole subtree when false.
Definition ui_element.h:60
float opacity
How opaque this element and its whole subtree draw, 0 to 1: what a panel fades in and out by,...
Definition ui_element.h:69
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