Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
ButtonWidget.cpp
Go to the documentation of this file.
1
2
3#include "ButtonWidget.hpp"
4
5#include "TextRenderer.hpp"
6
7ButtonWidget& ButtonWidget::setLabel(const std::string& label)
8{
9 _label = label;
10 return *this;
11}
12
14{
15 _x = x;
16 _y = y;
18 return *this;
19}
20
21ButtonWidget& ButtonWidget::setSize(float width, float height)
22{
23 _width = width;
24 _height = height;
25 return *this;
26}
27
29{
30 _anchor = anchor;
31 _margin = margin;
32 return *this;
33}
34
36{
37 _normalColor = color;
38 return *this;
39}
40
42{
43 _hoverColor = color;
44 return *this;
45}
46
48{
49 _textColor = color;
50 return *this;
51}
52
54{
55 _borderColor = color;
56 return *this;
57}
58
60{
61 _borderThickness = thickness;
62 return *this;
63}
64
66{
67 _roundness = roundness;
68 return *this;
69}
70
71ButtonWidget& ButtonWidget::setOnClick(std::function<void()> callback)
72{
73 _onClick = std::move(callback);
74 return *this;
75}
76
78{
79 if (_anchor == Anchor::None) return {_x, _y, _width, _height};
80
81 float sw = static_cast<float>(GetScreenWidth());
82 float sh = static_cast<float>(GetScreenHeight());
83 float x = 0.0f;
84 float y = 0.0f;
85
86 switch (_anchor) {
87 case Anchor::TopLeft:
88 x = _margin;
89 y = _margin;
90 break;
92 x = (sw - _width) / 2.0f;
93 y = _margin;
94 break;
96 x = sw - _width - _margin;
97 y = _margin;
98 break;
100 x = _margin;
101 y = (sh - _height) / 2.0f;
102 break;
103 case Anchor::Center:
104 x = (sw - _width) / 2.0f;
105 y = (sh - _height) / 2.0f;
106 break;
108 x = sw - _width - _margin;
109 y = (sh - _height) / 2.0f;
110 break;
112 x = _margin;
113 y = sh - _height - _margin;
114 break;
116 x = (sw - _width) / 2.0f;
117 y = sh - _height - _margin;
118 break;
120 x = sw - _width - _margin;
121 y = sh - _height - _margin;
122 break;
123 default:
124 break;
125 }
126
127 return {x, y, _width, _height};
128}
129
130Rectangle ButtonWidget::getBounds() const { return _resolvedBounds(); }
131
133{
134 Rectangle bounds = _resolvedBounds();
135 Vector2 mouse = GetMousePosition();
136 _hovered = CheckCollisionPointRec(mouse, bounds);
137
138 if (_hovered && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
139 _clicked = true;
140 if (_onClick) _onClick();
141 }
142
143 return _hovered;
144}
145
147{
148 bool result = _clicked;
149 _clicked = false;
150 return result;
151}
152
153void ButtonWidget::draw(int scaledFontSize) const
154{
155 Rectangle bounds = _resolvedBounds();
156
157 DrawRectangleRounded(bounds, _roundness, 8, _hovered ? _hoverColor : _normalColor);
158 if (_borderThickness > 0.0f)
159 DrawRectangleRoundedLines(bounds, _roundness, 8, _borderThickness, _borderColor);
160
161 if (!_label.empty()) {
162 int tw = TextRenderer::measure(_label, scaledFontSize);
163 int tx = static_cast<int>(bounds.x + (bounds.width - tw) / 2.0f);
164 int ty = static_cast<int>(bounds.y + (bounds.height - scaledFontSize) / 2.0f);
165 TextRenderer::draw(_label, tx, ty, scaledFontSize, _textColor);
166 }
167}
Reusable clickable button component.
A configurable, stateful clickable button.
ButtonWidget & setNormalColor(Color color)
bool handleInput() override
ButtonWidget & setOnClick(std::function< void()> callback)
Registers a callback invoked when the button is clicked.
ButtonWidget & setHoverColor(Color color)
void draw(int scaledFontSize) const override
Draws the widget.
bool wasClicked()
Returns true once per click, then resets.
ButtonWidget & setAnchor(Anchor anchor, float margin=10.0f)
Sets anchor: button is auto-positioned relative to screen edges.
ButtonWidget & setRoundness(float roundness)
Rectangle _resolvedBounds() const
ButtonWidget & setSize(float width, float height)
Rectangle getBounds() const
Returns the bounding rectangle (after anchor resolution).
ButtonWidget & setBorderColor(Color color)
float _borderThickness
ButtonWidget & setPosition(float x, float y)
Sets the button position directly (requires Anchor::None).
Anchor
Positioning modes for automatic layout.
@ None
No anchor: position set directly via setPosition().
ButtonWidget & setBorderThickness(float thickness)
ButtonWidget & setTextColor(Color color)
std::string _label
std::function< void()> _onClick
ButtonWidget & setLabel(const std::string &label)
static void draw(const std::string &text, int x, int y, int fontSize, Color color)
static int measure(const std::string &text, int fontSize)