-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButton.cpp
166 lines (146 loc) · 3.95 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "Button.h"
#include "MyWindow.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <string>
#include <iostream>
#include <functional>
Button::Button(int x, int y, LTexture *tex, LWindow &window, std::string text, TTF_Font *font, SDL_Color textColor) : x(x), y(y), w(tex->getWidth()), h(tex->getHeight()), mText(window, text, font, textColor), window(window), tex(tex), bgColor({255, 255, 255, 255}), centerX(false)
{
}
Button::Button(int x, int y, int w, int h, SDL_Color bgColor, LWindow &window, std::string text, TTF_Font *font, SDL_Color textColor) : x(x), y(y), w(w), h(h), window(window), mText(window, text, font, textColor), tex(NULL), bgColor(bgColor), centerX(false)
{
}
void Button::render(SDL_Renderer *renderer)
{
bool renderDefault = true;
// std::cout << "IN RENDER" << std::endl;
if (centerX)
{
x = (window.getWidth() - w) / 2;
}
if (clicked && clickTexture != NULL)
{
int newW = clickTexture->getWidth();
int newH = clickTexture->getHeight();
int newX = x - (newW - w) / 2;
int newY = y - (newH - h) / 2;
clickTexture->render(renderer, newX, newY);
renderDefault = false;
// std::cout << "HERE2" << std::endl;
}
else if (highlighted)
{
if (highlightTexture != NULL)
{
int newW = highlightTexture->getWidth();
int newH = highlightTexture->getHeight();
int newX = x - (newW - w) / 2;
int newY = y - (newH - h) / 2;
highlightTexture->render(renderer, newX, newY);
renderDefault = false;
}
else
{
SDL_Rect fillRect = {x - highlightWidth, y - highlightWidth, w + 2 * highlightWidth, h + 2 * highlightWidth};
SDL_SetRenderDrawColor(renderer, highlightColor.r, highlightColor.g, highlightColor.b, highlightColor.a);
SDL_RenderFillRect(renderer, &fillRect);
}
}
if (renderDefault)
{
if (tex != NULL)
{
tex->render(renderer, x, y);
// std::cout << x << " " << y << std::endl;
}
else
{
SDL_Rect fillRect = {x, y, w, h};
SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, bgColor.a);
SDL_RenderFillRect(renderer, &fillRect);
}
}
int textX = x + (w - mText.getWidth()) / 2;
int textY = y + (h - mText.getHeight()) / 2;
mText.render(renderer, textX, textY);
}
void Button::cleanUp()
{
if (tex != NULL)
{
tex->free();
}
mText.cleanUp();
}
void Button::setCenterX(bool centerX)
{
this->centerX = centerX;
}
void Button::handle(SDL_Event &e)
{
switch (e.type)
{
case SDL_MOUSEMOTION:
if (e.motion.x >= x && e.motion.x <= x + w && e.motion.y >= y && e.motion.y <= y + h)
{
highlighted = true;
}
else
{
highlighted = false;
clicked = false;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (e.button.x >= x && e.button.x <= x + w && e.button.y >= y && e.button.y <= y + h && e.button.button == SDL_BUTTON_LEFT)
{
clicked = true;
}
break;
case SDL_MOUSEBUTTONUP:
if (e.button.x >= x && e.button.x <= x + w && e.button.y >= y && e.button.y <= y + h && e.button.button == SDL_BUTTON_LEFT && clicked)
{
clickListener(window);
}
break;
default:
break;
}
}
void Button::setHighlightTexture(LTexture &texture)
{
highlightTexture = &texture;
}
void Button::setOnClickListener(std::function<void(LWindow &window)> f)
{
clickListener = f;
}
void Button::setClickTexture(LTexture &texture)
{
clickTexture = &texture;
}
int Button::getX()
{
return x;
}
int Button::getY()
{
return y;
}
int Button::getWidth()
{
return w;
}
int Button::getHeight()
{
return h;
}
void Button::setX(int x)
{
this->x = x;
}
void Button::setY(int y)
{
this->y = y;
}