-
Notifications
You must be signed in to change notification settings - Fork 0
/
CheckBox.cpp
125 lines (110 loc) · 3.11 KB
/
CheckBox.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "CheckBox.h"
CheckBox::CheckBox(StateManager* stateManager, ResID fontResID, std::string text, float x, float y, bool sharedFont) : stateManager(stateManager), fontResID(fontResID), text(text), x(x), y(y), sharedFont(sharedFont)
{
resManager = stateManager->GetResourceManager();
screen = stateManager->GetGpuTarget();
width = x + 40.0f;
height = y + 40.0f;
cbx = x + 10.0f;
cby = y + 10.0f;
cbw = width + 10.0f;
cbh = height + 10.0f;
edgeLightColor = { 127, 127, 127, 255 };
edgeDarkColor = { 27, 27, 27, 255 };
fillColor = { 55, 55, 55, 255 };
onColor = { 0, 224, 0, 255 };
hoverColor = { 127, 127, 127, 255 };
textColor = { 255, 255, 255, 255 };
click = false;
on = false;
}
CheckBox::~CheckBox()
{
Free();
}
void CheckBox::Free()
{
resManager = nullptr;
stateManager = nullptr;
screen = nullptr;
}
void CheckBox::Draw()
{
// CheckBox
if (GetEvent() == CheckBoxEventState::Hover)
GPU_RectangleFilled(screen, cbx, cby, cbw, cbh, hoverColor);
else
GPU_RectangleFilled(screen, cbx, cby, cbw, cbh, fillColor);
if (on)
GPU_RectangleFilled(screen, cbx + 8.0f, cby + 8.0f, cbw - 8.0f, cbh - 8.0f, onColor);
/*
GPU_Line(screen, cbx, cby, cbw, cby, edgeLightColor);
GPU_Line(screen, cbx, cby, cbx, cbh, edgeLightColor);
GPU_Line(screen, cbx, cbh, cbw, cbh, edgeDarkColor);
GPU_Line(screen, cbw, cby, cbw, cbh, edgeDarkColor);
*/
GPU_Rectangle(screen, cbx, cby, cbw, cbh, edgeLightColor);
//GPU_Rectangle(screen, x, y, width, height, { 255,0,0,255 });
//GPU_Rectangle(screen, x+1, y+1, width-1, height-1, { 0,0,255,255 });
// Text
if (sharedFont)
{
resManager->GetSharedFontResource(fontResID)->SetColor(textColor.r, textColor.g, textColor.b, textColor.a);
resManager->GetSharedFontResource(fontResID)->DrawText(text, x + 80.0f, y + 30.0f);
}
else
{
resManager->GetFontResource(fontResID)->SetColor(textColor.r, textColor.g, textColor.b, textColor.a);
resManager->GetFontResource(fontResID)->DrawText(text, x + 80.0f, y + 30.0f);
}
}
void CheckBox::Event(SDL_Event &e)
{
if (e.type == SDL_MOUSEBUTTONDOWN)
{
if ((e.motion.x >= cbx) && (e.motion.x < cbw) && (e.motion.y >= cby) && (e.motion.y < cbh))
{
SetEvent(CheckBox::CheckBoxEventState::Down);
}
}
else if (e.type == SDL_MOUSEBUTTONUP)
{
SetEvent(CheckBox::CheckBoxEventState::Up);
if ((e.motion.x >= cbx) && (e.motion.x < cbw) && (e.motion.y >= cby) && (e.motion.y < cbh))
{
SetClick(true);
on = !on;
}
}
else if ((e.motion.x >= cbx) && (e.motion.x < cbw) && (e.motion.y >= cby) && (e.motion.y < cbh))
{
if (GetEvent() != CheckBoxEventState::Down)
{
SetEvent(CheckBox::CheckBoxEventState::Hover);
}
}
else
{
SetEvent(CheckBox::CheckBoxEventState::Up);
}
}
void CheckBox::SetEvent(CheckBoxEventState cbeState)
{
this->checkBoxEventState = cbeState;
}
CheckBox::CheckBoxEventState CheckBox::GetEvent()
{
return checkBoxEventState;
}
void CheckBox::SetClick(bool click)
{
this->click = click;
}
bool CheckBox::GetClick()
{
return click;
}
void CheckBox::SetOn(bool on)
{
this->on = on;
}