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
7
ButtonWidget
&
ButtonWidget::setLabel
(
const
std::string& label)
8
{
9
_label
= label;
10
return
*
this
;
11
}
12
13
ButtonWidget
&
ButtonWidget::setPosition
(
float
x,
float
y)
14
{
15
_x
= x;
16
_y
= y;
17
_anchor
=
Anchor::None
;
18
return
*
this
;
19
}
20
21
ButtonWidget
&
ButtonWidget::setSize
(
float
width,
float
height)
22
{
23
_width
= width;
24
_height
= height;
25
return
*
this
;
26
}
27
28
ButtonWidget
&
ButtonWidget::setAnchor
(
Anchor
anchor,
float
margin)
29
{
30
_anchor
= anchor;
31
_margin
= margin;
32
return
*
this
;
33
}
34
35
ButtonWidget
&
ButtonWidget::setNormalColor
(Color color)
36
{
37
_normalColor
= color;
38
return
*
this
;
39
}
40
41
ButtonWidget
&
ButtonWidget::setHoverColor
(Color color)
42
{
43
_hoverColor
= color;
44
return
*
this
;
45
}
46
47
ButtonWidget
&
ButtonWidget::setTextColor
(Color color)
48
{
49
_textColor
= color;
50
return
*
this
;
51
}
52
53
ButtonWidget
&
ButtonWidget::setBorderColor
(Color color)
54
{
55
_borderColor
= color;
56
return
*
this
;
57
}
58
59
ButtonWidget
&
ButtonWidget::setBorderThickness
(
float
thickness)
60
{
61
_borderThickness
= thickness;
62
return
*
this
;
63
}
64
65
ButtonWidget
&
ButtonWidget::setRoundness
(
float
roundness)
66
{
67
_roundness
= roundness;
68
return
*
this
;
69
}
70
71
ButtonWidget
&
ButtonWidget::setOnClick
(std::function<
void
()> callback)
72
{
73
_onClick
= std::move(callback);
74
return
*
this
;
75
}
76
77
Rectangle
ButtonWidget::_resolvedBounds
()
const
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
;
91
case
Anchor::TopCenter
:
92
x = (sw -
_width
) / 2.0f;
93
y =
_margin
;
94
break
;
95
case
Anchor::TopRight
:
96
x = sw -
_width
-
_margin
;
97
y =
_margin
;
98
break
;
99
case
Anchor::MiddleLeft
:
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
;
107
case
Anchor::MiddleRight
:
108
x = sw -
_width
-
_margin
;
109
y = (sh -
_height
) / 2.0f;
110
break
;
111
case
Anchor::BottomLeft
:
112
x =
_margin
;
113
y = sh -
_height
-
_margin
;
114
break
;
115
case
Anchor::BottomCenter
:
116
x = (sw -
_width
) / 2.0f;
117
y = sh -
_height
-
_margin
;
118
break
;
119
case
Anchor::BottomRight
:
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
130
Rectangle
ButtonWidget::getBounds
()
const
{
return
_resolvedBounds
(); }
131
132
bool
ButtonWidget::handleInput
()
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
146
bool
ButtonWidget::wasClicked
()
147
{
148
bool
result =
_clicked
;
149
_clicked
=
false
;
150
return
result;
151
}
152
153
void
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
}
ButtonWidget.hpp
Reusable clickable button component.
TextRenderer.hpp
ButtonWidget
A configurable, stateful clickable button.
Definition
ButtonWidget.hpp:17
ButtonWidget::setNormalColor
ButtonWidget & setNormalColor(Color color)
Definition
ButtonWidget.cpp:35
ButtonWidget::_roundness
float _roundness
Definition
ButtonWidget.hpp:88
ButtonWidget::_normalColor
Color _normalColor
Definition
ButtonWidget.hpp:83
ButtonWidget::_margin
float _margin
Definition
ButtonWidget.hpp:81
ButtonWidget::handleInput
bool handleInput() override
Definition
ButtonWidget.cpp:132
ButtonWidget::setOnClick
ButtonWidget & setOnClick(std::function< void()> callback)
Registers a callback invoked when the button is clicked.
Definition
ButtonWidget.cpp:71
ButtonWidget::_anchor
Anchor _anchor
Definition
ButtonWidget.hpp:80
ButtonWidget::setHoverColor
ButtonWidget & setHoverColor(Color color)
Definition
ButtonWidget.cpp:41
ButtonWidget::_textColor
Color _textColor
Definition
ButtonWidget.hpp:85
ButtonWidget::draw
void draw(int scaledFontSize) const override
Draws the widget.
Definition
ButtonWidget.cpp:153
ButtonWidget::_borderColor
Color _borderColor
Definition
ButtonWidget.hpp:86
ButtonWidget::wasClicked
bool wasClicked()
Returns true once per click, then resets.
Definition
ButtonWidget.cpp:146
ButtonWidget::setAnchor
ButtonWidget & setAnchor(Anchor anchor, float margin=10.0f)
Sets anchor: button is auto-positioned relative to screen edges.
Definition
ButtonWidget.cpp:28
ButtonWidget::setRoundness
ButtonWidget & setRoundness(float roundness)
Definition
ButtonWidget.cpp:65
ButtonWidget::_resolvedBounds
Rectangle _resolvedBounds() const
Definition
ButtonWidget.cpp:77
ButtonWidget::setSize
ButtonWidget & setSize(float width, float height)
Definition
ButtonWidget.cpp:21
ButtonWidget::getBounds
Rectangle getBounds() const
Returns the bounding rectangle (after anchor resolution).
Definition
ButtonWidget.cpp:130
ButtonWidget::setBorderColor
ButtonWidget & setBorderColor(Color color)
Definition
ButtonWidget.cpp:53
ButtonWidget::_x
float _x
Definition
ButtonWidget.hpp:75
ButtonWidget::_borderThickness
float _borderThickness
Definition
ButtonWidget.hpp:87
ButtonWidget::_hovered
bool _hovered
Definition
ButtonWidget.hpp:90
ButtonWidget::setPosition
ButtonWidget & setPosition(float x, float y)
Sets the button position directly (requires Anchor::None).
Definition
ButtonWidget.cpp:13
ButtonWidget::Anchor
Anchor
Positioning modes for automatic layout.
Definition
ButtonWidget.hpp:20
ButtonWidget::Anchor::MiddleLeft
@ MiddleLeft
ButtonWidget::Anchor::TopRight
@ TopRight
ButtonWidget::Anchor::MiddleRight
@ MiddleRight
ButtonWidget::Anchor::Center
@ Center
ButtonWidget::Anchor::None
@ None
No anchor: position set directly via setPosition().
ButtonWidget::Anchor::BottomRight
@ BottomRight
ButtonWidget::Anchor::TopCenter
@ TopCenter
ButtonWidget::Anchor::BottomLeft
@ BottomLeft
ButtonWidget::Anchor::TopLeft
@ TopLeft
ButtonWidget::Anchor::BottomCenter
@ BottomCenter
ButtonWidget::_width
float _width
Definition
ButtonWidget.hpp:77
ButtonWidget::setBorderThickness
ButtonWidget & setBorderThickness(float thickness)
Definition
ButtonWidget.cpp:59
ButtonWidget::_hoverColor
Color _hoverColor
Definition
ButtonWidget.hpp:84
ButtonWidget::_height
float _height
Definition
ButtonWidget.hpp:78
ButtonWidget::setTextColor
ButtonWidget & setTextColor(Color color)
Definition
ButtonWidget.cpp:47
ButtonWidget::_clicked
bool _clicked
Definition
ButtonWidget.hpp:91
ButtonWidget::_y
float _y
Definition
ButtonWidget.hpp:76
ButtonWidget::_label
std::string _label
Definition
ButtonWidget.hpp:73
ButtonWidget::_onClick
std::function< void()> _onClick
Definition
ButtonWidget.hpp:93
ButtonWidget::setLabel
ButtonWidget & setLabel(const std::string &label)
Definition
ButtonWidget.cpp:7
TextRenderer::draw
static void draw(const std::string &text, int x, int y, int fontSize, Color color)
Definition
TextRenderer.cpp:52
TextRenderer::measure
static int measure(const std::string &text, int fontSize)
Definition
TextRenderer.cpp:59
gui
src
renderer
raylib_helpers
ButtonWidget.cpp
Generated by
1.9.8