Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
ButtonWidget.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#include <functional>
7#include <string>
8
9#include "IWidget.hpp"
10#include "raylib.h"
11
17class ButtonWidget : public IWidget {
18 public:
20 enum class Anchor {
21 TopLeft,
25 Center,
31 None
32 };
33
34 ButtonWidget() = default;
35 ~ButtonWidget() override = default;
36
37 // ── Content ────────────────────────────────────────────────────────────
38 ButtonWidget& setLabel(const std::string& label);
39
40 // ── Geometry ───────────────────────────────────────────────────────────
42 ButtonWidget& setPosition(float x, float y);
43 ButtonWidget& setSize(float width, float height);
46 ButtonWidget& setAnchor(Anchor anchor, float margin = 10.0f);
47
48 // ── Appearance ─────────────────────────────────────────────────────────
49 ButtonWidget& setNormalColor(Color color);
50 ButtonWidget& setHoverColor(Color color);
51 ButtonWidget& setTextColor(Color color);
52 ButtonWidget& setBorderColor(Color color);
53 ButtonWidget& setBorderThickness(float thickness);
54 ButtonWidget& setRoundness(float roundness);
55
56 // ── Behaviour ──────────────────────────────────────────────────────────
58 ButtonWidget& setOnClick(std::function<void()> callback);
59
60 // ── IWidget ────────────────────────────────────────────────────────────
61 void draw(int scaledFontSize) const override;
63 bool handleInput() override;
64
65 // ── State polling ──────────────────────────────────────────────────────
67 bool wasClicked();
68
70 Rectangle getBounds() const;
71
72 private:
73 std::string _label;
74
75 float _x = 0.0f;
76 float _y = 0.0f;
77 float _width = 120.0f;
78 float _height = 36.0f;
79
81 float _margin = 10.0f;
82
83 Color _normalColor = {60, 70, 90, 220};
84 Color _hoverColor = {100, 120, 160, 240};
85 Color _textColor = {210, 220, 240, 255};
86 Color _borderColor = {60, 70, 90, 200};
87 float _borderThickness = 2.0f;
88 float _roundness = 0.3f;
89
90 bool _hovered = false;
91 bool _clicked = false;
92
93 std::function<void()> _onClick;
94
95 Rectangle _resolvedBounds() const;
96};
Interface for GUI widgets.
A configurable, stateful clickable button.
ButtonWidget & setNormalColor(Color color)
bool handleInput() override
ButtonWidget()=default
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
~ButtonWidget() override=default
std::function< void()> _onClick
ButtonWidget & setLabel(const std::string &label)
Interface for GUI widgets.
Definition IWidget.hpp:11