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

Commit 1f87e89

Browse files
committed
Add some more functionality to Freecam
- Option to use Game's camera - Button to reset position back to original cam pos - Free cam position will be stored between stopping/starting free cam
1 parent 495bc41 commit 1f87e89

File tree

2 files changed

+153
-40
lines changed

2 files changed

+153
-40
lines changed
1.5 KB
Binary file not shown.

src/UI/Panels/FreeCamPanel.cs

Lines changed: 153 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System;
1+
using HarmonyLib;
2+
using System;
23
using System.Collections.Generic;
34
using System.Linq;
5+
using System.Reflection;
46
using System.Text;
57
using UnityEngine;
68
using UnityEngine.EventSystems;
@@ -21,90 +23,141 @@ public FreeCamPanel(UIBase owner) : base(owner)
2123

2224
public override string Name => "Freecam";
2325
public override UIManager.Panels PanelType => UIManager.Panels.Freecam;
24-
2526
public override int MinWidth => 400;
26-
public override int MinHeight => 300;
27+
public override int MinHeight => 320;
2728
public override Vector2 DefaultAnchorMin => new(0.4f, 0.4f);
2829
public override Vector2 DefaultAnchorMax => new(0.6f, 0.6f);
2930
public override bool NavButtonWanted => true;
3031
public override bool ShouldSaveActiveState => true;
3132

3233
internal static bool inFreeCamMode;
34+
internal static bool usingGameCamera;
3335
internal static Camera ourCamera;
3436
internal static Camera lastMainCamera;
35-
internal static Vector3 lastCameraMainPos;
36-
internal static Quaternion lastCameraMainRot;
37+
internal static FreeCamBehaviour freeCamScript;
3738

3839
internal static float desiredMoveSpeed = 10f;
40+
41+
internal static Vector3 originalCameraPosition;
42+
internal static Quaternion originalCameraRotation;
43+
44+
internal static Vector3? currentUserCameraPosition;
45+
internal static Quaternion? currentUserCameraRotation;
46+
3947
internal static Vector3 previousMousePosition;
48+
4049
internal static Vector3 lastSetCameraPosition;
4150

4251
static ButtonRef startStopButton;
52+
static Toggle useGameCameraToggle;
4353
static InputFieldRef positionInput;
4454
static InputFieldRef moveSpeedInput;
4555
static ButtonRef inspectButton;
4656

47-
void BeginFreecam()
57+
internal static void BeginFreecam()
4858
{
4959
inFreeCamMode = true;
5060

5161
previousMousePosition = InputManager.MousePosition;
5262

63+
CacheMainCamera();
64+
SetupFreeCamera();
65+
66+
inspectButton.GameObject.SetActive(true);
67+
}
68+
69+
static void CacheMainCamera()
70+
{
5371
Camera currentMain = Camera.main;
5472
if (currentMain)
5573
{
5674
lastMainCamera = currentMain;
57-
lastCameraMainPos = currentMain.transform.position;
58-
lastCameraMainRot = currentMain.transform.rotation;
59-
lastMainCamera.enabled = false;
75+
originalCameraPosition = currentMain.transform.position;
76+
originalCameraRotation = currentMain.transform.rotation;
77+
78+
if (currentUserCameraPosition == null)
79+
{
80+
currentUserCameraPosition = currentMain.transform.position;
81+
currentUserCameraRotation = currentMain.transform.rotation;
82+
}
6083
}
6184
else
62-
lastCameraMainRot = Quaternion.identity;
85+
originalCameraRotation = Quaternion.identity;
86+
}
87+
88+
static void SetupFreeCamera()
89+
{
90+
if (useGameCameraToggle.isOn)
91+
{
92+
if (!lastMainCamera)
93+
{
94+
ExplorerCore.LogWarning($"There is no previous Camera found, reverting to default Free Cam.");
95+
useGameCameraToggle.isOn = false;
96+
}
97+
else
98+
{
99+
usingGameCamera = true;
100+
ourCamera = lastMainCamera;
101+
}
102+
}
103+
104+
if (!useGameCameraToggle.isOn)
105+
{
106+
usingGameCamera = false;
107+
108+
if (lastMainCamera)
109+
lastMainCamera.enabled = false;
110+
}
63111

64112
if (!ourCamera)
65113
{
66114
ourCamera = new GameObject("UE_Freecam").AddComponent<Camera>();
67115
ourCamera.gameObject.tag = "MainCamera";
68116
GameObject.DontDestroyOnLoad(ourCamera.gameObject);
69117
ourCamera.gameObject.hideFlags = HideFlags.HideAndDontSave;
70-
ourCamera.gameObject.AddComponent<FreeCamBehaviour>();
71118
}
72119

73-
ourCamera.gameObject.SetActive(true);
74-
ourCamera.transform.position = lastCameraMainPos;
75-
ourCamera.transform.rotation = lastCameraMainRot;
120+
if (!freeCamScript)
121+
freeCamScript = ourCamera.gameObject.AddComponent<FreeCamBehaviour>();
76122

77-
inspectButton.GameObject.SetActive(true);
123+
ourCamera.transform.position = (Vector3)currentUserCameraPosition;
124+
ourCamera.transform.rotation = (Quaternion)currentUserCameraRotation;
125+
126+
ourCamera.gameObject.SetActive(true);
127+
ourCamera.enabled = true;
78128
}
79129

