-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdl_button.cpp
316 lines (273 loc) · 8.83 KB
/
sdl_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "sdl_button.h"
#include "font.h"
#include <iostream>
#include <math.h>
#include <string>
#include "sdl_tool.h"
/********************************************************************/
bool SDLButtonManager::handleEvent(Camera* camera) {
SDLCamera* sdl_camera = dynamic_cast<SDLCamera*>(camera);
const SDL_Event& e = sdl_camera->event();
bool button_pressed = false;
if( e.type == SDL_MOUSEBUTTONDOWN ) {
button_pressed = true;
}
bool result = false;
for( auto button : buttons_ ) {
// get mouse position
if( !button->isVisible() ) {
continue;
}
if( button->mouseOverButton(camera->mouse_x(), camera->mouse_y()) ) {
button->hasFocus(true);
if( button_pressed ) {
if( button->isActive() ) {
button->deactivate();
result = true;
} else {
button->activate();
if( connections_.find(button) != connections_.end()) {
(*connections_[button])();
}
result = true;
}
}
} else {
button->hasFocus(false);
if( button->isActive() && button->checkButtonPressed(button_pressed)) {
button->deactivate();
return true;
}
}
}
return result;
}
// remark: the delay_in_ms may be use to display animated buttons
void SDLButtonManager::do_render(Camera* camera, double /*delay_in_ms*/) {
SDLCamera* sdl_camera = dynamic_cast<SDLCamera*>(camera);
SDL_Renderer* main_renderer = sdl_camera->main_renderer();
// iterate over all visible buttons, get texture and display them
for( auto menu : menus_ ) {
if( !menu->isVisible() ) {
continue;
}
SDL_Rect rect = {menu->x(), menu->y(), menu->width(), menu->height()};
SDL_SetRenderDrawColor( main_renderer, 200, 200, 200, 125 );
SDL_RenderFillRect(main_renderer, &rect);
SDL_SetRenderDrawColor( main_renderer, 50, 50, 50, 255 );
SDL_RenderDrawRect(main_renderer, &rect);
}
for( auto b : buttons_ ) {
if( !b->isVisible() ) {
continue;
}
SDLButton* button = dynamic_cast<SDLButton*>(b);
sdl_camera->displayButton(button);
// display white rectangle if button has focus
if( button->hasFocus() ) {
SDL_SetRenderDrawColor( main_renderer, 250, 250, 250, 255 );
SDL_RenderDrawRect(main_renderer, &button->rect());
}
}
}
/********************************************************************/
SDLButton::SDLButton(const std::string& icon_name, int x, int y) : Button(icon_name,x,y) {
surface_ = Utility::IMGLoad(icon_name.c_str());
int w = surface_->w;
int h = surface_->h;
setSize(w,h);
rect_ = {x,y,w,h};
texture_ = nullptr;
}
SDLButton::SDLButton(const std::string& icon_name, const std::string& text, int x, int y) : Button(icon_name,x,y) {
surface_ = Utility::IMGLoad(icon_name.c_str());
int w = surface_->w;
int h = surface_->h;
setSize(w,h);
rect_ = {x,y,w,h};
texture_ = nullptr;
text_ = text;
}
SDLButton::SDLButton(int x, int y) : Button("none",x,y) {
surface_ = nullptr;
rect_ = {x,y,1,1};
texture_ = nullptr;
}
SDLButton::~SDLButton() {
if( surface_ != nullptr ) {
SDL_FreeSurface(surface_);
}
if( texture_ != nullptr ) {
SDL_DestroyTexture(texture_);
}
}
void SDLButton::setIcon(const std::string& icon_name) {
if( icon_name == buttonName() ) {
return; // icon is already set
}
setButtonName(icon_name);
if( surface_ != nullptr ) {
SDL_FreeSurface(surface_);
surface_ = nullptr;
}
if( texture_ != nullptr ) {
SDL_DestroyTexture(texture_);
texture_ = nullptr;
}
surface_ = Utility::IMGLoad(icon_name.c_str());
int w = surface_->w;
int h = surface_->h;
setSize(w,h);
rect_.w = w;
rect_.h = h;
}
SDL_Texture* SDLButton::getTexture(SDL_Renderer* renderer) {
if( texture_ == nullptr ) {
// Transparency with green color
Uint32 key = SDL_MapRGB(surface_->format, 0, 255, 0);
SDL_SetColorKey(surface_ , SDL_TRUE, key);
texture_ = SDL_CreateTextureFromSurface(renderer, surface_);
if( texture_ == nullptr ) {
Logger::error() << "CreateRGBSurface failed: " << SDL_GetError() << Logger::endl;
}
}
return texture_;
}
void SDLButton::setTexture(SDL_Texture* texture) {
if( texture == nullptr ) return;
if( texture_ != nullptr ) {
SDL_DestroyTexture(texture_);
}
texture_ = texture;
int w, h;
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
setSize(w,h);
rect_.w = w;
rect_.h = h;
}
void SDLButton::activate() {
Button::activate();
}
void SDLButton::deactivate() {
Button::deactivate();
}
void SDLButton::setSize(int w, int h) {
Button::setSize(w,h);
rect_ = {x(),y(),this->w(),this->h()};
}
void SDLButton::setPosition(int x,int y) {
Button::setPosition(x,y);
rect_ = {this->x(),this->y(),w(),h()};
}
/********************************************************************/
SDLCheckBox::SDLCheckBox(int x, int y) : Button("none",x,y) {
}
void SDLCheckBox::activate() {
}
void SDLCheckBox::deactivate() {
}
/********************************************************************/
void SDLButtonMenu::activate() {
SDLButton::activate();
menu_->show();
}
void SDLButtonMenu::deactivate() {
menu_->hide();
SDLButton::deactivate();
}
bool SDLButtonMenu::checkButtonPressed(bool button_pressed) const {
return button_pressed;
}
/********************************************************************/
void SDLQuitButton::activate() {
camera_->set_quit();
}
/********************************************************************/
SDLTextButton::SDLTextButton(
SDLCamera* camera,
const std::string& text,
int x, int y,
const SDL_Color& color,
const SDL_Color& bgcolor
) : SDLButton(x,y), camera_(camera), text_(text) {
// build texture with text
SDLText stext(text, FontLib::fontFamily(), FontLib::fontSize(), color, bgcolor);
stext.set_position(x,y);
SDL_Texture* texture = stext.texture(camera->main_renderer()) ;
setTexture(texture);
stext.releaseTexture();
}
/********************************************************************/
SDLToolButton::SDLToolButton(
SDLTool* tool, std::string icon_name, int x, int y
) : SDLButton(icon_name, x, y), tool_(tool){
int old_w = std::max(w(),64);
int old_h = std::max(h(),64);
setSize(64,64); // size of the button in the menu
rect_ = {x,y,old_w,old_h}; // size of the tool on the map
tool_->setRect( rect_ );
}
SDLToolButton::~SDLToolButton() {
delete tool_;
}
void SDLToolButton::activate() {
tool_->activate();
}
void SDLToolButton::deactivate() {
tool_->deactivate();
}
/********************************************************************/
SDLItemToolButton::SDLItemToolButton(
const BasicItem& item, SDLTool* tool,
std::string icon_name, int x, int y
) : SDLToolButton(tool, icon_name, x, y), item_(item) {
setTooltipPosition(TooltipPosition::OVER);
}
/*!
* \return the number of items available in the Command Center
*/
const std::string& SDLItemToolButton::text() const {
SDLItemToolButton* non_const_this = const_cast<SDLItemToolButton*>(this);
std::string text = tr(item_.name());
if( text.size() > 9 ) {
text = text.substr(0,9);
}
if( item_.name() == "workbench" || item_.name() == "chest" ) {
text.append(" +oo");
non_const_this->setText(text);
return SDLToolButton::text();
}
CommandCenter* cc = CommandCenter::cur_command_center;
if( cc == nullptr ) {
non_const_this->setText(std::string());
} else {
// get number of items in Command Center
int nb = cc->countedItems(item_);
if( nb == 0 ) {
text.append(" -");
} else {
text.append(" x");
text.append(Utility::itos(nb));
}
non_const_this->setText(text);
}
return SDLToolButton::text();
}
void SDLItemToolButton::activate() {
if( text() == "-" ) {
Logger::warning() << tr("No such item: please craft this item before using this tool.") << Logger::endl;
} else {
SDLToolButton::activate();
}
}
/********************************************************************/
SDLTransformToolButton::SDLTransformToolButton(
const BasicItem& item, SDLTool* tool,
std::string icon_name, int x, int y
) : SDLItemToolButton(item, tool, icon_name, x, y) {
setTooltipPosition(TooltipPosition::OVER);
}
const std::string& SDLTransformToolButton::text() const {
return SDLToolButton::text();
}
/********************************************************************/