Skip to content

Commit 095af43

Browse files
committed
Create overridable GoLeft, GoRight and Select functions for each MenuItem class to handle custom behavior without a massive if statement in the parent functions
1 parent b22d462 commit 095af43

8 files changed

+201
-209
lines changed

MenuAPI/Menu.cs

+27-176
Large diffs are not rendered by default.

MenuAPI/MenuController.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Text;
54
using System.Threading.Tasks;
65
using CitizenFX.Core;
76
using static CitizenFX.Core.Native.API;
8-
using static CitizenFX.Core.Native.Function;
9-
using static CitizenFX.Core.Native.Hash;
107

118
namespace MenuAPI
129
{

MenuAPI/items/MenuCheckboxItem.cs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using CitizenFX.Core;
1+
using CitizenFX.Core;
72
using static CitizenFX.Core.Native.API;
8-
using static CitizenFX.Core.Native.Function;
9-
using static CitizenFX.Core.Native.Hash;
103

114
namespace MenuAPI
125
{
@@ -158,5 +151,16 @@ internal override void Draw(int offset)
158151
}
159152
#endif
160153
}
154+
155+
internal override void GoRight()
156+
{
157+
ParentMenu.SelectItem(this);
158+
}
159+
160+
internal override void Select()
161+
{
162+
Checked = !Checked;
163+
ParentMenu.CheckboxChangedEvent(this, Index, Checked);
164+
}
161165
}
162166
}

MenuAPI/items/MenuDynamicListItem.cs

+36-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using CitizenFX.Core;
1+
using static CitizenFX.Core.Native.Function;
72
using static CitizenFX.Core.Native.API;
83

94
namespace MenuAPI
@@ -36,5 +31,40 @@ internal override void Draw(int indexOffset)
3631
}
3732
base.Draw(indexOffset);
3833
}
34+
35+
internal override void GoRight()
36+
{
37+
string oldValue = CurrentItem;
38+
string newSelectedItem = Callback(this, false);
39+
CurrentItem = newSelectedItem;
40+
ParentMenu.DynamicListItemCurrentItemChanged(ParentMenu, this, oldValue, newSelectedItem);
41+
#if FIVEM
42+
PlaySoundFrontend(-1, "NAV_LEFT_RIGHT", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
43+
#endif
44+
#if REDM
45+
// Has invalid parameter types in API.
46+
Call((CitizenFX.Core.Native.Hash)0xCE5D0FFE83939AF1, -1, "NAV_RIGHT", "HUD_SHOP_SOUNDSET", 1);
47+
#endif
48+
}
49+
50+
internal override void GoLeft()
51+
{
52+
string oldValue = CurrentItem;
53+
string newSelectedItem = Callback(this, true);
54+
CurrentItem = newSelectedItem;
55+
ParentMenu.DynamicListItemCurrentItemChanged(ParentMenu, this, oldValue, newSelectedItem);
56+
#if FIVEM
57+
PlaySoundFrontend(-1, "NAV_LEFT_RIGHT", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
58+
#endif
59+
#if REDM
60+
// Has invalid parameter types in API.
61+
Call((CitizenFX.Core.Native.Hash)0xCE5D0FFE83939AF1, -1, "NAV_RIGHT", "HUD_SHOP_SOUNDSET", 1);
62+
#endif
63+
}
64+
65+
internal override void Select()
66+
{
67+
ParentMenu.DynamicListItemSelectEvent(ParentMenu, this, CurrentItem);
68+
}
3969
}
4070
}

MenuAPI/items/MenuItem.cs

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63
using CitizenFX.Core;
74
using static CitizenFX.Core.Native.API;
85
using static CitizenFX.Core.Native.Function;
@@ -1056,6 +1053,36 @@ protected float GetSpriteX(Icon icon, bool leftAligned, bool leftSide)
10561053
#endif
10571054
}
10581055

1056+
/// <summary>
1057+
/// Handles menu navigation to the right for items that support it, otherwise the item will be selected.
1058+
/// </summary>
1059+
internal virtual void GoRight()
1060+
{
1061+
if (Enabled)
1062+
{
1063+
ParentMenu.SelectItem(this);
1064+
}
1065+
}
1066+
1067+
/// <summary>
1068+
/// Handles menu navigation to the left for items that support it, otherwise the menu will navigate to the parent menu.
1069+
/// </summary>
1070+
internal virtual void GoLeft()
1071+
{
1072+
if (MenuController.NavigateMenuUsingArrows && !MenuController.DisableBackButton && !(MenuController.PreventExitingMenu && ParentMenu == null))
1073+
{
1074+
ParentMenu.GoBack();
1075+
}
1076+
}
1077+
1078+
/// <summary>
1079+
/// Handles item selection.
1080+
/// </summary>
1081+
internal virtual void Select()
1082+
{
1083+
ParentMenu.ItemSelectedEvent(this, Index);
1084+
}
1085+
10591086
/// <summary>
10601087
/// Draws the item on the screen.
10611088
/// </summary>

