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
label.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 "component.hpp"
26#include "primitives/text.hpp"
27#include <string>
28
36class Label : public Component {
37private:
38 std::string _text;
39
40public:
46 Label(const std::string &text) : Component(), _text(text) {
47 // Store text in state
48 _state.set<std::string>("text", text);
49 }
50
57 Label(const std::string &text, const Properties &properties)
58 : Component(properties), _text(text) {
59 // Store text in state
60 _state.set<std::string>("text", text);
61 }
62
66 ~Label(void) override = default;
67
73 const std::string &getText(void) const { return _text; }
74
80 void setText(const std::string &text) {
81 _text = text;
82 _state.set<std::string>("text", text);
83 }
84
90 std::shared_ptr<Component> render(void) override {
91 // Update text from state if it has changed
92 if (_state.getData().find("text") != _state.getData().end()) {
93 _text = _state.get<std::string>("text");
94 }
95
96 // Clear existing primitives and regenerate
97 _primitives.clear();
98
99 // Create a text primitive for the label content
100 auto textPrimitive = std::make_shared<Text>(_text, Point(0, 0, 0));
101 _primitives.push_back(textPrimitive);
102
103 return shared_from_this();
104 }
105
113 void onEvent(const Event &event) override {
114 // Labels typically don't handle events
115 (void)event;
116 }
117};
Base class for all UI components.
Definition component.hpp:38
std::vector< std::shared_ptr< Primitive > > _primitives
Vector of primitives for rendering.
Definition component.hpp:56
State _state
The component's state.
Definition component.hpp:50
Represents a UI event with type, target, and optional data.
Definition event.hpp:38
Simple display component for text.
Definition label.hpp:36
Label(const std::string &text, const Properties &properties)
Constructs a Label object with properties.
Definition label.hpp:57
~Label(void) override=default
Destroys the Label object.
void setText(const std::string &text)
Sets the label's text.
Definition label.hpp:80
std::shared_ptr< Component > render(void) override
Renders the label.
Definition label.hpp:90
Label(const std::string &text)
Constructs a Label object.
Definition label.hpp:46
void onEvent(const Event &event) override
Handles events for the label.
Definition label.hpp:113
const std::string & getText(void) const
Gets the label's text.
Definition label.hpp:73
Represents a point in 3D space.
Definition point.hpp:35
Manages immutable component properties.
const std::map< std::string, std::any > & getData(void) const
Get the entire state data map.
Definition state.hpp:99
void set(const std::string &key, const T &value)
Set a state value by key.
Definition state.hpp:80
T get(const std::string &key) const
Get a state value by key with type casting.
Definition state.hpp:66