GitLab Commit is coming up on August 3-4. Learn how to innovate together using GitLab, the DevOps platform. Register for free: gitlabcommitvirtual2021.com

Menu.cs 13.6 KB
Newer Older
Jitnaught's avatar
init  
Jitnaught committed
1
using GTA;
2
3
4
using LemonUI;
using LemonUI.Elements;
using LemonUI.Menus;
Jitnaught's avatar
init  
Jitnaught committed
5
6
using System;
using System.Collections.Generic;
7
8
using System.ComponentModel;
using System.Drawing;
Jitnaught's avatar
init  
Jitnaught committed
9
10
11
12
13
14
using System.Linq;

namespace RopeCreator
{
	class Menu : Script
	{
15
16
17
18
19
20
21
		static readonly string[] ROPE_NAMES = { "Rope", "Thick rope", "Thick cable", "Medium cable", "Cable" };
		static readonly List<int> NO_ROPES_INDICES = new List<int> { -1 };

		static ObjectPool objPool;
		internal static NativeMenu mainMenu, editGroupMenu, editRopeMenu;

		internal static NativeItem miDeleteLast, miDeleteAll;
Jitnaught's avatar
Jitnaught committed
22
		internal static NativeCheckboxItem cbEnabled, cbWindAll, cbUnwindAll, cbShowAimMarker, cbShowEditMarkers, cbBreakable, cbAttachObjBone, cbAttachPedBone, cbAttachNothing;
23
24
25
26
27
28
29
30
31
32
		internal static NativeListItem<int> liGroupIndex;
		internal static NativeListItem<string> liType;
		internal static NativeSliderItem siSlack, siMinLength;

		internal static NativeItem miDeleteGroup;
		internal static NativeCheckboxItem cbWindGroup, cbUnwindGroup;

		internal static NativeItem miDelete;
		internal static NativeCheckboxItem cbWind, cbUnwind;
		internal static NativeListItem<int> liRopeIndex;
Jitnaught's avatar
init  
Jitnaught committed
33

Jitnaught's avatar
Jitnaught committed
34
		internal static bool modEnabled = false, showAimMarker = true, showEditMarkers = true, breakable = false, attachObjBone = false, attachPedBone = false, attachNothing = false;
Jitnaught's avatar
init  
Jitnaught committed
35
36
37
		internal static int type = 1;
		internal static float slack = 0.25f, minLength = 0.25f;

38
39
		static NativeMenu lastVisibleMenu;
		static bool settingChecks = false;
Jitnaught's avatar
init  
Jitnaught committed
40
41
42

