-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
42 lines (30 loc) · 1.44 KB
/
Button.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//
// Created by Daria Kuznetsova on 20.11.2024.
//
#include "Button.h"
Button::Button(const Rectangle &rect, const char *text, int textSize, bool isHovered,
Color idleColor, Color hoverColor, Color idleTextColor, Color hoverTextColor)
: rect(rect), text(text), textSize(textSize), idleColor(idleColor), hoverColor(hoverColor),
isHovered(isHovered), idleTextColor(idleTextColor), hoverTextColor(hoverTextColor) {}
// Handles user input and checks if the mouse is hovering over the button
void Button::handleInput(Vector2 mousePosition) {
// I don't know why I use it.
// Maybe because in this implementation it's possible to expand it in the future if there is something needed
isHovered = CheckCollisionPointRec(mousePosition, rect);
}
// Renders the button on the screen with appropriate color and text
void Button::render() const {
Color buttonColor = isHovered ? hoverColor : idleColor;
Color textColor = isHovered ? hoverTextColor : idleTextColor;
DrawRectangleRec(rect, buttonColor);
int textWidth = MeasureText(text, textSize);
float textX = rect.x + (rect.width - static_cast<float>(textWidth)) / 2;
float textY = rect.y + (rect.height - static_cast<float>(textSize)) / 2;
DrawText(text, static_cast<int>(textX), static_cast<int>(textY), textSize, textColor);
}
const Rectangle &Button::getRect() const {
return rect;
}
const char *Button::getText() const {
return text;
}