/
hu_lib.cpp
3613 lines (3019 loc) · 99.5 KB
/
hu_lib.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
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 hu_lib.cpp HUD widget library.
*
* @authors Copyright © 2005-2014 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/
#include "common.h"
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <de/memory.h>
#include "hu_chat.h"
#include "hu_lib.h"
#include "hu_log.h"
#include "hu_automap.h"
// @todo Remove external dependencies
#include "hu_menu.h" // For the menu sound ids.
extern int menuTime;
void Hu_MenuDrawFocusCursor(int x, int y, int focusObjectHeight, float alpha);
static dd_bool inited = false;
static int numWidgets;
static uiwidget_t *widgets;
static ui_rendstate_t uiRS;
const ui_rendstate_t *uiRendState = &uiRS;
// Menu (page) render state.
static mn_rendstate_t rs;
const mn_rendstate_t *mnRendState = &rs;
static patchid_t pSliderLeft;
static patchid_t pSliderRight;
static patchid_t pSliderMiddle;
static patchid_t pSliderHandle;
static patchid_t pEditLeft;
static patchid_t pEditRight;
static patchid_t pEditMiddle;
static void MNSlider_LoadResources();
static void MNEdit_LoadResources();
static mn_actioninfo_t *MNObject_FindActionInfoForId(mn_object_t *ob, mn_actionid_t id);
/**
* Lookup the logical index of an object thought to be on this page.
* @param ob MNObject to lookup the index of.
* @return Index of the found object else @c -1.
*/
static int MNPage_FindObjectIndex(mn_page_t *page, mn_object_t *ob);
/**
* Retrieve an object on this page by it's logical index.
* @return Found MNObject else fatal error.
*/
//static mn_object_t *MNPage_ObjectByIndex(mn_page_t *page, int idx);
static void errorIfNotInited(char const *callerName)
{
if(inited) return;
Con_Error("%s: GUI module is not presently initialized.", callerName);
// Unreachable. Prevents static analysers from getting rather confused, poor things.
exit(1);
}
static void lerpColor(float *dst, float const *a, float const *b, float t, dd_bool rgbaMode)
{
if(t <= 0)
{
dst[CR] = a[CR];
dst[CG] = a[CG];
dst[CB] = a[CB];
if(rgbaMode) dst[CA] = a[CA];
return;
}
if(t >= 1)
{
dst[CR] = b[CR];
dst[CG] = b[CG];
dst[CB] = b[CB];
if(rgbaMode) dst[CA] = b[CA];
return;
}
dst[CR] = (1 - t) * a[CR] + t * b[CR];
dst[CG] = (1 - t) * a[CG] + t * b[CG];
dst[CB] = (1 - t) * a[CB] + t * b[CB];
if(rgbaMode) dst[CA] = (1 - t) * a[CA] + t * b[CA];
}
static uiwidgetid_t nextUnusedId()
{
return uiwidgetid_t(numWidgets);
}
static uiwidget_t *allocateWidget(guiwidgettype_t type, uiwidgetid_t id, int player, void *typedata)
{
widgets = (uiwidget_t *) M_Realloc(widgets, sizeof(*widgets) * ++numWidgets);
uiwidget_t *ob = &widgets[numWidgets-1];
de::zapPtr(ob);
ob->geometry = Rect_New();
ob->type = type;
ob->id = id;
ob->player = player;
switch(ob->type)
{
case GUI_GROUP: {
ob->typedata = (guidata_group_t *) M_Calloc(sizeof(guidata_group_t));
break; }
default:
ob->typedata = typedata;
break;
}
switch(ob->type)
{
case GUI_AUTOMAP: {
guidata_automap_t *am = (guidata_automap_t*)ob->typedata;
am->mcfg = ST_AutomapConfig();
am->followPlayer = player;
am->oldViewScale = 1;
am->maxViewPositionDelta = 128;
am->alpha = am->targetAlpha = am->oldAlpha = 0;
/// Set initial geometry size.
/// @todo Should not be necessary...
Rect_SetWidthHeight(ob->geometry, SCREENWIDTH, SCREENHEIGHT);
break; }
default: break;
}
return ob;
}
static uiwidget_t *createWidget(guiwidgettype_t type, int player, int alignFlags,
fontid_t fontId, float opacity, void (*updateGeometry) (uiwidget_t *ob),
void (*drawer) (uiwidget_t *ob, Point2Raw const *offset),
void (*ticker) (uiwidget_t *ob, timespan_t ticLength), void *typedata)
{
DENG2_ASSERT(updateGeometry != 0);
uiwidget_t *ob = allocateWidget(type, nextUnusedId(), player, typedata);
ob->alignFlags = alignFlags;
ob->font = fontId;
ob->opacity = opacity;
ob->updateGeometry = updateGeometry;
ob->drawer = drawer;
ob->ticker = ticker;
return ob;
}
static void clearWidgets()
{
if(!numWidgets) return;
for(int i = 0; i < numWidgets; ++i)
{
uiwidget_t *ob = &widgets[i];
if(ob->type == GUI_GROUP)
{
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
M_Free(grp->widgetIds);
M_Free(grp);
}
Rect_Delete(ob->geometry);
}
M_Free(widgets); widgets = 0;
numWidgets = 0;
}
uiwidget_t *GUI_FindObjectById(uiwidgetid_t id)
{
if(!inited) return 0; // GUI not available.
if(id >= 0)
{
for(int i = 0; i < numWidgets; ++i)
{
uiwidget_t *ob = &widgets[i];
if(ob->id == id)
{
return ob;
}
}
}
return 0; // Not found.
}
uiwidget_t *GUI_MustFindObjectById(uiwidgetid_t id)
{
uiwidget_t *ob = GUI_FindObjectById(id);
if(!ob)
{
Con_Error("GUI_MustFindObjectById: Failed to locate object with id %i.", (int) id);
}
return ob;
}
void GUI_Register()
{
UIAutomap_Register();
UIChat_Register();
UILog_Register();
}
void GUI_Init()
{
if(inited) return;
numWidgets = 0;
widgets = 0;
UIChat_LoadMacros();
inited = true;
GUI_LoadResources();
}
void GUI_Shutdown()
{
if(!inited) return;
clearWidgets();
inited = false;
}
void GUI_LoadResources()
{
if(Get(DD_DEDICATED) || Get(DD_NOVIDEO)) return;
UIAutomap_LoadResources();
MNEdit_LoadResources();
MNSlider_LoadResources();
}
void GUI_ReleaseResources()
{
if(Get(DD_DEDICATED) || Get(DD_NOVIDEO)) return;
UIAutomap_ReleaseResources();
for(int i = 0; i < numWidgets; ++i)
{
uiwidget_t *ob = widgets + i;
switch(ob->type)
{
case GUI_AUTOMAP: UIAutomap_Reset(ob); break;
default: break;
}
}
}
uiwidgetid_t GUI_CreateWidget(guiwidgettype_t type, int player, int alignFlags,
fontid_t fontId, float opacity,
void (*updateGeometry) (uiwidget_t *ob), void (*drawer) (uiwidget_t *ob, Point2Raw const *offset),
void (*ticker) (uiwidget_t *ob, timespan_t ticLength), void *typedata)
{
errorIfNotInited("GUI_CreateWidget");
uiwidget_t *ob = createWidget(type, player, alignFlags, fontId, opacity, updateGeometry, drawer, ticker, typedata);
return ob->id;
}
uiwidgetid_t GUI_CreateGroup(int groupFlags, int player, int alignFlags, order_t order, int padding)
{
errorIfNotInited("GUI_CreateGroup");
uiwidget_t *ob = createWidget(GUI_GROUP, player, alignFlags, 1, 0, UIGroup_UpdateGeometry, NULL, NULL, NULL);
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
grp->flags = groupFlags;
grp->order = order;
grp->padding = padding;
return ob->id;
}
void UIGroup_AddWidget(uiwidget_t *ob, uiwidget_t *other)
{
DENG2_ASSERT(ob != 0 && ob->type == GUI_GROUP);
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
if(!other || other == ob)
{
DENG2_ASSERT(!"UIGroup::AddWidget: Attempt to add invalid widget");
return;
}
// Ensure widget is not already in this grp.
for(int i = 0; i < grp->widgetIdCount; ++i)
{
if(grp->widgetIds[i] == other->id)
return; // Already present. Ignore.
}
// Must add to this grp.
grp->widgetIds = (uiwidgetid_t *) M_Realloc(grp->widgetIds, sizeof(*grp->widgetIds) * ++grp->widgetIdCount);
grp->widgetIds[grp->widgetIdCount - 1] = other->id;
}
int UIGroup_Flags(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0 && ob->type == GUI_GROUP);
return static_cast<guidata_group_t *>(ob->typedata)->flags;
}
void UIGroup_SetFlags(uiwidget_t *ob, int flags)
{
DENG2_ASSERT(ob != 0 && ob->type == GUI_GROUP);
static_cast<guidata_group_t *>(ob->typedata)->flags = flags;
}
static void applyAlignmentOffset(uiwidget_t *ob, int *x, int *y)
{
DENG2_ASSERT(ob != 0);
if(x)
{
if(ob->alignFlags & ALIGN_RIGHT)
*x += UIWidget_MaximumWidth(ob);
else if(!(ob->alignFlags & ALIGN_LEFT))
*x += UIWidget_MaximumWidth(ob)/2;
}
if(y)
{
if(ob->alignFlags & ALIGN_BOTTOM)
*y += UIWidget_MaximumHeight(ob);
else if(!(ob->alignFlags & ALIGN_TOP))
*y += UIWidget_MaximumHeight(ob)/2;
}
}
static void updateWidgetGeometry(uiwidget_t *ob)
{
Rect_SetXY(ob->geometry, 0, 0);
ob->updateGeometry(ob);
if(Rect_Width(ob->geometry) <= 0 || Rect_Height(ob->geometry) <= 0) return;
if(ob->alignFlags & ALIGN_RIGHT)
Rect_SetX(ob->geometry, Rect_X(ob->geometry) - Rect_Width(ob->geometry));
else if(!(ob->alignFlags & ALIGN_LEFT))
Rect_SetX(ob->geometry, Rect_X(ob->geometry) - Rect_Width(ob->geometry)/2);
if(ob->alignFlags & ALIGN_BOTTOM)
Rect_SetY(ob->geometry, Rect_Y(ob->geometry) - Rect_Height(ob->geometry));
else if(!(ob->alignFlags & ALIGN_TOP))
Rect_SetY(ob->geometry, Rect_Y(ob->geometry) - Rect_Height(ob->geometry)/2);
}
void UIGroup_UpdateGeometry(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0 && ob->type == GUI_GROUP);
guidata_group_t *grp = (guidata_group_t*)ob->typedata;
Rect_SetWidthHeight(ob->geometry, 0, 0);
if(!grp->widgetIdCount) return;
int x = 0, y = 0;
applyAlignmentOffset(ob, &x, &y);
for(int i = 0; i < grp->widgetIdCount; ++i)
{
uiwidget_t *child = GUI_MustFindObjectById(grp->widgetIds[i]);
Rect const *childGeometry;
if(UIWidget_MaximumWidth(child) > 0 && UIWidget_MaximumHeight(child) > 0 &&
UIWidget_Opacity(child) > 0)
{
updateWidgetGeometry(child);
Rect_SetX(child->geometry, Rect_X(child->geometry) + x);
Rect_SetY(child->geometry, Rect_Y(child->geometry) + y);
childGeometry = UIWidget_Geometry(child);
if(Rect_Width(childGeometry) > 0 && Rect_Height(childGeometry) > 0)
{
if(grp->order == ORDER_RIGHTTOLEFT)
{
if(!(grp->flags & UWGF_VERTICAL))
x -= Rect_Width(childGeometry) + grp->padding;
else
y -= Rect_Height(childGeometry) + grp->padding;
}
else if(grp->order == ORDER_LEFTTORIGHT)
{
if(!(grp->flags & UWGF_VERTICAL))
x += Rect_Width(childGeometry) + grp->padding;
else
y += Rect_Height(childGeometry) + grp->padding;
}
Rect_Unite(ob->geometry, childGeometry);
}
}
}
}
#if 0 //def DENG2_DEBUG
static void drawWidgetGeometry(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
RectRaw geometry;
Rect_Raw(ob->geometry, &geometry);
DGL_Color3f(1, 1, 1);
DGL_Begin(DGL_LINES);
DGL_Vertex2f(geometry.origin.x, geometry.origin.y);
DGL_Vertex2f(geometry.origin.x + geometry.size.width, geometry.origin.y);
DGL_Vertex2f(geometry.origin.x + geometry.size.width, geometry.origin.y);
DGL_Vertex2f(geometry.origin.x + geometry.size.width, geometry.origin.y + geometry.size.height);
DGL_Vertex2f(geometry.origin.x + geometry.size.width, geometry.origin.y + geometry.size.height);
DGL_Vertex2f(geometry.origin.x, geometry.origin.y + geometry.size.height);
DGL_Vertex2f(geometry.origin.x, geometry.origin.y + geometry.size.height);
DGL_Vertex2f(geometry.origin.x, geometry.origin.y);
DGL_End();
}
static void drawWidgetAvailableSpace(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
DGL_Color4f(0, .4f, 0, .1f);
DGL_DrawRectf2(Rect_X(ob->geometry), Rect_Y(ob->geometry), ob->maxSize.width, ob->maxSize.height);
}
#endif
static void drawWidget2(uiwidget_t *ob, Point2Raw const *offset)
{
DENG2_ASSERT(ob != 0);
/*#if _DEBUG
drawWidgetAvailableSpace(ob);
#endif*/
if(ob->drawer && ob->opacity > .0001f)
{
Point2Raw origin;
Point2_Raw(Rect_Origin(ob->geometry), &origin);
// Configure the page render state.
uiRS.pageAlpha = ob->opacity;
FR_PushAttrib();
DGL_MatrixMode(DGL_MODELVIEW);
DGL_Translatef(origin.x, origin.y, 0);
// Do not pass a zero length offset.
ob->drawer(ob, ((offset && (offset->x || offset->y))? offset : NULL));
FR_PopAttrib();
DGL_MatrixMode(DGL_MODELVIEW);
DGL_Translatef(-origin.x, -origin.y, 0);
}
/*#if _DEBUG
drawWidgetGeometry(ob);
#endif*/
}
static void drawWidget(uiwidget_t *ob, Point2Raw const *origin)
{
DENG2_ASSERT(ob != 0);
if(origin)
{
DGL_MatrixMode(DGL_MODELVIEW);
DGL_Translatef(origin->x, origin->y, 0);
}
// First we draw ourself.
drawWidget2(ob, NULL/*no origin offset*/);
if(ob->type == GUI_GROUP)
{
// Now our children.
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
for(int i = 0; i < grp->widgetIdCount; ++i)
{
uiwidget_t *child = GUI_MustFindObjectById(grp->widgetIds[i]);
drawWidget(child, NULL/*no origin offset*/);
}
}
if(origin)
{
DGL_MatrixMode(DGL_MODELVIEW);
DGL_Translatef(-origin->x, -origin->y, 0);
}
}
void GUI_DrawWidget(uiwidget_t *ob, Point2Raw const *offset)
{
if(!ob) return;
if(UIWidget_MaximumWidth(ob) < 1 || UIWidget_MaximumHeight(ob) < 1) return;
if(UIWidget_Opacity(ob) <= 0) return;
FR_PushAttrib();
FR_LoadDefaultAttrib();
FR_SetLeading(0);
updateWidgetGeometry(ob);
FR_PopAttrib();
// Draw.
FR_PushAttrib();
FR_LoadDefaultAttrib();
FR_SetLeading(0);
// Do not pass a zero length offset.
drawWidget(ob, ((offset && (offset->x || offset->y))? offset : NULL));
FR_PopAttrib();
}
void GUI_DrawWidgetXY(uiwidget_t *ob, int x, int y)
{
Point2Raw origin;
origin.x = x;
origin.y = y;
GUI_DrawWidget(ob, &origin);
}
void UIWidget_RunTic(uiwidget_t *ob, timespan_t ticLength)
{
DENG2_ASSERT(ob != 0);
switch(ob->type)
{
case GUI_GROUP: {
// First our children then self.
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
for(int i = 0; i < grp->widgetIdCount; ++i)
{
UIWidget_RunTic(GUI_MustFindObjectById(grp->widgetIds[i]), ticLength);
}
/* Fallthrough*/ }
default:
if(ob->ticker)
{
ob->ticker(ob, ticLength);
}
break;
}
}
int UIWidget_Player(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return ob->player;
}
Point2 const *UIWidget_Origin(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return Rect_Origin(ob->geometry);
}
Rect const *UIWidget_Geometry(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return ob->geometry;
}
int UIWidget_MaximumHeight(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return ob->maxSize.height;
}
Size2Raw const *UIWidget_MaximumSize(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return &ob->maxSize;
}
int UIWidget_MaximumWidth(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return ob->maxSize.width;
}
void UIWidget_SetMaximumHeight(uiwidget_t *ob, int height)
{
DENG2_ASSERT(ob != 0);
if(ob->maxSize.height == height) return;
ob->maxSize.height = height;
if(ob->type == GUI_GROUP)
{
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
for(int i = 0; i < grp->widgetIdCount; ++i)
{
UIWidget_SetMaximumHeight(GUI_MustFindObjectById(grp->widgetIds[i]), height);
}
}
}
void UIWidget_SetMaximumSize(uiwidget_t *ob, Size2Raw const *size)
{
DENG2_ASSERT(ob != 0);
if(ob->maxSize.width == size->width &&
ob->maxSize.height == size->height) return;
ob->maxSize.width = size->width;
ob->maxSize.height = size->height;
if(ob->type == GUI_GROUP)
{
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
for(int i = 0; i < grp->widgetIdCount; ++i)
{
UIWidget_SetMaximumSize(GUI_MustFindObjectById(grp->widgetIds[i]), size);
}
}
}
void UIWidget_SetMaximumWidth(uiwidget_t *ob, int width)
{
DENG2_ASSERT(ob != 0);
if(ob->maxSize.width == width) return;
ob->maxSize.width = width;
if(ob->type == GUI_GROUP)
{
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
for(int i = 0; i < grp->widgetIdCount; ++i)
{
UIWidget_SetMaximumWidth(GUI_MustFindObjectById(grp->widgetIds[i]), width);
}
}
}
int UIWidget_Alignment(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return ob->alignFlags;
}
void UIWidget_SetAlignment(uiwidget_t *ob, int alignFlags)
{
DENG2_ASSERT(ob != 0);
ob->alignFlags = alignFlags;
}
float UIWidget_Opacity(uiwidget_t *ob)
{
DENG2_ASSERT(ob != 0);
return ob->opacity;
}
void UIWidget_SetOpacity(uiwidget_t *ob, float opacity)
{
DENG2_ASSERT(ob != 0);
ob->opacity = opacity;
if(ob->type == GUI_GROUP)
{
guidata_group_t *grp = (guidata_group_t *)ob->typedata;
for(int i = 0; i < grp->widgetIdCount; ++i)
{
uiwidget_t *child = GUI_MustFindObjectById(grp->widgetIds[i]);
UIWidget_SetOpacity(child, opacity);
}
}
}
static void MNSlider_LoadResources()
{
pSliderLeft = R_DeclarePatch(MNDATA_SLIDER_PATCH_LEFT);
pSliderRight = R_DeclarePatch(MNDATA_SLIDER_PATCH_RIGHT);
pSliderMiddle = R_DeclarePatch(MNDATA_SLIDER_PATCH_MIDDLE);
pSliderHandle = R_DeclarePatch(MNDATA_SLIDER_PATCH_HANDLE);
}
static void MNEdit_LoadResources()
{
#if defined(MNDATA_EDIT_BACKGROUND_PATCH_LEFT)
pEditLeft = R_DeclarePatch(MNDATA_EDIT_BACKGROUND_PATCH_LEFT);
#else
pEditLeft = 0;
#endif
#if defined(MNDATA_EDIT_BACKGROUND_PATCH_RIGHT)
pEditRight = R_DeclarePatch(MNDATA_EDIT_BACKGROUND_PATCH_RIGHT);
#else
pEditRight = 0;
#endif
pEditMiddle = R_DeclarePatch(MNDATA_EDIT_BACKGROUND_PATCH_MIDDLE);
}
mn_object_t *MN_MustFindObjectOnPage(mn_page_t *page, int group, int flags)
{
mn_object_t *ob = MNPage_FindObject(page, group, flags);
if(!ob)
Con_Error("MN_MustFindObjectOnPage: Failed to locate object in group #%i with flags %i on page %p.", group, flags, page);
return ob;
}
short MN_MergeMenuEffectWithDrawTextFlags(short f)
{
return ((~cfg.menuEffectFlags & DTF_NO_EFFECTS) | (f & ~DTF_NO_EFFECTS));
}
static void MN_DrawObject(mn_object_t *ob, Point2Raw const *offset)
{
if(!ob) return;
ob->drawer(ob, offset);
}
static void setupRenderStateForPageDrawing(mn_page_t *page, float alpha)
{
if(!page) return; // Huh?
rs.pageAlpha = alpha;
rs.textGlitter = cfg.menuTextGlitter;
rs.textShadow = cfg.menuShadow;
for(int i = 0; i < MENU_FONT_COUNT; ++i)
{
rs.textFonts[i] = MNPage_PredefinedFont(page, mn_page_fontid_t(i));
}
for(int i = 0; i < MENU_COLOR_COUNT; ++i)
{
MNPage_PredefinedColor(page, mn_page_colorid_t(i), rs.textColors[i]);
rs.textColors[i][CA] = alpha; // For convenience.
}
// Configure the font renderer (assume state has already been pushed if necessary).
FR_SetFont(rs.textFonts[0]);
FR_LoadDefaultAttrib();
FR_SetLeading(0);
FR_SetShadowStrength(rs.textShadow);
FR_SetGlitterStrength(rs.textGlitter);
}
static void updatePageObjectGeometries(mn_page_t *page)
{
if(!page) return;
// Update objects.
for(int i = 0; i < page->objectsCount; ++i)
{
mn_object_t *ob = &page->objects[i];
if(MNObject_Type(ob) == MN_NONE) continue;
FR_PushAttrib();
if(ob->updateGeometry)
{
Rect_SetXY(ob->_geometry, 0, 0);
ob->updateGeometry(ob, page);
}
FR_PopAttrib();
}
}
/// @return @c true iff this object is drawable (potentially visible).
dd_bool MNObject_IsDrawable(mn_object_t *ob)
{
return !(MNObject_Type(ob) == MN_NONE || !ob->drawer || (MNObject_Flags(ob) & MNF_HIDDEN));
}
int MNPage_LineHeight2(mn_page_t *page, int *lineOffset)
{
DENG2_ASSERT(page != 0);
fontid_t oldFont = FR_Font();
/// @kludge We cannot yet query line height from the font...
FR_SetFont(MNPage_PredefinedFont(page, MENU_FONT1));
int lineHeight = FR_TextHeight("{case}WyQ");
if(lineOffset)
{
*lineOffset = MAX_OF(1, .5f + lineHeight * .34f);
}
// Restore the old font.
FR_SetFont(oldFont);
return lineHeight;
}
int MNPage_LineHeight(mn_page_t *page)
{
return MNPage_LineHeight2(page, 0);
}
static void applyPageLayout(mn_page_t *page)
{
if(!page) return;
Rect_SetXY(page->geometry, 0, 0);
Rect_SetWidthHeight(page->geometry, 0, 0);
// Apply layout logic to this page.
if(page->flags & MPF_LAYOUT_FIXED)
{
// This page uses a fixed layout.
for(int i = 0; i < page->objectsCount; ++i)
{
mn_object_t *ob = &page->objects[i];
if(!MNObject_IsDrawable(ob)) continue;
Rect_SetXY(ob->_geometry, ob->_origin.x, ob->_origin.y);
Rect_Unite(page->geometry, ob->_geometry);
}
return;
}
// This page uses a dynamic layout.
int lineOffset;
int lineHeight = MNPage_LineHeight2(page, &lineOffset);
Point2Raw origin;
for(int i = 0; i < page->objectsCount;)
{
mn_object_t *ob = &page->objects[i];
mn_object_t *nextOb = i+1 < page->objectsCount? &page->objects[i+1] : 0;
if(!MNObject_IsDrawable(ob))
{
// Proceed to the next object!
i += 1;
continue;
}
// If the object has a fixed position, we will ignore it while doing
// dynamic layout.
if(MNObject_Flags(ob) & MNF_POSITION_FIXED)
{
Rect_SetXY(ob->_geometry, ob->_origin.x, ob->_origin.y);
Rect_Unite(page->geometry, ob->_geometry);
// To the next object.
i += 1;
continue;
}
// An additional offset requested?
if(MNObject_Flags(ob) & MNF_LAYOUT_OFFSET)
{
origin.x += ob->_origin.x;
origin.y += ob->_origin.y;
}
Rect_SetXY(ob->_geometry, origin.x, origin.y);
// Orient label plus button/inline-list/textual-slider pairs about a
// vertical dividing line, with the label on the left, other object
// on the right.
// @todo Do not assume pairing, an object should designate it's pair.
if(MNObject_Type(ob) == MN_TEXT && nextOb)
{
if(MNObject_IsDrawable(nextOb) &&
(MNObject_Type(nextOb) == MN_BUTTON ||
MNObject_Type(nextOb) == MN_LISTINLINE ||
MNObject_Type(nextOb) == MN_COLORBOX ||
MNObject_Type(nextOb) == MN_BINDINGS ||
(MNObject_Type(nextOb) == MN_SLIDER && nextOb->drawer == MNSlider_TextualValueDrawer)))
{
int const margin = lineOffset * 2;
RectRaw united;
Rect_SetXY(nextOb->_geometry, margin + Rect_Width(ob->_geometry), origin.y);
origin.y += Rect_United(ob->_geometry, nextOb->_geometry, &united)
->size.height + lineOffset;
Rect_UniteRaw(page->geometry, &united);
// Extra spacing between object groups.
if(i+2 < page->objectsCount &&
nextOb->_group != page->objects[i+2]._group)
origin.y += lineHeight;
// Proceed to the next object!
i += 2;
continue;
}
}
Rect_Unite(page->geometry, ob->_geometry);
origin.y += Rect_Height(ob->_geometry) + lineOffset;
// Extra spacing between object groups.
if(nextOb && nextOb->_group != ob->_group)
origin.y += lineHeight;
// Proceed to the next object!
i += 1;
}
}
#if __JDOOM__ || __JDOOM64__
static void composeSubpageString(mn_page_t *page, char *buf, size_t bufSize)
{
DENG2_ASSERT(page != 0);
if(!buf || 0 == bufSize) return;
dd_snprintf(buf, bufSize, "Page %i/%i", 0, 0);
}
#endif
static void drawPageNavigation(mn_page_t *page, int x, int y)
{
int const currentPage = 0;//(page->firstObject + page->numVisObjects/2) / page->numVisObjects + 1;
int const totalPages = 1;//(int)ceil((float)page->objectsCount/page->numVisObjects);
#if __JDOOM__ || __JDOOM64__
char buf[1024];
DENG2_UNUSED(currentPage);
#endif
if(!page || totalPages <= 1) return;
#if __JDOOM__ || __JDOOM64__
composeSubpageString(page, buf, 1024);
DGL_Enable(DGL_TEXTURE_2D);
FR_SetFont(FID(GF_FONTA));
FR_SetColorv(cfg.menuTextColors[1]);
FR_SetAlpha(mnRendState->pageAlpha);
FR_DrawTextXY3(buf, x, y, ALIGN_TOP, MN_MergeMenuEffectWithDrawTextFlags(0));
DGL_Disable(DGL_TEXTURE_2D);
#else
DGL_Enable(DGL_TEXTURE_2D);
DGL_Color4f(1, 1, 1, mnRendState->pageAlpha);
GL_DrawPatchXY2( pInvPageLeft[currentPage == 0 || (menuTime & 8)], x - 144, y, ALIGN_RIGHT);
GL_DrawPatchXY2(pInvPageRight[currentPage == totalPages-1 || (menuTime & 8)], x + 144, y, ALIGN_LEFT);
DGL_Disable(DGL_TEXTURE_2D);
#endif
}
static void drawPageHeading(mn_page_t *page, Point2Raw const *offset)
{
Point2Raw origin;
if(!page) return;
/// @kludge no title = no heading.
if(Str_IsEmpty(&page->title)) return;
// kludge end.
origin.x = SCREENWIDTH/2;
origin.y = (SCREENHEIGHT/2) - ((SCREENHEIGHT/2-5)/cfg.menuScale);
if(offset)
{
origin.x += offset->x;
origin.y += offset->y;
}
FR_PushAttrib();
Hu_MenuDrawPageTitle(Str_Text(&page->title), origin.x, origin.y); origin.y += 16;
drawPageNavigation(page, origin.x, origin.y);
FR_PopAttrib();
}
void MN_DrawPage(mn_page_t *page, float alpha, dd_bool showFocusCursor)
{
mn_object_t *focusObj;
int i, focusObjHeight;
Point2Raw cursorOrigin;
if(!page) return;