		public Menu()
		{
43
44
45
46
47
48
49
50
51
52
53
54
55
56
			objPool = new ObjectPool();

			mainMenu = new NativeMenu("Rope Creator");
			mainMenu.UseMouse = false;
			mainMenu.Opening += MainMenu_Opening;
			mainMenu.ItemActivated += MainMenu_ItemActivated;

			lastVisibleMenu = mainMenu;

			objPool.Add(mainMenu);

			cbEnabled = new NativeCheckboxItem("Enabled", modEnabled);
			cbEnabled.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
			liGroupIndex = new NativeListItem<int>("Rope group", 1, 2, 3, 4, 5);
57
			liGroupIndex.ItemChanged += liGroupIndex_ItemChanged;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
			miDeleteLast = new NativeItem("Delete last rope");
			miDeleteAll = new NativeItem("Delete all ropes");
			cbWindAll = new NativeCheckboxItem("Wind all ropes", false);
			cbWindAll.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
			cbUnwindAll = new NativeCheckboxItem("Unwind all ropes", false);
			cbUnwindAll.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
			cbShowAimMarker = new NativeCheckboxItem("Show aim marker", showAimMarker);
			cbShowAimMarker.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
			cbShowEditMarkers = new NativeCheckboxItem("Show edit rope markers", showEditMarkers);
			cbShowEditMarkers.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
			cbBreakable = new NativeCheckboxItem("Breakable", breakable);
			cbBreakable.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
			liType = new NativeListItem<string>("Type", ROPE_NAMES);
			liType.ItemChanged += liType_ItemChanged;
			siSlack = new NativeSliderItem("Slack", slack.ToString("0.00") + " meters");
Jitnaught's avatar
init  
Jitnaught committed
73
74
75
			siSlack.Value = (int)(slack * 4);
			siSlack.Multiplier = 1;
			siSlack.Maximum = 200;
76
77
			siSlack.ValueChanged += MainMenu_SliderItem_ValueChanged;
			siMinLength = new NativeSliderItem("Minimum length", minLength.ToString("0.00") + " meters");
Jitnaught's avatar
init  
Jitnaught committed
78
79
80
			siMinLength.Value = (int)(minLength * 4);
			siMinLength.Multiplier = 1;
			siMinLength.Maximum = 100;
81
82
83
84
85
			siMinLength.ValueChanged += MainMenu_SliderItem_ValueChanged;
			cbAttachObjBone = new NativeCheckboxItem("Attach to object bone", attachObjBone);
			cbAttachObjBone.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
			cbAttachPedBone = new NativeCheckboxItem("Attach to ped bone", attachPedBone);
			cbAttachPedBone.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
Jitnaught's avatar
Jitnaught committed
86
87
			cbAttachNothing = new NativeCheckboxItem("Attach to nothing", attachNothing);
			cbAttachNothing.CheckboxChanged += MainMenu_CheckboxItem_CheckChanged;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

			editGroupMenu = new NativeMenu("Edit Group");
			editGroupMenu.UseMouse = false;
			editGroupMenu.Parent = mainMenu;
			editGroupMenu.Opening += EditGroupMenu_Opening;

			objPool.Add(editGroupMenu);

			miDeleteGroup = new NativeItem("Delete all");
			miDeleteGroup.Activated += miDeleteGroup_Activated;
			cbWindGroup = new NativeCheckboxItem("Wind all");
			cbWindGroup.CheckboxChanged += EditGroup_CheckboxItem_CheckChanged;
			cbUnwindGroup = new NativeCheckboxItem("Unwind all");
			cbUnwindGroup.CheckboxChanged += EditGroup_CheckboxItem_CheckChanged;

			editRopeMenu = new NativeMenu("Edit Rope", "Edit specific rope");
			editRopeMenu.UseMouse = false;
			editRopeMenu.Opening += EditRopeMenu_Opening;

			objPool.Add(editRopeMenu);

			liRopeIndex = new NativeListItem<int>("Rope index", NO_ROPES_INDICES.ToArray());
			liRopeIndex.ItemChanged += liRopeIndex_ItemChanged;
			miDelete = new NativeItem("Delete");
Jitnaught's avatar
init  
Jitnaught committed
112
			miDelete.Activated += miDelete_Activated;
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
			cbWind = new NativeCheckboxItem("Wind", false);
			cbWind.CheckboxChanged += EditRope_CheckboxItem_CheckChanged;
			cbUnwind = new NativeCheckboxItem("Unwind", false);
			cbUnwind.CheckboxChanged += EditRope_CheckboxItem_CheckChanged;

			mainMenu.Add(cbEnabled);
			mainMenu.Add(liGroupIndex);
			mainMenu.Add(miDeleteLast);
			mainMenu.Add(miDeleteAll);
			mainMenu.Add(cbWindAll);
			mainMenu.Add(cbUnwindAll);
			mainMenu.Add(cbShowAimMarker);
			mainMenu.Add(cbShowEditMarkers);
			mainMenu.Add(cbBreakable);
			mainMenu.Add(liType);
			mainMenu.Add(siSlack);
			mainMenu.Add(siMinLength);
			mainMenu.Add(cbAttachObjBone);
			mainMenu.Add(cbAttachPedBone);
Jitnaught's avatar
Jitnaught committed
132
			mainMenu.Add(cbAttachNothing);
133
134
135
136
137
138
139
140
141
142

			editGroupMenu.AddSubMenu(editRopeMenu);
			editGroupMenu.Add(miDeleteGroup);
			editGroupMenu.Add(cbWindGroup);
			editGroupMenu.Add(cbUnwindGroup);

			editRopeMenu.Add(liRopeIndex);
			editRopeMenu.Add(miDelete);
			editRopeMenu.Add(cbWind);
			editRopeMenu.Add(cbUnwind);
Jitnaught's avatar
init  
Jitnaught committed
143
144
145
146

			Tick += Menu_Tick;
		}

147
		private void MainMenu_Opening(object sender, CancelEventArgs e)
Jitnaught's avatar
init  
Jitnaught committed
148
		{
149
			lastVisibleMenu = mainMenu;
150
151
152
			SetWindChecks();
		}

153
154
155
156
157
		private void liGroupIndex_ItemChanged(object sender, ItemChangedEventArgs<int> e)
		{
			ReloadRopeIndices(true);
		}

158
159
160
161
162
		private void MainMenu_ItemActivated(object sender, ItemActivatedArgs e)
		{
			var selectedGroup = RopeCreator.ropeGroups[liGroupIndex.SelectedIndex];

			if (e.Item == liGroupIndex)
Jitnaught's avatar
init  
Jitnaught committed
163
			{
164
165
166
167
168
169
170
171
172
				mainMenu.Close();

				if (!mainMenu.Visible)
				{
					editGroupMenu.Title = new ScaledText(PointF.Empty, $"Edit Group {liGroupIndex.SelectedItem}", 1.02f, GTA.Font.HouseScript)
					{
						Color = Color.White,
						Alignment = Alignment.Center
					};
Jitnaught's avatar
init  
Jitnaught committed
173

174
175
176
177
178
179
180
181
182
					editGroupMenu.Recalculate();

					editGroupMenu.Open();
				}
			}
			else if (e.Item == miDeleteLast)
			{
				RopeCreator.DeleteLastRope();
				ReloadRopeIndices();
Jitnaught's avatar
init  
Jitnaught committed
183
			}
184
			else if (e.Item == miDeleteAll)
Jitnaught's avatar
init  
Jitnaught committed
185
186
			{
				RopeCreator.DeleteAllRopes();
187
				ReloadRopeIndices();
Jitnaught's avatar
init  
Jitnaught committed
188
189
190
			}
		}

191
		private void MainMenu_SliderItem_ValueChanged(object sender, EventArgs e)
Jitnaught's avatar
init  
Jitnaught committed
192
193
194
		{
			if (sender == siSlack)
			{
195
				slack = siSlack.Value * 0.25f;
Jitnaught's avatar
init  
Jitnaught committed
196
197
198
199
				siSlack.Description = slack.ToString("0.00") + " meters";
			}
			else if (sender == siMinLength)
			{
200
				minLength = siMinLength.Value * 0.25f;
Jitnaught's avatar
init  
Jitnaught committed
201
202
203
				siMinLength.Description = minLength.ToString("0.00") + " meters";
			}

204
			mainMenu.Recalculate(); //so new text is drawn
Jitnaught's avatar
init  
Jitnaught committed
205
206
		}

207
		private void liType_ItemChanged(object sender, ItemChangedEventArgs<string> e)
Jitnaught's avatar
init  
Jitnaught committed
208
209
		{
			//index is rope type - 1 (rope types range from 1 to 6)
210
			type = liType.SelectedIndex + 1;
Jitnaught's avatar
init  
Jitnaught committed
211
212
		}

213
		private void MainMenu_CheckboxItem_CheckChanged(object sender, EventArgs e)
Jitnaught's avatar
init  
Jitnaught committed
214
		{
215
216
217
218
219
220
221
222
			if (settingChecks) return; //fix so can set checkboxes

			if (sender == cbEnabled) modEnabled = cbEnabled.Checked;
			else if (sender == cbShowAimMarker) showAimMarker = cbShowAimMarker.Checked;
			else if (sender == cbShowEditMarkers) showEditMarkers = cbShowEditMarkers.Checked;
			else if (sender == cbBreakable) breakable = cbBreakable.Checked;
			else if (sender == cbAttachObjBone) attachObjBone = cbAttachObjBone.Checked;
			else if (sender == cbAttachPedBone) attachPedBone = cbAttachPedBone.Checked;
Jitnaught's avatar
Jitnaught committed
223
			else if (sender == cbAttachNothing) attachNothing = cbAttachNothing.Checked;
Jitnaught's avatar
init  
Jitnaught committed
224
225
			else if (sender == cbWindAll)
			{
226
				if (cbWindAll.Checked) RopeCreator.StartWindAllRopes();
Jitnaught's avatar
init  
Jitnaught committed
227
228
				else RopeCreator.StopWindAllRopes();

229
				cbWindAll.Checked = cbWindAll.Checked;
Jitnaught's avatar
init  
Jitnaught committed
230
231
232
233
				cbUnwindAll.Checked = false;
			}
			else if (sender == cbUnwindAll)
			{
234
				if (cbUnwindAll.Checked) RopeCreator.StartUnwindAllRopes();
Jitnaught's avatar
init  
Jitnaught committed
235
236
				else RopeCreator.StopUnwindAllRopes();

237
				cbUnwindAll.Checked = cbUnwindAll.Checked;
Jitnaught's avatar
init  
Jitnaught committed
238
239
240
241
				cbWindAll.Checked = false;
			}
		}

242
		private void EditGroupMenu_Opening(object sender, CancelEventArgs e)
Jitnaught's avatar
init  
Jitnaught committed
243
		{
244
			lastVisibleMenu = editGroupMenu;
245
			SetWindChecks();
Jitnaught's avatar
init  
Jitnaught committed
246
247
		}

248
		private void EditGroup_CheckboxItem_CheckChanged(object sender, EventArgs e)
Jitnaught's avatar
init  
Jitnaught committed
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
			if (settingChecks) return; //fix so can set checkboxes

