Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 0d8ab8b

Browse files
committed
Allow FreeCam to run when menu closed
1 parent 5a3cad9 commit 0d8ab8b

File tree

1 file changed

+91
-75
lines changed

1 file changed

+91
-75
lines changed

src/UI/Panels/FreeCamPanel.cs

Lines changed: 91 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ public FreeCamPanel(UIBase owner) : base(owner)
2929
public override bool NavButtonWanted => true;
3030
public override bool ShouldSaveActiveState => true;
3131

32-
bool inFreeCamMode;
33-
Camera ourCamera;
34-
Camera lastMainCamera;
35-
Vector3 lastCameraMainPos;
36-
Quaternion lastCameraMainRot;
32+
internal static bool inFreeCamMode;
33+
internal static Camera ourCamera;
34+
internal static Camera lastMainCamera;
35+
internal static Vector3 lastCameraMainPos;
36+
internal static Quaternion lastCameraMainRot;
3737

38-
float desiredMoveSpeed = 10f;
39-
Vector3 previousMousePosition;
38+
internal static float desiredMoveSpeed = 10f;
39+
internal static Vector3 previousMousePosition;
40+
internal static Vector3 lastSetCameraPosition;
4041

41-
ButtonRef toggleButton;
42-
InputFieldRef positionInput;
43-
InputFieldRef moveSpeedInput;
44-
ButtonRef inspectButton;
42+
static ButtonRef startStopButton;
43+
static InputFieldRef positionInput;
44+
static InputFieldRef moveSpeedInput;
45+
static ButtonRef inspectButton;
4546

4647
void BeginFreecam()
4748
{
@@ -66,6 +67,7 @@ void BeginFreecam()
6667
ourCamera.gameObject.tag = "MainCamera";
6768
GameObject.DontDestroyOnLoad(ourCamera.gameObject);
6869
ourCamera.gameObject.hideFlags = HideFlags.HideAndDontSave;
70+
ourCamera.gameObject.AddComponent<FreeCamBehaviour>();
6971
}
7072

7173
ourCamera.gameObject.SetActive(true);
@@ -92,62 +94,16 @@ void SetToggleButtonState()
9294
{
9395
if (inFreeCamMode)
9496
{
95-
RuntimeHelper.SetColorBlockAuto(toggleButton.Component, new(0.4f, 0.2f, 0.2f));
96-
toggleButton.ButtonText.text = "End Freecam";
97+
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.4f, 0.2f, 0.2f));
98+
startStopButton.ButtonText.text = "End Freecam";
9799
}
98100
else
99101
{
100-
RuntimeHelper.SetColorBlockAuto(toggleButton.Component, new(0.2f, 0.4f, 0.2f));
101-
toggleButton.ButtonText.text = "Begin Freecam";
102+
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.2f, 0.4f, 0.2f));
103+
startStopButton.ButtonText.text = "Begin Freecam";
102104
}
103105
}
104106

105-
public override void Update()
106-
{
107-
if (inFreeCamMode)
108-
{
109-
Transform transform = ourCamera.transform;
110-
111-
float moveSpeed = desiredMoveSpeed * Time.deltaTime;
112-
113-
if (InputManager.GetKey(KeyCode.LeftShift) || InputManager.GetKey(KeyCode.RightShift))
114-
moveSpeed *= 10f;
115-
116-
if (InputManager.GetKey(KeyCode.LeftArrow) || InputManager.GetKey(KeyCode.A))
117-
transform.position += transform.right * -1 * moveSpeed;
118-
119-
if (InputManager.GetKey(KeyCode.RightArrow) || InputManager.GetKey(KeyCode.D))
120-
transform.position += transform.right * moveSpeed;
121-
122-
if (InputManager.GetKey(KeyCode.UpArrow) || InputManager.GetKey(KeyCode.W))
123-
transform.position += transform.forward * moveSpeed;
124-
125-
if (InputManager.GetKey(KeyCode.DownArrow) || InputManager.GetKey(KeyCode.S))
126-
transform.position += transform.forward * -1 * moveSpeed;
127-
128-
if (InputManager.GetKey(KeyCode.Space) || InputManager.GetKey(KeyCode.PageUp))
129-
transform.position += transform.up * moveSpeed;
130-
131-
if (InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.PageDown))
132-
transform.position += transform.up * -1 * moveSpeed;
133-
134-
if (InputManager.GetMouseButton(1))
135-
{
136-
Vector3 mouseDelta = InputManager.MousePosition - previousMousePosition;
137-
138-
float newRotationX = transform.localEulerAngles.y + mouseDelta.x * 0.3f;
139-
float newRotationY = transform.localEulerAngles.x - mouseDelta.y * 0.3f;
140-
transform.localEulerAngles = new Vector3(newRotationY, newRotationX, 0f);
141-
}
142-
143-
UpdatePositionInput();
144-
145-
previousMousePosition = InputManager.MousePosition;
146-
}
147-
}
148-
149-
Vector3 lastSetCameraPosition;
150-
151107
void SetCameraPosition(Vector3 pos)
152108
{
153109
if (!ourCamera || lastSetCameraPosition == pos)
@@ -157,7 +113,7 @@ void SetCameraPosition(Vector3 pos)
157113
lastSetCameraPosition = pos;
158114
}
159115

