forked from TomGrobbe/MenuAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuController.cs
More file actions
1094 lines (1016 loc) · 43.7 KB
/
MenuController.cs
File metadata and controls
1094 lines (1016 loc) · 43.7 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CitizenFX.Core;
using static CitizenFX.Core.Native.API;
using static CitizenFX.Core.Native.Function;
using static CitizenFX.Core.Native.Hash;
namespace MenuAPI
{
public class MenuController : BaseScript
{
public static List<Menu> Menus { get; protected set; } = new List<Menu>();
#if FIVEM
public const string _texture_dict = "commonmenu";
public const string _header_texture = "interaction_bgd";
#endif
#if REDM
public const string _texture_dict = "menu_textures";
public const string _header_texture = "translate_bg_1a";
#endif
private static List<string> menuTextureAssets = new List<string>()
{
#if FIVEM
"commonmenu",
"commonmenutu",
"mpleaderboard",
"mphud",
"mpshopsale",
"mpinventory",
"mprankbadge",
"mpcarhud",
"mpcarhud2",
"shared",
"char_creator_portraits",
"pause_menu_pages_char_mom_dad"
#endif
#if REDM
"menu_textures",
"boot_flow",
"generic_textures",
#endif
};
#if FIVEM
private static float AspectRatio => GetScreenAspectRatio(false);
#endif
#if REDM
private static float AspectRatio => 16 / 9;
#endif
public static float ScreenWidth => 1080 * AspectRatio;
public static float ScreenHeight => 1080;
public static bool DisableMenuButtons { get; set; } = false;
#if FIVEM
public static bool AreMenuButtonsEnabled => Menus.Any((m) => m.Visible) && !Game.IsPaused && CitizenFX.Core.UI.Screen.Fading.IsFadedIn && !IsPlayerSwitchInProgress() && !DisableMenuButtons && !Game.Player.IsDead;
#endif
#if REDM
public static bool AreMenuButtonsEnabled =>
Menus.Any((m) => m.Visible) &&
!Call<bool>(IS_PAUSE_MENU_ACTIVE) &&
Call<bool>(IS_SCREEN_FADED_IN) &&
!DisableMenuButtons &&
!Call<bool>(IS_ENTITY_DEAD, PlayerPedId());
#endif
public static bool NavigateMenuUsingArrows { get; set; } = true;
public static bool EnableManualGCs { get; set; } = true;
public static bool DontOpenAnyMenu { get; set; } = false;
public static bool PreventExitingMenu { get; set; } = false;
public static bool DisableBackButton { get; set; } = false;
public static bool SetDrawOrder { get; set; } = true;
public static Control MenuToggleKey { get; set; }
#if FIVEM
= Control.InteractionMenu
#endif
#if REDM
= Control.PlayerMenu
#endif
;
public static bool EnableMenuToggleKeyOnController { get; set; } = true;
internal static Dictionary<MenuItem, Menu> MenuButtons { get; private set; } = new Dictionary<MenuItem, Menu>();
public static Menu MainMenu { get; set; } = null;
#if FIVEM
internal static int _scale = RequestScaleformMovie("INSTRUCTIONAL_BUTTONS");
#endif
private static int ManualTimerForGC = GetGameTimer();
#if FIVEM
private static MenuAlignmentOption _alignment = MenuAlignmentOption.Left;
public static MenuAlignmentOption MenuAlignment
{
get
{
return _alignment;
}
set
{
if (AspectRatio < 1.888888888888889f)
{
// alignment can be whatever the resource wants it to be because this aspect ratio is supported.
_alignment = value;
}
// right aligned menus are not supported for aspect ratios 17:9 or 21:9.
else
{
// no matter what the new value would've been, the aspect ratio does not support right aligned menus,
// so (re)set it to be left aligned.
_alignment = MenuAlignmentOption.Left;
// In case the value was being changed to be right aligned, notify the user properly.
if (value == MenuAlignmentOption.Right)
Debug.WriteLine($"[MenuAPI ({GetCurrentResourceName()})] Warning: Right aligned menus are not supported for aspect ratios 17:9 or 21:9, left aligned will be used instead.");
}
}
}
public enum MenuAlignmentOption
{
Left,
Right
}
#endif
/// <summary>
/// Constructor
/// </summary>
public MenuController()
{
Tick += ProcessMenus;
#if FIVEM
Tick += DrawInstructionalButtons;
#endif
Tick += ProcessMainButtons;
Tick += ProcessDirectionalButtons;
Tick += ProcessToggleMenuButton;
Tick += MenuButtonsDisableChecks;
}
/// <summary>
/// This binds the <paramref name="childMenu"/> menu to the <paramref name="menuItem"/> and sets the menu's parent to <paramref name="parentMenu"/>.
/// </summary>
/// <param name="parentMenu"></param>
/// <param name="childMenu"></param>
/// <param name="menuItem"></param>
public static void BindMenuItem(Menu parentMenu, Menu childMenu, MenuItem menuItem)
{
AddSubmenu(parentMenu, childMenu);
if (MenuButtons.ContainsKey(menuItem))
{
MenuButtons[menuItem] = childMenu;
}
else
{
MenuButtons.Add(menuItem, childMenu);
}
}
/// <summary>
/// This adds the <paramref name="menu"/> <see cref="Menu"/> to the <see cref="Menus"/> list.
/// </summary>
/// <param name="menu"></param>
public static void AddMenu(Menu menu)
{
if (!Menus.Contains(menu))
{
Menus.Add(menu);
// automatically set the first menu as the main menu if none is set yet, this can be changed at any time though.
if (MainMenu == null)
{
MainMenu = menu;
}
}
}
/// <summary>
/// Adds the <paramref name="child"/> <see cref="Menu"/> to the menus list and sets the menu's parent to <paramref name="parent"/>.
/// </summary>
/// <param name="parent"></param>
/// <param name="child"></param>
public static void AddSubmenu(Menu parent, Menu child)
{
if (!Menus.Contains(child))
AddMenu(child);
child.ParentMenu = parent;
}
/// <summary>
/// Loads the texture dict for the common menu sprites.
/// </summary>
/// <returns></returns>
private static async Task LoadAssets()
{
#if FIVEM
menuTextureAssets.ForEach(asset =>
{
if (!HasStreamedTextureDictLoaded(asset))
{
RequestStreamedTextureDict(asset, false);
}
});
while (menuTextureAssets.Any(asset => { return !HasStreamedTextureDictLoaded(asset); }))
{
await Delay(0);
}
#endif
#if REDM
menuTextureAssets.ForEach(asset =>
{
if (!Call<bool>(HAS_STREAMED_TEXTURE_DICT_LOADED, asset))
{
Call(REQUEST_STREAMED_TEXTURE_DICT, asset, false);
}
});
while (menuTextureAssets.Any(asset => { return !Call<bool>(HAS_STREAMED_TEXTURE_DICT_LOADED, asset); }))
{
await Delay(0);
}
#endif
}
/// <summary>
/// Unloads the texture dict for the common menu sprites.
/// </summary>
private static void UnloadAssets()
{
#if FIVEM
menuTextureAssets.ForEach(asset =>
{
if (HasStreamedTextureDictLoaded(asset))
{
SetStreamedTextureDictAsNoLongerNeeded(asset);
}
});
#endif
#if REDM
menuTextureAssets.ForEach(asset =>
{
if (!string.IsNullOrEmpty(asset))
{
if (Call<bool>(HAS_STREAMED_TEXTURE_DICT_LOADED, asset))
{
#if DEBUG
Debug.WriteLine($"[DEBUG] [{GetCurrentResourceName()}] [MenuAPI] Attempting to set asset as no longer needed: {asset}");
#endif
Call(SET_STREAMED_TEXTURE_DICT_AS_NO_LONGER_NEEDED, asset);
}
}
#if DEBUG
else
{
Debug.WriteLine($"[WARNING] [{GetCurrentResourceName()}] [MenuAPI] a menu asset is null somehow, can't set it as no longer needed.");
}
#endif
});
#endif
}
/// <summary>
/// Returns the currently opened menu.
/// </summary>
/// <returns></returns>
public static Menu GetCurrentMenu()
{
if (Menus.Any((m) => m.Visible))
return Menus.Find((m) => m.Visible);
return null;
}
/// <summary>
/// Returns true if any menu is currently open.
/// </summary>
/// <returns></returns>
public static bool IsAnyMenuOpen() => Menus.Any((m) => m.Visible);
#region Process Menu Buttons
/// <summary>
/// Process the select & go back/cancel buttons.
/// </summary>
/// <returns></returns>
private async Task ProcessMainButtons()
{
if (IsAnyMenuOpen())
{
#if REDM
if (Call<bool>(IS_PAUSE_MENU_ACTIVE))
{
return;
}
#endif
var currentMenu = GetCurrentMenu();
if (currentMenu != null && !DontOpenAnyMenu)
{
if (PreventExitingMenu)
{
#if FIVEM
Game.DisableControlThisFrame(0, Control.FrontendPause);
Game.DisableControlThisFrame(0, Control.FrontendPauseAlternate);
#endif
#if REDM
Call(DISABLE_CONTROL_ACTION, 0, Control.FrontendPause, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.FrontendPauseAlternate, true);
#endif
}
if (currentMenu.Visible && AreMenuButtonsEnabled)
{
// Select / Enter
if (
#if FIVEM
Game.IsDisabledControlJustReleased(0, Control.FrontendAccept) ||
Game.IsControlJustReleased(0, Control.FrontendAccept) ||
Game.IsDisabledControlJustReleased(0, Control.VehicleMouseControlOverride) ||
Game.IsControlJustReleased(0, Control.VehicleMouseControlOverride)
#endif
#if REDM
Call<bool>(IS_DISABLED_CONTROL_JUST_RELEASED, 0, Control.FrontendAccept) ||
Call<bool>(IS_CONTROL_JUST_RELEASED, 0, Control.FrontendAccept)
#endif
)
{
if (currentMenu.Size > 0)
{
currentMenu.SelectItem(currentMenu.CurrentIndex);
}
}
// Cancel / Go Back
else if (
#if FIVEM
Game.IsDisabledControlJustReleased(0, Control.PhoneCancel)
#endif
#if REDM
Call<bool>(IS_DISABLED_CONTROL_JUST_RELEASED, 0, Control.FrontendCancel)
#endif
&& !DisableBackButton)
{
// Wait for the next frame to make sure the "cinematic camera" button doesn't get "re-enabled" before the menu gets closed.
await Delay(0);
currentMenu.GoBack();
}
else if (
#if FIVEM
Game.IsDisabledControlJustReleased(0, Control.PhoneCancel)
#endif
#if REDM
Call<bool>(IS_DISABLED_CONTROL_JUST_RELEASED, 0, Control.CellphoneCancel)
#endif
&& PreventExitingMenu && !DisableBackButton)
{
// if there's a parent menu, allow going back to that, but don't allow a 'top-level' menu to be closed.
if (currentMenu.ParentMenu != null)
{
currentMenu.GoBack();
}
await Delay(0);
}
}
}
#if FIVEM
Game.DisableControlThisFrame(0, Control.MultiplayerInfo);
#endif
}
}
/// <summary>
/// Returns true when one of the 'up' controls is currently pressed, only if the button can be active according to some conditions.
/// </summary>
/// <returns></returns>
private bool IsUpPressed()
{
// Return false if the buttons are not currently enabled.
if (!AreMenuButtonsEnabled)
{
return false;
}
#if FIVEM
// when the player is holding TAB, while not in a vehicle, and when the scrollwheel is being used, return false to prevent interferring with weapon selection.
if (!Game.PlayerPed.IsInVehicle())
{
if (Game.IsControlPressed(0, Control.SelectWeapon))
{
if (Game.IsControlPressed(0, Control.SelectNextWeapon) || Game.IsControlPressed(0, Control.SelectPrevWeapon))
{
return false;
}
}
}
// return true if the scrollwheel up or the arrow up key is being used at this frame.
if (Game.IsControlPressed(0, Control.FrontendUp) ||
Game.IsDisabledControlPressed(0, Control.FrontendUp) ||
Game.IsControlPressed(0, Control.PhoneScrollBackward) ||
Game.IsDisabledControlPressed(0, Control.PhoneScrollBackward))
{
return true;
}
#endif
#if REDM
if (Call<bool>(IS_CONTROL_PRESSED, 0, Control.FrontendUp) ||
Call<bool>(IS_DISABLED_CONTROL_PRESSED, 0, Control.FrontendUp) ||
Call<bool>(IS_CONTROL_PRESSED, 0, Control.CellphoneScrollBackward) ||
Call<bool>(IS_DISABLED_CONTROL_PRESSED, 0, Control.CellphoneScrollBackward)
)
{
return true;
}
#endif
// return false if none of the conditions matched.
return false;
}
/// <summary>
/// Returns true when one of the 'down' controls is currently pressed, only if the button can be active according to some conditions.
/// </summary>
/// <returns></returns>
private bool IsDownPressed()
{
// Return false if the buttons are not currently enabled.
if (!AreMenuButtonsEnabled)
{
return false;
}
#if FIVEM
// when the player is holding TAB, while not in a vehicle, and when the scrollwheel is being used, return false to prevent interferring with weapon selection.
if (!Game.PlayerPed.IsInVehicle())
{
if (Game.IsControlPressed(0, Control.SelectWeapon))
{
if (Game.IsControlPressed(0, Control.SelectNextWeapon) || Game.IsControlPressed(0, Control.SelectPrevWeapon))
{
return false;
}
}
}
// return true if the scrollwheel down or the arrow down key is being used at this frame.
if (Game.IsControlPressed(0, Control.FrontendDown) ||
Game.IsDisabledControlPressed(0, Control.FrontendDown) ||
Game.IsControlPressed(0, Control.PhoneScrollForward) ||
Game.IsDisabledControlPressed(0, Control.PhoneScrollForward))
{
return true;
}
#endif
#if REDM
if (Call<bool>(IS_CONTROL_PRESSED, 0, Control.FrontendDown) ||
Call<bool>(IS_DISABLED_CONTROL_PRESSED, 0, Control.FrontendDown) ||
Call<bool>(IS_CONTROL_PRESSED, 0, Control.CellphoneScrollForward) ||
Call<bool>(IS_DISABLED_CONTROL_PRESSED, 0, Control.CellphoneScrollForward)
)
{
return true;
}
#endif
// return false if none of the conditions matched.
return false;
}
/// <summary>
/// Processes the menu toggle button to check if the menu should open or close.
/// </summary>
/// <returns></returns>
private async Task ProcessToggleMenuButton()
{
#if FIVEM
if (!Game.IsPaused && !IsPauseMenuRestarting() && IsScreenFadedIn() && !IsPlayerSwitchInProgress() && !Game.Player.IsDead && !DisableMenuButtons)
{
if (IsAnyMenuOpen())
{
Game.DisableControlThisFrame(0, MenuToggleKey);
if (Game.CurrentInputMode == InputMode.MouseAndKeyboard)
{
if ((Game.IsControlJustPressed(0, MenuToggleKey) || Game.IsDisabledControlJustPressed(0, MenuToggleKey)) && !PreventExitingMenu)
{
var menu = GetCurrentMenu();
if (menu != null)
{
menu.CloseMenu();
}
}
}
}
else
{
if (Game.CurrentInputMode == InputMode.GamePad)
{
if (!EnableMenuToggleKeyOnController)
return;
int tmpTimer = GetGameTimer();
while ((Game.IsControlPressed(0, Control.InteractionMenu) || Game.IsDisabledControlPressed(0, Control.InteractionMenu)) && !Game.IsPaused && IsScreenFadedIn() && !Game.Player.IsDead && !IsPlayerSwitchInProgress() && !DontOpenAnyMenu)
{
if (GetGameTimer() - tmpTimer > 400)
{
if (MainMenu != null)
{
MainMenu.OpenMenu();
}
else
{
if (Menus.Count > 0)
{
Menus[0].OpenMenu();
}
}
break;
}
await Delay(0);
}
}
else
{
if ((Game.IsControlJustPressed(0, MenuToggleKey) || Game.IsDisabledControlJustPressed(0, MenuToggleKey)) && !Game.IsPaused && IsScreenFadedIn() && !Game.Player.IsDead && !IsPlayerSwitchInProgress() && !DontOpenAnyMenu)
{
if (Menus.Count > 0)
{
if (MainMenu != null)
{
MainMenu.OpenMenu();
}
else
{
Menus[0].OpenMenu();
}
}
}
}
}
}
#endif
#if REDM
Call(DISABLE_CONTROL_ACTION, 0, MenuToggleKey, true);
if (!Call<bool>(IS_PAUSE_MENU_ACTIVE) && Call<bool>(IS_SCREEN_FADED_IN) && !IsAnyMenuOpen() && !DisableMenuButtons && !Call<bool>(IS_ENTITY_DEAD, PlayerPedId()) && Call<bool>(IS_DISABLED_CONTROL_JUST_RELEASED, 0, MenuToggleKey))
{
if (MainMenu != null)
{
MainMenu.OpenMenu();
}
else
{
Debug.WriteLine($"[ERROR] [{GetCurrentResourceName()}] [MenuAPI] MainMenu is null, so we can't open it! Make sure that MenuController.MainMenu is set to a valid Menu which is not null!");
}
}
#endif
await Task.FromResult(0);
}
/// <summary>
/// Process left/right/up/down buttons (also holding down buttons will speed up after 3 iterations)
/// </summary>
/// <returns></returns>
private async Task ProcessDirectionalButtons()
{
// Return if the buttons are not currently enabled.
if (!AreMenuButtonsEnabled)
{
return;
}
// Get the currently open menu.
var currentMenu = GetCurrentMenu();
// If it exists.
if (currentMenu != null && !DontOpenAnyMenu && currentMenu.Size > 0)
{
if (currentMenu.Visible)
{
// Check if the Go Up controls are pressed.
if (IsUpPressed())
{
// Update the currently selected item to the new one.
currentMenu.GoUp();
// Get the current game time.
var time = GetGameTimer();
var times = 0;
var delay = 200;
// Do the following as long as the controls are being pressed.
while (IsUpPressed() && IsAnyMenuOpen() && GetCurrentMenu() != null)
{
// Update the current menu.
currentMenu = GetCurrentMenu();
// Check if the game time has changed by "delay" amount.
if (GetGameTimer() - time > delay)
{
// Increment the "changed indexes" counter
times++;
// If the controls are still being held down after moving 3 indexes, reduce the delay between index changes.
if (times > 2)
{
delay = 150;
}
if (times > 5)
{
delay = 100;
}
if (times > 25)
{
delay = 50;
}
if (times > 60)
{
delay = 25;
}
// Update the currently selected item to the new one.
currentMenu.GoUp();
// Reset the time to the current game timer.
time = GetGameTimer();
}
// Wait for the next game tick.
await Delay(0);
}
}
// Check if the Go Down controls are pressed.
else if (IsDownPressed())
{
currentMenu.GoDown();
var time = GetGameTimer();
var times = 0;
var delay = 200;
while (IsDownPressed() && GetCurrentMenu() != null)
{
currentMenu = GetCurrentMenu();
if (GetGameTimer() - time > delay)
{
times++;
if (times > 2)
{
delay = 150;
}
if (times > 5)
{
delay = 100;
}
if (times > 25)
{
delay = 50;
}
if (times > 60)
{
delay = 25;
}
currentMenu.GoDown();
time = GetGameTimer();
}
await Delay(0);
}
}
// Check if the Go Left controls are pressed.
#if FIVEM
else if (Game.IsDisabledControlJustPressed(0, Control.PhoneLeft) || Game.IsControlJustPressed(0, Control.PhoneLeft))
#endif
#if REDM
else if (Call<bool>(IS_DISABLED_CONTROL_JUST_PRESSED, 0, Control.FrontendLeft) || Call<bool>(IS_CONTROL_JUST_PRESSED, 0, Control.FrontendLeft))
#endif
{
var item = currentMenu.GetMenuItems()[currentMenu.CurrentIndex];
if (item.Enabled)
{
currentMenu.GoLeft();
var time = GetGameTimer();
var times = 0;
var delay = 200;
#if FIVEM
while ((Game.IsDisabledControlPressed(0, Control.PhoneLeft) || Game.IsControlPressed(0, Control.PhoneLeft)) && GetCurrentMenu() != null && AreMenuButtonsEnabled)
#endif
#if REDM
while ((Call<bool>(IS_DISABLED_CONTROL_PRESSED, 0, Control.FrontendLeft) || Call<bool>(IS_CONTROL_PRESSED, 0, Control.FrontendLeft)) && GetCurrentMenu() != null && AreMenuButtonsEnabled)
#endif
{
currentMenu = GetCurrentMenu();
if (GetGameTimer() - time > delay)
{
times++;
if (times > 2)
{
delay = 150;
}
if (times > 5)
{
delay = 100;
}
if (times > 25)
{
delay = 50;
}
if (times > 60)
{
delay = 25;
}
currentMenu.GoLeft();
time = GetGameTimer();
}
await Delay(0);
}
}
}
// Check if the Go Right controls are pressed.
#if FIVEM
else if (Game.IsDisabledControlJustPressed(0, Control.PhoneRight) || Game.IsControlJustPressed(0, Control.PhoneRight))
#endif
#if REDM
else if (AreMenuButtonsEnabled && Call<bool>(IS_DISABLED_CONTROL_JUST_PRESSED, 0, Control.FrontendRight) || Call<bool>(IS_CONTROL_JUST_PRESSED, 0, Control.FrontendRight))
#endif
{
var item = currentMenu.GetMenuItems()[currentMenu.CurrentIndex];
if (item.Enabled)
{
currentMenu.GoRight();
var time = GetGameTimer();
var times = 0;
var delay = 200;
#if FIVEM
while ((Game.IsDisabledControlPressed(0, Control.PhoneRight) || Game.IsControlPressed(0, Control.PhoneRight)) && GetCurrentMenu() != null && AreMenuButtonsEnabled)
#endif
#if REDM
while ((Call<bool>(IS_DISABLED_CONTROL_PRESSED, 0, Control.FrontendRight) || Call<bool>(IS_CONTROL_PRESSED, 0, Control.FrontendRight)) && GetCurrentMenu() != null && AreMenuButtonsEnabled)
#endif
{
currentMenu = GetCurrentMenu();
if (GetGameTimer() - time > delay)
{
times++;
if (times > 2)
{
delay = 150;
}
if (times > 5)
{
delay = 100;
}
if (times > 25)
{
delay = 50;
}
if (times > 60)
{
delay = 25;
}
currentMenu.GoRight();
time = GetGameTimer();
}
await Delay(0);
}
}
}
}
}
}
private async Task MenuButtonsDisableChecks()
{
bool isInputVisible() => UpdateOnscreenKeyboard() == 0;
if (isInputVisible())
{
bool buttonsState = DisableMenuButtons;
while (isInputVisible())
{
await Delay(0);
DisableMenuButtons = true;
}
int timer = GetGameTimer();
while (GetGameTimer() - timer < 300)
{
await Delay(0);
DisableMenuButtons = true;
}
DisableMenuButtons = buttonsState;
}
}
#endregion
/// <summary>
/// Closes all menus.
/// </summary>
public static void CloseAllMenus()
{
Menus.ForEach((m) => { if (m.Visible) { m.CloseMenu(); } });
}
/// <summary>
/// Disables the most important controls for when a menu is open.
/// </summary>
private static void DisableControls()
{
#region Disable Inputs when any menu is open.
if (IsAnyMenuOpen())
{
var currMenu = GetCurrentMenu();
if (currMenu != null)
{
var currentItem = currMenu.GetCurrentMenuItem();
if (currentItem != null)
{
#if FIVEM
if (currentItem is MenuSliderItem || currentItem is MenuListItem || currentItem is MenuDynamicListItem)
{
if (Game.CurrentInputMode == InputMode.GamePad)
Game.DisableControlThisFrame(0, Control.SelectWeapon);
}
#endif
}
// Close all menus when the player dies.
#if FIVEM
if (Game.PlayerPed.IsDead)
#endif
#if REDM
if (Call<bool>(IS_ENTITY_DEAD, PlayerPedId()))
#endif
{
CloseAllMenus();
}
#if FIVEM
// Disable Gamepad/Controller Specific controls:
if (Game.CurrentInputMode == InputMode.GamePad)
{
Game.DisableControlThisFrame(0, Control.MultiplayerInfo);
// when in a vehicle.
if (Game.PlayerPed.IsInVehicle())
{
Game.DisableControlThisFrame(0, Control.VehicleHeadlight);
Game.DisableControlThisFrame(0, Control.VehicleDuck);
// toggles boost in some dlc vehicles, hence it's disabled for controllers only (pressing select in the menu would trigger this).
Game.DisableControlThisFrame(0, Control.VehicleFlyTransform);
}
}
else // when not using a controller.
{
Game.DisableControlThisFrame(0, Control.FrontendPauseAlternate); // disable the escape key opening the pause menu, pressing P still works.
// Disable the scrollwheel button changing weapons while the menu is open.
// Only if you press TAB (to show the weapon wheel) then it will allow you to change weapons.
if (!Game.IsControlPressed(0, Control.SelectWeapon))
{
Game.DisableControlThisFrame(24, Control.SelectNextWeapon);
Game.DisableControlThisFrame(24, Control.SelectPrevWeapon);
}
}
#endif
#if REDM
if (Call<bool>(_IS_INPUT_DISABLED, 2))
{
Call(DISABLE_CONTROL_ACTION, 0, Control.FrontendPauseAlternate, true);
}
#endif
// Disable Shared Controls
#if FIVEM
// Radio Inputs
Game.DisableControlThisFrame(0, Control.RadioWheelLeftRight);
Game.DisableControlThisFrame(0, Control.RadioWheelUpDown);
Game.DisableControlThisFrame(0, Control.VehicleNextRadio);
Game.DisableControlThisFrame(0, Control.VehicleRadioWheel);
Game.DisableControlThisFrame(0, Control.VehiclePrevRadio);
// Phone / Arrows Inputs
Game.DisableControlThisFrame(0, Control.Phone);
Game.DisableControlThisFrame(0, Control.PhoneCancel);
Game.DisableControlThisFrame(0, Control.PhoneDown);
Game.DisableControlThisFrame(0, Control.PhoneLeft);
Game.DisableControlThisFrame(0, Control.PhoneRight);
// Attack Controls
Game.DisableControlThisFrame(0, Control.Attack);
Game.DisableControlThisFrame(0, Control.Attack2);
Game.DisableControlThisFrame(0, Control.MeleeAttack1);
Game.DisableControlThisFrame(0, Control.MeleeAttack2);
Game.DisableControlThisFrame(0, Control.MeleeAttackAlternate);
Game.DisableControlThisFrame(0, Control.MeleeAttackHeavy);
Game.DisableControlThisFrame(0, Control.MeleeAttackLight);
Game.DisableControlThisFrame(0, Control.VehicleAttack);
Game.DisableControlThisFrame(0, Control.VehicleAttack2);
Game.DisableControlThisFrame(0, Control.VehicleFlyAttack);
Game.DisableControlThisFrame(0, Control.VehiclePassengerAttack);
Game.DisableControlThisFrame(0, Control.Aim);
Game.DisableControlThisFrame(0, Control.VehicleAim); // fires vehicle specific weapons when using right click on the mouse sometimes.
// When in a vehicle
if (Game.PlayerPed.IsInVehicle())
{
Game.DisableControlThisFrame(0, Control.VehicleSelectNextWeapon);
Game.DisableControlThisFrame(0, Control.VehicleSelectPrevWeapon);
Game.DisableControlThisFrame(0, Control.VehicleCinCam);
}
#endif
#if REDM
Call(DISABLE_CONTROL_ACTION, 0, Control.Attack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.Attack2, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.HorseAim, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.HorseAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.HorseAttack2, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.HorseMelee, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeBlock, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeGrapple, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeGrappleAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeGrappleBreakout, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeGrappleChoke, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeGrappleMountSwitch, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeGrappleReversal, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeGrappleStandSwitch, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeHorseAttackPrimary, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeHorseAttackSecondary, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.MeleeModifier, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehAttack2, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehBoatAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehBoatAttack2, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehCarAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehCarAttack2, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehDraftAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehDraftAttack2, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehFlyAttack, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehFlyAttack2, true);
Call(DISABLE_CONTROL_ACTION, 0, Control.VehPassengerAttack, true);
#endif
}
}
#endregion
}
/// <summary>
/// Draws all the menus that are visible on the screen.
/// </summary>
/// <returns></returns>
private static async Task ProcessMenus()
{
if (Menus.Count > 0 &&
IsAnyMenuOpen() &&
#if FIVEM
IsScreenFadedIn() &&
!Game.IsPaused &&
!Game.Player.IsDead &&
!IsPlayerSwitchInProgress()
#endif
#if REDM
Call<bool>(IS_SCREEN_FADED_IN) &&
!Call<bool>(IS_PAUSE_MENU_ACTIVE) &&
!Call<bool>(IS_ENTITY_DEAD, PlayerPedId())
#endif
)
{
await LoadAssets();
DisableControls();
Menu menu = GetCurrentMenu();
if (menu != null)
{
if (DontOpenAnyMenu)
{
if (menu.Visible && !menu.IgnoreDontOpenMenus)
{
menu.CloseMenu();
}
}
else if (menu.Visible)
{
menu.Draw();
}
}
if (EnableManualGCs)
{
// once a minute
if (GetGameTimer() - ManualTimerForGC > 60000)
{
GC.Collect();
ManualTimerForGC = GetGameTimer();
}
}