-
Notifications
You must be signed in to change notification settings - Fork 39
/
UIMenu.cs
2161 lines (1837 loc) · 81.5 KB
/
UIMenu.cs
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
/*
*
*
* Created by: Guad, CamxxCore, jedijosh920
*
*
* Ported by: alexguirre, Stealth22, LtFlash
*
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Rage;
using Rage.Native;
using RAGENativeUI.Elements;
using RAGENativeUI.Internals;
namespace RAGENativeUI
{
public delegate void IndexChangedEvent(UIMenu sender, int newIndex);
public delegate void ListChangedEvent(UIMenu sender, UIMenuListItem listItem, int newIndex);
public delegate void CheckboxChangeEvent(UIMenu sender, UIMenuCheckboxItem checkboxItem, bool Checked);
public delegate void ScrollerChangedEvent(UIMenu sender, UIMenuScrollerItem item, int oldIndex, int newIndex);
public delegate void ItemSelectEvent(UIMenu sender, UIMenuItem selectedItem, int index);
public delegate void MenuOpenEvent(UIMenu sender);
public delegate void MenuCloseEvent(UIMenu sender);
public delegate void MenuChangeEvent(UIMenu oldMenu, UIMenu newMenu, bool forward);
public delegate void ItemActivatedEvent(UIMenu sender, UIMenuItem selectedItem);
public delegate void ItemCheckboxEvent(UIMenuCheckboxItem sender, bool Checked);
public delegate void ItemListEvent(UIMenuItem sender, int newIndex);
public delegate void ItemScrollerEvent(UIMenuScrollerItem sender, int oldIndex, int newIndex);
/// <summary>
/// Base class for RAGENativeUI. Calls the next events: OnIndexChange, OnListChanged, OnCheckboxChange, OnItemSelect, OnMenuClose, OnMenuchange.
/// </summary>
public class UIMenu : ScrollableListBase<UIMenuItem>
{
/// <summary>
/// Represents the default value of <see cref="MaxItemsOnScreen"/>.
/// </summary>
public const int DefaultMaxItemsOnScreen = 10;
/// <summary>
/// Represents the default value of <see cref="Width"/>.
/// </summary>
public const float DefaultWidth = 0.225f;
/// <summary>
/// Represents the default value of <see cref="TitleStyle"/>.
/// </summary>
public static readonly TextStyle DefaultTitleStyle = TextStyle.Default.With(
color: Color.White,
font: TextFont.HouseScript,
justification: TextJustification.Center,
scale: 1.025f);
/// <summary>
/// Represents the default value of <see cref="SubtitleStyle"/>.
/// </summary>
public static readonly TextStyle DefaultSubtitleStyle = TextStyle.Default.With(
color: Color.White,
font: TextFont.ChaletLondon,
justification: TextJustification.Left,
scale: 0.35f);
/// <summary>
/// Represents the default value of <see cref="CounterStyle"/>.
/// </summary>
public static readonly TextStyle DefaultCounterStyle = TextStyle.Default.With(
color: Color.White,
font: TextFont.ChaletLondon,
justification: TextJustification.Left,
scale: 0.35f);
/// <summary>
/// Represents the default value of <see cref="DescriptionStyle"/>.
/// </summary>
public static readonly TextStyle DefaultDescriptionStyle = TextStyle.Default.With(
color: Color.WhiteSmoke,
font: TextFont.ChaletLondon,
justification: TextJustification.Left,
scale: 0.35f);
/// <summary>
/// Represets the default value of <see cref="DescriptionSeparatorColor"/>.
/// </summary>
public static readonly Color DefaultDescriptionSeparatorColor = Color.Black;
/// <summary>
/// Represets the default value of <see cref="SubtitleBackgroundColor"/>.
/// </summary>
public static readonly Color DefaultSubtitleBackgroundColor = Color.Black;
/// <summary>
/// Represets the default value of <see cref="UpDownArrowsBackgroundColor"/>.
/// </summary>
public static readonly Color DefaultUpDownArrowsBackgroundColor = Color.FromArgb(204, 0, 0, 0);
/// <summary>
/// Represets the default value of <see cref="UpDownArrowsHighlightColor"/>.
/// </summary>
public static readonly Color DefaultUpDownArrowsHighlightColor = Color.FromArgb(25, 255, 255, 255);
/// <summary>
/// Represets the default value of <see cref="UpDownArrowsForegroundColor"/>.
/// </summary>
public static readonly Color DefaultUpDownArrowsForegroundColor = Color.White;
internal const string CommonTxd = "commonmenu";
internal const string BackgroundTextureName = "gradient_bgd";
internal const string UpAndDownTextureName = "shop_arrows_upanddown";
internal const string NavBarTextureName = "gradient_nav";
internal const string ArrowLeftTextureName = "arrowleft";
internal const string ArrowRightTextureName = "arrowright";
internal const string CheckboxTickTextureName = "shop_box_tick";
internal const string CheckboxCrossTextureName = "shop_box_cross";
internal const string CheckboxBlankTextureName = "shop_box_blank";
internal const string CheckboxTickSelectedTextureName = "shop_box_tickb";
internal const string CheckboxCrossSelectedTextureName = "shop_box_crossb";
internal const string CheckboxBlankSelectedTextureName = "shop_box_blankb";
internal const string DefaultBannerTextureName = "interaction_bgd";
/// <summary>
/// Keeps track of the number of visible menus from the executing plugin.
/// Used to keep <see cref="Shared.NumberOfVisibleMenus"/> consistent when unloading the plugin with some menu open.
/// </summary>
internal static uint NumberOfVisibleMenus { get; set; }
/// <summary>
/// Gets whether any menu is currently visible. Includes menus from the executing plugin and from other plugins.
/// </summary>
public static bool IsAnyMenuVisible => Shared.NumberOfVisibleMenus > 0;
#if false
// This can be used to detect if any native script menu is initialized such that the script can draw it.
// Initially added to add support for native script menus to IsAnyMenuVisible but this method is not accurate enough,
// some scripts may only set the menuId in the array while the menu is visible, others may keep it when not drawing it (i.e. gunshop, golf).
// So for now disabled
public static bool IsAnyGameMenuReady
{
get
{
foreach (uint menuId in ScriptGlobals.MenuIds)
{
if (menuId != 0)
{
return true;
}
}
return false;
}
}
#endif
private Sprite _bannerSprite;
private ResRectangle _bannerRectangle;
private Texture _customBanner;
private RectangleF currentItemBounds = RectangleF.Empty;
private bool visible;
private bool justOpenedProcessInputFirstTick = true;
private bool justOpenedProcessInput = true;
private bool justOpenedProcessMouse = true;
private int hoveredUpDown = 0; // 0 = none, 1 = up, 2 = down
public string AUDIO_LIBRARY = "HUD_FRONTEND_DEFAULT_SOUNDSET";
public string AUDIO_UPDOWN = "NAV_UP_DOWN";
public string AUDIO_LEFTRIGHT = "NAV_LEFT_RIGHT";
public string AUDIO_SELECT = "SELECT";
public string AUDIO_BACK = "BACK";
public string AUDIO_ERROR = "ERROR";
public List<UIMenuItem> MenuItems = new List<UIMenuItem>();
protected override List<UIMenuItem> Items {
get => MenuItems;
set => MenuItems = value;
}
public bool MouseEdgeEnabled = true;
public bool ControlDisablingEnabled = true;
public bool ResetCursorOnOpen = true;
[Obsolete("Now description are always wrapped to fit the description box."), EditorBrowsable(EditorBrowsableState.Never)]
public bool FormatDescriptions = true;
public bool MouseControlsEnabled = true;
public bool AllowCameraMovement = false;
public bool ScaleWithSafezone = true;
// Whether the value of ResetCursorOnOpen should be respected,
// set to true while opening the parent menu to avoid resetting the cursor while navigating the menus
private bool IgnoreResetCursorOnOpen { get; set; }
//Events
/// <summary>
/// Called when user presses up or down, changing current selection.
/// </summary>
public event IndexChangedEvent OnIndexChange;
/// <summary>
/// Called when user presses left or right, changing a list position.
/// </summary>
public event ListChangedEvent OnListChange;
/// <summary>
/// Called when user scrolls through a <see cref="UIMenuScrollerItem"/>, changing its index.
/// </summary>
public event ScrollerChangedEvent OnScrollerChange;
/// <summary>
/// Called when user presses enter on a checkbox item.
/// </summary>
public event CheckboxChangeEvent OnCheckboxChange;
/// <summary>
/// Called when user selects a simple item.
/// </summary>
public event ItemSelectEvent OnItemSelect;
/// <summary>
/// Called when the menu becomes visible.
/// </summary>
public event MenuOpenEvent OnMenuOpen;
/// <summary>
/// Called when user closes the menu or goes back in a menu chain.
/// </summary>
public event MenuCloseEvent OnMenuClose;
/// <summary>
/// Called when user either clicks on a binded button or goes back to a parent menu.
/// </summary>
public event MenuChangeEvent OnMenuChange;
// internal, needed by UIMenuSwitchMenusItem
internal readonly Controls controls = new Controls();
//Tree structure
public Dictionary<UIMenuItem, UIMenu> Children { get; private set; }
/// <summary>
/// Gets the <see cref="Elements.InstructionalButtons"/> instance used by this menu.
/// </summary>
public InstructionalButtons InstructionalButtons { get; }
/// <summary>
/// Gets or sets whether the instructional buttons are currently visible.
/// </summary>
public bool InstructionalButtonsEnabled { get; set; } = true;
private readonly List<IInstructionalButtonSlot> instructionalButtonsFromPanels = new();
private readonly List<UIMenuPanel> lastPanels = new();
/// <summary>
/// Basic Menu constructor.
/// </summary>
/// <param name="title">Title that appears on the big banner.</param>
/// <param name="subtitle">Subtitle that appears in capital letters in a small black bar.</param>
public UIMenu(string title, string subtitle) : this(title, subtitle, new Point(0, 0), CommonTxd, DefaultBannerTextureName)
{
}
/// <summary>
/// Basic Menu constructor with an offset.
/// </summary>
/// <param name="title">Title that appears on the big banner.</param>
/// <param name="subtitle">Subtitle that appears in capital letters in a small black bar. Set to "" if you dont want a subtitle.</param>
/// <param name="offset">Point object with X and Y data for offsets. Applied to all menu elements.</param>
public UIMenu(string title, string subtitle, Point offset) : this(title, subtitle, offset, CommonTxd, DefaultBannerTextureName)
{
}
/// <summary>
/// Initialise a menu with a custom texture banner.
/// </summary>
/// <param name="title">Title that appears on the big banner. Set to "" if you don't want a title.</param>
/// <param name="subtitle">Subtitle that appears in capital letters in a small black bar. Set to "" if you dont want a subtitle.</param>
/// <param name="offset">Point object with X and Y data for offsets. Applied to all menu elements.</param>
/// <param name="customBanner">Your custom Rage.Texture.</param>
public UIMenu(string title, string subtitle, Point offset, Texture customBanner) : this(title, subtitle, offset, CommonTxd, DefaultBannerTextureName)
{
_customBanner = customBanner;
}
/// <summary>
/// Advanced Menu constructor that allows custom title banner.
/// </summary>
/// <param name="title">Title that appears on the big banner. Set to "" if you are using a custom banner.</param>
/// <param name="subtitle">Subtitle that appears in capital letters in a small black bar.</param>
/// <param name="offset">Point object with X and Y data for offsets. Applied to all menu elements.</param>
/// <param name="spriteLibrary">Sprite library name for the banner.</param>
/// <param name="spriteName">Sprite name for the banner.</param>
public UIMenu(string title, string subtitle, Point offset, string spriteLibrary, string spriteName)
{
Offset = offset;
Children = new Dictionary<UIMenuItem, UIMenu>();
InstructionalButtons = new InstructionalButtons { MouseButtonsEnabled = true };
InstructionalButtons.Buttons.Add(new InstructionalButton(GameControl.FrontendAccept, "Select"),
new InstructionalButton(GameControl.FrontendCancel, "Back"));
_bannerSprite = new Sprite(spriteLibrary, spriteName, Point.Empty, Size.Empty);
TitleText = title;
SubtitleText = subtitle;
if (subtitle != null && subtitle.StartsWith("~"))
{
CounterPretext = subtitle.Substring(0, subtitle.IndexOf('~', 1) + 1);
}
MaxItemsOnScreen = DefaultMaxItemsOnScreen;
CurrentSelection = -1;
SetKey(Common.MenuControls.Up, GameControl.FrontendUp, 2);
SetKey(Common.MenuControls.Up, GameControl.CursorScrollUp, 2);
SetKey(Common.MenuControls.Down, GameControl.FrontendDown, 2);
SetKey(Common.MenuControls.Down, GameControl.CursorScrollDown, 2);
SetKey(Common.MenuControls.Left, GameControl.FrontendLeft, 2);
SetKey(Common.MenuControls.Right, GameControl.FrontendRight, 2);
SetKey(Common.MenuControls.Select, GameControl.FrontendAccept, 2);
SetKey(Common.MenuControls.Back, GameControl.FrontendCancel, 2);
SetKey(Common.MenuControls.Back, GameControl.FrontendPause, 2);
SetKey(Common.MenuControls.Back, GameControl.CursorCancel, 2);
}
/// <summary>
/// Gets or sets the current width offset in pixels.
/// </summary>
public int WidthOffset { get; set; } = 0;
/// <summary>
/// Gets or sets the menu width in relative coordinates.
/// </summary>
public float Width { get; set; } = DefaultWidth;
/// <summary>
/// Gets the menu width in relative coordinates, adjusted for the current aspect ratio and <see cref="WidthOffset"/> value.
/// </summary>
public float AdjustedWidth
{
get
{
float adjusted = Width * (16f / 9f / AspectRatio);
if (WidthOffset != 0)
{
adjusted += WidthOffset / ActualResolution.Width;
}
return adjusted;
}
}
/// <summary>
/// Change the menu's width. The width is calculated as DefaultWidth + WidthOffset, so a width offset of 10 would enlarge the menu by 10 pixels.
/// </summary>
/// <param name="widthOffset">New width offset.</param>
[Obsolete("Use UIMenu.WidthOffset setter instead."), EditorBrowsable(EditorBrowsableState.Never)]
public void SetMenuWidthOffset(int widthOffset)
{
WidthOffset = widthOffset;
}
/// <summary>
/// Gets or sets the current position offset in pixels.
/// </summary>
public Point Offset { get; set; }
/// <summary>
/// Enable or disable all controls but the necessary to operate a menu.
/// </summary>
/// <param name="enable"></param>
public static void DisEnableControls(bool enable)
{
if (enable)
{
N.EnableAllControlActions(0);
}
else
{
for (int i = 0; i < ControlsToDisable.Length; i++)
{
N.DisableControlAction(0, ControlsToDisable[i]);
}
}
}
// controls taken from the game scripts
private static readonly GameControl[] ControlsToDisable = new[]
{
GameControl.SelectWeapon,
GameControl.SelectWeaponUnarmed,
GameControl.SelectWeaponMelee,
GameControl.SelectWeaponHandgun,
GameControl.SelectWeaponShotgun,
GameControl.SelectWeaponSmg,
GameControl.SelectWeaponAutoRifle,
GameControl.SelectWeaponSniper,
GameControl.SelectWeaponHeavy,
GameControl.SelectWeaponSpecial,
GameControl.WeaponWheelNext,
GameControl.WeaponWheelPrev,
GameControl.WeaponSpecial,
GameControl.WeaponSpecialTwo,
GameControl.MeleeAttackLight,
GameControl.MeleeAttackHeavy,
GameControl.MeleeBlock,
GameControl.Detonate,
GameControl.Context,
GameControl.Reload,
GameControl.Dive,
GameControl.Sprint,
GameControl.VehicleDuck,
GameControl.VehicleHeadlight,
GameControl.VehicleRadioWheel,
GameControl.NextCamera,
GameControl.VehicleCinCam,
GameControl.VehiclePushbikeSprint,
GameControl.VehiclePushbikePedal,
GameControl.SelectCharacterMichael,
GameControl.SelectCharacterFranklin,
GameControl.SelectCharacterTrevor,
GameControl.SelectCharacterMultiplayer,
GameControl.CharacterWheel,
};
/// <summary>
/// Enable or disable the instructional buttons.
/// </summary>
/// <param name="disable"></param>
public void DisableInstructionalButtons(bool disable)
{
InstructionalButtonsEnabled = !disable;
}
/// <summary>
/// Set the banner to your own Sprite object.
/// </summary>
/// <param name="spriteBanner">Sprite object. The position and size does not matter.</param>
public void SetBannerType(Sprite spriteBanner)
{
_bannerRectangle = null;
_customBanner = null;
_bannerSprite = spriteBanner;
}
/// <summary>
/// Set the banner to your own Rectangle.
/// </summary>
/// <param name="rectangle">UIResRectangle object. Position and size does not matter.</param>
[Obsolete("Use UIMenu.SetBannerType(Color) instead."), EditorBrowsable(EditorBrowsableState.Never)]
public void SetBannerType(ResRectangle rectangle)
{
_bannerSprite = null;
_customBanner = null;
_bannerRectangle = rectangle;
}
/// <summary>
/// Sets the banner to a single color rectangle.
/// </summary>
/// <param name="color">The new color of the banner.</param>
public void SetBannerType(Color color)
{
#pragma warning disable CS0612, CS0618 // Type or member is obsolete
SetBannerType(new ResRectangle(Point.Empty, Size.Empty, color));
#pragma warning restore CS0612, CS0618 // Type or member is obsolete
}
/// <summary>
/// Set the banner to your own custom texture.
/// </summary>
/// <param name="texture">Rage.Texture object</param>
public void SetBannerType(Texture texture)
{
_bannerSprite = null;
_bannerRectangle = null;
_customBanner = texture;
}
/// <summary>
/// Removes the banner.
/// </summary>
public void RemoveBanner() => SetBannerType((Sprite)null);
/// <summary>
/// Add an item to the menu.
/// </summary>
/// <param name="item">Item object to be added. Can be normal item, checkbox or list item.</param>
public void AddItem(UIMenuItem item)
{
item.Parent = this;
MenuItems.Add(item);
RefreshCurrentSelection();
}
/// <summary>
/// Add an item to the menu at the specified index.
/// </summary>
/// <param name="item">Item object to be added. Can be normal item, checkbox or list item.</param>
/// <param name="index"></param>
public void AddItem(UIMenuItem item, int index)
{
if (index >= 0 && index < MenuItems.Count && MenuItems[index].Selected)
{
// if the item at the specified position is selected, unselect it and select the new item
MenuItems[index].Selected = false;
item.Selected = true;
}
item.Parent = this;
MenuItems.Insert(index, item);
RefreshCurrentSelection();
}
public void AddItems(IEnumerable<UIMenuItem> items)
{
foreach (UIMenuItem i in items)
{
i.Parent = this;
MenuItems.Add(i);
}
RefreshCurrentSelection();
}
public void AddItems(params UIMenuItem[] items) => AddItems((IEnumerable<UIMenuItem>)items);
/// <summary>
/// Remove an item at index n.
/// </summary>
/// <param name="index">Index to remove the item at.</param>
public void RemoveItemAt(int index)
{
if (MaxItem == MenuItems.Count - 1) // if max visible item matches last item, move the visible item range up by one item
{
MinItem = Math.Max(MinItem - 1, 0);
MaxItem = Math.Max(MaxItem - 1, 0);
}
MenuItems.RemoveAt(index);
RefreshCurrentSelection(); // refresh the current selection in case we removed the selected item
}
/// <summary>
/// Draw your custom banner.
/// </summary>
/// <param name="e">Rage.GraphicsEventArgs to draw on.</param>
[Obsolete("UIMenu.DrawBanner(GraphicsEventArgs) will be removed soon, use UIMenu.DrawBanner(Graphics) instead."), EditorBrowsable(EditorBrowsableState.Never)]
public void DrawBanner(GraphicsEventArgs e)
{
}
/// <summary>
/// Draw your custom banner.
/// <para>
/// To prevent flickering call it inside a <see cref="Game.RawFrameRender"/> event handler.
/// </para>
/// </summary>
/// <param name="g">The <see cref="Rage.Graphics"/> to draw on.</param>
public virtual void DrawBanner(Rage.Graphics g)
{
if (!Visible || _customBanner == null)
{
return;
}
g.DrawTexture(_customBanner, customBannerX, customBannerY, customBannerW, customBannerH);
}
// drawing variables
private float menuWidth;
private float itemHeight = 0.034722f;
private float itemsX, itemsY; // start position of the items, for mouse input
private float upDownX, upDownY; // start position of the up-down arrows, for mouse input
private float customBannerX, customBannerY,
customBannerW, customBannerH;
private static SizeF ActualResolution => Internals.Screen.ActualResolution;
private static float AspectRatio => N.GetAspectRatio(false);
private static bool IsWideScreen => AspectRatio > 1.5f; // equivalent to GET_IS_WIDESCREEN
private static bool IsUltraWideScreen => AspectRatio > 3.5f; // > 32:9
internal static void DrawSprite(string txd, string texName, float x, float y, float w, float h, Color c)
=> N.DrawSprite(txd, texName, x, y, w, h, 0.0f, c.R, c.G, c.B, c.A);
internal static void DrawRect(float x, float y, float w, float h, Color c)
=> N.DrawRect(x, y, w, h, c.R, c.G, c.B, c.A);
/// <summary>
/// Called before drawing the menu or before getting screen coordinates for mouse input.
/// Intended to align the menu with the safe zone using the <c>SET_SCRIPT_GFX_*</c> natives.
/// </summary>
protected virtual void BeginDraw()
{
if (ScaleWithSafezone)
{
N.SetScriptGfxAlign('L', 'T');
N.SetScriptGfxAlignParams(-0.05f, -0.05f, 0.0f, 0.0f);
}
}
/// <summary>
/// Called after drawing the menu or after getting screen coordinates for mouse input to reset the state modified in <see cref="BeginDraw"/>.
/// </summary>
protected virtual void EndDraw()
{
if (ScaleWithSafezone)
{
N.ResetScriptGfxAlign();
}
}
/// <summary>
/// Draw the menu and all of it's components.
/// </summary>
public virtual void Draw()
{
if (!Visible)
{
return;
}
if (_bannerSprite != null)
{
_bannerSprite.LoadTextureDictionary();
if (!_bannerSprite.IsTextureDictionaryLoaded)
{
return;
}
}
N.RequestStreamedTextureDict(CommonTxd);
if (!N.HasStreamedTextureDictLoaded(CommonTxd))
{
return;
}
if (ControlDisablingEnabled)
DisEnableControls(false);
if (AllowCameraMovement && ControlDisablingEnabled)
EnableCameraMovement();
if (InstructionalButtonsEnabled)
{
InstructionalButtons.Draw();
}
menuWidth = AdjustedWidth; // save to a field to avoid calculating AdjustedWidth multiple times each tick
BeginDraw();
var res = ActualResolution;
float x = 0.05f + Offset.X / res.Width;
float y = 0.05f + Offset.Y / res.Height;
DrawBanner(x, ref y);
DrawSubtitle(x, ref y);
DrawBackground(x, y);
DrawItems(x, ref y);
DrawUpDownArrows(x, ref y);
DrawDescription(x, ref y);
DrawPanels(x, ref y);
EndDraw();
}
/// <summary>
/// Draws the banner and title at the specified position.
/// </summary>
/// <param name="x">The position along the X-axis in relative coordinates.</param>
/// <param name="y">
/// The position along the Y-axis in relative coordinates.
/// When this method returns, contains the position right below the banner.
/// </param>
protected virtual void DrawBanner(float x, ref float y)
{
if (_bannerSprite == null && _bannerRectangle == null && _customBanner == null)
{
return;
}
GetBannerDrawSize(out float bannerWidth, out float bannerHeight);
if (_bannerSprite != null)
{
DrawSprite(_bannerSprite.TextureDictionary, _bannerSprite.TextureName, x + menuWidth * 0.5f, y + bannerHeight * 0.5f, bannerWidth, bannerHeight, Color.White);
}
else if (_bannerRectangle != null)
{
DrawRect(x + menuWidth * 0.5f, y + bannerHeight * 0.5f, bannerWidth, bannerHeight, _bannerRectangle.Color);
}
if (_bannerSprite != null || _bannerRectangle != null)
{
DrawTitle(x, y, bannerHeight);
}
else if (_customBanner != null)
{
N.GetScriptGfxPosition(x, y, out customBannerX, out customBannerY);
N.GetScriptGfxPosition(x + menuWidth, y + bannerHeight, out customBannerW, out customBannerH);
customBannerW -= customBannerX;
customBannerH -= customBannerY;
// relative coords to pixel coords
var mainRes = ActualResolution;
customBannerX *= mainRes.Width;
customBannerY *= mainRes.Height;
customBannerW *= mainRes.Width;
customBannerH *= mainRes.Height;
// offset to the middle screen
// in multi-monitor setups game uses the middle screen as the origin, but Rage.Graphics uses
// the left-most edge so we need to offset the coordinates
N.GetActiveScreenResolution(out int totalWidth, out int totalHeight);
var ox = totalWidth / 2.0f - mainRes.Width / 2;
var oy = totalHeight / 2.0f - mainRes.Height / 2;
customBannerX += ox;
customBannerY += oy;
}
y += bannerHeight;
}
private void DrawTitle(float x, float y, float bannerHeight)
{
if (string.IsNullOrEmpty(TitleText))
{
return;
}
float titleX = x + menuWidth * 0.5f;
float titleY = y + bannerHeight * 0.225f;
TextStyle s = TitleStyle;
s.Wrap = (x, x + menuWidth);
TextCommands.Display(TitleText, s, titleX, titleY);
}
/// <summary>
/// Draws the subtitle at the specified position.
/// </summary>
/// <param name="x">The position along the X-axis in relative coordinates.</param>
/// <param name="y">
/// The position along the Y-axis in relative coordinates.
/// When this method returns, contains the position right below the subtitle.
/// </param>
protected virtual void DrawSubtitle(float x, ref float y)
{
if (string.IsNullOrEmpty(SubtitleText))
{
return;
}
float subtitleWidth = menuWidth;
float subtitleHeight = itemHeight;
DrawRect(x + subtitleWidth * 0.5f, y + subtitleHeight * 0.5f, subtitleWidth, subtitleHeight, SubtitleBackgroundColor);
DrawSubtitleText(x, y);
DrawSubtitleCounter(x, y);
y += subtitleHeight;
}
private void DrawSubtitleText(float x, float y)
{
float subTextX = x + 0.00390625f;
float subTextY = y + 0.00416664f;
TextStyle s = SubtitleStyle;
s.Wrap = (x + 0.0046875f, x + menuWidth - 0.0046875f);
TextCommands.Display(SubtitleText, s, subTextX, subTextY);
}
private void DrawSubtitleCounter(float x, float y)
{
string counterText = null;
if (CounterOverride != null)
{
counterText = CounterPretext + CounterOverride;
}
else if (MenuItems.Count > MaxItemsOnScreen)
{
if (HasSelection)
{
counterText = CounterPretext + (CurrentSelection + 1) + " / " + MenuItems.Count;
}
else
{
counterText = CounterPretext + "- / " + MenuItems.Count;
}
}
if (counterText == null)
{
return;
}
TextStyle s = CounterStyle;
s.Wrap = (x + 0.0046875f, x + menuWidth - 0.0046875f);
float counterWidth = TextCommands.GetWidth(counterText, s);
float counterX = x + menuWidth - 0.00390625f - counterWidth;
float counterY = y + 0.00416664f;
TextCommands.Display(counterText, s, counterX, counterY);
}
/// <summary>
/// Draws the items background at the specified position.
/// </summary>
/// <param name="x">The position along the X-axis in relative coordinates.</param>
/// <param name="y">The position along the Y-axis in relative coordinates.</param>
protected virtual void DrawBackground(float x, float y)
{
float bgWidth = menuWidth;
float bgHeight = itemHeight * Math.Min(MenuItems.Count, MaxItemsOnScreen);
DrawSprite(CommonTxd, BackgroundTextureName,
x + bgWidth * 0.5f,
y + bgHeight * 0.5f - 0.00138888f,
bgWidth,
bgHeight,
Color.White);
}
/// <summary>
/// Draws the items beginning at the specified position.
/// </summary>
/// <param name="x">The position along the X-axis in relative coordinates.</param>
/// <param name="y">
/// The position along the Y-axis in relative coordinates.
/// When this method returns, contains the position right below the items.
/// </param>
protected virtual void DrawItems(float x, ref float y)
{
if (MenuItems.Count == 0)
{
return;
}
itemsX = x;
itemsY = y;
foreach ((int iterIndex, int itemIndex, UIMenuItem item, bool selectedItem) in IterateVisibleItems())
{
if (selectedItem)
{
float x1 = x, y1 = y;
float x2 = x + menuWidth, y2 = y + itemHeight;
N.GetScriptGfxPosition(x1, y1, out x1, out y1);
N.GetScriptGfxPosition(x2, y2, out x2, out y2);
currentItemBounds = new RectangleF(x1, y1, x2 - x1, y2 - y1);
}
item.Draw(x, y, menuWidth, itemHeight);
y += itemHeight;
}
}
/// <summary>
/// Draws the up-down arrows at the specified position.
/// </summary>
/// <param name="x">The position along the X-axis in relative coordinates.</param>
/// <param name="y">
/// The position along the Y-axis in relative coordinates.
/// When this method returns, contains the position right below the up-down arrows.
/// </param>
protected virtual void DrawUpDownArrows(float x, ref float y)
{
if (MenuItems.Count > MaxItemsOnScreen)
{
float upDownRectWidth = menuWidth;
float upDownRectHeight = itemHeight;
upDownX = x;
upDownY = y;
DrawRect(x + upDownRectWidth * 0.5f, y + upDownRectHeight * 0.5f, upDownRectWidth, upDownRectHeight, UpDownArrowsBackgroundColor);
if (hoveredUpDown != 0)
{
float hoverRectH = upDownRectHeight * 0.5f;
DrawRect(x + upDownRectWidth * 0.5f, y + (hoverRectH * (hoveredUpDown - 0.5f)), upDownRectWidth, hoverRectH, UpDownArrowsHighlightColor);
}
Vector3 upDownSize = N.GetTextureResolution(CommonTxd, UpAndDownTextureName);
float upDownWidth = (upDownSize.X * 0.5f) / 1280.0f;
float upDownHeight = (upDownSize.Y * 0.5f) / 720.0f;
DrawSprite(CommonTxd, UpAndDownTextureName, x + upDownRectWidth * 0.5f, y + upDownRectHeight * 0.5f, upDownWidth, upDownHeight, UpDownArrowsForegroundColor);
y += itemHeight;
}
}
/// <summary>
/// Draws the description text and background at the specified position.
/// </summary>
/// <param name="x">The position along the X-axis in relative coordinates.</param>
/// <param name="y">
/// The position along the Y-axis in relative coordinates.
/// When this method returns, contains the position right below the description.
/// </param>
protected virtual void DrawDescription(float x, ref float y)
{
if (DescriptionOverride == null && !HasSelection)
{
return;
}
string description = DescriptionOverride ?? MenuItems[CurrentSelection].Description;
if (!String.IsNullOrWhiteSpace(description))
{
y += 0.00277776f * 2f;
float textX = x + 0.0046875f; // x - menuWidth * 0.5f + 0.0046875f
float textXEnd = x + menuWidth - 0.0046875f; // x - menuWidth * 0.5f + menuWidth - 0.0046875f
Color backColor = Color.FromArgb(186, 0, 0, 0);
TextStyle s = DescriptionStyle;
s.Wrap = (textX, textXEnd);
int lineCount = TextCommands.GetLineCount(description, s, textX, y + 0.00277776f);
float descHeight = (s.CharacterHeight * lineCount) + (0.00138888f * 13f) + (0.00138888f * 5f * (lineCount - 1));
DrawSprite(CommonTxd, BackgroundTextureName,
x + menuWidth * 0.5f,
y + (descHeight * 0.5f) - 0.00138888f,
menuWidth,
descHeight,
backColor);
DrawRect(x + menuWidth * 0.5f, y - 0.00277776f * 0.5f, menuWidth, 0.00277776f, DescriptionSeparatorColor);
TextCommands.Display(description, s, textX, y + 0.00277776f);
y += descHeight;
}
}
/// <summary>
/// Draws the description text and background at the specified position.
/// </summary>
/// <param name="x">The position along the X-axis in relative coordinates.</param>
/// <param name="y">
/// The position along the Y-axis in relative coordinates.
/// When this method returns, contains the position right below the description.
/// </param>
protected virtual void DrawPanels(float x, ref float y)
{
if (DescriptionOverride == null && MenuItems.Count == 0)
{
return;
}
var panels = CurrentPanels;
if (panels.Count != 0)
{
y += 0.00277776f * 1.75f;
foreach (var panel in panels)
{
panel?.Draw(x, ref y, menuWidth);
}
}