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

Commit

Permalink
Add some more functionality to Freecam
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
sinai-dev committed Apr 19, 2022
1 parent 495bc41 commit 1f87e89
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 40 deletions.
Binary file modified UnityEditorPackage/Runtime/UnityExplorer.STANDALONE.Mono.dll
Binary file not shown.
193 changes: 153 additions & 40 deletions src/UI/Panels/FreeCamPanel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
Expand All @@ -21,90 +23,141 @@ public FreeCamPanel(UIBase owner) : base(owner)

public override string Name => "Freecam";
public override UIManager.Panels PanelType => UIManager.Panels.Freecam;

public override int MinWidth => 400;
public override int MinHeight => 300;
public override int MinHeight => 320;
public override Vector2 DefaultAnchorMin => new(0.4f, 0.4f);
public override Vector2 DefaultAnchorMax => new(0.6f, 0.6f);
public override bool NavButtonWanted => true;
public override bool ShouldSaveActiveState => true;

internal static bool inFreeCamMode;
internal static bool usingGameCamera;
internal static Camera ourCamera;
internal static Camera lastMainCamera;
internal static Vector3 lastCameraMainPos;
internal static Quaternion lastCameraMainRot;
internal static FreeCamBehaviour freeCamScript;

internal static float desiredMoveSpeed = 10f;

internal static Vector3 originalCameraPosition;
internal static Quaternion originalCameraRotation;

internal static Vector3? currentUserCameraPosition;
internal static Quaternion? currentUserCameraRotation;

internal static Vector3 previousMousePosition;

internal static Vector3 lastSetCameraPosition;

static ButtonRef startStopButton;
static Toggle useGameCameraToggle;
static InputFieldRef positionInput;
static InputFieldRef moveSpeedInput;
static ButtonRef inspectButton;

void BeginFreecam()
internal static void BeginFreecam()
{
inFreeCamMode = true;

previousMousePosition = InputManager.MousePosition;

CacheMainCamera();
SetupFreeCamera();

inspectButton.GameObject.SetActive(true);
}

static void CacheMainCamera()
{
Camera currentMain = Camera.main;
if (currentMain)
{
lastMainCamera = currentMain;
lastCameraMainPos = currentMain.transform.position;
lastCameraMainRot = currentMain.transform.rotation;
lastMainCamera.enabled = false;
originalCameraPosition = currentMain.transform.position;
originalCameraRotation = currentMain.transform.rotation;

if (currentUserCameraPosition == null)
{
currentUserCameraPosition = currentMain.transform.position;
currentUserCameraRotation = currentMain.transform.rotation;
}
}
else
lastCameraMainRot = Quaternion.identity;
originalCameraRotation = Quaternion.identity;
}

static void SetupFreeCamera()
{
if (useGameCameraToggle.isOn)
{
if (!lastMainCamera)
{
ExplorerCore.LogWarning($"There is no previous Camera found, reverting to default Free Cam.");
useGameCameraToggle.isOn = false;
}
else
{
usingGameCamera = true;
ourCamera = lastMainCamera;
}
}

if (!useGameCameraToggle.isOn)
{
usingGameCamera = false;

if (lastMainCamera)
lastMainCamera.enabled = false;
}

if (!ourCamera)
{
ourCamera = new GameObject("UE_Freecam").AddComponent<Camera>();
ourCamera.gameObject.tag = "MainCamera";
GameObject.DontDestroyOnLoad(ourCamera.gameObject);
ourCamera.gameObject.hideFlags = HideFlags.HideAndDontSave;
ourCamera.gameObject.AddComponent<FreeCamBehaviour>();
}

ourCamera.gameObject.SetActive(true);
ourCamera.transform.position = lastCameraMainPos;
ourCamera.transform.rotation = lastCameraMainRot;
if (!freeCamScript)
freeCamScript = ourCamera.gameObject.AddComponent<FreeCamBehaviour>();

inspectButton.GameObject.SetActive(true);
ourCamera.transform.position = (Vector3)currentUserCameraPosition;
ourCamera.transform.rotation = (Quaternion)currentUserCameraRotation;

ourCamera.gameObject.SetActive(true);
ourCamera.enabled = true;
}

void EndFreecam()
internal static void EndFreecam()
{
inFreeCamMode = false;

if (usingGameCamera)
{
ourCamera = null;

if (lastMainCamera)
{
lastMainCamera.transform.position = originalCameraPosition;
lastMainCamera.transform.rotation = originalCameraRotation;
}
}

if (ourCamera)
ourCamera.gameObject.SetActive(false);
else
inspectButton.GameObject.SetActive(false);

if (lastMainCamera)
lastMainCamera.enabled = true;
}

void SetToggleButtonState()
{
if (inFreeCamMode)
if (freeCamScript)
{
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.4f, 0.2f, 0.2f));
startStopButton.ButtonText.text = "End Freecam";
}
else
{
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.2f, 0.4f, 0.2f));
startStopButton.ButtonText.text = "Begin Freecam";
GameObject.Destroy(freeCamScript);
freeCamScript = null;
}

