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
button.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/rectangle.hpp"
27#include "primitives/text.hpp"
28#include <functional>
29#include <string>
30
38class Button : public Component {
39private:
40 std::string _label;
41 std::function<void()> _onClick;
42
43public:
49 Button(const std::string &label)
50 : Component(), _label(label), _onClick(nullptr) {
51 // Store label in state
52 _state.set<std::string>("label", label);
53 }
54
61 Button(const std::string &label, const Properties &properties)
62 : Component(properties), _label(label), _onClick(nullptr) {
63 // Store label in state
64 _state.set<std::string>("label", label);
65 }
66
70 ~Button(void) override = default;
71
78 void setOnClick(std::function<void()> callback) { _onClick = callback; }
79
85 const std::string &getLabel(void) const { return _label; }
86
92 void setLabel(const std::string &label) {
93 _label = label;
94 _state.set<std::string>("label", label);
95 }
96
102 std::shared_ptr<Component> render(void) override {
103 // Update label from state if it has changed
104 if (_state.getData().find("label") != _state.getData().end()) {
105 _label = _state.get<std::string>("label");
106 }
107
108 // Clear existing primitives and regenerate
109 _primitives.clear();
110
111 // Create a rectangle primitive for the button background (centered at
112 // (50,15,0), width 100, height 30, no rotation)
113 auto rectPrimitive = std::make_shared<Rectangle>(Point(50.0f, 15.0f, 0.0f), 100.0f, 30.0f, Point(0.0f, 0.0f, 0.0f));
114 _primitives.push_back(rectPrimitive);
115
116 // Create a text primitive for the button label
117 auto textPrimitive = std::make_shared<Text>(_label, Point(10, 15, 0));
118 _primitives.push_back(textPrimitive);
119
120 return shared_from_this();
121 }
122
130 void onEvent(const Event &event) override {
131 if (event.getType() == "click" && _onClick) {
132 _onClick();
133 }
134 }
135};
Interactive button component.
Definition button.hpp:38
Button(const std::string &label)
Constructs a Button object.
Definition button.hpp:49
void setLabel(const std::string &label)
Sets the button's label.
Definition button.hpp:92
void onEvent(const Event &event) override
Handles events for the button.
Definition button.hpp:130
Button(const std::string &label, const Properties &properties)
Constructs a Button object with properties.
Definition button.hpp:61
void setOnClick(std::function< void()> callback)
Sets the onClick callback function.
Definition button.hpp:78
~Button(void) override=default
Destroys the Button object.
std::shared_ptr< Component > render(void) override
Renders the button.
Definition button.hpp:102
const std::string & getLabel(void) const
Gets the button's label.
Definition button.hpp:85
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
const std::string & getType(void) const
Get the event type.
Definition event.hpp:61
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