-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathez_slider.c
469 lines (376 loc) · 13.3 KB
/
ez_slider.c
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
Copyright (C) 2007 ezQuake team
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; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: ez_scrollpane.c,v 1.78 2007/10/27 14:51:15 cokeman1982 Exp $
*/
#include "quakedef.h"
#include "keys.h"
#include "utils.h"
#include "common_draw.h"
#include "ez_slider.h"
// =========================================================================================
// Slider
// =========================================================================================
// TODO : Slider - Show somehow that it's focused?
//
// Slider - Creates a new button and initializes it.
//
ez_slider_t *EZ_slider_Create(ez_tree_t *tree, ez_control_t *parent,
char *name, char *description,
int x, int y, int width, int height,
ez_control_flags_t flags)
{
ez_slider_t *slider = NULL;
// We have to have a tree to add the control to.
if (!tree)
{
return NULL;
}
slider = (ez_slider_t *)Q_malloc(sizeof(ez_slider_t));
memset(slider, 0, sizeof(ez_slider_t));
EZ_slider_Init(slider, tree, parent, name, description, x, y, width, height, flags);
return slider;
}
//
// Slider - Initializes a button.
//
void EZ_slider_Init(ez_slider_t *slider, ez_tree_t *tree, ez_control_t *parent,
char *name, char *description,
int x, int y, int width, int height,
ez_control_flags_t flags)
{
height = max(height, 8);
// Initialize the inherited class first.
EZ_control_Init(&slider->super, tree, parent, name, description, x, y, width, height, flags);
// Initilize the button specific stuff.
((ez_control_t *)slider)->CLASS_ID = EZ_SLIDER_ID;
((ez_control_t *)slider)->ext_flags |= (flags | control_focusable | control_contained | control_resizeable);
// Overrided control events.
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnDraw, OnDraw, ez_control_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnMouseDown, OnMouseDown, ez_control_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnMouseUpOutside, OnMouseUpOutside, ez_control_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnMouseEvent, OnMouseEvent, ez_control_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnResize, OnResize, ez_control_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnKeyDown, OnKeyDown, ez_control_t);
// Slider specific events.
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnSliderPositionChanged, OnSliderPositionChanged, ez_slider_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnMaxValueChanged, OnMaxValueChanged, ez_slider_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnMinValueChanged, OnMinValueChanged, ez_slider_t);
CONTROL_REGISTER_EVENT(slider, EZ_slider_OnScaleChanged, OnScaleChanged, ez_slider_t);
// Set default values.
EZ_slider_SetMax(slider, 100);
EZ_slider_SetScale(slider, 1.0);
EZ_slider_SetPosition(slider, 0);
}
//
// Slider - Calculates the actual slider position.
//
__inline void EZ_slider_CalculateRealSliderPos(ez_slider_t *slider)
{
int pos = slider->slider_pos - slider->min_value;
// Calculate the real position of the slider by multiplying by the gap size between each value.
// (Don't start drawing at the exact start cause that would overwrite the edge marker)
slider->real_slider_pos = Q_rint((slider->scaled_char_size / 2.0) + (pos * slider->gap_size));
}
//
// Slider - Calculates the gap size between values.
//
__inline void EZ_slider_CalculateGapSize(ez_slider_t *slider)
{
slider->range = abs(slider->max_value - slider->min_value);
// Calculate the gap between each value in pixels (floating point so that we don't get rounding errors).
// Don't include the first and last characters in the calculation, the slider is not allowed to move over those.
slider->gap_size = ((float)(((ez_control_t *)slider)->width - (2 * slider->scaled_char_size)) / (float)slider->range);
}
//
// Slider - Event handler for OnSliderPositionChanged.
//
void EZ_slider_AddOnSliderPositionChanged(ez_slider_t *slider, ez_eventhandler_fp OnSliderPositionChanged, void *payload)
{
CONTROL_ADD_EVENTHANDLER(slider, EZ_CONTROL_HANDLER, OnSliderPositionChanged, ez_slider_t, OnSliderPositionChanged, payload);
CONTROL_RAISE_EVENT(NULL, slider, ez_control_t, OnEventHandlerChanged, NULL);
}
//
// Slider - Event handler for OnMaxValueChanged.
//
void EZ_slider_AddOnMaxValueChanged(ez_slider_t *slider, ez_eventhandler_fp OnMaxValueChanged, void *payload)
{
CONTROL_ADD_EVENTHANDLER(slider, EZ_CONTROL_HANDLER, OnMaxValueChanged, ez_slider_t, OnMaxValueChanged, payload);
CONTROL_RAISE_EVENT(NULL, slider, ez_control_t, OnEventHandlerChanged, NULL);
}
//
// Slider - Event handler for OnMinValueChanged.
//
void EZ_slider_AddOnMinValueChanged(ez_slider_t *slider, ez_eventhandler_fp OnMinValueChanged, void *payload)
{
CONTROL_ADD_EVENTHANDLER(slider, EZ_CONTROL_HANDLER, OnMinValueChanged, ez_slider_t, OnMinValueChanged, payload);
CONTROL_RAISE_EVENT(NULL, slider, ez_control_t, OnEventHandlerChanged, NULL);
}
//
// Slider - Event handler for OnSliderPositionChanged.
//
void EZ_slider_AddOnScaleChanged(ez_slider_t *slider, ez_eventhandler_fp OnScaleChanged, void *payload)
{
CONTROL_ADD_EVENTHANDLER(slider, EZ_CONTROL_HANDLER, OnScaleChanged, ez_slider_t, OnScaleChanged, payload);
CONTROL_RAISE_EVENT(NULL, slider, ez_control_t, OnEventHandlerChanged, NULL);
}
//
// Slider - Gets the current position of the mouse in slider scale. (This does not care if the mouse is within the control)
//
int EZ_slider_GetPositionFromMouse(ez_slider_t *slider, float mouse_x, float mouse_y)
{
ez_control_t *self = (ez_control_t *)slider;
int x = Q_rint(self->absolute_virtual_x + (slider->scaled_char_size / 2.0));
return Q_rint((mouse_x - x) / slider->gap_size);
}
//
// Slider - Get the current slider position.
//
int EZ_slider_GetPosition(ez_slider_t *slider)
{
return slider->slider_pos;
}
//
// Slider - Set the slider position.
//
void EZ_slider_SetPosition(ez_slider_t *slider, int slider_pos)
{
clamp(slider_pos, slider->min_value, slider->max_value);
slider->slider_pos = slider_pos;
CONTROL_RAISE_EVENT(NULL, slider, ez_slider_t, OnSliderPositionChanged, NULL);
}
//
// Slider - Set the max slider value.
//
void EZ_slider_SetMax(ez_slider_t *slider, int max_value)
{
slider->max_value = max_value;
CONTROL_RAISE_EVENT(NULL, slider, ez_slider_t, OnMaxValueChanged, NULL);
}
//
// Slider - Set the max slider value.
//
void EZ_slider_SetMin(ez_slider_t *slider, int min_value)
{
slider->min_value = min_value;
CONTROL_RAISE_EVENT(NULL, slider, ez_slider_t, OnMinValueChanged, NULL);
}
//
// Slider - Set the scale of the slider characters.
//
void EZ_slider_SetScale(ez_slider_t *slider, float scale)
{
slider->scale = max(0.1, scale);
CONTROL_RAISE_EVENT(NULL, slider, ez_slider_t, OnScaleChanged, NULL);
}
//
// Slider - Scale changed.
//
int EZ_slider_OnScaleChanged(ez_control_t *self, void *ext_event_info)
{
ez_slider_t *slider = (ez_slider_t *)self;
// Calculate the new character size.
slider->scaled_char_size = slider->scale * 8;
// Refit the control to fit the slider.
EZ_control_SetSize(self, slider->super.width + slider->scaled_char_size, slider->scaled_char_size);
CONTROL_EVENT_HANDLER_CALL(NULL, slider, ez_slider_t, OnScaleChanged, NULL);
return 0;
}
//
// Slider - Draw function for the slider.
//
int EZ_slider_OnDraw(ez_control_t *self, void *ext_event_info)
{
int x, y, i;
ez_slider_t *slider = (ez_slider_t *)self;
EZ_control_GetDrawingPosition(self, &x, &y);
// Draw the background.
{
// Left edge.
Draw_SCharacter(x, y, 128, slider->scale);
for (i = 1; i < Q_rint((float)(self->width - slider->scaled_char_size) / slider->scaled_char_size); i++)
{
Draw_SCharacter(x + (i * slider->scaled_char_size), y, 129, slider->scale);
}
// Right edge.
Draw_SCharacter(x + (i * slider->scaled_char_size), y, 130, slider->scale);
}
// Slider.
Draw_SCharacter(x + slider->real_slider_pos, y, 131, slider->scale);
return 0;
}
//
// Slider - The max value changed.
//
int EZ_slider_OnMaxValueChanged(ez_control_t *self, void *ext_event_info)
{
ez_slider_t *slider = (ez_slider_t *)self;
// Only allow positive values greater than the min value.
slider->max_value = max(0, max(slider->max_value, slider->min_value));
// Calculate the gap between each slider value.
EZ_slider_CalculateGapSize(slider);
CONTROL_EVENT_HANDLER_CALL(NULL, slider, ez_slider_t, OnMaxValueChanged, NULL);
return 0;
}
//
// Slider - The min value changed.
//
int EZ_slider_OnMinValueChanged(ez_control_t *self, void *ext_event_info)
{
ez_slider_t *slider = (ez_slider_t *)self;
// Only allow positive values less than the max value.
slider->min_value = max(0, min(slider->max_value, slider->min_value));
// Calculate the gap between each slider value.
EZ_slider_CalculateGapSize(slider);
CONTROL_EVENT_HANDLER_CALL(NULL, slider, ez_slider_t, OnMinValueChanged, NULL);
return 0;
}
//
// Slider - The slider position changed.
//
int EZ_slider_OnSliderPositionChanged(ez_control_t *self, void *ext_event_info)
{
ez_slider_t *slider = (ez_slider_t *)self;
// Recalculate the drawing position.
EZ_slider_CalculateRealSliderPos(slider);
CONTROL_EVENT_HANDLER_CALL(NULL, slider, ez_slider_t, OnSliderPositionChanged, NULL);
return 0;
}
//
// Slider - Jumps to the point that was clicked on the slider
// (Normal behavior is to make a "big step" when clicking the slider outside the handle).
//
void EZ_slider_SetJumpToClick(ez_slider_t *slider, qbool jump_to_click)
{
SET_FLAG(slider->ext_flags, slider_jump_to_click, jump_to_click);
CONTROL_RAISE_EVENT(NULL, (ez_control_t *)slider, ez_control_t, OnFlagsChanged, NULL);
}
//
// Slider - Mouse down event.
//
int EZ_slider_OnMouseDown(ez_control_t *self, mouse_state_t *ms)
{
ez_slider_t *slider = (ez_slider_t *)self;
int big_step = max(1, slider->range / 10);
int slider_left_edge;
int slider_right_edge;
int x, y;
EZ_control_GetDrawingPosition(self, &x, &y);
slider_left_edge = (x + slider->real_slider_pos);
slider_right_edge = (x + slider->real_slider_pos + slider->scaled_char_size);
// Super class.
EZ_control_OnMouseDown(self, ms);
if ((ms->x >= slider_left_edge) && (ms->x <= slider_right_edge))
{
slider->int_flags |= slider_dragging;
}
else if (slider->ext_flags & slider_jump_to_click)
{
// Jump to the exact position on the slider that was clicked.
int newpos = EZ_slider_GetPositionFromMouse(slider, ms->x, ms->y);
EZ_slider_SetPosition(slider, newpos);
}
else if (ms->x < slider_left_edge)
{
// When clicking on the slider (but not the slider handle)
// make a "big step" instead of dragging it.
EZ_slider_SetPosition(slider, slider->slider_pos - big_step);
}
else if (ms->x > slider_right_edge)
{
EZ_slider_SetPosition(slider, slider->slider_pos + big_step);
}
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnMouseDown, ms);
return true;
}
//
// Slider - Mouse up event.
//
int EZ_slider_OnMouseUpOutside(ez_control_t *self, mouse_state_t *ms)
{
ez_slider_t *slider = (ez_slider_t *)self;
// Super class.
EZ_control_OnMouseUpOutside(self, ms);
// Not dragging anymore.
slider->int_flags &= ~slider_dragging;
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnMouseUp, ms);
return true;
}
//
// Slider - Handles a mouse event.
//
int EZ_slider_OnMouseEvent(ez_control_t *self, mouse_state_t *ms)
{
// Make sure we handle all mouse events when we're over the control
// otherwise they will fall through to controls below.
int mouse_handled = POINT_IN_CONTROL_RECT(self, ms->x, ms->y);
ez_slider_t *slider = (ez_slider_t *)self;
// Call the super class first.
EZ_control_OnMouseEvent(self, ms);
if (slider->int_flags & slider_dragging)
{
int new_slider_pos = slider->min_value + Q_rint((ms->x - self->absolute_x) / slider->gap_size);
EZ_slider_SetPosition(slider, new_slider_pos);
mouse_handled = true;
}
// Event handler call.
{
int mouse_handled_tmp = false;
CONTROL_EVENT_HANDLER_CALL(&mouse_handled_tmp, self, ez_control_t, OnMouseEvent, ms);
mouse_handled = (mouse_handled || mouse_handled_tmp);
}
return mouse_handled;
}
//
// Slider - The slider was resized.
//
int EZ_slider_OnResize(ez_control_t *self, void *ext_event_info)
{
ez_slider_t *slider = (ez_slider_t *)self;
// Call super class.
EZ_control_OnResize(self, NULL);
EZ_slider_CalculateGapSize(slider);
EZ_slider_CalculateRealSliderPos(slider);
CONTROL_EVENT_HANDLER_CALL(NULL, self, ez_control_t, OnResize, NULL);
return 0;
}
//
// Slider - Key event.
//
int EZ_slider_OnKeyDown(ez_control_t *self, int key, int unichar)
{
qbool key_handled = false;
ez_slider_t *slider = (ez_slider_t *)self;
int big_step = max(1, slider->max_value / 10);
int step = keydown[K_CTRL] ? big_step : 1;
switch(key)
{
case K_RIGHTARROW :
{
EZ_slider_SetPosition(slider, slider->slider_pos + step);
key_handled = true;
break;
}
case K_LEFTARROW :
{
EZ_slider_SetPosition(slider, slider->slider_pos - step);
key_handled = true;
break;
}
default :
{
break;
}
}
return key_handled;
}