/
ui_main.c
2553 lines (2240 loc) · 73.7 KB
/
ui_main.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
/**\file ui_main.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2012 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2012 Daniel Swanson <danij@dengine.net>
*
* 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., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Graphical User Interface
*
* Has ties to the console routines.
*/
// HEADER FILES ------------------------------------------------------------
#include <math.h>
#include "de_base.h"
#include "de_console.h"
#include "de_refresh.h"
#include "de_system.h"
#include "de_graphics.h"
#include "de_render.h"
#include "de_ui.h"
#include "de_misc.h"
#include "image.h"
#include "texturecontent.h"
#include "rend_console.h" // \todo Move Rend_ConsoleUpdateTitle somewhere more suitable.
// MACROS ------------------------------------------------------------------
#define SCROLL_TIME 3
#define UICURSORWIDTH 32
#define UICURSORHEIGHT 64
enum {
UITEX_MOUSE,
UITEX_CORNER,
UITEX_FILL,
UITEX_SHADE,
UITEX_HINT,
UITEX_LOGO,
UITEX_BACKGROUND,
NUM_UITEXTURES
};
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
D_CMD(UIColor);
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static int listItemHeight(uidata_list_t* listdata);
static int listButtonHeight(ui_object_t* ob);
static int listThumbPos(ui_object_t* ob);
static void strCpyLen(char* dest, const char* src, int maxWidth);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern int glMaxTexSize;
extern int gameDrawHUD;
extern boolean stopTime;
extern boolean tickUI;
extern boolean drawGame;
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static boolean uiActive = false; /// The user interface is active.
static boolean uiShowMouse = true;
static ui_page_t* uiCurrentPage = 0; /// Currently active page.
static int uiFontHgt; /// Height of the UI font.
static DGLuint uiTextures[NUM_UITEXTURES]; /// Cursor texture.
static int uiCX, uiCY; /// Cursor position.
static int uiRestCX, uiRestCY;
static uint uiRestStart; /// Start time of current resting.
static uint uiRestTime = TICSPERSEC / 2; /// 500 ms.
static int uiRestOffsetLimit = 2;
static int uiMoved; /// True if the mouse has been moved.
static float uiAlpha = 1.0; /// Main alpha for the entire UI.
static float uiTargetAlpha = 1.0; // Target alpha for the entire UI.
/// The game view should be drawn while the UI active.
static boolean uiDrawGame = false;
static float uiCursorWidthMul = 0.75;
static float uiCursorHeightMul = 0.75;
/// Modify these colors to change the look of the UI.
static ui_color_t ui_colors[NUM_UI_COLORS] = {
/* UIC_TEXT */ { .85f, .87f, 1 },
/* UIC_TITLE */ { 1, 1, 1 },
/* UIC_SHADOW */ { 0, 0, 0 },
/* UIC_BG_LIGHT */ { .18f, .18f, .22f },
/* UIC_BG_MEDIUM */ { .4f, .4f, .52f },
/* UIC_BG_DARK */ { .28f, .28f, .33f },
/* UIC_BRD_HI */ { 1, 1, 1 },
/* UIC_BRD_MED */ { 0, 0, 0 },
/* UIC_BRD_LOW */ { .25f, .25f, .55f },
/* UIC_HELP */ { .4f, .4f, .52f }
};
static boolean allowEscape; /// Allow the user to exit a ui page using the escape key.
// CODE --------------------------------------------------------------------
void UI_Register(void)
{
// Cvars
C_VAR_FLOAT("ui-cursor-width", &uiCursorWidthMul, 0, 0.5f, 1.5f);
C_VAR_FLOAT("ui-cursor-height", &uiCursorHeightMul, 0, 0.5f, 1.5f);
// Ccmds
C_CMD_FLAGS("uicolor", "sfff", UIColor, CMDF_NO_DEDICATED);
CP_Register();
Fonts_Register();
}
void UI_PageInit(boolean halttime, boolean tckui, boolean tckframe, boolean drwgame, boolean noescape)
{
if(uiActive)
return;
uiActive = true;
B_ActivateContext(B_ContextByName(UI_BINDING_CONTEXT_NAME), true);
// Restore full alpha.
uiAlpha = uiTargetAlpha = 1.0;
// Adjust the engine state
stopTime = halttime;
tickUI = tckui;
tickFrame = tckframe;
uiDrawGame = drawGame = drwgame;
// Advise the game not to draw any HUD displays
gameDrawHUD = false;
I_SetUIMouseMode(true);
// Change font.
FR_SetFont(fontVariable[FS_NORMAL]);
FR_LoadDefaultAttrib();
uiFontHgt = FR_SingleLineHeight("UI");
// Should the mouse cursor be visible?
uiShowMouse = !ArgExists("-nomouse");
// Allow use of the escape key to exit the ui?
allowEscape = !noescape;
// Init cursor to the center of the screen.
uiCX = theWindow->geometry.size.width / 2;
uiCY = theWindow->geometry.size.height / 2;
uiMoved = false;
}
void UI_End(void)
{
ddevent_t rel;
if(!uiActive)
return;
uiActive = false;
B_ActivateContext(B_ContextByName(UI_BINDING_CONTEXT_NAME), false);
// Restore full alpha.
uiAlpha = uiTargetAlpha = 1.0;
// Update the secondary title and the game status.
Rend_ConsoleUpdateTitle();
// Restore the engine state
tickFrame = true;
stopTime = false;
tickUI = false;
drawGame = true;
// Advise the game it can now draw HUD displays again.
gameDrawHUD = true;
I_SetUIMouseMode(false);
// Inform everybody that the shift key was (possibly) released while
// the UI was eating all the input events.
if(!shiftDown)
{
rel.device = IDEV_KEYBOARD;
rel.type = E_TOGGLE;
rel.toggle.state = ETOG_UP;
rel.toggle.id = DDKEY_RSHIFT;
DD_PostEvent(&rel);
}
}
boolean UI_IsActive(void)
{
return uiActive;
}
int UI_FontHeight(void)
{
return uiFontHgt;
}
ui_color_t* UI_Color(uint id)
{
if(id >= NUM_UI_COLORS)
return NULL;
return &ui_colors[id];
}
void UI_SetAlpha(float alpha)
{
// The UI's alpha will start moving towards this target value.
uiTargetAlpha = alpha;
}
float UI_Alpha(void)
{
return uiAlpha;
}
ui_page_t* UI_CurrentPage(void)
{
if(uiActive)
return uiCurrentPage;
return NULL;
}
void UI_LoadTextures(void)
{
const char* picNames[NUM_UITEXTURES] = {
"Mouse",
"BoxCorner",
"BoxFill",
"BoxShade",
"Hint",
"Logo",
"Background",
};
int i;
if(novideo) return;
for(i = 0; i < NUM_UITEXTURES; ++i)
{
image_t image;
if(uiTextures[i])
continue;
if(GL_LoadExtTexture(&image, picNames[i], (i == UITEX_BACKGROUND? LGM_GRAYSCALE : LGM_NORMAL)))
{ // Loaded successfully and converted accordingly.
// Upload the image to GL.
uiTextures[i] = GL_NewTextureWithParams2(
( image.pixelSize == 2 ? DGL_LUMINANCE_PLUS_A8 :
image.pixelSize == 3 ? DGL_RGB :
image.pixelSize == 4 ? DGL_RGBA : DGL_LUMINANCE ),
image.size.width, image.size.height, image.pixels,
TXCF_NO_COMPRESSION, 0, GL_LINEAR, GL_LINEAR,
0 /*no anisotropy*/, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
GL_DestroyImage(&image);
}
else
uiTextures[i] = 0;
}
}
void UI_ReleaseTextures(void)
{
glDeleteTextures(NUM_UITEXTURES, (const GLuint*) uiTextures);
memset(uiTextures, 0, sizeof(uiTextures));
}
void UI_DefaultFocus(ui_page_t* page)
{
ui_object_t* deffocus = NULL;
uint i;
for(i = 0; i < page->count; ++i)
{
page->_objects[i].flags &= ~UIF_FOCUS;
if(page->_objects[i].flags & UIF_DEFAULT)
deffocus = page->_objects + i;
}
if(deffocus)
{
page->focus = deffocus - page->_objects;
deffocus->flags |= UIF_FOCUS;
return;
}
// Find an object for focus.
for(i = 0; i < page->count; ++i)
{
if(!(page->_objects[i].flags & UIF_NO_FOCUS))
{
// Set default focus.
page->focus = i;
page->_objects[i].flags |= UIF_FOCUS;
break;
}
}
}
void UI_InitPage(ui_page_t* page, ui_object_t* objects)
{
ui_object_t meta;
uint i;
memset(&meta, 0, sizeof(meta));
memset(page, 0, sizeof(*page));
page->_objects = objects;
page->capture = -1; /// No capture.
page->focus = -1;
page->responder = UIPage_Responder;
page->drawer = UIPage_Drawer;
page->flags.showBackground = true; /// Draw background by default.
page->ticker = UIPage_Ticker;
page->count = UI_CountObjects(objects);
for(i = 0; i < page->count; ++i)
{
if(objects[i].type == UI_TEXT || objects[i].type == UI_BOX ||
objects[i].type == UI_META)
{
objects[i].flags |= UIF_NO_FOCUS;
}
// Reset timer.
objects[i].timer = 0;
}
UI_DefaultFocus(page);
// Meta effects.
for(i = 0, meta.type = 0; i < page->count; ++i)
{
if(!meta.type && objects[i].type != UI_META)
continue;
if(objects[i].type == UI_META)
{
// This will be the meta for now.
memcpy(&meta, objects + i, sizeof(meta));
// Neutralize the actual object.
objects[i].group = UIG_NONE;
objects[i].flags |= UIF_HIDDEN;
objects[i].relx = objects[i].rely = 0;
objects[i].relw = objects[i].relh = 0;
continue;
}
// Apply the meta.
if(meta.group != UIG_NONE)
objects[i].group = meta.group;
objects[i].relx += meta.relx;
objects[i].rely += meta.rely;
objects[i].relw += meta.relw;
objects[i].relh += meta.relh;
}
}
int UI_AvailableWidth(void)
{
return theWindow->geometry.size.width - UI_BORDER * 4;
}
int UI_AvailableHeight(void)
{
return theWindow->geometry.size.height - UI_BORDER * 4;
}
int UI_ScreenX(int relx)
{
return UI_BORDER * 2 + ((relx / UI_WIDTH) * UI_AvailableWidth());
}
int UI_ScreenY(int rely)
{
return UI_BORDER * 2 + ((rely / UI_HEIGHT) * UI_AvailableHeight());
}
int UI_ScreenW(int relw)
{
return (relw / UI_WIDTH) * UI_AvailableWidth();
}
int UI_ScreenH(int relh)
{
return (relh / UI_HEIGHT) * UI_AvailableHeight();
}
void UI_SetPage(ui_page_t* page)
{
ui_object_t* ob;
uint i;
uiCurrentPage = page;
if(!page)
return;
// Init objects.
for(i = 0, ob = page->_objects; i < page->count; ++i, ob++)
{
// Calculate real coordinates.
ob->geometry.origin.x = UI_ScreenX(ob->relx);
ob->geometry.origin.y = UI_ScreenY(ob->rely);
ob->geometry.size.width = UI_ScreenW(ob->relw);
ob->geometry.size.height = UI_ScreenH(ob->relh);
// Update objects on page
if(ob->type == UI_EDIT)
{
// Update edit box text.
memset(ob->text, 0, sizeof(ob->text));
strncpy(ob->text, ((uidata_edit_t*) ob->data)->ptr, 255);
}
else if(ob->type == UI_BUTTON2)
{
// Stay-down button state.
if(*(char*) ob->data)
ob->flags |= UIF_ACTIVE;
else
ob->flags &= ~UIF_ACTIVE;
}
else if(ob->type == UI_BUTTON2EX)
{
// Stay-down button state, with extended data.
if(*(char*) ((uidata_button_t*)ob->data)->data)
ob->flags |= UIF_ACTIVE;
else
ob->flags &= ~UIF_ACTIVE;
}
else if(ob->type == UI_LIST)
{
// List box number of visible items.
uidata_list_t* dat = ob->data;
dat->numvis = (ob->geometry.size.height - 2 * UI_BORDER) / listItemHeight(dat);
if(dat->selection >= 0)
{
if(dat->selection < dat->first)
dat->first = dat->selection;
if(dat->selection > dat->first + dat->numvis - 1)
dat->first = dat->selection - dat->numvis + 1;
}
UI_InitColumns(ob);
}
}
// The mouse has not yet been moved on this page.
uiMoved = false;
}
int UI_Responder(ddevent_t* ev)
{
if(!uiActive)
return false;
if(!uiCurrentPage)
return false;
if(IS_MOUSE_MOTION(ev))
{
uiMoved = true;
if(ev->axis.id == 0) // xaxis.
{
uiCX += ev->axis.pos;
if(uiCX < 0)
uiCX = 0;
if(uiCX >= theWindow->geometry.size.width)
uiCX = theWindow->geometry.size.width - 1;
}
else if(ev->axis.id == 1) // yaxis.
{
uiCY += ev->axis.pos;
if(uiCY < 0)
uiCY = 0;
if(uiCY >= theWindow->geometry.size.height)
uiCY = theWindow->geometry.size.height - 1;
}
}
// Call the page's responder.
uiCurrentPage->responder(uiCurrentPage, ev);
// If the UI is active, all events are eaten by it.
return true;
}
void UI_Ticker(timespan_t time)
{
#define UIALPHA_FADE_STEP .07
static trigger_t fixed = { 1 / 35.0, 0 };
float diff = 0;
if(!uiActive || !uiCurrentPage) return;
// Time to think?
if(!M_RunTrigger(&fixed, time)) return;
// Move towards the target alpha level for the entire UI.
diff = uiTargetAlpha - uiAlpha;
if(fabs(diff) > UIALPHA_FADE_STEP)
{
uiAlpha += UIALPHA_FADE_STEP * (diff > 0? 1 : -1);
}
else
{
uiAlpha = uiTargetAlpha;
}
if(!uiDrawGame)
{
// By default, the game is not visible, but since the alpha is not
// fully opaque, it must be shown anyway.
drawGame = (uiAlpha < 1.0);
}
// Call the active page's ticker.
uiCurrentPage->ticker(uiCurrentPage);
#undef UIALPHA_FADE_STEP
}
void UI_Drawer(void)
{
if(!uiActive || !uiCurrentPage) return;
// Go into screen projection mode.
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, theWindow->geometry.size.width, theWindow->geometry.size.height, 0, -1, 1);
// Call the active page's drawer.
uiCurrentPage->drawer(uiCurrentPage);
// Draw mouse cursor?
if(uiShowMouse)
{
Point2Raw origin;
Size2Raw size;
float scale;
if(theWindow->geometry.size.width >= theWindow->geometry.size.height)
scale = (theWindow->geometry.size.width / UI_WIDTH) *
(theWindow->geometry.size.height / (float) theWindow->geometry.size.width);
else
scale = (theWindow->geometry.size.height / UI_HEIGHT) *
(theWindow->geometry.size.width / (float) theWindow->geometry.size.height);
origin.x = uiCX - 1;
origin.y = uiCY - 1;
size.width = UICURSORWIDTH * scale * uiCursorWidthMul;
size.height = UICURSORHEIGHT * scale * uiCursorHeightMul;
UI_DrawMouse(&origin, &size);
}
// Restore the original matrices.
glMatrixMode(GL_PROJECTION);
glPopMatrix();
}
int UI_CountObjects(ui_object_t* list)
{
int count;
for(count = 0; list->type != UI_NONE; list++, count++);
return count;
}
void UI_FlagGroup(ui_object_t* list, int group, int flags, int set)
{
for(; list->type; list++)
{
if(list->group != group)
continue;
switch(set)
{
case UIFG_CLEAR:
list->flags &= ~flags;
break;
case UIFG_SET:
list->flags |= flags;
break;
case UIFG_XOR:
list->flags ^= flags;
break;
default:
Con_Error("UI_FlagGroup: Unknown flag bit op %i\n", set);
break;
}
}
}
ui_object_t* UI_FindObject(ui_object_t* list, int group, int flags)
{
for(; list->type; list++)
if(list->group == group && (list->flags & flags) == flags)
return list;
return NULL;
}
void UI_MouseFocus(void)
{
ui_object_t* ob;
uint i;
for(i = 0, ob = uiCurrentPage->_objects; i < uiCurrentPage->count; ++i, ob++)
if(!(ob->flags & UIF_NO_FOCUS) && UI_MouseInside(ob))
{
UI_Focus(ob);
break;
}
}
void UI_Focus(ui_object_t* ob)
{
uint i;
if(!ob)
Con_Error("UI_Focus: Tried to set focus on NULL.\n");
// Can the object receive focus?
if(ob->flags & UIF_NO_FOCUS)
return;
uiCurrentPage->focus = ob - uiCurrentPage->_objects;
for(i = 0; i < uiCurrentPage->count; ++i)
{
if(i == uiCurrentPage->focus)
uiCurrentPage->_objects[i].flags |= UIF_FOCUS;
else
uiCurrentPage->_objects[i].flags &= ~UIF_FOCUS;
}
}
void UI_Capture(ui_object_t* ob)
{
if(!ob)
{
// End capture.
uiCurrentPage->capture = -1;
return;
}
if(!ob->responder)
return;
// Set the capture object.
uiCurrentPage->capture = ob - uiCurrentPage->_objects;
// Set focus.
UI_Focus(ob);
}
//---------------------------------------------------------------------------
// Default Callback Functions
//---------------------------------------------------------------------------
int UIPage_Responder(ui_page_t* page, ddevent_t* ev)
{
ui_object_t* ob;
ddevent_t translated;
uint i;
// Translate mouse wheel?
if(IS_MOUSE_DOWN(ev))
{
if(ev->toggle.id == DD_MWHEEL_UP || ev->toggle.id == DD_MWHEEL_DOWN)
{
UI_MouseFocus();
translated.device = IDEV_KEYBOARD;
translated.type = E_TOGGLE;
translated.toggle.state = ETOG_DOWN;
translated.toggle.id = (ev->toggle.id == DD_MWHEEL_UP ? DDKEY_UPARROW : DDKEY_DOWNARROW);
ev = &translated;
}
}
if(page->capture >= 0)
{
// There is an object that has captured input.
ob = page->_objects + page->capture;
// Capture objects must have a responder!
// This object gets to decide what happens.
return ob->responder(ob, ev);
}
// Check for Esc key.
if(IS_KEY_PRESS(ev))
{
// We won't accept repeats with Esc.
if(ev->toggle.id == DDKEY_ESCAPE && ev->toggle.state == ETOG_DOWN &&
allowEscape)
{
UI_SetPage(page->previous);
// If we have no more a page, disactive UI.
if(!uiCurrentPage)
UI_End();
return true; // The event was used.
}
// Tab is used for navigation.
if(ev->toggle.id == DDKEY_TAB)
{
uint k;
// Remove the focus flag from the current focus object.
page->_objects[page->focus].flags &= ~UIF_FOCUS;
// Move focus.
k = 0;
do
{
page->focus += shiftDown ? -1 : 1;
// Check range.
if(page->focus < 0)
page->focus = page->count - 1;
else if((unsigned) page->focus >= page->count)
page->focus = 0;
} while(++k < page->count && (page->_objects[page->focus].flags & (UIF_DISABLED | UIF_NO_FOCUS | UIF_HIDDEN)));
// Flag the new focus object.
page->_objects[page->focus].flags |= UIF_FOCUS;
return true; // The event was used.
}
}
// Call responders until someone uses the event.
// We start with the focus object.
for(i = 0; i < page->count; ++i)
{
int k;
// Determine the index of the object to process.
k = page->focus + i;
// Wrap around.
if(k < 0)
k += page->count;
if((unsigned)k >= page->count)
k -= page->count;
ob = page->_objects + k;
// Check the flags of this object.
if((ob->flags & UIF_HIDDEN) || (ob->flags & UIF_DISABLED))
continue; // These flags prevent response.
if(!ob->responder)
continue; // Must have a responder.
if(ob->responder(ob, ev))
{ // The event was used by this object.
UI_Focus(ob); // Move focus to it.
return true;
}
}
if(uiTargetAlpha < 1.0 && IS_MOUSE_DOWN(ev))
{
// When the UI the faded, an unhandled click brings back the UI.
UI_DefaultFocus(page);
}
// We didn't use the event.
return false;
}
void UIPage_Ticker(ui_page_t* page)
{
boolean fadedAway = false;
ui_object_t* ob;
uint i;
// Call the ticker of each object, unless they're hidden or paused.
for(i = 0, ob = page->_objects; i < page->count; ++i, ob++)
{
if((ob->flags & UIF_PAUSED) || (ob->flags & UIF_HIDDEN))
continue;
// Fadeaway objects cause the UI to fade away when the mouse is over the control.
if((ob->flags & UIF_FOCUS) && (ob->flags & UIF_FADE_AWAY))
{
UI_SetAlpha(0);
fadedAway = true;
}
if(ob->ticker)
ob->ticker(ob);
// Advance object timer.
ob->timer++;
}
if(!fadedAway)
{
UI_SetAlpha(1.0);
}
page->_timer++;
// Check mouse resting.
if(abs(uiCX - uiRestCX) > uiRestOffsetLimit || abs(uiCY - uiRestCY) > uiRestOffsetLimit)
{
// Restart resting period.
uiRestCX = uiCX;
uiRestCY = uiCY;
uiRestStart = page->_timer;
}
}
void UIPage_Drawer(ui_page_t* page)
{
ui_object_t* ob;
ui_color_t focuscol;
float t;
uint i;
// Draw background?
if(page->flags.showBackground)
{
Point2Raw origin = { 0, 0 };
UI_DrawDDBackground(&origin, &theWindow->geometry.size, uiAlpha);
}
// Draw each object, unless they're hidden.
for(i = 0, ob = page->_objects; i < page->count; ++i, ob++)
{
float currentUIAlpha = uiAlpha;
if((ob->flags & UIF_HIDDEN) || !ob->drawer)
continue;
if(ob->flags & UIF_FADE_AWAY)
{
if(ob->flags & UIF_FOCUS)
{
// The focused object must not fade away completely.
uiAlpha = MAX_OF(currentUIAlpha, 0.75);
}
else
{
// Other fadeaway objects remain slightly visible.
uiAlpha = MAX_OF(currentUIAlpha, 0.333);
}
}
if(ob->flags & UIF_NEVER_FADE)
{
uiAlpha = 1.0;
}
// Draw the object itself.
ob->drawer(ob);
if((ob->flags & UIF_FOCUS) &&
(ob->type != UI_EDIT || !(ob->flags & UIF_ACTIVE)))
{
Point2Raw focusOrigin;
Size2Raw focusSize;
t = (1 + sin(page->_timer / (float) TICSPERSEC * 1.5f * PI)) / 2;
UI_MixColors(UI_Color(UIC_BRD_LOW), UI_Color(UIC_BRD_HI), &focuscol, t);
glEnable(GL_TEXTURE_2D);
UI_Shade(&ob->geometry.origin, &ob->geometry.size, UI_BORDER, UI_Color(UIC_BRD_LOW), UI_Color(UIC_BRD_LOW), .2f + t * .3f, -1);
GL_BlendMode(BM_ADD);
// Draw a focus rectangle.
focusOrigin.x = ob->geometry.origin.x - 1;
focusOrigin.y = ob->geometry.origin.y - 1;
focusSize.width = ob->geometry.size.width + 2;
focusSize.height = ob->geometry.size.height + 2;
UI_DrawRect(&focusOrigin, &focusSize, UI_BORDER, &focuscol, 1);
GL_BlendMode(BM_NORMAL);
glDisable(GL_TEXTURE_2D);
}
// Restore the correct UI alpha.
uiAlpha = currentUIAlpha;
}
}
void UIFrame_Drawer(ui_object_t* ob)
{
int b = UI_BORDER;
glEnable(GL_TEXTURE_2D);
UI_GradientEx(&ob->geometry.origin, &ob->geometry.size, b, UI_Color(UIC_BG_MEDIUM), 0, .6f, 0);
UI_DrawRect(&ob->geometry.origin, &ob->geometry.size, b, UI_Color(UIC_BRD_HI), 1);
glDisable(GL_TEXTURE_2D);
}
void UIText_Drawer(ui_object_t* ob)
{
Point2Raw origin;
glEnable(GL_TEXTURE_2D);
FR_SetFont(fontVariable[FS_NORMAL]);
FR_LoadDefaultAttrib();
FR_SetShadowOffset(UI_SHADOW_OFFSET, UI_SHADOW_OFFSET);
FR_SetShadowStrength(UI_SHADOW_STRENGTH);
origin.x = ob->geometry.origin.x;
origin.y = ob->geometry.origin.y + ob->geometry.size.height / 2;
UI_TextOutEx2(ob->text, &origin, UI_Color(UIC_TEXT), ob->flags & UIF_DISABLED ? .2f : 1, ALIGN_LEFT, DTF_ONLY_SHADOW);
glDisable(GL_TEXTURE_2D);
}
void UIText_BrightDrawer(ui_object_t* ob)
{
Point2Raw origin;
glEnable(GL_TEXTURE_2D);
FR_SetFont(fontVariable[FS_NORMAL]);
FR_LoadDefaultAttrib();
FR_SetShadowOffset(UI_SHADOW_OFFSET, UI_SHADOW_OFFSET);
FR_SetShadowStrength(UI_SHADOW_STRENGTH);
origin.x = ob->geometry.origin.x;
origin.y = ob->geometry.origin.y + ob->geometry.size.height / 2;
UI_TextOutEx2(ob->text, &origin, UI_Color(UIC_TITLE), ob->flags & UIF_DISABLED ? .2f : 1, ALIGN_LEFT, DTF_ONLY_SHADOW);
glDisable(GL_TEXTURE_2D);
}
int UIButton_Responder(ui_object_t* ob, ddevent_t* ev)
{
if(ob->flags & UIF_CLICKED)
{
if((ev->device == IDEV_KEYBOARD || ev->device == IDEV_MOUSE) && IS_TOGGLE_UP(ev))
{
UI_Capture(0);
ob->flags &= ~UIF_CLICKED;
if(UI_MouseInside(ob) || ev->device == IDEV_KEYBOARD)
{
// Activate?
if(ob->action)
ob->action(ob);
}
return true;
}
}
else if(IS_TOGGLE_DOWN(ev) &&
((ev->device == IDEV_MOUSE && UI_MouseInside(ob)) ||
(ev->device == IDEV_KEYBOARD && IS_ACTKEY(ev->toggle.id))))
{
if(ob->type == UI_BUTTON)
{
// Capture input.
UI_Capture(ob);
ob->flags |= UIF_CLICKED;
}
else
{
// Stay-down buttons change state.
ob->flags ^= UIF_ACTIVE;
if(ob->data)
{
void* data;
if(ob->type == UI_BUTTON2EX)
data = ((uidata_button_t*) ob->data)->data;
else
data = ob->data;
*(char*) data = (ob->flags & UIF_ACTIVE) != 0;
}
// Call the action function.
if(ob->action)
ob->action(ob);
}
ob->timer = 0;
return true;
}
return false;
}
void UIButton_Drawer(ui_object_t* ob)
{
int dis = (ob->flags & UIF_DISABLED) != 0;
int act = (ob->flags & UIF_ACTIVE) != 0;
int click = (ob->flags & UIF_CLICKED) != 0;
Point2Raw labelOrigin;
boolean down = act || click;
ui_color_t back;
float t = ob->timer / 15.0f;