-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
slider.cpp
423 lines (368 loc) · 8.92 KB
/
slider.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
Copyright (C) 2003 - 2015 by David White <dave@whitevine.net>
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 "global.hpp"
#include "widgets/slider.hpp"
#include "game_config.hpp"
#include "font.hpp"
#include "image.hpp"
#include "sdl/rect.hpp"
#include "sound.hpp"
#include "video.hpp"
namespace {
const std::string slider_image = ".png";
const std::string disabled_image = ".png~GS()";
const std::string pressed_image = "-pressed.png";
const std::string active_image = "-active.png";
}
namespace gui {
slider::slider(CVideo &video, const std::string& image, bool black)
: widget(video), image_(image::get_image(image + slider_image)),
pressedImage_(image::get_image(image + pressed_image)),
activeImage_(image::get_image(image + active_image)),
disabledImage_(image::get_image(image + disabled_image)),
line_color_(black ? font::BLACK_COLOR : font::NORMAL_COLOR),
min_(-100000), max_(100000), value_(0),
increment_(1), value_change_(false), state_(NORMAL)
{
}
void slider::enable(bool new_val)
{
if(new_val != enabled())
{
state_ = NORMAL;
widget::enable(new_val);
}
}
void slider::set_location(SDL_Rect const &rect)
{
SDL_Rect r = rect;
r.h = image_->h;
widget::set_location(r);
}
void slider::set_min(int value)
{
min_ = value;
if (value_ < min_) {
value_ = min_;
value_change_ = true;
}
set_dirty(true);
}
void slider::set_max(int value)
{
max_ = value;
if (value_ > max_) {
value_ = max_;
value_change_ = true;
}
set_dirty(true);
}
void slider::set_value(int value)
{
if (value > max_)
value = max_;
if (value < min_)
value = min_;
if (increment_ > 1) {
int hi = increment_ / 2;
value = ((value + hi) / increment_) * increment_;
}
if (value == value_)
return;
value_ = value;
value_change_ = true;
set_dirty(true);
}
void slider::set_increment(int increment)
{
increment_ = increment;
}
int slider::value() const
{
return value_;
}
int slider::max_value() const
{
return max_;
}
bool slider::value_change()
{
if (value_change_) {
value_change_ = false;
return true;
}
return false;
}
SDL_Rect slider::slider_area() const
{
static const SDL_Rect default_value = {0,0,0,0};
SDL_Rect const &loc = location();
if (image_.null() || image_->w >= loc.w)
return default_value;
int xpos = loc.x + (value_ - min_) * (loc.w - image_->w) / (max_ - min_);
return sdl::create_rect(xpos, loc.y, image_->w, image_->h);
}
void slider::draw_contents()
{
surface image;
switch (state_) {
case NORMAL:
image.assign(image_);
break;
case ACTIVE:
image.assign(activeImage_);
break;
default:
image.assign(pressedImage_);
break;
}
assert(image != NULL);
SDL_Color line_color = line_color_;
if (!enabled()) {
image.assign(disabledImage_);
line_color = font::DISABLED_COLOR;
}
SDL_Rect const &loc = location();
if (image->w >= loc.w)
return;
surface screen = video().getSurface();
SDL_Rect line_rect = sdl::create_rect(loc.x + image->w / 2
, loc.y + loc.h / 2
, loc.w - image->w
, 1);
sdl::fill_rect(screen, &line_rect, SDL_MapRGB(screen->format,
line_color.r, line_color.g, line_color.b));
SDL_Rect const &slider = slider_area();
video().blit_surface(slider.x, slider.y, image);
}
void slider::set_slider_position(int x)
{
SDL_Rect const &loc = location();
int tmp = x - loc.x - image_->w / 2;
if (tmp < 0)
tmp = 0;
if (tmp > loc.w - image_->w)
tmp = loc.w - image_->w;
set_value(tmp * (max_ - min_) / (loc.w - image_->w) + min_);
}
void slider::mouse_motion(const SDL_MouseMotionEvent& event)
{
if (state_ == NORMAL || state_ == ACTIVE) {
bool on = sdl::point_in_rect(event.x, event.y, location());
state_ = on ? ACTIVE : NORMAL;
} else if (state_ == CLICKED || state_ == DRAGGED) {
state_ = DRAGGED;
bool prev_change = value_change_;
value_change_ = false;
set_slider_position(event.x);
if(value_change_) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
} else {
value_change_ = prev_change;
}
}
}
void slider::mouse_down(const SDL_MouseButtonEvent& event)
{
bool prev_change = value_change_;
if (!sdl::point_in_rect(event.x, event.y, location()))
return;
#if !SDL_VERSION_ATLEAST(2,0,0)
if (event.button == SDL_BUTTON_WHEELUP || event.button == SDL_BUTTON_WHEELRIGHT) {
value_change_ = false;
set_focus(true);
set_value(value_ + increment_);
if(value_change_) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
} else {
value_change_ = prev_change;
}
}
if (event.button == SDL_BUTTON_WHEELDOWN || event.button == SDL_BUTTON_WHEELLEFT) {
value_change_ = false;
set_focus(true);
set_value(value_ - increment_);
if(value_change_) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
} else {
value_change_ = prev_change;
}
}
#endif
if (event.button != SDL_BUTTON_LEFT)
return;
state_ = CLICKED;
set_focus(true);
if (sdl::point_in_rect(event.x, event.y, slider_area())) {
sound::play_UI_sound(game_config::sounds::button_press);
} else {
value_change_ = false;
set_slider_position(event.x);
if(value_change_) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
} else {
value_change_ = prev_change;
}
}
}
#if SDL_VERSION_ATLEAST(2,0,0)
void slider::mouse_wheel(const SDL_MouseWheelEvent& event) {
bool prev_change = value_change_;
int x, y;
SDL_GetMouseState(&x, &y);
if (!sdl::point_in_rect(x, y, location()))
return;
if (event.y > 0 || event.x > 0) {
value_change_ = false;
set_focus(true);
set_value(value_ + increment_);
if(value_change_) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
} else {
value_change_ = prev_change;
}
}
if (event.y < 0 || event.x < 0) {
value_change_ = false;
set_focus(true);
set_value(value_ - increment_);
if(value_change_) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
} else {
value_change_ = prev_change;
}
}
}
#endif
bool slider::requires_event_focus(const SDL_Event* event) const
{
if(!focus_ || !enabled() || hidden()) {
return false;
}
if(event == NULL) {
//when event is not specified, signal that focus may be desired later
return true;
}
if(event->type == SDL_KEYDOWN) {
SDLKey key = event->key.keysym.sym;
switch(key) {
case SDLK_LEFT:
case SDLK_RIGHT:
return true;
default:
break;
}
}
//mouse events are processed regardless of focus
return false;
}
void slider::handle_event(const SDL_Event& event)
{
if (!enabled() || hidden())
return;
STATE start_state = state_;
switch(event.type) {
case SDL_MOUSEBUTTONUP:
if (!mouse_locked()) {
bool on = sdl::point_in_rect(event.button.x, event.button.y, slider_area());
state_ = on ? ACTIVE : NORMAL;
}
break;
case SDL_MOUSEBUTTONDOWN:
if (!mouse_locked())
mouse_down(event.button);
break;
case SDL_MOUSEMOTION:
if (!mouse_locked())
mouse_motion(event.motion);
break;
//TODO enable if you know how to fix the zoom slider bug
case SDL_KEYDOWN:
if(focus(&event)) {
const SDL_keysym& key = reinterpret_cast<const SDL_KeyboardEvent&>(event).keysym;
const int c = key.sym;
if(c == SDLK_LEFT) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
set_value(value_ - increment_);
} else if(c == SDLK_RIGHT) {
sound::play_UI_sound(game_config::sounds::slider_adjust);
set_value(value_ + increment_);
}
}
break;
#if SDL_VERSION_ATLEAST(2,0,0)
case SDL_MOUSEWHEEL:
if (!mouse_locked())
mouse_wheel(event.wheel);
break;
#endif
default:
return;
}
if (start_state != state_)
set_dirty(true);
}
template<typename T>
list_slider<T>::list_slider(CVideo &video) :
slider(video)
{
set_min(0);
set_increment(1);
slider::set_value(0);
}
template<typename T>
list_slider<T>::list_slider(CVideo &video, const std::vector<T> &items) :
slider(video),
items_(items)
{
set_min(0);
set_increment(1);
if(items.size() > 0)
{
set_max(items.size() - 1);
}
slider::set_value(0);
}
template<typename T>
const T& list_slider<T>::item_selected() const
{
return items_[value()];
}
template<typename T>
bool list_slider<T>::select_item(const T& item)
{
for(unsigned i = 0, nb = items_.size(); i < nb; ++i)
{
if(item == items_[i])
{
slider::set_value(i);
return true;
}
}
return false;
}
template<typename T>
void list_slider<T>::set_items(const std::vector<T> &items)
{
items_ = items;
if(items.size() > 0)
{
set_max(items.size() - 1);
}
slider::set_value(0);
}
// Force compilation of the following template instantiations
template class list_slider< double >;
template class list_slider< int >;
template class list_slider< std::string >;
} //end namespace gui