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

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafacasari committed May 15, 2021
1 parent 2140aa6 commit 9a9f264
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 133 deletions.
86 changes: 0 additions & 86 deletions BindManager.cs

This file was deleted.

100 changes: 54 additions & 46 deletions Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Usings
#region
using MelonLoader;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -9,26 +9,26 @@ namespace PlayspaceMover
{
public static class ModInfo
{
public const string Name = "OculusPlayspaceMover";
public const string Description = "A SteamVR's Playspace clone for VRChat from Oculus Store";
public const string Author = "Rafa";
public const string Company = "RBX";
public const string Version = "1.1.2";
public const string DownloadLink = null;
public static readonly string Name = "OculusPlayspaceMover";
public static readonly string Description = "A SteamVR's Playspace clone for VRChat from Oculus Store";
public static readonly string Author = "Rafa";
public static readonly string Company = "RBX";
public static readonly string Version = "1.1.3";
public static readonly string DownloadLink = null;
}

public class Main : MelonMod
{
#region Settings
private readonly string Category = "PlayspaceMover";
private bool Enabled = true;
private float Strength = 1f;
private float DoubleClickTime = 0.25f;
private bool DisableDoubleClick = false;
private bool DisableLeftHand = false;
private bool DisableRightHand = false;
private bool DisableDoubleClick;
private bool DisableLeftHand;
private bool DisableRightHand;
#endregion

private readonly string Category = "PlayspaceMover";

public override void OnApplicationStart()
{
MelonPreferences.CreateCategory(Category, "Oculus Playspace Mover");
Expand All @@ -42,26 +42,6 @@ public override void OnApplicationStart()
ApplySettings();

MelonCoroutines.Start(WaitInitialization());

//if (MelonHandler.Mods.Any(x => x.Info.Name == "UI Expansion Kit"))
//{
// BindManager.Initialize();

// var playspaceSettings = ExpansionKitApi.CreateCustomFullMenuPopup(LayoutDescription.WideSlimList);
// playspaceSettings.AddSimpleButton("Left Hand", new Action(() => BindManager.Show("Left Hand Spacedrag", new Action<KeyCode>(key =>
// {

// }), null)
// ));

// playspaceSettings.AddSimpleButton("Right Hand", new Action(() =>
// BindManager.Show("Right Hand Spacedrag", new Action<KeyCode>(key => {

// }), null)
// ));

// ExpansionKitApi.GetExpandedMenu(ExpandedMenu.SettingsMenu).AddSimpleButton("Oculus Playspace", new Action(() => playspaceSettings.Show()));
//}
}

private void ApplySettings()
Expand All @@ -77,13 +57,18 @@ private void ApplySettings()
public override void OnPreferencesSaved() => ApplySettings();

private OVRCameraRig Camera;
private bool isLeftPressed, isRightPressed = false;
//private bool isLeftPressed, isRightPressed = false;
private OVRInput.Controller LastPressed;
private Vector3 startingOffset;
private Vector3 StartPosition;

private IEnumerator WaitInitialization()
{
while (VRCUiManager.prop_VRCUiManager_0 == null) yield return new WaitForFixedUpdate();
// Wait for the VRCUiManager
while (VRCUiManager.prop_VRCUiManager_0 == null)
{
yield return new WaitForFixedUpdate();
}

var objects = Object.FindObjectsOfType(UnhollowerRuntimeLib.Il2CppType.Of<OVRCameraRig>());
if (objects != null && objects.Length > 0)
Expand All @@ -94,36 +79,50 @@ private IEnumerator WaitInitialization()
}

MelonLogger.Error("OVRCameraRig not found, this mod only work in Oculus for now!");
yield break;
}

public override void OnUpdate()
{
if (!Enabled || Camera == null) return;
if (!Enabled || Camera == null)
{
return;
}

if (!DisableDoubleClick && (HasDoubleClicked(OVRInput.Button.Three, DoubleClickTime) || HasDoubleClicked(OVRInput.Button.One, DoubleClickTime)))
{
Camera.trackingSpace.localPosition = StartPosition;
return;
}

isLeftPressed = IsKeyJustPressed(OVRInput.Button.Three);
isRightPressed = IsKeyJustPressed(OVRInput.Button.One);
bool isLeftPressed = IsKeyJustPressed(OVRInput.Button.Three);
bool isRightPressed = IsKeyJustPressed(OVRInput.Button.One);

if (isLeftPressed || isRightPressed) startingOffset = OVRInput.GetLocalControllerPosition(isLeftPressed ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch);
if (isLeftPressed || isRightPressed)
{
startingOffset = OVRInput.GetLocalControllerPosition(isLeftPressed ? OVRInput.Controller.LTouch : OVRInput.Controller.RTouch);

if (isLeftPressed)
{
LastPressed = OVRInput.Controller.LTouch;
}
else if (isRightPressed)
{
LastPressed = OVRInput.Controller.RTouch;
}
}

bool leftTrigger = OVRInput.Get(OVRInput.Button.Three, OVRInput.Controller.Touch);
bool rightTrigger = OVRInput.Get(OVRInput.Button.One, OVRInput.Controller.Touch);

if (leftTrigger && !DisableLeftHand)
if (leftTrigger && LastPressed == OVRInput.Controller.LTouch && !DisableLeftHand)
{
Vector3 currentOffset = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
Vector3 calculatedOffset = (currentOffset - startingOffset) * -Strength;
startingOffset = currentOffset;
Camera.trackingSpace.localPosition += calculatedOffset;
}

if (rightTrigger && !DisableRightHand)
if (rightTrigger && LastPressed == OVRInput.Controller.RTouch && !DisableRightHand)
{
Vector3 currentOffset = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
Vector3 calculatedOffset = (currentOffset - startingOffset) * -Strength;
Expand All @@ -139,10 +138,12 @@ public override void OnUpdate()

private static bool IsKeyJustPressed(OVRInput.Button key)
{
if (!PreviousStates.ContainsKey(key)) PreviousStates.Add(key, false);
if (!PreviousStates.ContainsKey(key))
{
PreviousStates.Add(key, false);
}

if (OVRInput.Get(key, OVRInput.Controller.Touch) && !PreviousStates[key]) return PreviousStates[key] = true;
else return PreviousStates[key] = false;
return PreviousStates[key] = OVRInput.Get(key, OVRInput.Controller.Touch) && !PreviousStates[key];
}

private static readonly Dictionary<OVRInput.Button, float> lastTime = new Dictionary<OVRInput.Button, float>();
Expand All @@ -151,8 +152,15 @@ private static bool IsKeyJustPressed(OVRInput.Button key)
// https://github.com/Psychloor/DoubleTapRunner/blob/master/DoubleTapSpeed/Utilities.cs#L30
public static bool HasDoubleClicked(OVRInput.Button keyCode, float threshold)
{
if (!OVRInput.GetDown(keyCode, OVRInput.Controller.Touch)) return false;
if (!lastTime.ContainsKey(keyCode)) lastTime.Add(keyCode, Time.time);
if (!OVRInput.GetDown(keyCode, OVRInput.Controller.Touch))
{
return false;
}

if (!lastTime.ContainsKey(keyCode))
{
lastTime.Add(keyCode, Time.time);
}

if (Time.time - lastTime[keyCode] <= threshold)
{
Expand Down
1 change: 0 additions & 1 deletion PlayspaceMover.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BindManager.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down

0 comments on commit 9a9f264

Please sign in to comment.