/
tristate_button.cpp
409 lines (337 loc) · 9.14 KB
/
tristate_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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
Copyright (C) 2013 - 2018 by Fabian Mueller <fabianmueller5@gmx.de>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "editor/palette/tristate_button.hpp"
#include "game_config.hpp"
#include "image.hpp"
#include "log.hpp"
#include "sdl/rect.hpp"
#include "sound.hpp"
#include "video.hpp"
static lg::log_domain log_display("display");
#define ERR_DP LOG_STREAM(err, log_display)
namespace gui {
tristate_button::tristate_button(CVideo& video,
editor::tristate_palette* palette,
std::string button_image_name,
const bool auto_join) :
widget(video, auto_join),
baseImage_(nullptr), touchedBaseImage_(nullptr), activeBaseImage_(nullptr),
itemImage_(nullptr),
pressedDownImage_(nullptr), pressedUpImage_(nullptr), pressedBothImage_(nullptr),
pressedBothActiveImage_(nullptr), pressedDownActiveImage_(nullptr), pressedUpActiveImage_(nullptr),
touchedDownImage_(nullptr), touchedUpImage_(nullptr), touchedBothImage_(nullptr),
textRect_(),
state_(NORMAL), pressed_(false),
base_height_(0), base_width_(0),
palette_(palette), item_id_()
{
if (button_image_name.empty()) {
button_image_name = "buttons/button_selectable/button_selectable_38_";
}
baseImage_.assign(
image::get_image(button_image_name + "base.png"));
activeBaseImage_.assign(
image::get_image(button_image_name + "base-active.png"));
touchedBaseImage_.assign(
image::get_image(button_image_name + "base-touched.png"));
touchedBothImage_.assign(
image::get_image(button_image_name + "border-touched-both.png"));
touchedUpImage_.assign(
image::get_image(button_image_name + "border-touched-up.png"));
touchedDownImage_.assign(
image::get_image(button_image_name + "border-touched-down.png"));
pressedUpImage_.assign(
image::get_image(button_image_name + "border-pressed-up.png"));
pressedDownImage_.assign(
image::get_image(button_image_name + "border-pressed-down.png"));
pressedBothImage_.assign(
image::get_image(button_image_name + "border-pressed-both.png"));
pressedUpActiveImage_.assign(
image::get_image(button_image_name + "border-active-pressed-up.png"));
pressedDownActiveImage_.assign(
image::get_image(button_image_name + "border-active-pressed-down.png"));
pressedBothActiveImage_.assign(
image::get_image(button_image_name + "border-active-pressed-both.png"));
//TODO
// if (button_image.null()) {
// ERR_DP<< "error initializing button!" << std::endl;
// throw error();
// }
base_height_ = baseImage_->h;
base_width_ = baseImage_->w;
}
tristate_button::~tristate_button() {}
void tristate_button::set_pressed(PRESSED_STATE new_pressed_state) {
STATE new_state = state_;
switch (new_pressed_state) {
case LEFT:
new_state = (state_ == PRESSED_ACTIVE_LEFT) ? PRESSED_ACTIVE_LEFT : PRESSED_LEFT;
break;
case RIGHT:
new_state = (state_ == PRESSED_ACTIVE_RIGHT) ? PRESSED_ACTIVE_RIGHT : PRESSED_RIGHT;
break;
case BOTH:
new_state = (state_ == PRESSED_ACTIVE_BOTH) ? PRESSED_ACTIVE_BOTH : PRESSED_BOTH;
break;
case NONE:
new_state = NORMAL;
}
if (state_ != new_state) {
state_ = new_state;
set_dirty();
}
}
//TODO
//void tristate_button::set_active(bool active) {
// if ((state_ == NORMAL) && active) {
// state_ = ACTIVE;
// set_dirty();
// } else if ((state_ == ACTIVE) && !active) {
// state_ = NORMAL;
// set_dirty();
// }
//}
tristate_button::PRESSED_STATE tristate_button::pressed_state() const {
switch (state_) {
case PRESSED_LEFT:
case PRESSED_ACTIVE_LEFT:
case TOUCHED_BOTH_LEFT:
return LEFT;
case PRESSED_RIGHT:
case PRESSED_ACTIVE_RIGHT:
case TOUCHED_BOTH_RIGHT:
return RIGHT;
case PRESSED_BOTH:
case PRESSED_ACTIVE_BOTH:
return BOTH;
default:
return NONE;
}
}
void tristate_button::enable(bool new_val) {
if (new_val != enabled()) {
pressed_ = false;
// check buttons should keep their state
state_ = NORMAL;
widget::enable(new_val);
}
}
void tristate_button::draw_contents() {
surface image(nullptr);
surface overlay(nullptr);
surface base = baseImage_;
switch (state_) {
case UNINIT:
return;
case NORMAL:
break;
case TOUCHED_LEFT:
overlay = touchedUpImage_;
base = touchedBaseImage_;
break;
case TOUCHED_RIGHT:
overlay = touchedDownImage_;
base = touchedBaseImage_;
break;
case TOUCHED_BOTH_LEFT:
case TOUCHED_BOTH_RIGHT:
overlay = touchedBothImage_;
base = touchedBaseImage_;
break;
case ACTIVE:
// overlay = activeImage_;
base = activeBaseImage_;
break;
case PRESSED_LEFT:
base = activeBaseImage_;
overlay = pressedUpImage_;
break;
case PRESSED_RIGHT:
base = activeBaseImage_;
overlay = pressedDownImage_;
break;
case PRESSED_BOTH:
base = activeBaseImage_;
overlay = pressedBothImage_;
break;
case PRESSED_ACTIVE_LEFT:
overlay = pressedUpActiveImage_;
base = activeBaseImage_;
break;
case PRESSED_ACTIVE_BOTH:
overlay = pressedBothActiveImage_;
base = activeBaseImage_;
break;
case PRESSED_ACTIVE_RIGHT:
overlay = pressedDownActiveImage_;
base = activeBaseImage_;
break;
}
image = base;
const SDL_Rect& loc = location();
surface scalled_item;
scalled_item.assign(scale_surface(itemImage_,
36, 36));
surface nitem = make_neutral_surface(scalled_item);
surface nbase = make_neutral_surface(base);
//TODO avoid magic numbers
SDL_Rect r {1, 1, 0, 0};
sdl_blit(nitem, nullptr, nbase, &r);
if (!overlay.null()) {
surface noverlay = make_neutral_surface(overlay);
sdl_blit(noverlay, nullptr, nbase, nullptr);
}
bg_restore();
image = nbase;
video().blit_surface(loc.x, loc.y, image);
}
//TODO move to widget
bool tristate_button::hit(int x, int y) const {
return sdl::point_in_rect(x, y, location());
}
void tristate_button::mouse_motion(const SDL_MouseMotionEvent& event) {
if (hit(event.x, event.y))
{ // the cursor is over the widget
switch (state_) {
case UNINIT:
return;
case NORMAL:
state_ = ACTIVE;
break;
case PRESSED_LEFT:
state_ = PRESSED_ACTIVE_LEFT;
break;
case PRESSED_RIGHT:
state_ = PRESSED_ACTIVE_RIGHT;
break;
case PRESSED_BOTH:
state_ = PRESSED_ACTIVE_BOTH;
break;
default:
//assert(false);
break;
}
} else { // the cursor is not over the widget
switch (state_) {
case ACTIVE:
state_ = NORMAL;
break;
case TOUCHED_LEFT:
case TOUCHED_RIGHT:
state_ = ACTIVE;
break;
case PRESSED_ACTIVE_RIGHT:
case TOUCHED_BOTH_RIGHT:
state_ = PRESSED_RIGHT;
break;
case PRESSED_ACTIVE_BOTH:
state_ = PRESSED_BOTH;
break;
case PRESSED_ACTIVE_LEFT:
case TOUCHED_BOTH_LEFT:
state_ = PRESSED_LEFT;
break;
default:
break;
}
}
}
void tristate_button::mouse_down(const SDL_MouseButtonEvent& event) {
if (!hit(event.x, event.y))
return;
if (event.button == SDL_BUTTON_RIGHT) {
if (state_ == ACTIVE)
state_ = TOUCHED_RIGHT;
if (state_ == PRESSED_ACTIVE_LEFT)
state_ = TOUCHED_BOTH_LEFT;
}
if (event.button == SDL_BUTTON_LEFT) {
if (state_ == ACTIVE)
state_ = TOUCHED_LEFT;
if (state_ == PRESSED_ACTIVE_RIGHT)
state_ = TOUCHED_BOTH_RIGHT;
}
}
void tristate_button::release() {
state_ = NORMAL;
draw_contents();
}
void tristate_button::mouse_up(const SDL_MouseButtonEvent& event) {
if (!(hit(event.x, event.y)))
return;
// the user has stopped pressing the mouse left button while on the widget
if (event.button == SDL_BUTTON_LEFT) {
if (state_ == TOUCHED_LEFT) {
state_ = PRESSED_ACTIVE_LEFT;
palette_->select_fg_item(item_id_);
//TODO
// palette_->draw(true);
pressed_ = true;
}
if (state_ == TOUCHED_BOTH_RIGHT) {
state_ = PRESSED_ACTIVE_BOTH;
palette_->select_fg_item(item_id_);
// palette_->select_bg_item(item_id_);
// palette_->draw(true);
pressed_ = true;
}
}
if (event.button == SDL_BUTTON_RIGHT) {
pressed_ = true;
palette_->select_bg_item(item_id_);
if (state_ == TOUCHED_RIGHT) {
state_ = PRESSED_ACTIVE_RIGHT;
// palette_->select_bg_item(item_id_);
// palette_->draw(true);
// pressed_ = true;
}
if (state_ == TOUCHED_BOTH_LEFT) {
state_ = PRESSED_ACTIVE_BOTH;
// palette_->select_fg_item(item_id_);
// palette_->select_bg_item(item_id_);
// palette_->draw(true);
// pressed_ = true;
}
}
if (pressed_)
sound::play_UI_sound(game_config::sounds::checkbox_release);
}
void tristate_button::handle_event(const SDL_Event& event) {
gui::widget::handle_event(event);
if (hidden() || !enabled())
return;
STATE start_state = state_;
if (!mouse_locked()) {
switch (event.type) {
case SDL_MOUSEBUTTONDOWN:
mouse_down(event.button);
break;
case SDL_MOUSEBUTTONUP:
mouse_up(event.button);
break;
case SDL_MOUSEMOTION:
mouse_motion(event.motion);
break;
default:
return;
}
}
if (start_state != state_)
set_dirty(true);
}
bool tristate_button::pressed() {
const bool res = pressed_;
pressed_ = false;
return res;
}
}