TazGraph Project v0.1.0
Loading...
Searching...
No Matches
ButtonComponent.h
1#pragma once
2
3#include "../../../Components.h"
4#include "../../../UtilComponents.h"
5#include <functional> // For std::function
6
7class ButtonComponent : public Component {
8public:
9 enum class ButtonState {
10 NORMAL,
11 HOVERED,
12 PRESSED
13 };
14
15private:
16 ButtonState _state = ButtonState::NORMAL;
17 std::function<void()> _onClick; // Callback function for click action
18
19 std::string buttonLabel = "";
20 glm::vec2 bDimensions{0,0};
21 Color bBackground{255,255,255,255};
22
23 Entity* uiLabel = nullptr;
24 Entity* buttonBackground = nullptr;
25public:
27 : _state(ButtonState::NORMAL),
28 _onClick([]() {}) // Default empty function
29 {
30 }
31 ButtonComponent(std::function<void()> onClick)
32 : _state(ButtonState::NORMAL), _onClick(onClick) {}
33
34 ButtonComponent(std::function<void()> onClick, std::string button_label, glm::vec2 b_dimensions, Color b_background)
35 : _state(ButtonState::NORMAL),
36 _onClick(onClick),
37 buttonLabel(button_label),
38 bDimensions(b_dimensions),
39 bBackground(b_background){
40 // create UI label with the string given
41 // set the button background
42 }
43
44 void init() override {
45 if (buttonLabel.length() > 0) {
46 // need different shader to render text so it has to be different
47 uiLabel = &entity->getManager()->addEntity<Node>();
48
49 uiLabel->addComponent<TransformComponent>(glm::vec2(0, 0), Layer::action, glm::ivec2(32, 32), 1);
50 uiLabel->addComponent<UILabel>(uiLabel->getManager(), buttonLabel, "arial");
51
52 uiLabel->setParentEntity(entity);
53
54 uiLabel->addGroup(Manager::buttonLabels);
55 }
56 if (bDimensions != glm::vec2(0.0f, 0.0f)) {
57 buttonBackground = &entity->getManager()->addEntity<Node>();
58
59 buttonBackground->addComponent<TransformComponent>(glm::vec2(0, 0), Layer::action, bDimensions, 1);
60 buttonBackground->addComponent<Rectangle_w_Color>();
61
62 buttonBackground->GetComponent<Rectangle_w_Color>().color = bBackground; // Grey color
63 buttonBackground->setParentEntity(entity);
64
65 buttonBackground->addGroup(Manager::panelBackground);
66 }
67 }
68
69 void setOnClick(std::function<void()> newOnClick) {
70 _onClick = newOnClick;
71 }
72
73 void setState(ButtonState state) {
74 _state = state;
75 }
76
77 void update(float deltaTime) override {
78 // Update the button state based on user input
79 // Change the texture or appearance based on the state
80 // ...
81
82 if (_state == ButtonState::PRESSED && _onClick) {
83 _onClick(); // Call the click action callback
84 }
85 setState(ButtonState::NORMAL); // Reset the state
86 }
87
88 // Other methods for setting textures, handling mouse events, etc.
89
90 std::string GetComponentName() override {
91 return "ButtonComponent";
92 }
93};
Definition ButtonComponent.h:7
Definition GECS.h:123
Definition GECS.h:140
T & addComponent(TArgs &&... mArgs)
have addScript function
Definition GECS.h:239
Definition GECSEntityTypes.h:56
Definition Rectangle_w_Color.h:6
Definition TransformComponent.h:6
Definition UILabel.h:10
Definition Vertex.h:47