R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
RaylibButton.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** r-type
4** File description:
5** RaylibButton - Raylib implementation of IButton
6*/
7
8#include <math.h>
9
11#include "UI/TextUtils.hpp"
12
13namespace UI {
14 RaylibButton::RaylibButton(Graphics::IGraphics &graphics) : _graphics(graphics) {}
15
17 if (!_enabled) {
19 return;
20 }
21
22 bool mouseOver = IsMouseOver();
23 bool mouseDown = _graphics.IsMouseButtonDown(0); // Left mouse button
24
25 if (mouseOver) {
26 if (mouseDown) {
28 } else {
30
31 // Detect click (mouse was down and now released over button)
32 if (_wasMouseDown && !mouseDown && _callback) {
33 _callback();
34 }
35 }
36 } else {
38 }
39
40 _wasMouseDown = mouseDown && mouseOver;
41 }
42
44 if (!_enabled) {
45 // Render disabled state (could use a different color)
46 constexpr unsigned int disabledColor = 0xFF606060; // Darker gray
47 _graphics.DrawRectFilled(static_cast<int>(_x), static_cast<int>(_y), static_cast<int>(_width),
48 static_cast<int>(_height), disabledColor);
49 return;
50 }
51
52 // Choose color based on state (focused or hovered takes priority)
53 unsigned int currentColor = _backgroundColor;
54 if (_focused) {
55 currentColor = _hoverColor; // Use hover color when focused
57 currentColor = _hoverColor;
58 }
59
60 // Draw filled rectangle for button background
61 _graphics.DrawRectFilled(static_cast<int>(_x), static_cast<int>(_y), static_cast<int>(_width),
62 static_cast<int>(_height), currentColor);
63
64 // Draw border - blue if focused, black otherwise
65 unsigned int borderColor = _focused ? _focusColor : 0xFF000000;
66 _graphics.DrawRect(static_cast<int>(_x), static_cast<int>(_y), static_cast<int>(_width),
67 static_cast<int>(_height), borderColor);
68
69 // Draw thicker border when focused (draw inner border)
70 if (_focused) {
71 _graphics.DrawRect(static_cast<int>(_x) + 1, static_cast<int>(_y) + 1,
72 static_cast<int>(_width) - 2, static_cast<int>(_height) - 2, borderColor);
73 }
74
75 // ===== Centered text rendering =====
76 if (!_text.empty()) {
77 const int textWidth = TextUtils::EstimateTextWidth(_text, _textSize);
78 const int textHeight = _graphics.GetFontHeight(_fontHandle, _textSize);
79
80 const int textX = static_cast<int>(_x + ((_width - static_cast<float>(textWidth)) / 2.0f));
81 const int textY = static_cast<int>(_y + ((_height - static_cast<float>(textHeight)) / 2.0f));
82
83 _graphics.DrawText(_fontHandle, _text.c_str(), textX, textY, _textSize, _textColor);
84 }
85 }
86
87 void RaylibButton::SetCallback(std::function<void()> callback) {
88 _callback = std::move(callback);
89 }
90
91 void RaylibButton::SetPosition(float x, float y) {
92 _x = x;
93 _y = y;
94 }
95
96 void RaylibButton::GetPosition(float &x, float &y) const {
97 x = _x;
98 y = _y;
99 }
100
101 void RaylibButton::SetSize(float width, float height) {
102 _width = width;
103 _height = height;
105 }
106
107 void RaylibButton::GetSize(float &width, float &height) const {
108 width = _width;
109 height = _height;
110 }
111
113 _align = align;
115 }
116
118 return _align;
119 }
120
122 if (_align == Align::NONE) {
123 return;
124 }
125
126 const auto winW = static_cast<float>(_graphics.GetWindowWidth());
127 const auto winH = static_cast<float>(_graphics.GetWindowHeight());
128
130 _x = (winW - _width) / 2.0F;
131 }
133 _y = (winH - _height) / 2.0F;
134 }
135 }
136
137 void RaylibButton::SetBackgroundColor(unsigned int color) {
138 _backgroundColor = color;
139 }
140
141 void RaylibButton::SetHoverColor(unsigned int color) {
142 _hoverColor = color;
143 }
144
146 return _state;
147 }
148
150 return _enabled;
151 }
152
153 void RaylibButton::SetEnabled(bool enabled) {
154 _enabled = enabled;
155 }
156
158 float mouseX = 0;
159 float mouseY = 0;
160 _graphics.GetMousePosition(mouseX, mouseY);
161
162 return mouseX >= _x && mouseX <= _x + _width && mouseY >= _y && mouseY <= _y + _height;
163 }
164
165 void RaylibButton::SetText(const std::string &text) {
166 _text = text;
167 }
168
169 const std::string &RaylibButton::GetText() const {
170 return _text;
171 }
172
174 _textSize = size;
175 }
176
178 return _textSize;
179 }
180
181 void RaylibButton::SetTextColor(unsigned int color) {
182 _textColor = color;
183 }
184
185 unsigned int RaylibButton::GetTextColor() const {
186 return _textColor;
187 }
188
189 void RaylibButton::SetFont(int fontHandle) {
190 _fontHandle = fontHandle;
191 }
192
194 return _fontHandle;
195 }
196
197 void RaylibButton::SetFocused(bool focused) {
198 _focused = focused;
199 }
200
202 return _focused;
203 }
204
206 if (_enabled && _callback) {
207 _callback();
208 }
209 }
210} // namespace UI
Abstract interface for graphics rendering operations.
Definition IGraphics.hpp:32
virtual bool IsMouseButtonDown(int button) const =0
Check if a mouse button is currently being held down.
virtual int GetWindowHeight() const =0
Get current window height in pixels.
virtual void DrawRectFilled(int x, int y, int width, int height, unsigned int color)=0
Draw a filled rectangle.
virtual void DrawText(int fontHandle, const char *text, int x, int y, int fontSize, unsigned int color)=0
Draw text using a loaded font.
virtual void GetMousePosition(float &x, float &y) const =0
Get the current mouse cursor position.
virtual int GetWindowWidth() const =0
Get current window width in pixels.
virtual void DrawRect(int x, int y, int width, int height, unsigned int color)=0
Draw a rectangle outline.
virtual int GetFontHeight(int fontHandle, int fontSize)=0
Get the height of a font at a given size.
RaylibButton(Graphics::IGraphics &graphics)
Construct a new RaylibButton.
void SetHoverColor(unsigned int color) override
Set button hover color.
unsigned int GetTextColor() const override
Get label color.
void TriggerClick() override
Programmatically trigger the button's click callback.
void SetPosition(float x, float y) override
Set the top-left position of the button.
Graphics::IGraphics & _graphics
unsigned int _hoverColor
ButtonState GetState() const override
Get current button state.
void SetAlign(Align align) override
Set alignment mode relative to the current window.
void SetFocused(bool focused) override
Set the keyboard focus state of the button.
std::function< void()> _callback
void ApplyAlignment() override
Apply alignment (recomputes position based on window size).
unsigned int _textColor
void SetFont(int fontHandle) override
Set font handle to use for button text.
unsigned int _focusColor
bool IsFocused() const override
Check if this button currently has keyboard focus.
int GetFont() const override
Get current font handle used by this button.
const std::string & GetText() const override
Get button label text.
bool IsEnabled() const override
Check if button is enabled.
void SetSize(float width, float height) override
Set the button size.
bool IsMouseOver() const
Check whether the mouse cursor is currently over the button rectangle.
void Update() override
Update the button internal state (hover/pressed) and trigger callbacks.
Align GetAlign() const override
Get current alignment mode.
void SetTextColor(unsigned int color) override
Set label color.
void SetText(const std::string &text) override
Set button label text.
void SetCallback(std::function< void()> callback) override
Set callback invoked on click.
unsigned int _backgroundColor
void SetEnabled(bool enabled) override
Enable/disable the button.
void SetBackgroundColor(unsigned int color) override
Set button background color.
int GetTextSize() const override
Get label font size.
void GetPosition(float &x, float &y) const override
Get the current top-left position of the button.
void Render() override
Render the button.
void GetSize(float &width, float &height) const override
Get the current size of the button.
void SetTextSize(int size) override
Set label font size in pixels.
int EstimateTextWidth(const std::string &text, int fontSize)
Estimate the width of text in pixels.
Definition TextUtils.hpp:27
Definition IButton.hpp:13
ButtonState
Button state enumeration.
Definition IButton.hpp:17
@ NORMAL
Default state.
@ PRESSED
Mouse button is held down on the button.
@ HOVERED
Mouse is over the button.
Align
Alignment modes relative to the current window.
Definition IButton.hpp:33
@ CENTER_VERTICAL
Center on the Y axis.
@ CENTER_BOTH
Center on both axes.
@ CENTER_HORIZONTAL
Center on the X axis.
@ NONE
No alignment.