80-
void EndFreecam()
130+
internal static void EndFreecam()
81131
{
82132
inFreeCamMode = false;
83133

134+
if (usingGameCamera)
135+
{
136+
ourCamera = null;
137+
138+
if (lastMainCamera)
139+
{
140+
lastMainCamera.transform.position = originalCameraPosition;
141+
lastMainCamera.transform.rotation = originalCameraRotation;
142+
}
143+
}
144+
84145
if (ourCamera)
85146
ourCamera.gameObject.SetActive(false);
86147
else
87148
inspectButton.GameObject.SetActive(false);
88149

89-
if (lastMainCamera)
90-
lastMainCamera.enabled = true;
91-
}
92-
93-
void SetToggleButtonState()
94-
{
95-
if (inFreeCamMode)
150+
if (freeCamScript)
96151
{
97-
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.4f, 0.2f, 0.2f));
98-
startStopButton.ButtonText.text = "End Freecam";
99-
}
100-
else
101-
{
102-
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.2f, 0.4f, 0.2f));
103-
startStopButton.ButtonText.text = "Begin Freecam";
152+
GameObject.Destroy(freeCamScript);
153+
freeCamScript = null;
104154
}
155+
156+
if (lastMainCamera)
157+
lastMainCamera.enabled = true;
105158
}
106159

107-
void SetCameraPosition(Vector3 pos)
160+
static void SetCameraPosition(Vector3 pos)
108161
{
109162
if (!ourCamera || lastSetCameraPosition == pos)
110163
return;
@@ -122,16 +175,30 @@ internal static void UpdatePositionInput()
122175
positionInput.Text = ParseUtility.ToStringForInput<Vector3>(lastSetCameraPosition);
123176
}
124177

