-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
101 lines (77 loc) · 2.13 KB
/
Button.cpp
File metadata and controls
101 lines (77 loc) · 2.13 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "Button.h"
#include <iostream>
#include "RaylibWrapper.h"
Button::Button()
{
_position = Vec2<float>(0, 0);
_dimensions = Vec2<float>(150, 75);
_color = ROUGE;
_textColor = BLANC;
_text = "Maco";
_fontSize = 30;
_isFocused = false;
int textSize = raywrp::MesureText(_text, _fontSize);
float xOffset = (_dimensions.GetX() - textSize) / 2.0f;
float yOffset = (_dimensions.GetY() - _fontSize) / 2.0f;
std::cout << "text : " << _text << "\n";
std::cout << "textSize : " << textSize << "\n";
std::cout << "xOffset : " << xOffset << ", yOffset : " << yOffset << "\n";
_textPosition.SetX(_position.GetX() + xOffset);
_textPosition.SetY(_position.GetY() + yOffset);
}
void Button::Init(Vec2<float> position, Vec2<float> dimensions, std::string text, Couleur color)
{
_text = text;
_color = color;
_dimensions = dimensions;
_position = position - dimensions / 2.0f;
_textColor = BLANC;
_fontSize = 30;
int textSize = raywrp::MesureText(_text, _fontSize);
float xOffset = (dimensions.GetX() - textSize) / 2.0f;
float yOffset = (dimensions.GetY() - _fontSize) / 2.0f;
std::cout << "text : " << text << "\n";
std::cout << "textSize : " << textSize << "\n";
std::cout << "xOffset : " << xOffset << ", yOffset : " << yOffset << "\n";
_textPosition.SetX(_position.GetX() + xOffset);
_textPosition.SetY(_position.GetY() + yOffset);
_isFocused = false;
// Sound
_selectEffect = LoadSound("Ressources\\select.ogg");
}
bool Button::IsClicked()
{
if (!_isFocused)
{
return false;
}
return IsMouseButtonReleased(0);
}
void Button::SetTextColor(Couleur color)
{
_textColor = color;
}
void Button::Update(Vec2<float> mousePos)
{
Rect button(
_position.GetX(),
_position.GetY(),
_dimensions.GetX(),
_dimensions.GetY());
bool isMouseIn = raywrp::CheckCollisionPointRec(mousePos, button);
if (!_isFocused && isMouseIn)
PlaySound(_selectEffect);
_isFocused = isMouseIn;
}
void Button::Draw()
{
if (_isFocused)
{
raywrp::DrawRectangleV(_position, _dimensions, _color);
}
else
{
raywrp::DrawRectangleLinesV(_position, _dimensions, _color);
}
raywrp::DrawTextV(_text, _textPosition, _fontSize, _textColor);
}