/
menudef.cpp
1686 lines (1568 loc) · 43.2 KB
/
menudef.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
/*
** menudef.cpp
** MENUDEF parser amd menu generation code
**
**---------------------------------------------------------------------------
** Copyright 2010 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include <float.h>
#include "menu.h"
#include "filesystem.h"
#include "c_bind.h"
#include "i_music.h"
#include "i_sound.h"
#include "cmdlib.h"
#include "vm.h"
#include "types.h"
#include "m_argv.h"
#include "i_soundfont.h"
#include "i_system.h"
#include "v_video.h"
#include "gstrings.h"
#include <zmusic.h>
#include "texturemanager.h"
#include "printf.h"
#include "i_interface.h"
bool CheckSkipGameOptionBlock(FScanner& sc);
MenuDescriptorList MenuDescriptors;
static DListMenuDescriptor *DefaultListMenuSettings; // contains common settings for all list menus
static DOptionMenuDescriptor *DefaultOptionMenuSettings; // contains common settings for all Option menus
FOptionMenuSettings OptionSettings;
FOptionMap OptionValues;
bool mustPrintErrors;
PClass *DefaultListMenuClass;
PClass *DefaultOptionMenuClass;
void I_BuildALDeviceList(FOptionValues *opt);
void I_BuildALResamplersList(FOptionValues *opt);
DEFINE_GLOBAL_NAMED(OptionSettings, OptionMenuSettings)
DEFINE_ACTION_FUNCTION(FOptionValues, GetCount)
{
PARAM_PROLOGUE;
PARAM_NAME(grp);
int cnt = 0;
FOptionValues **pGrp = OptionValues.CheckKey(grp);
if (pGrp != nullptr)
{
cnt = (*pGrp)->mValues.Size();
}
ACTION_RETURN_INT(cnt);
}
DEFINE_ACTION_FUNCTION(FOptionValues, GetValue)
{
PARAM_PROLOGUE;
PARAM_NAME(grp);
PARAM_UINT(index);
double val = 0;
FOptionValues **pGrp = OptionValues.CheckKey(grp);
if (pGrp != nullptr)
{
if (index < (*pGrp)->mValues.Size())
{
val = (*pGrp)->mValues[index].Value;
}
}
ACTION_RETURN_FLOAT(val);
}
DEFINE_ACTION_FUNCTION(FOptionValues, GetTextValue)
{
PARAM_PROLOGUE;
PARAM_NAME(grp);
PARAM_UINT(index);
FString val;
FOptionValues **pGrp = OptionValues.CheckKey(grp);
if (pGrp != nullptr)
{
if (index < (*pGrp)->mValues.Size())
{
val = (*pGrp)->mValues[index].TextValue;
}
}
ACTION_RETURN_STRING(val);
}
DEFINE_ACTION_FUNCTION(FOptionValues, GetText)
{
PARAM_PROLOGUE;
PARAM_NAME(grp);
PARAM_UINT(index);
FString val;
FOptionValues **pGrp = OptionValues.CheckKey(grp);
if (pGrp != nullptr)
{
if (index < (*pGrp)->mValues.Size())
{
val = (*pGrp)->mValues[index].Text;
}
}
ACTION_RETURN_STRING(val);
}
void DeinitMenus()
{
M_ClearMenus();
{
FOptionMap::Iterator it(OptionValues);
FOptionMap::Pair *pair;
while (it.NextPair(pair))
{
delete pair->Value;
pair->Value = nullptr;
}
}
MenuDescriptors.Clear();
OptionValues.Clear();
if (menuDelegate)
{
menuDelegate->Destroy();
menuDelegate = nullptr;
}
}
FTextureID GetMenuTexture(const char* const name)
{
const FTextureID texture = TexMan.CheckForTexture(name, ETextureType::MiscPatch);
if (!texture.Exists() && mustPrintErrors)
{
Printf("Missing menu texture: \"%s\"\n", name);
}
return texture;
}
//=============================================================================
//
//
//
//=============================================================================
static void SkipSubBlock(FScanner &sc)
{
sc.MustGetStringName("{");
sc.SkipToEndOfBlock();
}
//=============================================================================
//
//
//
//=============================================================================
static bool CheckSkipGameBlock(FScanner &sc, bool yes = true)
{
bool filter = false;
sc.MustGetStringName("(");
do
{
sc.MustGetString();
if (sysCallbacks.CheckGame) filter |= sysCallbacks.CheckGame(sc.String);
}
while (sc.CheckString(","));
sc.MustGetStringName(")");
if (filter != yes)
{
SkipSubBlock(sc);
return !sc.CheckString("else");
}
return false;
}
//=============================================================================
//
//
//
//=============================================================================
static bool CheckSkipOptionBlock(FScanner &sc)
{
bool filter = false;
sc.MustGetStringName("(");
do
{
sc.MustGetString();
if (sysCallbacks.CheckMenudefOption && sysCallbacks.CheckMenudefOption(sc.String)) filter = true;
else if (sc.Compare("Windows"))
{
#ifdef _WIN32
filter = true;
#endif
}
else if (sc.Compare("unix"))
{
#ifdef __unix__
filter = true;
#endif
}
else if (sc.Compare("Mac"))
{
#ifdef __APPLE__
filter = true;
#endif
}
else if (sc.Compare("OpenAL"))
{
filter |= IsOpenALPresent();
}
else if (sc.Compare("MMX"))
{
#ifdef HAVE_MMX
filter = true;
#endif
}
else if (sc.Compare("SWRender"))
{
#ifndef NO_SWRENDERER
filter = true;
#endif
}
}
while (sc.CheckString(","));
sc.MustGetStringName(")");
if (!filter)
{
SkipSubBlock(sc);
return !sc.CheckString("else");
}
return false;
}
//=============================================================================
//
//
//
//=============================================================================
static void DoParseListMenuBody(FScanner &sc, DListMenuDescriptor *desc, bool &sizecompatible, int insertIndex)
{
sc.MustGetStringName("{");
while (!sc.CheckString("}"))
{
sc.MustGetString();
if (sc.Compare("else"))
{
SkipSubBlock(sc);
}
else if (sc.Compare("ifgame"))
{
if (!CheckSkipGameBlock(sc))
{
// recursively parse sub-block
DoParseListMenuBody(sc, desc, sizecompatible, insertIndex);
}
}
else if (sc.Compare("ifnotgame"))
{
if (!CheckSkipGameBlock(sc, false))
{
// recursively parse sub-block
DoParseListMenuBody(sc, desc, sizecompatible, insertIndex);
}
}
else if (sc.Compare("ifoption"))
{
if (!CheckSkipOptionBlock(sc))
{
// recursively parse sub-block
DoParseListMenuBody(sc, desc, sizecompatible, insertIndex);
}
}
else if (sc.Compare("Class"))
{
sc.MustGetString();
PClass *cls = PClass::FindClass(sc.String);
if (cls == nullptr || !cls->IsDescendantOf("ListMenu"))
{
sc.ScriptError("Unknown menu class '%s'", sc.String);
}
desc->mClass = cls;
sizecompatible = false;
}
else if (sc.Compare("Selector"))
{
sc.MustGetString();
desc->mSelector = GetMenuTexture(sc.String);
sc.MustGetStringName(",");
sc.MustGetFloat();
desc->mSelectOfsX = sc.Float;
sc.MustGetStringName(",");
sc.MustGetFloat();
desc->mSelectOfsY = sc.Float;
}
else if (sc.Compare("Linespacing"))
{
sc.MustGetNumber();
desc->mLinespacing = sc.Number;
}
else if (sc.Compare("Position"))
{
sc.MustGetFloat();
desc->mXpos = sc.Float;
sc.MustGetStringName(",");
sc.MustGetFloat();
desc->mYpos = sc.Float;
}
else if (sc.Compare("Centermenu"))
{
desc->mCenter = true;
}
else if (sc.Compare("Selecteditem"))
{
desc->mSelectedItem = desc->mItems.Size() - 1;
}
else if (sc.Compare("animatedtransition"))
{
desc->mAnimatedTransition = true;
}
else if (sc.Compare("animated"))
{
desc->mAnimated = true;
}
else if (sc.Compare("DontDim"))
{
desc->mDontDim = true;
}
else if (sc.Compare("DontBlur"))
{
desc->mDontBlur = true;
}
else if (sc.Compare("MouseWindow"))
{
sc.MustGetNumber();
desc->mWLeft = sc.Number;
sc.MustGetStringName(",");
sc.MustGetNumber();
desc->mWRight = sc.Number;
}
else if (sc.Compare("Font"))
{
sc.MustGetString();
FFont *newfont = V_GetFont(sc.String);
if (newfont != nullptr) desc->mFont = newfont;
if (sc.CheckString(","))
{
sc.MustGetString();
desc->mFontColor2 = desc->mFontColor = V_FindFontColor((FName)sc.String);
if (sc.CheckString(","))
{
sc.MustGetString();
desc->mFontColor2 = V_FindFontColor((FName)sc.String);
}
}
else
{
desc->mFontColor = OptionSettings.mFontColor;
desc->mFontColor2 = OptionSettings.mFontColorValue;
}
}
else if (sc.Compare("NetgameMessage"))
{
sc.MustGetString();
desc->mNetgameMessage = sc.String;
}
else if (sc.Compare("size"))
{
desc->mCustomSizeSet = true;
if (sc.CheckNumber())
{
desc->mVirtWidth = sc.Number;
sc.MustGetStringName(",");
sc.MustGetNumber();
desc->mVirtHeight = sc.Number;
}
else
{
sc.MustGetString();
if (sc.Compare("clean"))
{
desc->mVirtWidth = -1;
}
else if (sc.Compare("optclean"))
{
desc->mVirtWidth = -2;
}
else
{
sc.ScriptError("Invalid value '%s' for 'size'", sc.String);
}
}
}
else if (sc.Compare("ForceList"))
{
desc->mForceList = true;
}
else
{
// all item classes from which we know that they support sized scaling.
// If anything else comes through here the option to swich scaling mode is disabled for this menu.
static const char* const compatibles[] = { "StaticPatch", "StaticText", "StaticTextCentered", "TextItem", "PatchItem", "PlayerDisplay", nullptr };
if (sc.MatchString(compatibles) < 0) sizecompatible = false;
bool success = false;
FStringf buildname("ListMenuItem%s", sc.String);
PClass *cls = PClass::FindClass(buildname);
if (cls != nullptr && cls->IsDescendantOf("ListMenuItem"))
{
bool inserting = insertIndex >= 0;
bool isSelectable = cls->IsDescendantOf("ListMenuItemSelectable");
double oldYpos = desc->mYpos;
// [Player701] If this is a selectable item and we're inserting in the middle,
// set the Y position of the descriptor to the Y of the item we're inserting before.
if (isSelectable && inserting)
{
desc->mYpos = desc->mItems[insertIndex]->mYpos;
}
auto func = dyn_cast<PFunction>(cls->FindSymbol("Init", true));
if (func != nullptr && !(func->Variants[0].Flags & (VARF_Protected | VARF_Private))) // skip internal classes which have a protected init method.
{
auto &args = func->Variants[0].Proto->ArgumentTypes;
TArray<VMValue> params;
int start = 1;
params.Push(0);
if (args.Size() > 1 && args[1] == NewPointer(PClass::FindClass("ListMenuDescriptor")))
{
params.Push(desc);
start = 2;
}
auto TypeCVar = NewPointer(NewStruct("CVar", nullptr, true));
// Note that this array may not be reallocated so its initial size must be the maximum possible elements.
TArray<FString> strings(args.Size());
for (unsigned i = start; i < args.Size(); i++)
{
sc.MustGetString();
if (args[i] == TypeString)
{
strings.Push(sc.String);
params.Push(&strings.Last());
}
else if (args[i] == TypeName)
{
params.Push(FName(sc.String).GetIndex());
}
else if (args[i] == TypeColor)
{
params.Push(V_GetColor(sc));
}
else if (args[i] == TypeFont)
{
auto f = V_GetFont(sc.String);
if (f == nullptr)
{
sc.ScriptError("Unknown font %s", sc.String);
}
params.Push(f);
}
else if (args[i] == TypeTextureID)
{
auto f = TexMan.CheckForTexture(sc.String, ETextureType::MiscPatch);
if (!f.Exists())
{
sc.ScriptMessage("Unknown texture %s", sc.String);
}
params.Push(f.GetIndex());
}
else if (args[i]->isIntCompatible())
{
char *endp;
int v = (int)strtoll(sc.String, &endp, 0);
if (*endp != 0)
{
// special check for font color ranges.
v = V_FindFontColor(sc.String);
if (v == CR_UNTRANSLATED && !sc.Compare("untranslated"))
{
// todo: check other data types that may get used.
sc.ScriptError("Integer expected, got %s", sc.String);
}
}
if (args[i] == TypeBool) v = !!v;
params.Push(v);
}
else if (args[i]->isFloat())
{
char *endp;
double v = strtod(sc.String, &endp);
if (*endp != 0)
{
sc.ScriptError("Float expected, got %s", sc.String);
}
params.Push(v);
}
else if (args[i] == TypeCVar)
{
auto cv = FindCVar(sc.String, nullptr);
if (cv == nullptr && *sc.String)
{
sc.ScriptError("Unknown CVar %s", sc.String);
}
params.Push(cv);
}
else
{
sc.ScriptError("Invalid parameter type %s for menu item", args[i]->DescriptiveName());
}
if (sc.CheckString(","))
{
if (i == args.Size() - 1)
{
sc.ScriptError("Too many parameters for %s", cls->TypeName.GetChars());
}
}
else
{
if (i < args.Size() - 1 && !(func->Variants[0].ArgFlags[i + 1] & VARF_Optional))
{
sc.ScriptError("Insufficient parameters for %s", cls->TypeName.GetChars());
}
break;
}
}
DMenuItemBase *item = (DMenuItemBase*)cls->CreateNew();
params[0] = item;
VMCallWithDefaults(func->Variants[0].Implementation, params, nullptr, 0);
if (!inserting)
{
desc->mItems.Push(item);
}
else
{
// [Player701] Insert item in between
desc->mItems.Insert(insertIndex, item);
insertIndex++;
}
if (isSelectable)
{
if (inserting)
{
// [Player701] If we've inserted a selectable item,
// shift all following selectable items downwards
// NB: index has been incremented, so we're not affecting the newly inserted item here.
for (unsigned int i = insertIndex; i < desc->mItems.Size(); i++)
{
auto litem = desc->mItems[i];
if (litem->GetClass()->IsDescendantOf("ListMenuItemSelectable"))
{
desc->mItems[i]->mYpos += desc->mLinespacing;
}
}
}
desc->mYpos = oldYpos + desc->mLinespacing;
if (desc->mSelectedItem == -1) desc->mSelectedItem = desc->mItems.Size() - 1;
}
success = true;
}
}
if (!success)
{
sc.ScriptError("Unknown keyword '%s'", sc.String);
}
}
}
for (auto &p : desc->mItems)
{
GC::WriteBarrier(p);
}
}
static void ParseListMenuBody(FScanner& sc, DListMenuDescriptor* desc, int insertIndex)
{
bool sizecompatible = true;
DoParseListMenuBody(sc, desc, sizecompatible, insertIndex);
if (!desc->mCustomSizeSet && !sizecompatible)
{
// No custom size and incompatible items,
// so force clean scaling for this menu
desc->mVirtWidth = -1;
}
}
//=============================================================================
//
//
//
//=============================================================================
static bool CheckCompatible(DMenuDescriptor *newd, DMenuDescriptor *oldd)
{
if (oldd->mClass == nullptr) return true;
return newd->mClass->IsDescendantOf(oldd->mClass);
}
static int GetGroup(DMenuItemBase *desc)
{
if (desc->IsKindOf(NAME_OptionMenuItemCommand)) return 2;
if (desc->IsKindOf(NAME_OptionMenuItemSubmenu)) return 1;
if (desc->IsKindOf(NAME_OptionMenuItemControlBase)) return 3;
if (desc->IsKindOf(NAME_OptionMenuItemOptionBase)) return 4;
if (desc->IsKindOf(NAME_OptionMenuSliderBase)) return 4;
if (desc->IsKindOf(NAME_OptionMenuFieldBase)) return 4;
if (desc->IsKindOf(NAME_OptionMenuItemColorPicker)) return 4;
if (desc->IsKindOf(NAME_OptionMenuItemStaticText)) return 5;
if (desc->IsKindOf(NAME_OptionMenuItemStaticTextSwitchable)) return 5;
return 0;
}
static bool FindMatchingItem(DMenuItemBase *desc)
{
int grp = GetGroup(desc);
if (grp == 0) return false; // no idea what this is.
if (grp == 5) return true; // static texts always match
FName name = desc->mAction;
if (grp == 1)
{
// Check for presence of menu
auto menu = MenuDescriptors.CheckKey(name);
if (menu == nullptr) return false;
}
else if (grp == 4)
{
static const FName CVarBlacklist[] = {
NAME_snd_waterlp, NAME_snd_output, NAME_snd_output_format, NAME_snd_speakermode, NAME_snd_resampler, NAME_AlwaysRun };
// Check for presence of CVAR and blacklist
auto cv = FindCVar(name.GetChars(), nullptr);
if (cv == nullptr) return true;
for (auto bname : CVarBlacklist)
{
if (name == bname) return true;
}
}
MenuDescriptorList::Iterator it(MenuDescriptors);
MenuDescriptorList::Pair *pair;
while (it.NextPair(pair))
{
for (auto item : pair->Value->mItems)
{
if (item->mAction == name && GetGroup(item) == grp) return true;
}
}
return false;
}
static bool ReplaceMenu(FScanner &sc, DMenuDescriptor *desc)
{
DMenuDescriptor **pOld = MenuDescriptors.CheckKey(desc->mMenuName);
if (pOld != nullptr && *pOld != nullptr)
{
if ((*pOld)->mProtected)
{
// If this tries to replace an option menu with an option menu, let's append all new entries to the old menu.
// Otherwise bail out because for list menus it's not that simple.
if (!desc->IsKindOf(RUNTIME_CLASS(DOptionMenuDescriptor)) || !(*pOld)->IsKindOf(RUNTIME_CLASS(DOptionMenuDescriptor)))
{
sc.ScriptMessage("Cannot replace protected menu %s.", desc->mMenuName.GetChars());
return true;
}
for (int i = desc->mItems.Size()-1; i >= 0; i--)
{
if (FindMatchingItem(desc->mItems[i]))
{
desc->mItems.Delete(i);
}
}
if (desc->mItems.Size() > 0)
{
auto sep = CreateOptionMenuItemStaticText(" ");
(*pOld)->mItems.Push(sep);
sep = CreateOptionMenuItemStaticText("---------------", 1);
(*pOld)->mItems.Push(sep);
for (auto it : desc->mItems)
{
(*pOld)->mItems.Push(it);
}
desc->mItems.Clear();
//sc.ScriptMessage("Merged %d items into %s", desc->mItems.Size(), desc->mMenuName.GetChars());
}
return true;
}
if (!CheckCompatible(desc, *pOld))
{
sc.ScriptMessage("Tried to replace menu '%s' with a menu of different type", desc->mMenuName.GetChars());
return true;
}
}
MenuDescriptors[desc->mMenuName] = desc;
GC::WriteBarrier(desc);
return false;
}
//=============================================================================
//
//
//
//=============================================================================
static void ParseListMenu(FScanner &sc)
{
sc.MustGetString();
DListMenuDescriptor *desc = Create<DListMenuDescriptor>();
desc->Reset();
desc->mMenuName = sc.String;
desc->mSelectedItem = -1;
desc->mAutoselect = -1;
desc->mSelectOfsX = DefaultListMenuSettings->mSelectOfsX;
desc->mSelectOfsY = DefaultListMenuSettings->mSelectOfsY;
desc->mSelector = DefaultListMenuSettings->mSelector;
desc->mDisplayTop = DefaultListMenuSettings->mDisplayTop;
desc->mXpos = DefaultListMenuSettings->mXpos;
desc->mYpos = DefaultListMenuSettings->mYpos;
desc->mLinespacing = DefaultListMenuSettings->mLinespacing;
desc->mNetgameMessage = DefaultListMenuSettings->mNetgameMessage;
desc->mFont = DefaultListMenuSettings->mFont;
desc->mFontColor = DefaultListMenuSettings->mFontColor;
desc->mFontColor2 = DefaultListMenuSettings->mFontColor2;
desc->mClass = nullptr;
desc->mWLeft = 0;
desc->mWRight = 0;
desc->mCenter = false;
desc->mFromEngine = fileSystem.GetFileContainer(sc.LumpNum) == 0; // flags menu if the definition is from the IWAD.
desc->mVirtWidth = -2;
desc->mCustomSizeSet = false;
desc->mAnimatedTransition = false;
desc->mAnimated = false;
desc->mDontDim = false;
desc->mDontBlur = false;
desc->mForceList = false;
if (DefaultListMenuSettings->mCustomSizeSet)
{
desc->mVirtHeight = DefaultListMenuSettings->mVirtHeight;
desc->mVirtWidth = DefaultListMenuSettings->mVirtWidth;
desc->mCustomSizeSet = true;
}
ParseListMenuBody(sc, desc, -1);
ReplaceMenu(sc, desc);
}
//=============================================================================
//
// [Player701] Common function for figuring out where to insert items
// for AddListMenu and AddOptionMenu
//
//=============================================================================
static int GetInsertIndex(FScanner& sc, DMenuDescriptor* desc)
{
bool before = sc.CheckString("BEFORE");
bool after = sc.CheckString("AFTER");
int insertIndex = -1;
if (before || after)
{
// Find an existing menu item to use as insertion point
sc.MustGetString();
int n = desc->mItems.Size();
for (int i = 0; i < n; i++)
{
auto item = desc->mItems[i];
if (item->mAction == sc.String)
{
insertIndex = before ? i : i + 1;
break;
}
}
// Inserting after the last item is the same as inserting at the end
if (insertIndex == n) insertIndex = -1;
// Don't error out if we haven't found a suitable item
// to avoid backwards compatibility issues.
}
return insertIndex;
}
//=============================================================================
//
// [Player701] Allow extending list menus
//
//=============================================================================
static void ParseAddListMenu(FScanner& sc)
{
sc.MustGetString();
DMenuDescriptor** pOld = MenuDescriptors.CheckKey(sc.String);
if (pOld == nullptr || *pOld == nullptr || !(*pOld)->IsKindOf(RUNTIME_CLASS(DListMenuDescriptor)))
{
sc.ScriptError("%s is not a list menu that can be extended", sc.String);
return;
}
ParseListMenuBody(sc, (DListMenuDescriptor*)(*pOld), GetInsertIndex(sc, *pOld));
}
//=============================================================================
//
//
//
//=============================================================================
static void ParseOptionValue(FScanner &sc)
{
FOptionValues *val = new FOptionValues;
sc.MustGetString();
FName optname = sc.String;
sc.MustGetStringName("{");
while (!sc.CheckString("}"))
{
FOptionValues::Pair &pair = val->mValues[val->mValues.Reserve(1)];
sc.MustGetFloat();
pair.Value = sc.Float;
sc.MustGetStringName(",");
sc.MustGetString();
pair.Text = strbin1(sc.String);
}
FOptionValues **pOld = OptionValues.CheckKey(optname);
if (pOld != nullptr && *pOld != nullptr)
{
delete *pOld;
}
OptionValues[optname] = val;
}
//=============================================================================
//
//
//
//=============================================================================
static void ParseOptionString(FScanner &sc)
{
FOptionValues *val = new FOptionValues;
sc.MustGetString();
FName optname = sc.String;
sc.MustGetStringName("{");
while (!sc.CheckString("}"))
{
FOptionValues::Pair &pair = val->mValues[val->mValues.Reserve(1)];
sc.MustGetString();
pair.Value = DBL_MAX;
pair.TextValue = sc.String;
sc.MustGetStringName(",");
sc.MustGetString();
pair.Text = strbin1(sc.String);
}
FOptionValues **pOld = OptionValues.CheckKey(optname);
if (pOld != nullptr && *pOld != nullptr)
{
delete *pOld;
}
OptionValues[optname] = val;
}
//=============================================================================
//
//
//
//=============================================================================
static void ParseOptionSettings(FScanner &sc)
{
sc.MustGetStringName("{");
while (!sc.CheckString("}"))
{
sc.MustGetString();
if (sc.Compare("else"))
{
SkipSubBlock(sc);
}
else if (sc.Compare("ifgame"))
{
if (!CheckSkipGameBlock(sc))
{
// recursively parse sub-block
ParseOptionSettings(sc);
}
}
else if (sc.Compare("ifnotgame"))
{
if (!CheckSkipGameBlock(sc, false))
{
// recursively parse sub-block
ParseOptionSettings(sc);
}
}
else if (sc.Compare("Linespacing"))
{
sc.MustGetNumber();
// ignored
}
else if (sc.Compare("LabelOffset"))
{
sc.MustGetNumber();
// ignored
}
else
{
sc.ScriptError("Unknown keyword '%s'", sc.String);
}
}
}
//=============================================================================
//
//
//
//=============================================================================
static void ParseOptionMenuBody(FScanner &sc, DOptionMenuDescriptor *desc, int insertIndex)
{
sc.MustGetStringName("{");
while (!sc.CheckString("}"))
{
sc.MustGetString();
if (sc.Compare("else"))
{
SkipSubBlock(sc);
}
else if (sc.Compare("ifgame"))
{
if (!CheckSkipGameBlock(sc))
{
// recursively parse sub-block
ParseOptionMenuBody(sc, desc, insertIndex);
}
}
else if (sc.Compare("ifnotgame"))
{
if (!CheckSkipGameBlock(sc, false))
{
// recursively parse sub-block
ParseOptionMenuBody(sc, desc, insertIndex);
}
}
else if (sc.Compare("ifoption"))
{
if (!CheckSkipOptionBlock(sc))
{
// recursively parse sub-block
ParseOptionMenuBody(sc, desc, insertIndex);
}
}
else if (sc.Compare("Class"))