-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbutton.cpp
More file actions
62 lines (49 loc) · 1.35 KB
/
button.cpp
File metadata and controls
62 lines (49 loc) · 1.35 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "button.hpp"
Button::Button(const char *imagePath, Vector2 imagePosition, float scale)
{
Image image = LoadImage(imagePath);
int originalWidth = image.width;
int originalHeight = image.height;
int newWidth = static_cast<int>(originalWidth * scale);
int newHeight = static_cast<int>(originalHeight * scale);
ImageResize(&image, newWidth, newHeight);
texture = LoadTextureFromImage(image);
UnloadImage(image);
position = imagePosition;
}
Button::~Button()
{
UnloadTexture(texture);
}
void Button::Draw()
{
DrawTextureV(texture, position, (wasHoveredBefore ? darkenColor : normalColor));
}
void Button::SetPosition(Vector2 newPosition)
{
position = newPosition;
}
bool Button::isPressed(Vector2 mousePos, bool mousePressed)
{
Rectangle rect = {position.x, position.y, static_cast<float>(texture.width), static_cast<float>(texture.height)};
bool isHovered = CheckCollisionPointRec(mousePos, rect);
if (!isHovered && wasHoveredBefore)
{
UnHovered();
wasHoveredBefore = false;
}
else if (isHovered && !wasHoveredBefore)
{
Hovered();
wasHoveredBefore = true;
}
return isHovered && mousePressed;
}
void Button::Hovered()
{
SetMouseCursor(MOUSE_CURSOR_POINTING_HAND);
}
void Button::UnHovered()
{
SetMouseCursor(MOUSE_CURSOR_DEFAULT);
}