forked from CamxxCore/ScriptCamTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.cs
318 lines (277 loc) · 12.2 KB
/
Menu.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
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using GTA;
using GTA.Math;
using GTA.Native;
using NativeUI;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace GTAV_ScriptCamTool
{
public class Menu : Script
{
UIContainer msgBox = new UIContainer();
string msgText;
float msgScale;
Timer msgTimer = new Timer(1000);
bool fadingMsg = false;
int nodeDuration = 5000;
private SplineCamera splineCam;
private MenuPool activePool;
private UIMenu mainMenu, cameraOptionsMenu;
private PositionSelector selector;
public Menu()
{
Tick += OnTick;
KeyUp += KeyIsUp;
cameraOptionsMenu = new UIMenu("Camera Options", string.Empty);
var menuItem = new UIMenuListItem("Speed", Enumerable.Range(0, 100).Cast<dynamic>().ToList<dynamic>(), 1);
cameraOptionsMenu.AddItem(menuItem);
menuItem = new UIMenuListItem("Field Of View", Enumerable.Range(0, 100).Cast<dynamic>().ToList<dynamic>(), 50);
cameraOptionsMenu.AddItem(menuItem);
var menuItemB = new UIMenuCheckboxItem("Use Player View", false, "(May create smoother terrain rendering when enabled but restricts player movement and is prone to bugs.)");
cameraOptionsMenu.AddItem(menuItemB);
menuItemB = new UIMenuCheckboxItem("End At Player", false, "(Camera tracks to player view at end of the scene)");
cameraOptionsMenu.AddItem(menuItemB);
cameraOptionsMenu.OnListChange += OnListChanged;
cameraOptionsMenu.OnCheckboxChange += OnCheckboxChanged;
mainMenu = new UIMenu("Script Cam Tool", string.Empty);
var menuItemA = new UIMenuItem("~g~Start Rendering");
menuItemA.Activated += (sender, item) => StartInterpolatingCam();
mainMenu.AddItem(menuItemA);
menuItemA = new UIMenuItem("~r~Stop Rendering");
menuItemA.Activated += (sender, item) => StopInterpolatingCam();
mainMenu.AddItem(menuItemA);
menuItemA = new UIMenuItem("Setup Nodes");
menuItemA.Activated += (sender, item) => EnterPointSelector();
mainMenu.AddItem(menuItemA);
menuItemA = new UIMenuItem("Camera Options");
mainMenu.BindMenuToItem(cameraOptionsMenu, menuItemA);
mainMenu.AddItem(menuItemA);
menuItemA = new UIMenuItem("Reset All Cams");
menuItemA.Activated += (sender, item) => ResetAllCameras();
mainMenu.AddItem(menuItemA);
menuItemA = new UIMenuItem("Close");
menuItemA.Activated += (sender, item) => activePool.CloseAllMenus();
mainMenu.AddItem(menuItemA);
activePool = new MenuPool();
activePool.Add(mainMenu);
activePool.Add(cameraOptionsMenu);
menuItemA = new UIMenuItem("Save Nodes");
menuItemA.Activated += (sender, item) => saveNodes();
mainMenu.AddItem(menuItemA);
menuItemA = new UIMenuItem("Load Nodes");
menuItemA.Activated += (sender, item) => loadNodes();
mainMenu.AddItem(menuItemA);
ResetAllCameras();
}
private void OnTick(object sender, EventArgs e)
{
if (fadingMsg)
{
if (msgBox.Color.A > 10 || msgBox.Items[0].Color.A > 10)
{
msgBox.Draw();
msgBox.Items[0].Draw();
msgBox.Color = Color.FromArgb(msgBox.Color.A - 5, msgBox.Color.R, msgBox.Color.G, msgBox.Color.B);
msgBox.Items[0].Color = Color.FromArgb(msgBox.Color.A - 5, msgBox.Color.R, msgBox.Color.G, msgBox.Color.B);
}
else
{
msgBox = null;
fadingMsg = false;
}
}
else if (msgTimer.Enabled)
{
if (Game.GameTime > msgTimer.Waiter)
{
msgTimer.Enabled = false;
fadingMsg = true;
}
else
{
msgBox = new UIContainer(new Point(1100, 0), new Size(150, 34), Color.FromArgb(140, 0, 0, 0));
msgBox.Items.Add(new UIText(msgText, new Point(1121, 2), msgScale, Color.White));
msgBox.Draw();
msgBox.Items[0].Draw();
}
}
splineCam.Update();
selector.Update();
if (!activePool.IsAnyMenuOpen())
{
if (selector.MainCamera.IsActive)
{
if (Game.IsControlJustPressed(0, GTA.Control.SelectNextWeapon))
{
nodeDuration -= 100;
msgText = string.Format("Duration: {0}", nodeDuration);
msgScale = 0.4f;
msgTimer.Start();
}
else if (Game.IsControlJustPressed(0, GTA.Control.SelectPrevWeapon))
{
nodeDuration += 100;
msgText = string.Format("Duration: {0}", nodeDuration);
msgScale = 0.4f;
msgTimer.Start();
}
else if (Game.IsControlJustPressed(0, GTA.Control.ScriptRDown))
{
splineCam.AddNode(selector.MainCamera.Position, selector.MainCamera.Rotation, nodeDuration);
msgText = string.Format("~e~New node: {0}", selector.MainCamera.Position);
msgScale = 0.3f;
msgTimer.Start();
}
else if (Game.IsControlJustPressed(0, GTA.Control.ScriptRRight))
{
ExitPointSelector();
}
}
}
activePool.ProcessMenus();
}
private void KeyIsUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T)
ToggleMenu();
}
private void ToggleMenu()
{
var menus = new List<UIMenu> { mainMenu };
if (menus.Find(item => item.Visible) != null)
menus.ForEach(item => { if (item.Visible) item.Visible = false; });
else
mainMenu.Visible = !mainMenu.Visible;
}
private void OnListChanged(UIMenu sender, UIMenuListItem selectedItem, int index)
{
if (sender != cameraOptionsMenu) return;
if (selectedItem == sender.MenuItems[0])
{
//avoid dividing by zero
var speed = selectedItem.Index;
speed = speed > 0 ? speed : 1;
splineCam.Speed = speed;
}
else if (selectedItem == sender.MenuItems[1])
{
//avoid dividing by zero
var fov = selectedItem.Index;
fov = fov > 0 ? fov : 1;
splineCam.MainCamera.FieldOfView = fov;
}
}
private void OnCheckboxChanged(UIMenu sender, UIMenuCheckboxItem selectedItem, bool isChecked)
{
if (sender != cameraOptionsMenu) return;
if (selectedItem == sender.MenuItems[2])
splineCam.UsePlayerView = isChecked;
else if (selectedItem == sender.MenuItems[3])
splineCam.InterpToPlayer = isChecked;
}
private void saveNodes() {
List<Tuple<Vector3, Vector3>> nodes = splineCam.Nodes;
var sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.UTF8;
using (var writer = XmlWriter.Create(sb, settings)) {
writer.WriteStartElement("Nodes");
foreach (Tuple<Vector3, Vector3> node in nodes) {
writer.WriteStartElement("Node");
writer.WriteAttributeString("locX", node.Item1[0].ToString());
writer.WriteAttributeString("locY", node.Item1[1].ToString());
writer.WriteAttributeString("locZ", node.Item1[2].ToString());
writer.WriteAttributeString("rotX", node.Item2[0].ToString());
writer.WriteAttributeString("rotY", node.Item2[1].ToString());
writer.WriteAttributeString("rotZ", node.Item2[2].ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
}
File.WriteAllText("nodes.xml", sb.ToString());
}
private void loadNodes() {
List<Tuple<Vector3, Vector3>> nodes = new List<Tuple<Vector3, Vector3>>();
XmlDocument doc = new XmlDocument();
doc.Load("nodes.xml");
XmlNodeList xmlNodes = doc.DocumentElement.SelectNodes("/Nodes/Node");
foreach(XmlNode xmlNode in xmlNodes) {
// var attr = xmlNode.Attributes[0].Value;
Vector3 position = new Vector3(float.Parse(xmlNode.Attributes[0].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(xmlNode.Attributes[1].Value, System.Globalization.CultureInfo.InvariantCulture),float.Parse(xmlNode.Attributes[2].Value, System.Globalization.CultureInfo.InvariantCulture));
Vector3 rotation = new Vector3(float.Parse(xmlNode.Attributes[3].Value, System.Globalization.CultureInfo.InvariantCulture), float.Parse(xmlNode.Attributes[4].Value, System.Globalization.CultureInfo.InvariantCulture),float.Parse(xmlNode.Attributes[5].Value, System.Globalization.CultureInfo.InvariantCulture));
nodes.Add(new Tuple<Vector3, Vector3>(position, rotation));
}
splineCam.Nodes = nodes;
}
private void EnterPointSelector()
{
if (selector.MainCamera.IsActive || splineCam.MainCamera.IsActive)
{
UI.ShowSubtitle("Camera is Active.");
return;
}
Game.Player.Character.FreezePosition = true;
selector.EnterCameraView(Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0, 0, 10f)));
ToggleMenu();
}
private void ExitPointSelector()
{
selector.ExitCameraView();
Game.Player.Character.FreezePosition = false;
}
private void StartInterpolatingCam()
{
if (splineCam.Nodes.Count < 2)
{
UI.ShowSubtitle("Setup camera nodes first!");
return;
}
splineCam.EnterCameraView(Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0, 0, 10f)));
}
private void StopInterpolatingCam()
{
if (splineCam.MainCamera.IsActive)
{
splineCam.ExitCameraView();
}
else
UI.ShowSubtitle("Camera not active.");
}
private void ResetAllCameras()
{
if (splineCam != null)
{
if (splineCam.MainCamera.IsActive)
splineCam.ExitCameraView();
if (splineCam.Nodes.Count > 0)
splineCam.Nodes.Clear();
}
if (selector != null && selector.MainCamera.IsActive)
selector.ExitCameraView();
World.RenderingCamera = null;
Function.Call(Hash.DESTROY_ALL_CAMS, false);
splineCam = new SplineCamera();
selector = new PositionSelector(Vector3.Zero, Vector3.Zero);
}
protected override void Dispose(bool A_0)
{
if (splineCam != null && splineCam.UsePlayerView)
{
Game.Player.Character.IsInvincible = false;
Game.Player.Character.IsVisible = true;
}
World.RenderingCamera = null;
Function.Call(Hash.CLEAR_FOCUS);
base.Dispose(A_0);
}
}
}