forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ez_controls.c
3072 lines (2619 loc) · 90.9 KB
/
ez_controls.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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_controls.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_controls.h"
#ifdef _MSC_VER
#pragma warning( disable : 4189 )
#endif
// =========================================================================================
// Double Linked List
// =========================================================================================
//
// Add item to double linked list.
//
void EZ_double_linked_list_Add(ez_double_linked_list_t *list, void *payload)
{
ez_dllist_node_t *item = (ez_dllist_node_t *)Q_malloc(sizeof(ez_dllist_node_t));
memset(item, 0, sizeof(ez_dllist_node_t));
item->payload = payload;
item->next = NULL;
// Add the item to the start of the list if it's empty
// otherwise add it to the tail.
if(!list->head)
{
list->head = item;
item->next = NULL;
item->previous = NULL;
}
else
{
item->previous = list->tail;
}
// Make sure the previous item knows who we are.
if(item->previous)
{
item->previous->next = item;
}
list->tail = item;
list->count++;
}
//
// Finds a given node based on the specified payload.
//
ez_dllist_node_t *EZ_double_linked_list_FindByPayload(const ez_double_linked_list_t *list, const void *payload)
{
ez_dllist_node_t *iter = list->head;
while(iter)
{
if(iter->payload == payload)
{
return iter;
}
iter = iter->next;
}
return NULL;
}
//
// Double Linked List - Find a item by its index.
//
ez_dllist_node_t *EZ_double_linked_list_FindByIndex(const ez_double_linked_list_t *list, int index)
{
int i;
ez_dllist_node_t *iter = NULL;
qbool fromStart = (list->count - index) >= index;
if (fromStart)
{
iter = list->head;
for (i = 0; i < index; i++)
{
iter = iter->next;
}
}
else
{
iter = list->tail;
for (i = list->count - 1; i >= index; i--)
{
iter = iter->previous;
}
}
return iter;
}
//
// Double Linked List - Removes the first occurance of the item from double linked list and returns its payload.
//
void *EZ_double_linked_list_Remove(ez_double_linked_list_t *list, ez_dllist_node_t *item)
{
void *payload = item->payload;
if (item->previous)
{
item->previous->next = item->next;
}
else
{
// We removed the first item, so make sure we still have a head.
list->head = item->next;
}
if (item->next)
{
item->next->previous = item->previous;
}
else if (item->previous)
{
// We removed the last item, so make sure we have a tail.
list->tail = item->previous;
}
list->count--;
Q_free(item);
return payload; // WARNING THIS MUST BE FREED BY THE CALLER!
}
//
// Double Linked List - Removes an item from a linked list by its index.
//
void *EZ_double_linked_list_RemoveByIndex(ez_double_linked_list_t *list, int index)
{
ez_dllist_node_t *node;
if ((index < 0) || (index >= list->count))
{
return NULL;
}
node = EZ_double_linked_list_FindByIndex(list, index);
return EZ_double_linked_list_Remove(list, node);
}
//
// Double Linked List - Removes a range of items in the list.
// A cleanup function needs to be supplied so that the payload of the item is also cleaned up.
//
void EZ_double_linked_list_RemoveRange(ez_double_linked_list_t *list, int start, int end, ez_removerange_cleanup_function_t func)
{
int i;
ez_dllist_node_t *iter = list->head;
ez_dllist_node_t *tmp;
if (!iter || (start < 0) || (end < 0) || (start >= list->count) || (end >= list->count) || (start > end))
{
return;
}
// Find the start index.
for (i = 0; i <= start; i++)
{
iter = iter->next;
}
// Remove all items until the end.
for (i = start; i <= end; i++)
{
tmp = iter;
// Run the cleanup function on the current
func(EZ_double_linked_list_Remove(list, iter));
iter = tmp->next;
}
}
//
// Double Linked List - Removes an item from a linked list by its payload.
//
void *EZ_double_linked_list_RemoveByPayload(ez_double_linked_list_t *list, void *payload)
{
ez_dllist_node_t *node = EZ_double_linked_list_FindByPayload(list, payload);
return EZ_double_linked_list_Remove(list, node);
}
typedef void * PVOID;
//
// Double Linked List - Orders a list.
//
void EZ_double_linked_list_Sort(ez_double_linked_list_t *list, PtFuncCompare compare_function)
{
int i = 0;
ez_dllist_node_t *iter = NULL;
PVOID **items = (PVOID **)Q_calloc(list->count, sizeof(PVOID *));
iter = list->head;
while(iter)
{
items[i] = iter->payload;
i++;
iter = iter->next;
}
qsort(items, list->count, sizeof(PVOID *), compare_function);
iter = list->head;
for(i = 0; i < list->count; i++)
{
iter->payload = items[i];
iter = iter->next;
}
Q_free(items);
}
// =========================================================================================
// Control Tree
// =========================================================================================
//
// Control tree -
// Sets the drawing bounds for a control and then calls the function
// recursivly on all it's children. These bounds are used to restrict the drawing
// of all children that should be contained within it's parent, and their children
// to within the bounds of the parent.
//
static void EZ_tree_SetDrawBounds(ez_control_t *control)
{
ez_dllist_node_t *iter = NULL;
ez_control_t *child = NULL;
ez_control_t *p = control->parent;
qbool contained = control->ext_flags & control_contained;
// Calculate the controls bounds.
int top = control->absolute_y;
int bottom = control->absolute_y + control->height;
int left = control->absolute_x;
int right = control->absolute_x + control->width;
int p_bound_top = p ? p->bound_top : 0;
int p_bound_bottom = p ? p->bound_bottom : 0;
int p_bound_left = p ? p->bound_left : 0;
int p_bound_right = p ? p->bound_right : 0;
// Change the bounds so that we don't draw over our parents resize handles.
if (p)
{
if (p->ext_flags & control_resize_h)
{
p_bound_right -= p->resize_handle_thickness;
p_bound_left += p->resize_handle_thickness;
}
if (p->ext_flags & control_resize_v)
{
p_bound_bottom -= p->resize_handle_thickness;
p_bound_top += p->resize_handle_thickness;
}
}
// If the control has a parent (and should be contained within it's parent),
// set the corresponding bound to the parents bound (ex. button),
// otherwise use the drawing area of the control as bounds (ex. windows).
control->bound_top = (p && contained && (top < p_bound_top)) ? (p_bound_top) : top;
control->bound_bottom = (p && contained && (bottom > p_bound_bottom)) ? (p_bound_bottom) : bottom;
control->bound_left = (p && contained && (left < p_bound_left)) ? (p_bound_left) : left;
control->bound_right = (p && contained && (right > p_bound_right)) ? (p_bound_right) : right;
// Make sure that the left bounds isn't greater than the right bounds and so on.
// This would lead to controls being visible in a few incorrect cases.
clamp(control->bound_top, 0, max(0, control->bound_bottom));
clamp(control->bound_bottom, control->bound_top, vid.conheight);
clamp(control->bound_left, 0, max(0, control->bound_right));
clamp(control->bound_right, control->bound_left, vid.conwidth);
// Calculate the bounds for the children.
for (iter = control->children.head; iter; iter = iter->next)
{
child = (ez_control_t *)iter->payload;
// TODO : Probably should some better check for infinte loop here also.
if (child == control)
{
Sys_Error("EZ_tree_SetDrawBounds(): Infinite loop, child is its own parent.\n");
}
EZ_tree_SetDrawBounds(child);
}
}
//
// Control Tree - Draws a control tree.
//
static void EZ_tree_Draw(ez_tree_t *tree)
{
ez_control_t *payload = NULL;
ez_dllist_node_t *iter = tree->drawlist.head;
while (iter)
{
payload = (ez_control_t *)iter->payload;
// TODO : Remove this test stuff.
/*
Draw_AlphaRectangleRGB(
payload->absolute_virtual_x,
payload->absolute_virtual_y,
payload->virtual_width,
payload->virtual_height,
1, false, RGBA_TO_COLOR(255, 0, 0, 125));
*/
// Don't draw the invisible controls.
if (!(payload->ext_flags & control_visible) || (payload->int_flags & control_hidden_by_parent))
{
iter = iter->next;
continue;
}
// TODO : Remove this test stuff.
/*
if (!strcmp(payload->name, "Close button"))
{
Draw_String(payload->absolute_virtual_x, payload->absolute_virtual_y - 10,
va("x: %i y: %i", payload->x, payload->y));
}
*/
/*
if (!strcmp(payload->name, "label"))
{
Draw_String(payload->absolute_virtual_x, payload->absolute_virtual_y - 10,
va("avx: %i avy: %i vx: %i vy %i",
payload->absolute_virtual_x, payload->absolute_virtual_y, payload->virtual_x, payload->virtual_y));
}
if (!strcasecmp(payload->name, "Child 1"))
{
Draw_String(payload->absolute_virtual_x, payload->absolute_virtual_y - 10,
va("vw: %i vh: %i w: %i h %i",
payload->virtual_width, payload->virtual_height, payload->width, payload->height));
}
*/
/*
if (!strcasecmp(payload->name, "button"))
{
Draw_AlphaLineRGB(payload->bound_left, 0, payload->bound_left, vid.conheight, 1, RGBA_TO_COLOR(255, 0, 0, 255));
Draw_AlphaLineRGB(payload->bound_right, 0, payload->bound_right, vid.conheight, 1, RGBA_TO_COLOR(255, 0, 0, 255));
}
*/
/*
if (!strcasecmp(payload->name, "Vertical scrollbar"))
{
Draw_String(payload->absolute_virtual_x, payload->absolute_virtual_y - 10,
va("b: %i r: %i", payload->bottom_edge_gap, payload->right_edge_gap));
}
*/
/*
if (!strcmp(payload->name, "Close button") || !strcmp(payload->name, "Window titlebar"))
{
Draw_AlphaRectangleRGB(
payload->absolute_virtual_x,
payload->absolute_virtual_y,
payload->virtual_width,
payload->virtual_height,
1, false, RGBA_TO_COLOR(0, 0, 255, 125));
}
*/
// Bugfix: Make sure we don't even bother trying to draw something that is completly offscreen
// it will cause a weird flickering bug because of glScissor.
if ((payload->bound_left == payload->bound_right) || (payload->bound_top == payload->bound_bottom))
{
iter = iter->next;
continue;
}
// Make sure we keep raising Mouse hover events while inside the control.
if (POINT_IN_CONTROL_DRAWBOUNDS(payload, payload->prev_mouse_state.x, payload->prev_mouse_state.y))
{
CONTROL_RAISE_EVENT(NULL, payload, ez_control_t, OnMouseHover, &payload->prev_mouse_state);
}
// Make sure that the control draws only within its bounds.
Draw_EnableScissor(payload->bound_left, payload->bound_right, payload->bound_top, payload->bound_bottom);
Draw_SetOverallAlpha(payload->overall_opacity);
// Raise the draw event for the control.
CONTROL_RAISE_EVENT(NULL, payload, ez_control_t, OnDraw, NULL);
// Reset the drawing bounds to the entire screen after the control has been drawn.
Draw_DisableScissor();
Draw_SetOverallAlpha(1.0);
iter = iter->next;
}
}
//
// Control Tree - Checks how long mouse buttons have been pressed.
//
static void EZ_tree_RaiseRepeatedMouseButtonEvents(ez_tree_t *tree)
{
if (tree->focused_node && tree->focused_node->payload)
{
ez_control_t *control = (ez_control_t *)tree->focused_node->payload;
// Notify controls that are listening to repeat mouse events
// if the control's set delay has been reached.
if (control->ext_flags & control_listen_repeat_mouse)
{
int i;
double now = Sys_DoubleTime();
mouse_state_t ms = tree->prev_mouse_state;
ms.button_up = 0;
// Go through all the mouse buttons and compare the time since
// the mouse buttons where pressed with the controls delay time.
for (i = 1; i <= 8; i++)
{
// Only repeat the mouse click if the button is already down
// and the repeat isn't set to be all the time.
if (ms.buttons[i]
&& (control->mouse_repeat_delay > 0.0) && (tree->mouse_pressed_time[i] > 0.0)
&& ((now - tree->mouse_pressed_time[i]) >= control->mouse_repeat_delay))
{
ms.button_down = i;
CONTROL_RAISE_EVENT(NULL, control, ez_control_t, OnMouseEvent, &ms);
}
}
}
}
}
//
// Control Tree - Needs to be called every frame to keep the tree alive.
//
void EZ_tree_EventLoop(ez_tree_t *tree)
{
if (!tree)
{
return;
}
// We wait with destroying until after all events have run.
if (tree->destroying)
{
EZ_control_Destroy(tree->root, true);
memset(tree, 0, sizeof(ez_tree_t));
return;
}
if (!tree->root)
{
return;
}
// Check if it's time to raise any repeat mouse events for controls listening to those.
EZ_tree_RaiseRepeatedMouseButtonEvents(tree);
// Calculate the drawing bounds for all the controls in the control tree.
EZ_tree_SetDrawBounds(tree->root);
EZ_tree_Draw(tree);
}
//
// Control Tree - Dispatches a mouse event to a control tree.
//
qbool EZ_tree_MouseEvent(ez_tree_t *tree, mouse_state_t *ms)
{
int mouse_handled = false;
ez_control_t *control = NULL;
ez_dllist_node_t *iter = NULL;
if (!tree)
{
Sys_Error("EZ_tree_MouseEvent: NULL tree reference.\n");
}
// Save the time that the specified button was last pressed.
if (ms->button_down || ms->button_up)
{
// Set the time for the pressed button.
if (ms->button_down)
{
tree->mouse_pressed_time[ms->button_down] = Sys_DoubleTime();
}
else if (ms->button_up)
{
tree->mouse_pressed_time[ms->button_up] = 0.0;
}
}
// Save the mouse state so that it can be used when raising
// repeated mouse click events for controls that wants them.
tree->prev_mouse_state = *ms;
// Propagate the mouse event in the opposite order that we drew
// the controls (Since they are drawn from back to front), so
// that the foremost control gets it first.
for (iter = tree->drawlist.tail; iter; iter = iter->previous)
{
control = (ez_control_t *)iter->payload;
// Notify the control of the mouse event.
CONTROL_RAISE_EVENT(&mouse_handled, control, ez_control_t, OnMouseEvent, ms);
if (mouse_handled)
{
return mouse_handled;
}
}
return mouse_handled;
}
//
// Control Tree - Refreshes the position of all controls in the tree.
//
void EZ_tree_Refresh(ez_tree_t *tree)
{
if (tree->root)
{
ez_dllist_node_t *iter = tree->drawlist.head;
ez_control_t *payload = NULL;
//CONTROL_RAISE_EVENT(NULL, (ez_control_t *)tree->root, ez_control_t, OnResize, NULL);
//CONTROL_RAISE_EVENT(NULL, (ez_control_t *)tree->root, ez_control_t, OnMove, NULL);
while (iter)
{
payload = (ez_control_t *)iter->payload;
CONTROL_RAISE_EVENT(NULL, payload, ez_control_t, OnResize, NULL);
CONTROL_RAISE_EVENT(NULL, payload, ez_control_t, OnMove, NULL);
CONTROL_RAISE_EVENT(NULL, payload, ez_control_t, OnAnchorChanged, NULL);
iter = iter->next;
}
}
}
//
// Control Tree - Moves the focus to the next control in the control tree.
//
void EZ_tree_ChangeFocus(ez_tree_t *tree, qbool next_control)
{
qbool found = false;
ez_dllist_node_t *node_iter = NULL;
if(tree->focused_node)
{
node_iter = (next_control) ? tree->focused_node->next : tree->focused_node->previous;
// Find the next control that can be focused.
while(node_iter && !found)
{
// ez_control_t *ha = (ez_control_t *)node_iter->payload;
found = EZ_control_SetFocusByNode((ez_control_t *)node_iter->payload, node_iter);
node_iter = (next_control) ? node_iter->next : node_iter->previous;
}
}
// We haven't found a focusable control yet,
// or there was no focused control to start with.
// So search for one from the start/end of the tab list
// (depending on what direction we're searching in)
if(!found || !tree->focused_node)
{
node_iter = (next_control) ? tree->tablist.head : tree->tablist.tail;
// Find the next control that can be focused.
while(node_iter && !found)
{
found = EZ_control_SetFocusByNode((ez_control_t *)node_iter->payload, node_iter);
node_iter = (next_control) ? node_iter->next : node_iter->previous;
}
}
// There is nothing to focus on.
if(!found)
{
tree->focused_node = NULL;
}
}
//
// Control Tree - Key event.
//
qbool EZ_tree_KeyEvent(ez_tree_t *tree, int key, int unichar, qbool down)
{
int key_handled = false;
// ez_control_t *payload = NULL;
// ez_dllist_node_t *iter = NULL;
if (!tree)
{
Sys_Error("EZ_tree_KeyEvent(): NULL control tree specified.\n");
}
if (tree->root && down)
{
switch (key)
{
case 'k' :
key_handled = true;
break;
case K_TAB :
{
// Focus on the next focusable control (TAB)
// or the previous (Shift + TAB)
EZ_tree_ChangeFocus(tree, !keydown[K_SHIFT]);
key_handled = true;
break;
}
}
}
// Send key events to the focused control.
if (tree->focused_node && tree->focused_node->payload)
{
CONTROL_RAISE_EVENT(&key_handled, (ez_control_t *)tree->focused_node->payload, ez_control_t, OnKeyEvent, key, unichar, down);
}
return key_handled;
}
//
// Control Tree - Finds any orphans and adds them to the root control.
//
void EZ_tree_UnOrphanizeChildren(ez_tree_t *tree)
{
ez_control_t *payload = NULL;
ez_dllist_node_t *iter = NULL;
if(!tree)
{
assert(!"EZ_control_UnOrphanizeChildren: No control tree specified.\n");
return;
}
iter = tree->drawlist.head;
while(iter)
{
payload = (ez_control_t *)iter->payload;
if(!payload->parent && payload != tree->root)
{
// The control is an orphan, and not the root control.
EZ_double_linked_list_Add(&tree->root->children, payload);
}
else if(!tree->root)
{
// There was no root (should never happen).
tree->root = payload;
}
iter = iter->next;
}
}
//
// Control Tree - Destroys a tree. Will not free the memory for the tree.
//
void EZ_tree_Destroy(ez_tree_t *tree)
{
if (!tree)
{
return;
}
// Make sure we abort all events before we destroy the tree
// or they might try to read from freed memory.
// The actual destruction is done in EZ_tree_EventLoop.
tree->destroying = true;
}
//
// Control Tree - Order function for the draw list based on the draw_order property.
//
static int EZ_tree_DrawOrderFunc(const void *val1, const void *val2)
{
const ez_control_t **control1 = (const ez_control_t **)val1;
const ez_control_t **control2 = (const ez_control_t **)val2;
return ((*control1)->draw_order - (*control2)->draw_order);
}
//
// Control Tree - Orders the draw list based on the draw order property.
//
void EZ_tree_OrderDrawList(ez_tree_t *tree)
{
EZ_double_linked_list_Sort(&tree->drawlist, EZ_tree_DrawOrderFunc);
}
//
// Control Tree - Tree Order function for the tab list based on the tab_order property.
//
static int EZ_tree_TabOrderFunc(const void *val1, const void *val2)
{
const ez_control_t **control1 = (const ez_control_t **)val1;
const ez_control_t **control2 = (const ez_control_t **)val2;
return ((*control1)->tab_order - (*control2)->tab_order);
}
//
// Control Tree - Orders the tab list based on the tab order property.
//
void EZ_tree_OrderTabList(ez_tree_t *tree)
{
EZ_double_linked_list_Sort(&tree->tablist, EZ_tree_TabOrderFunc);
}
// =========================================================================================
// Control
// =========================================================================================
//
// Control - Creates a new control and initializes it.
//
ez_control_t *EZ_control_Create(ez_tree_t *tree, ez_control_t *parent,
char *name, char *description,
int x, int y, int width, int height,
int flags)
{
ez_control_t *control = NULL;
// We have to have a tree to add the control to.
if(!tree || tree->destroying)
{
return NULL;
}
control = (ez_control_t *)Q_malloc(sizeof(ez_control_t));
memset(control, 0, sizeof(ez_control_t));
EZ_control_Init(control, tree, parent, name, description, x, y, width, height, flags);
return control;
}
//
// Control -
// Returns the screen position of the control. This will be different for a scrollable window
// since it's drawing position differs from the windows actual position on screen.
//
void EZ_control_GetDrawingPosition(ez_control_t *self, int *x, int *y)
{
if (self->ext_flags & control_scrollable)
{
(*x) = self->absolute_virtual_x;
(*y) = self->absolute_virtual_y;
}
else
{
(*x) = self->absolute_x;
(*y) = self->absolute_y;
}
}
//
// Eventhandler - Creates a eventhandler.
//
ez_eventhandler_t *EZ_eventhandler_Create(void *event_func, int func_type, void *payload)
{
ez_eventhandler_t *e = Q_calloc(1, sizeof(ez_eventhandler_t));
e->function_type = func_type;
e->payload = payload;
switch(func_type)
{
case EZ_CONTROL_HANDLER :
e->function.normal = (ez_eventhandler_fp)event_func;
break;
case EZ_CONTROL_MOUSE_HANDLER :
e->function.mouse = (ez_mouse_eventhandler_fp)event_func;
break;
case EZ_CONTROL_KEY_HANDLER :
e->function.key = (ez_key_eventhandler_fp)event_func;
break;
case EZ_CONTROL_KEYSP_HANDLER :
e->function.key_sp = (ez_keyspecific_eventhandler_fp)event_func;
break;
case EZ_CONTROL_DESTROY_HANDLER :
e->function.destroy = (ez_destroy_eventhandler_fp)event_func;
break;
case EZ_CONTROL_CHILD_HANDLER :
e->function.child = (ez_child_eventhandler_event_fp)event_func;
break;
default :
e->function.normal = NULL;
break;
}
e->next = NULL;
return e;
}
//
// Eventhandler - Goes through the list of events and removes the one with the specified function.
//
void EZ_eventhandler_Remove(ez_eventhandler_t *eventhandler, void *event_func, qbool all)
{
ez_eventhandler_t *it = eventhandler;
ez_eventhandler_t *prev = NULL;
while (it)
{
if (all)
{
prev = it->next;
Q_free(it);
it = prev;
}
else
{
if (event_func && ((void *)it->function.normal == (void *)event_func))
{
if (prev)
{
prev->next = it->next;
}
Q_free(it);
return;
}
prev = it;
it = it->next;
}
}
}
//
// Eventhandler - Execute an event handler.
//
void EZ_eventhandler_Exec(ez_eventhandler_t *event_handler, ez_control_t *ctrl, ...)
{
va_list argptr;
int ft = event_handler->function_type;
void *payload = event_handler->payload;
ez_eventhandlerfunction_t *et = &event_handler->function;
va_start(argptr, ctrl);
// Pass on the proper amount of arguments, depending on the type of function.
if (ft == EZ_CONTROL_HANDLER)
{
et->normal(ctrl, payload, va_arg(argptr, void *));
}
else if (ft == EZ_CONTROL_MOUSE_HANDLER)
{
et->mouse(ctrl, payload, va_arg(argptr, mouse_state_t *));
}
else if (ft == EZ_CONTROL_KEY_HANDLER)
{
et->key(ctrl, payload, va_arg(argptr, int), va_arg(argptr, int), va_arg(argptr, qbool));
}
else if (ft == EZ_CONTROL_KEYSP_HANDLER)
{
et->key_sp(ctrl, payload, va_arg(argptr, int), va_arg(argptr, int));
}
else if (ft == EZ_CONTROL_DESTROY_HANDLER)
{
et->destroy(ctrl, payload, va_arg(argptr, qbool));
}
else if (ft == EZ_CONTROL_CHILD_HANDLER)
{
et->child(ctrl, payload, va_arg(argptr, ez_control_t *));
}
va_end(argptr);
}
//
// Control - Initializes a control and adds it to the specified control tree.
//
void EZ_control_Init(ez_control_t *control, 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)
{
static int order = 0;
control->initializing = true;
control->CLASS_ID = EZ_CONTROL_ID;
control->control_tree = tree;
control->name = name;
control->description = description;
control->ext_flags = flags | control_enabled | control_visible | control_bg_tile_center | control_bg_tile_edges | control_resizeable;
control->anchor_flags = anchor_none;
// Set the default opacity to full :)
control->overall_opacity = 1.0;
control->opacity = 1.0;
// Default to containing a child within it's parent
// if the parent is being contained by it's parent.
if (parent && (parent->ext_flags & control_contained))
{
control->ext_flags |= control_contained;
}
control->draw_order = order++;
control->resize_handle_thickness = 5;
control->width_max = vid.conwidth;
control->height_max = vid.conheight;
control->width_min = 5;
control->height_min = 5;
// Setup the default event functions, none of these should ever be NULL.
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseEvent, OnMouseEvent, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseClick, OnMouseClick, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseDown, OnMouseDown, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseUp, OnMouseUp, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseUpOutside, OnMouseUpOutside, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseEnter, OnMouseEnter, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseLeave, OnMouseLeave, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMouseHover, OnMouseHover, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_Destroy, OnDestroy, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnDraw, OnDraw, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnGotFocus, OnGotFocus, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnKeyEvent, OnKeyEvent, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnKeyDown, OnKeyDown, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnKeyUp, OnKeyUp, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnLostFocus, OnLostFocus, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnLayoutChildren, OnLayoutChildren, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMove, OnMove, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnScroll, OnScroll, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnParentScroll, OnParentScroll, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnResize, OnResize, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnParentResize, OnParentResize, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnMinVirtualResize, OnMinVirtualResize, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnVirtualResize, OnVirtualResize, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnFlagsChanged, OnFlagsChanged, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnEventHandlerChanged, OnEventHandlerChanged, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnAnchorChanged, OnAnchorChanged, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnVisibilityChanged, OnVisibilityChanged, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnOpacityChanged, OnOpacityChanged, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnChildMoved, OnChildMoved, ez_control_t);
CONTROL_REGISTER_EVENT(control, EZ_control_OnChildResize, OnChildResize, ez_control_t);
// Add the control to the draw and tab list.
EZ_double_linked_list_Add(&tree->drawlist, (void *)control);
EZ_double_linked_list_Add(&tree->tablist, (void *)control);
// Order the lists.
EZ_tree_OrderDrawList(tree);
EZ_tree_OrderTabList(tree);
// Position and size of the control.
control->width = width;
control->height = height;
control->virtual_width = width;
control->virtual_height = height;
control->prev_width = width;
control->prev_height = height;