if (lastMainCamera)
lastMainCamera.enabled = true;
}

void SetCameraPosition(Vector3 pos)
static void SetCameraPosition(Vector3 pos)
{
if (!ourCamera || lastSetCameraPosition == pos)
return;
Expand All @@ -122,16 +175,30 @@ internal static void UpdatePositionInput()
positionInput.Text = ParseUtility.ToStringForInput<Vector3>(lastSetCameraPosition);
}

// ~~~~~~~~ UI construction / callbacks ~~~~~~~~

protected override void ConstructPanelContent()
{
startStopButton = UIFactory.CreateButton(ContentRoot, "ToggleButton", "Freecam");
UIFactory.SetLayoutElement(startStopButton.GameObject, minWidth: 150, minHeight: 25, flexibleWidth: 9999);
startStopButton.OnClick += ToggleButton_OnClick;
startStopButton.OnClick += StartStopButton_OnClick;
SetToggleButtonState();

AddSpacer(5);

AddInputField("Position", "Camera Pos:", "eg. 0 0 0", out positionInput, PositionInput_OnEndEdit);
GameObject toggleObj = UIFactory.CreateToggle(ContentRoot, "UseGameCameraToggle", out useGameCameraToggle, out Text toggleText);
UIFactory.SetLayoutElement(toggleObj, minHeight: 25, flexibleWidth: 9999);
useGameCameraToggle.onValueChanged.AddListener(OnUseGameCameraToggled);
useGameCameraToggle.isOn = false;
toggleText.text = "Use Game Camera?";

AddSpacer(5);

GameObject posRow = AddInputField("Position", "Freecam Pos:", "eg. 0 0 0", out positionInput, PositionInput_OnEndEdit);

ButtonRef resetPosButton = UIFactory.CreateButton(posRow, "ResetButton", "Reset");
UIFactory.SetLayoutElement(resetPosButton.GameObject, minWidth: 70, minHeight: 25);
resetPosButton.OnClick += OnResetPosButtonClicked;

AddSpacer(5);

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

string instructions = @"Controls:
- WASD/Arrows: Movement
- Space/PgUp: Move up
- LeftControl/PgDown: Move down
- WASD / Arrows: Movement
- Space / PgUp: Move up
- LeftCtrl / PgDown: Move down
- Right Mouse Button: Free look
- Left Shift: Super speed";
- Shift: Super speed";

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

void ToggleButton_OnClick()
void StartStopButton_OnClick()
{
EventSystemHelper.SetSelectedGameObject(null);

Expand All @@ -192,7 +259,44 @@ void ToggleButton_OnClick()
SetToggleButtonState();
}

private void PositionInput_OnEndEdit(string input)
void SetToggleButtonState()
{
if (inFreeCamMode)
{
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.4f, 0.2f, 0.2f));
startStopButton.ButtonText.text = "End Freecam";
}
else
{
RuntimeHelper.SetColorBlockAuto(startStopButton.Component, new(0.2f, 0.4f, 0.2f));
startStopButton.ButtonText.text = "Begin Freecam";
}
}

void OnUseGameCameraToggled(bool value)
{
EventSystemHelper.SetSelectedGameObject(null);

if (!inFreeCamMode)
return;

EndFreecam();
BeginFreecam();
}

void OnResetPosButtonClicked()
{
currentUserCameraPosition = originalCameraPosition;
currentUserCameraRotation = originalCameraRotation;

if (inFreeCamMode && ourCamera)
{
ourCamera.transform.position = (Vector3)currentUserCameraPosition;
ourCamera.transform.rotation = (Quaternion)currentUserCameraRotation;
}
}

void PositionInput_OnEndEdit(string input)
{
EventSystemHelper.SetSelectedGameObject(null);

Expand All @@ -206,7 +310,7 @@ private void PositionInput_OnEndEdit(string input)
SetCameraPosition(parsed);
}

private void MoveSpeedInput_OnEndEdit(string input)
void MoveSpeedInput_OnEndEdit(string input)
{
EventSystemHelper.SetSelectedGameObject(null);

Expand Down Expand Up @@ -236,8 +340,17 @@ internal void Update()
{
if (FreeCamPanel.inFreeCamMode)
{
if (!FreeCamPanel.ourCamera)
{
FreeCamPanel.EndFreecam();
return;
}

Transform transform = FreeCamPanel.ourCamera.transform;

FreeCamPanel.currentUserCameraPosition = transform.position;
FreeCamPanel.currentUserCameraRotation = transform.rotation;

float moveSpeed = FreeCamPanel.desiredMoveSpeed * Time.deltaTime;

if (InputManager.GetKey(KeyCode.LeftShift) || InputManager.GetKey(KeyCode.RightShift))
Expand Down

0 comments on commit 1f87e89

Please sign in to comment.