MenuAPI/items/MenuListItem.cs

+60-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using CitizenFX.Core;
1+
using System.Collections.Generic;
2+
using static CitizenFX.Core.Native.Function;
73
using static CitizenFX.Core.Native.API;
84

95
namespace MenuAPI
@@ -68,5 +64,63 @@ internal override void Draw(int indexOffset)
6864

6965
base.Draw(indexOffset);
7066
}
67+
68+
internal override void GoRight()
69+
{
70+
if (ItemsCount > 0)
71+
{
72+
int oldIndex = ListIndex;
73+
int newIndex = oldIndex;
74+
if (ListIndex >= ItemsCount - 1)
75+
{
76+
newIndex = 0;
77+
}
78+
else
79+
{
80+
newIndex++;
81+
}
82+
ListIndex = newIndex;
83+
ParentMenu.ListItemIndexChangeEvent(ParentMenu, this, oldIndex, newIndex, Index);
84+
#if FIVEM
85+
PlaySoundFrontend(-1, "NAV_LEFT_RIGHT", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
86+
#endif
87+
#if REDM
88+
// Has invalid parameter types in API.
89+
Call((CitizenFX.Core.Native.Hash)0xCE5D0FFE83939AF1, -1, "NAV_RIGHT", "HUD_SHOP_SOUNDSET", 1);
90+
#endif
91+
}
92+
}
93+
94+
internal override void GoLeft()
95+
{
96+
if (ItemsCount > 0)
97+
{
98+
int oldIndex = ListIndex;
99+
int newIndex = oldIndex;
100+
if (ListIndex < 1)
101+
{
102+
newIndex = ItemsCount - 1;
103+
}
104+
else
105+
{
106+
newIndex--;
107+
}
108+
ListIndex = newIndex;
109+
110+
ParentMenu.ListItemIndexChangeEvent(ParentMenu, this, oldIndex, newIndex, Index);
111+
#if FIVEM
112+
PlaySoundFrontend(-1, "NAV_LEFT_RIGHT", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
113+
#endif
114+
#if REDM
115+
// Has invalid parameter types in API.
116+
Call((CitizenFX.Core.Native.Hash)0xCE5D0FFE83939AF1, -1, "NAV_LEFT", "HUD_SHOP_SOUNDSET", 1);
117+
#endif
118+
}
119+
}
120+
121+
internal override void Select()
122+
{
123+
ParentMenu.ListItemSelectEvent(ParentMenu, this, ListIndex, Index);
124+
}
71125
}
72126
}

MenuAPI/items/MenuSliderItem.cs

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
#if FIVEM
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Text;
@@ -8,7 +9,6 @@
89

910
namespace MenuAPI
1011
{
11-
#if FIVEM
1212
public class MenuSliderItem : MenuItem
1313
{
1414
public int Min { get; private set; } = 0;
@@ -142,6 +142,39 @@ internal override void Draw(int indexOffset)
142142
#endregion
143143
ResetScriptGfxAlign();
144144
}
145+
146+
internal override void GoRight()
147+
{
148+
if (Position < Max)
149+
{
150+
Position++;
151+
ParentMenu.SliderItemChangedEvent(ParentMenu, this, Position - 1, Position, Index);
152+
PlaySoundFrontend(-1, "NAV_LEFT_RIGHT", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
153+
}
154+
else
155+
{
156+
PlaySoundFrontend(-1, "ERROR", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
157+
}
158+
}
159+
160+
internal override void GoLeft()
161+
{
162+
if (Position > Min)
163+
{
164+
Position--;
165+
ParentMenu.SliderItemChangedEvent(ParentMenu, this, Position + 1, Position, Index);
166+
PlaySoundFrontend(-1, "NAV_LEFT_RIGHT", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
167+
}
168+
else
169+
{
170+
PlaySoundFrontend(-1, "ERROR", "HUD_FRONTEND_DEFAULT_SOUNDSET", false);
171+
}
172+
}
173+
174+
internal override void Select()
175+
{
176+
ParentMenu.SliderSelectedEvent(ParentMenu, this, Position, Index);
177+
}
145178
}
146-
#endif
147179
}
180+
#endif

TestMenu/ExampleMenu.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63
using MenuAPI;
74
using CitizenFX.Core;
8-
using static CitizenFX.Core.Native.API;
95

106
namespace TestMenu
117
{

0 commit comments

Comments
 (0)