-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuiButton.cpp
144 lines (128 loc) · 3.01 KB
/
GuiButton.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "GuiButton.h"
GuiButton::GuiButton(TTF_Font* font, SDL_Color fontColor, SDL_Renderer* rendererIn, std::string textIn, int x, int y, int width, int height, STATE onClickIn)
{
renderer = rendererIn;
edgeTexture = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/edge.png").c_str());
texture = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/border.png").c_str());
textureHover = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/borderHover.png").c_str());
textureClick = IMG_LoadTexture(renderer, std::string(Utility::getBasePath() + "assets/images/borderClick.png").c_str());
text = new GuiText(font, fontColor, renderer, textIn, x, y);
leftEdge.x = x;
leftEdge.y = y;
leftEdge.h = height;
leftEdge.w = 2;
rightEdge.x = x+width-2;
rightEdge.y = y;
rightEdge.h = height;
rightEdge.w = 2;
middle.x = x+2;
middle.y = y;
middle.h = height;
middle.w = width-4;
rect.x = x;
rect.y = y;
rect.h = height;
rect.w = width;
state = NORMAL;
onClick = onClickIn;
clicked = false;
}
void GuiButton::setState(int stateIn)
{
state = stateIn;
}
int GuiButton::getState(void)
{
return state;
}
GuiText* GuiButton::getGuiText(void)
{
return text;
}
STATE GuiButton::getOnClick(void)
{
return onClick;
}
void GuiButton::render(void)
{
SDL_RenderCopy(renderer, edgeTexture, NULL, &leftEdge);
SDL_RenderCopyEx(renderer, edgeTexture, NULL, &rightEdge, 0.0, NULL, SDL_FLIP_HORIZONTAL);
switch (state)
{
case BUTTONSTATE::NORMAL:
SDL_RenderCopy(renderer, texture, NULL, &middle);
break;
case BUTTONSTATE::HOVER:
SDL_RenderCopy(renderer, textureHover, NULL, &middle);
break;
case BUTTONSTATE::CLICK:
SDL_RenderCopy(renderer, textureClick, NULL, &middle);
break;
default:
break;
}
text->render();
}
void GuiButton::onEvent(SDL_Event* ev, const Uint8*)
{
switch (ev->type)
{
case SDL_MOUSEMOTION:
{
SDL_Rect mouse;
mouse.x = ev->button.x;
mouse.y = ev->button.y;
mouse.w = 1;
mouse.h = 1;
if(state != GuiButton::BUTTONSTATE::CLICK)
{
if(SDL_HasIntersection(&mouse, &rect))
state = GuiButton::BUTTONSTATE::HOVER;
else
state = GuiButton::BUTTONSTATE::NORMAL;
}
break;
}
case SDL_MOUSEBUTTONDOWN:
{
if(ev->button.button == SDL_BUTTON_LEFT)
{
SDL_Rect mouse;
mouse.x = ev->button.x;
mouse.y = ev->button.y;
mouse.w = 1;
mouse.h = 1;
if(SDL_HasIntersection(&mouse, &rect))
state = GuiButton::BUTTONSTATE::CLICK;
else
state = GuiButton::BUTTONSTATE::NORMAL;
}
break;
}
case SDL_MOUSEBUTTONUP:
{
if(ev->button.button == SDL_BUTTON_LEFT)
{
SDL_Rect mouse;
mouse.x = ev->button.x;
mouse.y = ev->button.y;
mouse.w = 1;
mouse.h = 1;
if(state == GuiButton::BUTTONSTATE::CLICK)
{
if(SDL_HasIntersection(&mouse, &rect))
{
clicked = true;
}
else
state = GuiButton::BUTTONSTATE::NORMAL;
}
}
break;
}
}
}
bool GuiButton::hasClicked(void)
{
return clicked;
}