guillaume 1.0.0
Guillaume is a component-based UI framework library built around a primitive rendering system. It's designed for building user interfaces across XR platforms and traditional platforms with custom rendering backends and input handling. The framework is lightweight, flexible, and easy to integrate into existing projects.
Loading...
Searching...
No Matches
component.hpp
1/*
2 Copyright (c) 2025 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#pragma once
24
25#include "event.hpp"
26#include "primitive.hpp"
27#include "properties.hpp"
28#include "state.hpp"
29#include <memory>
30#include <vector>
31
38class Component : public std::enable_shared_from_this<Component> {
39private:
40 typedef unsigned int ComponentID;
41
42 ComponentID _id;
43
44 ComponentID generateID(void) const {
45 static ComponentID currentID = 0;
46 return ++currentID;
47 }
48
49protected:
52 std::shared_ptr<Component> _parent;
53 std::vector<std::shared_ptr<Component>>
55 std::vector<std::shared_ptr<Primitive>>
57
63 void setParent(const std::shared_ptr<Component> &parent) { _parent = parent; }
64
65public:
69 Component(void) : _id(generateID()), _state(), _properties() {
70 // Note: State change callback is not set here to avoid bad_weak_ptr
71 // when Component is created on the stack. Use setStateChangeCallback()
72 // if needed when Component is managed by shared_ptr.
73 }
74
80 Component(const Properties &properties)
81 : _id(generateID()), _state(), _properties(properties) {
82 // Note: State change callback is not set here to avoid bad_weak_ptr
83 // when Component is created on the stack. Use setStateChangeCallback()
84 // if needed when Component is managed by shared_ptr.
85 }
86
91 virtual ~Component(void) = default;
92
98 ComponentID getID(void) const { return _id; }
99
105 virtual void addChild(const std::shared_ptr<Component> &child) {
106 child->setParent(shared_from_this());
107 _children.push_back(child);
108 }
109
115 virtual void addPrimitive(const std::shared_ptr<Primitive> &primitive) {
116 _primitives.push_back(primitive);
117 }
118
127 virtual std::shared_ptr<Component> render(void) {
128 // Default implementation returns itself
129 return shared_from_this();
130 }
131
140 virtual void onEvent(const Event &event) {
141 // Default implementation does nothing
142 (void)event;
143 }
144
150 State &getState(void) { return _state; }
151
157 const Properties &getProperties(void) const { return _properties; }
158
165 const std::vector<std::shared_ptr<Component>> &getChildren(void) const {
166 return _children;
167 }
168
174 std::shared_ptr<Component> getParent(void) const { return _parent; }
175
182 const std::vector<std::shared_ptr<Primitive>> &getPrimitives(void) const {
183 return _primitives;
184 }
185};
Base class for all UI components.
Definition component.hpp:38
Properties _properties
The component's properties.
Definition component.hpp:51
std::vector< std::shared_ptr< Primitive > > _primitives
Vector of primitives for rendering.
Definition component.hpp:56
std::shared_ptr< Component > _parent
Pointer to the parent component.
Definition component.hpp:52
virtual void addChild(const std::shared_ptr< Component > &child)
Add a child component.
State _state
The component's state.
Definition component.hpp:50
ComponentID getID(void) const
Get the ID object.
Definition component.hpp:98
virtual void addPrimitive(const std::shared_ptr< Primitive > &primitive)
Add a primitive for rendering.
virtual void onEvent(const Event &event)
Handles events.
void setParent(const std::shared_ptr< Component > &parent)
Set the Parent object.
Definition component.hpp:63
const std::vector< std::shared_ptr< Primitive > > & getPrimitives(void) const
Gets the component's primitives.
std::shared_ptr< Component > getParent(void) const
Gets the component's parent.
const Properties & getProperties(void) const
Gets the component's properties.
virtual ~Component(void)=default
Destroy the Component object.
State & getState(void)
Gets the component's state.
Component(const Properties &properties)
Constructs a Component object with properties.
Definition component.hpp:80
const std::vector< std::shared_ptr< Component > > & getChildren(void) const
Gets the component's children.
virtual std::shared_ptr< Component > render(void)
Renders the component.
Component(void)
Constructs a Component object.
Definition component.hpp:69
std::vector< std::shared_ptr< Component > > _children
Vector of child components.
Definition component.hpp:54
Represents a UI event with type, target, and optional data.
Definition event.hpp:38
Manages immutable component properties.
Manages dynamic component state with type-safe storage.
Definition state.hpp:37