			var group = RopeCreator.ropeGroups[liGroupIndex.SelectedIndex];

			if (sender == cbWindGroup)
			{
				if (cbWindGroup.Checked)
				{
					cbUnwindGroup.Checked = false;
					group.StartWindRopes();
				}
				else group.StopWindRopes();
			}
			else if (sender == cbUnwindGroup)
			{
				if (cbUnwindGroup.Checked)
				{
					cbWindGroup.Checked = false;
					group.StartUnwindRopes();
				}
				else group.StopUnwindRopes();
			}
		}

		private void miDeleteGroup_Activated(object sender, EventArgs e)
		{
			var group = RopeCreator.ropeGroups[liGroupIndex.SelectedIndex];

			group.DeleteRopes();
		}

		private void EditRopeMenu_Opening(object sender, CancelEventArgs e)
		{
283
			lastVisibleMenu = editRopeMenu;
284
285
286
287
288
289
290
291
292
293
294
295
296
			ReloadRopeIndices();
		}

		private void liRopeIndex_ItemChanged(object sender, ItemChangedEventArgs<int> e)
		{
			SetWindChecks();
		}

		private void miDelete_Activated(object sender, EventArgs e)
		{
			var group = RopeCreator.ropeGroups[liGroupIndex.SelectedIndex];

			var selectedRopeIndex = liRopeIndex.SelectedItem;
Jitnaught's avatar
init  
Jitnaught committed
297
298
299

			if (selectedRopeIndex > -1)
			{
300
301
				group.ropes[selectedRopeIndex].Delete();
				group.ropes.RemoveAt(selectedRopeIndex);
Jitnaught's avatar
init  
Jitnaught committed
302

303
				ReloadRopeIndices();
Jitnaught's avatar
init  
Jitnaught committed
304
305
306
307
308

				UI.ShowSubtitle("Deleted");
			}
		}

309
		private void EditRope_CheckboxItem_CheckChanged(object sender, EventArgs e)
Jitnaught's avatar
init  
Jitnaught committed
310
		{
311
312
313
314
315
			if (settingChecks) return; //fix so can set checkboxes

			var group = RopeCreator.ropeGroups[liGroupIndex.SelectedIndex];

			int selectedRopeIndex = liRopeIndex.SelectedItem;
Jitnaught's avatar
init  
Jitnaught committed
316
317
318

			if (selectedRopeIndex > -1)
			{
319
				var selectedRope = group.ropes[selectedRopeIndex];
Jitnaught's avatar
init  
Jitnaught committed
320
321
322
323
324

				if (sender == cbWind)
				{
					cbUnwind.Checked = false;

325
					if (cbWind.Checked)
Jitnaught's avatar
init  
Jitnaught committed
326
327
328
329
330
331
332
333
334
335
336
337
338
					{
						selectedRope.StopUnwind();
						selectedRope.StartWind();
					}
					else
					{
						selectedRope.StopWind();
					}
				}
				else if (sender == cbUnwind)
				{
					cbWind.Checked = false;

339
					if (cbUnwind.Checked)
Jitnaught's avatar
init  
Jitnaught committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
					{
						selectedRope.StopWind();
						selectedRope.StartUnwind();
					}
					else
					{
						selectedRope.StopUnwind();
					}
				}
			}
			else
			{
				cbWind.Checked = false;
				cbUnwind.Checked = false;
			}
		}