178+
// ~~~~~~~~ UI construction / callbacks ~~~~~~~~
179+
125180
protected override void ConstructPanelContent()
126181
{
127182
startStopButton = UIFactory.CreateButton(ContentRoot, "ToggleButton", "Freecam");
128183
UIFactory.SetLayoutElement(startStopButton.GameObject, minWidth: 150, minHeight: 25, flexibleWidth: 9999);
129-
startStopButton.OnClick += ToggleButton_OnClick;
184+
startStopButton.OnClick += StartStopButton_OnClick;
130185
SetToggleButtonState();
131186

132187
AddSpacer(5);
133188

134-
AddInputField("Position", "Camera Pos:", "eg. 0 0 0", out positionInput, PositionInput_OnEndEdit);
189+
GameObject toggleObj = UIFactory.CreateToggle(ContentRoot, "UseGameCameraToggle", out useGameCameraToggle, out Text toggleText);
190+
UIFactory.SetLayoutElement(toggleObj, minHeight: 25, flexibleWidth: 9999);
191+
useGameCameraToggle.onValueChanged.AddListener(OnUseGameCameraToggled);
192+
useGameCameraToggle.isOn = false;
193+
toggleText.text = "Use Game Camera?";
194+
195+
AddSpacer(5);
196+
197+
GameObject posRow = AddInputField("Position", "Freecam Pos:", "eg. 0 0 0", out positionInput, PositionInput_OnEndEdit);
198+
199+
ButtonRef resetPosButton = UIFactory.CreateButton(posRow, "ResetButton", "Reset");
200+
UIFactory.SetLayoutElement(resetPosButton.GameObject, minWidth: 70, minHeight: 25);
201+
resetPosButton.OnClick += OnResetPosButtonClicked;
135202

136203
AddSpacer(5);
137204

@@ -141,11 +208,11 @@ protected override void ConstructPanelContent()
141208
AddSpacer(5);
142209

143210
string instructions = @"Controls:
144-
- WASD/Arrows: Movement
145-
- Space/PgUp: Move up
146-
- LeftControl/PgDown: Move down
211+
- WASD / Arrows: Movement
212+
- Space / PgUp: Move up
213+
- LeftCtrl / PgDown: Move down
147214
- Right Mouse Button: Free look
148-
- Left Shift: Super speed";
215+
- Shift: Super speed";
149216

150217
Text instructionsText = UIFactory.CreateLabel(ContentRoot, "Instructions", instructions, TextAnchor.UpperLeft);
151218
UIFactory.SetLayoutElement(instructionsText.gameObject, flexibleWidth: 9999, flexibleHeight: 9999);
@@ -180,7 +247,7 @@ GameObject AddInputField(string name, string labelText, string placeHolder, out
180247
return row;
181248
}
182249

183-
void ToggleButton_OnClick()
250+
void StartStopButton_OnClick()
184251
{
185252
EventSystemHelper.SetSelectedGameObject(null);
186253

@@ -192,7 +259,44 @@ void ToggleButton_OnClick()
192259
SetToggleButtonState();
193260
}
194261

195-
private void PositionInput_OnEndEdit(string input)
262+
void SetToggleButtonState()
263+
{
264+
if (inFreeCamMode)
265+
{
266+
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.4f, 0.2f, 0.2f));
267+
startStopButton.ButtonText.text = "End Freecam";
268+
}
269+
else
270+
{
271+
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.2f, 0.4f, 0.2f));
272+
startStopButton.ButtonText.text = "Begin Freecam";
273+
}
274+
}
275+
276+
void OnUseGameCameraToggled(bool value)
277+
{
278+
EventSystemHelper.SetSelectedGameObject(null);
279+
280+
if (!inFreeCamMode)
281+
return;
282+
283+
EndFreecam();
284+
BeginFreecam();
285+
}
286+
287+
void OnResetPosButtonClicked()
288+
{
289+
currentUserCameraPosition = originalCameraPosition;
290+
currentUserCameraRotation = originalCameraRotation;
291+
292+
if (inFreeCamMode && ourCamera)
293+
{
294+
ourCamera.transform.position = (Vector3)currentUserCameraPosition;
295+
ourCamera.transform.rotation = (Quaternion)currentUserCameraRotation;
296+
}
297+
}
298+
299+
void PositionInput_OnEndEdit(string input)
196300
{
197301
EventSystemHelper.SetSelectedGameObject(null);
198302

@@ -206,7 +310,7 @@ private void PositionInput_OnEndEdit(string input)
206310
SetCameraPosition(parsed);
207311
}
208312

209-
private void MoveSpeedInput_OnEndEdit(string input)
313+
void MoveSpeedInput_OnEndEdit(string input)
210314
{
211315
EventSystemHelper.SetSelectedGameObject(null);
212316

@@ -236,8 +340,17 @@ internal void Update()
236340
{
237341
if (FreeCamPanel.inFreeCamMode)
238342
{
343+
if (!FreeCamPanel.ourCamera)
344+
{
345+
FreeCamPanel.EndFreecam();
346+
return;
347+
}
348+
239349
Transform transform = FreeCamPanel.ourCamera.transform;
240350

351+
FreeCamPanel.currentUserCameraPosition = transform.position;
352+
FreeCamPanel.currentUserCameraRotation = transform.rotation;
353+
241354
float moveSpeed = FreeCamPanel.desiredMoveSpeed * Time.deltaTime;
242355

243356
if (InputManager.GetKey(KeyCode.LeftShift) || InputManager.GetKey(KeyCode.RightShift))

0 commit comments

Comments
 (0)