160-
void UpdatePositionInput()
116+
internal static void UpdatePositionInput()
161117
{
162118
if (!ourCamera)
163119
return;
@@ -168,30 +124,34 @@ void UpdatePositionInput()
168124

169125
protected override void ConstructPanelContent()
170126
{
171-
toggleButton = UIFactory.CreateButton(ContentRoot, "ToggleButton", "Freecam");
172-
UIFactory.SetLayoutElement(toggleButton.GameObject, minWidth: 150, minHeight: 25, flexibleWidth: 9999);
173-
toggleButton.OnClick += ToggleButton_OnClick;
174-
SetToggleButtonState();
175-
176-
AddSpacer(5);
177-
AddInputField("Position", "Camera Pos:", "eg. 0 0 0", out positionInput, PositionInput_OnEndEdit);
178-
179-
AddSpacer(5);
180-
AddInputField("MoveSpeed", "Move Speed:", "Default: 1", out moveSpeedInput, MoveSpeedInput_OnEndEdit);
181-
moveSpeedInput.Text = desiredMoveSpeed.ToString();
182-
183127
string instructions = @"Controls:
184128
- WASD/Arrows: Movement
185129
- Space/PgUp: Move up
186130
- LeftControl/PgDown: Move down
187131
- Right Mouse Button: Free look
188132
- Left Shift: Super speed";
189133

190-
AddSpacer(5);
191134
Text instructionsText = UIFactory.CreateLabel(ContentRoot, "Instructions", instructions, TextAnchor.UpperLeft);
192135
UIFactory.SetLayoutElement(instructionsText.gameObject, flexibleWidth: 9999, flexibleHeight: 9999);
193136

194137
AddSpacer(5);
138+
139+
startStopButton = UIFactory.CreateButton(ContentRoot, "ToggleButton", "Freecam");
140+
UIFactory.SetLayoutElement(startStopButton.GameObject, minWidth: 150, minHeight: 25, flexibleWidth: 9999);
141+
startStopButton.OnClick += ToggleButton_OnClick;
142+
SetToggleButtonState();
143+
144+
AddSpacer(5);
145+
146+
AddInputField("Position", "Camera Pos:", "eg. 0 0 0", out positionInput, PositionInput_OnEndEdit);
147+
148+
AddSpacer(5);
149+
150+
AddInputField("MoveSpeed", "Move Speed:", "Default: 1", out moveSpeedInput, MoveSpeedInput_OnEndEdit);
151+
moveSpeedInput.Text = desiredMoveSpeed.ToString();
152+
153+
AddSpacer(5);
154+
195155
inspectButton = UIFactory.CreateButton(ContentRoot, "InspectButton", "Inspect Free Camera");
196156
UIFactory.SetLayoutElement(inspectButton.GameObject, flexibleWidth: 9999, minHeight: 25);
197157
inspectButton.OnClick += () => { InspectorManager.Inspect(ourCamera); };
@@ -260,4 +220,60 @@ private void MoveSpeedInput_OnEndEdit(string input)
260220
desiredMoveSpeed = parsed;
261221
}
262222
}
223+
224+
internal class FreeCamBehaviour : MonoBehaviour
225+
{
226+
#if CPP
227+
static FreeCamBehaviour()
228+
{
229+
UnhollowerRuntimeLib.ClassInjector.RegisterTypeInIl2Cpp<FreeCamBehaviour>();
230+
}
231+
232+
public FreeCamBehaviour(IntPtr ptr) : base(ptr) { }
233+
#endif
234+
235+
internal void Update()
236+
{
237+
if (FreeCamPanel.inFreeCamMode)
238+
{
239+
Transform transform = FreeCamPanel.ourCamera.transform;
240+
241+
float moveSpeed = FreeCamPanel.desiredMoveSpeed * Time.deltaTime;
242+
243+
if (InputManager.GetKey(KeyCode.LeftShift) || InputManager.GetKey(KeyCode.RightShift))
244+
moveSpeed *= 10f;
245+
246+
if (InputManager.GetKey(KeyCode.LeftArrow) || InputManager.GetKey(KeyCode.A))
247+
transform.position += transform.right * -1 * moveSpeed;
248+
249+
if (InputManager.GetKey(KeyCode.RightArrow) || InputManager.GetKey(KeyCode.D))
250+
transform.position += transform.right * moveSpeed;
251+
252+
if (InputManager.GetKey(KeyCode.UpArrow) || InputManager.GetKey(KeyCode.W))
253+
transform.position += transform.forward * moveSpeed;
254+
255+
if (InputManager.GetKey(KeyCode.DownArrow) || InputManager.GetKey(KeyCode.S))
256+
transform.position += transform.forward * -1 * moveSpeed;
257+
258+
if (InputManager.GetKey(KeyCode.Space) || InputManager.GetKey(KeyCode.PageUp))
259+
transform.position += transform.up * moveSpeed;
260+
261+
if (InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.PageDown))
262+
transform.position += transform.up * -1 * moveSpeed;
263+
264+
if (InputManager.GetMouseButton(1))
265+
{
266+
Vector3 mouseDelta = InputManager.MousePosition - FreeCamPanel.previousMousePosition;
267+
268+
float newRotationX = transform.localEulerAngles.y + mouseDelta.x * 0.3f;
269+
float newRotationY = transform.localEulerAngles.x - mouseDelta.y * 0.3f;
270+
transform.localEulerAngles = new Vector3(newRotationY, newRotationX, 0f);
271+
}
272+
273+
FreeCamPanel.UpdatePositionInput();
274+
275+
FreeCamPanel.previousMousePosition = InputManager.MousePosition;
276+
}
277+
}
278+
}
263279
}

0 commit comments

Comments
 (0)