		private void Menu_Tick(object sender, EventArgs e)
		{
359
360
361
362
363
364
			objPool.Process();
		}

		private static void SetWindChecks()
		{
			settingChecks = true; //fix so can set checks without setting off events
Jitnaught's avatar
init  
Jitnaught committed
365

366
367
			//main menu
			if (RopeCreator.AreAllRopesWinding())
Jitnaught's avatar
init  
Jitnaught committed
368
			{
369
370
				cbWindAll.Checked = true;
				cbUnwindAll.Checked = false;
Jitnaught's avatar
init  
Jitnaught committed
371
			}
372
			else if (RopeCreator.AreAllRopesUnwinding())
Jitnaught's avatar
init  
Jitnaught committed
373
			{
374
375
376
377
378
379
380
				cbUnwindAll.Checked = true;
				cbWindAll.Checked = false;
			}
			else
			{
				cbWindAll.Checked = false;
				cbUnwindAll.Checked = false;
Jitnaught's avatar
init  
Jitnaught committed
381
382
			}

383
384
			//edit group menu
			var group = RopeCreator.ropeGroups[liGroupIndex.SelectedIndex];
Jitnaught's avatar
init  
Jitnaught committed
385

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
			if (group.AreAllRopesWinding())
			{
				cbWindGroup.Checked = true;
				cbUnwindGroup.Checked = false;
			}
			else if (group.AreAllRopesUnwinding())
			{
				cbUnwindGroup.Checked = true;
				cbWindGroup.Checked = false;
			}
			else
			{
				cbWindGroup.Checked = false;
				cbUnwindGroup.Checked = false;
			}
Jitnaught's avatar
init  
Jitnaught committed
401

402
403
404
405
406
407
408
			//edit rope menu
			if (group.ropes.Count == 0)
			{
				cbWind.Checked = false;
				cbUnwind.Checked = false;
			}
			else
Jitnaught's avatar
init  
Jitnaught committed
409
			{
410
				var selectedRopeIndex = liRopeIndex.Items[liRopeIndex.SelectedIndex];
Jitnaught's avatar
init  
Jitnaught committed
411

412
413
414
415
416
417
418
				if (selectedRopeIndex > -1)
				{
					var selectedRope = group.ropes[selectedRopeIndex];

					cbWind.Checked = selectedRope.winding;
					cbUnwind.Checked = selectedRope.unwinding;
				}
Jitnaught's avatar
init  
Jitnaught committed
419
420
			}

421
			settingChecks = false;
Jitnaught's avatar
init  
Jitnaught committed
422
423
		}

424
		internal static void ReloadRopeIndices(bool selectLast = false)
Jitnaught's avatar
init  
Jitnaught committed
425
		{
426
427
428
429
430
431
432
			int oldIndex = liRopeIndex.SelectedIndex;

			//select first item, otherwise will get Exception when SelectedIndex > liRopeIndex.Items.Count
			liRopeIndex.SelectedIndex = 0;

			var group = RopeCreator.ropeGroups[liGroupIndex.SelectedIndex];
			int count = group.ropes.Count;
Jitnaught's avatar
init  
Jitnaught committed
433
434
435
436
437
438
439
440
441
442

			if (count == 0)
			{
				liRopeIndex.Items = NO_ROPES_INDICES;

				cbWind.Checked = false;
				cbUnwind.Checked = false;
			}
			else
			{
443
				liRopeIndex.Items = Enumerable.Range(0, count).ToList();
Jitnaught's avatar
init  
Jitnaught committed
444

445
446
447
448
				if (selectLast || oldIndex >= count) liRopeIndex.SelectedIndex = count - 1;
				else liRopeIndex.SelectedIndex = oldIndex;

				SetWindChecks();
Jitnaught's avatar
init  
Jitnaught committed
449
450
451
452
453
			}
		}

		internal static void Toggle()
		{
454
			if (objPool.AreAnyVisible)
Jitnaught's avatar
init  
Jitnaught committed
455
			{
456
457
458
459
460
				if (mainMenu.Visible) lastVisibleMenu = mainMenu;
				else if (editGroupMenu.Visible) lastVisibleMenu = editGroupMenu;
				else lastVisibleMenu = editRopeMenu;

				lastVisibleMenu.Close();
Jitnaught's avatar
init  
Jitnaught committed
461
			}
462
			else lastVisibleMenu.Open();
Jitnaught's avatar
init  
Jitnaught committed
463
464
465
		}
	}
}