This repository has been archived by the owner on Jan 28, 2019. It is now read-only.
forked from citizenfx/NativeUI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MenuPool.cs
249 lines (204 loc) · 8.05 KB
/
MenuPool.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
using System.Collections.Generic;
using System.Linq;
using Control = CitizenFX.Core.Control;
namespace NativeUI
{
/// <summary>
/// Helper class that handles all of your Menus. After instatiating it, you will have to add your menu by using the Add method.
/// </summary>
public class MenuPool
{
public bool MouseEdgeEnabled { set { _menuList.ForEach(m => m.MouseEdgeEnabled = value); } }
public bool ControlDisablingEnabled { set { _menuList.ForEach(m => m.ControlDisablingEnabled = value); } }
public bool ResetCursorOnOpen { set { _menuList.ForEach(m => m.ResetCursorOnOpen = value); } }
public bool FormatDescriptions { set { _menuList.ForEach(m => m.FormatDescriptions = value); } }
public string AUDIO_LIBRARY { set { _menuList.ForEach(m => m.AUDIO_LIBRARY = value); } }
public string AUDIO_UPDOWN { set { _menuList.ForEach(m => m.AUDIO_UPDOWN = value); } }
public string AUDIO_SELECT { set { _menuList.ForEach(m => m.AUDIO_SELECT = value); } }
public string AUDIO_BACK { set { _menuList.ForEach(m => m.AUDIO_BACK = value); } }
public string AUDIO_ERROR { set { _menuList.ForEach(m => m.AUDIO_ERROR = value); } }
public int WidthOffset { set { _menuList.ForEach(m => m.SetMenuWidthOffset(value)); } }
public string CounterPretext { set { _menuList.ForEach(m => m.CounterPretext = value); } }
public bool DisableInstructionalButtons { set { _menuList.ForEach(m => m.DisableInstructionalButtons(value)); } }
private readonly List<UIMenu> _menuList = new List<UIMenu>();
/// <summary>
/// Add your menu to the menu pool.
/// </summary>
/// <param name="menu"></param>
public void Add(UIMenu menu)
{
_menuList.Add(menu);
}
/// <summary>
/// Create and add a submenu to the menu pool.
/// Adds an item with the given text to the menu, creates a corresponding submenu, and binds the submenu to the item.
/// The submenu inherits its title from the menu, and its subtitle from the item text.
/// </summary>
/// <param name="menu">The parent menu to which the submenu must be added.</param>
/// <param name="text">The name of the submenu.</param>
/// <returns>The newly created submenu.</returns>
public UIMenu AddSubMenu(UIMenu menu, string text)
{
var item = new UIMenuItem(text);
menu.AddItem(item);
var submenu = new UIMenu(menu.Title.Caption, text);
this.Add(submenu);
menu.BindMenuToItem(submenu, item);
return submenu;
}
/// <summary>
/// Create and add a submenu to the menu pool.
/// Adds an item with the given text and description to the menu, creates a corresponding submenu, and binds the submenu to the item.
/// The submenu inherits its title from the menu, and its subtitle from the item text.
/// </summary>
/// <param name="menu">The parent menu to which the submenu must be added.</param>
/// <param name="text">The name of the submenu.</param>
/// <param name="description">The name of the submenu.</param>
/// <returns>The newly created submenu.</returns>
public UIMenu AddSubMenu(UIMenu menu, string text, string description)
{
var item = new UIMenuItem(text, description);
menu.AddItem(item);
var submenu = new UIMenu(menu.Title.Caption, text);
this.Add(submenu);
menu.BindMenuToItem(submenu, item);
return submenu;
}
/// <summary>
/// Refresh index of every menu in the pool.
/// Use this after you have finished constructing the entire menu pool.
/// </summary>
public void RefreshIndex()
{
foreach (UIMenu menu in _menuList) menu.RefreshIndex();
}
/// <summary>
/// Returns all of your menus.
/// </summary>
/// <returns></returns>
public List<UIMenu> ToList()
{
return _menuList;
}
/// <summary>
/// Processes all of your visible menus' controls.
/// </summary>
public void ProcessControl()
{
/*foreach (var menu in _menuList.Where(menu => menu.Visible)) // foreach works slower with List. Also why make a new enumerable every tick
{
menu.ProcessControl();
}*/
int count = _menuList.Count; // Cache count
for (int i = 0; i < count; i++)
{
if (_menuList[i].Visible)
_menuList[i].ProcessControl();
}
}
/// <summary>
/// Processes all of your visible menus' keys.
/// </summary>
/// <param name="key"></param>
public void ProcessKey(Keys key)
{
/*foreach (var menu in _menuList.Where(menu => menu.Visible))
{
menu.ProcessKey(key);
}*/
int count = _menuList.Count;
for (int i = 0; i < count; i++)
{
if (_menuList[i].Visible)
_menuList[i].ProcessKey(key);
}
}
/// <summary>
/// Processes all of your visible menus' mouses.
/// </summary>
public void ProcessMouse()
{
/*foreach (var menu in _menuList.Where(menu => menu.Visible))
{
menu.ProcessMouse();
}*/
int count = _menuList.Count;
for (int i = 0; i < count; i++)
{
if (_menuList[i].Visible)
_menuList[i].ProcessMouse();
}
}
/// <summary>
/// Draws all visible menus.
/// </summary>
public void Draw()
{
/*foreach (var menu in _menuList.Where(menu => menu.Visible))
{
menu.Draw();
}*/
int count = _menuList.Count;
for (int i = 0; i < count; i++)
{
if (_menuList[i].Visible)
_menuList[i].Draw();
}
}
/// <summary>
/// Checks if any menu is currently visible.
/// </summary>
/// <returns>true if at least one menu is visible, false if not.</returns>
public bool IsAnyMenuOpen()
{
return _menuList.Any(menu => menu.Visible);
}
/// <summary>
/// Process all of your menus' functions. Call this in a tick event.
/// </summary>
public void ProcessMenus()
{
ProcessControl();
ProcessMouse();
Draw();
}
/// <summary>
/// Closes all of your menus.
/// </summary>
public void CloseAllMenus()
{
foreach (var menu in _menuList.Where(menu => menu.Visible))
{
menu.Visible = false;
}
}
public void SetBannerType(Sprite bannerType)
{
_menuList.ForEach(m => m.SetBannerType(bannerType));
}
public void SetBannerType(UIResRectangle bannerType)
{
_menuList.ForEach(m => m.SetBannerType(bannerType));
}
public void SetBannerType(string bannerPath)
{
_menuList.ForEach(m => m.SetBannerType(bannerPath));
}
public void SetKey(UIMenu.MenuControls menuControl, Control control)
{
_menuList.ForEach(m => m.SetKey(menuControl, control));
}
public void SetKey(UIMenu.MenuControls menuControl, Control control, int controllerIndex)
{
_menuList.ForEach(m => m.SetKey(menuControl, control, controllerIndex));
}
public void SetKey(UIMenu.MenuControls menuControl, Keys control)
{
_menuList.ForEach(m => m.SetKey(menuControl, control));
}
public void ResetKey(UIMenu.MenuControls menuControl)
{
_menuList.ForEach(m => m.ResetKey(menuControl));
}
}
}