Skip to content

Commit

Permalink
Hide mouse when moving camera
Browse files Browse the repository at this point in the history
+ refactor
  • Loading branch information
Keelhauled committed Mar 20, 2021
1 parent 28cfff1 commit d4ce953
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 55 deletions.
87 changes: 68 additions & 19 deletions src/RealPOV.Core/RealPOVCore.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Illusion;
using UnityEngine;
using UnityEngine.EventSystems;

namespace RealPOV.Core
{
Expand All @@ -18,16 +20,18 @@ public abstract class RealPOVCore : BaseUnityPlugin
internal static ConfigEntry<float> MouseSens { get; set; }
internal static ConfigEntry<KeyboardShortcut> POVHotkey { get; set; }

internal static bool POVEnabled;
internal static float CurrentFOV = -1;
internal static Vector3 LookRotation;
internal static Camera GameCamera;
protected static bool POVEnabled;
protected static float? CurrentFOV;
protected static Vector3 LookRotation;
protected static Camera GameCamera;
protected static float defaultViewOffset = 0.03f;
protected static float defaultFov = 70f;

private static float backupFOV;
private static float backupNearClip;

protected static float defaultViewOffset = 0.03f;
protected static float defaultFov = 70f;
private static bool allowCamera;
private bool mouseButtonDown0;
private bool mouseButtonDown1;

protected virtual void Awake()
{
Expand All @@ -44,41 +48,86 @@ private void Update()
if(POVHotkey.Value.IsDown())
{
if(POVEnabled)
DisablePOV();
DisablePov();
else
EnablePOV();
EnablePov();
}

}

private void LateUpdate()
{
if(POVEnabled)
{
if(Input.GetMouseButton(0))
if(!allowCamera)
{
var x = Input.GetAxis("Mouse X") * MouseSens.Value;
var y = -Input.GetAxis("Mouse Y") * MouseSens.Value;
LookRotation += new Vector3(y, x, 0f);
if(GUIUtility.hotControl == 0 && !EventSystem.current.IsPointerOverGameObject())
{
if(Input.GetMouseButtonDown(0))
{
mouseButtonDown0 = true;
allowCamera = true;
GameCursor.Instance.SetCursorLock(true);
}

if(Input.GetMouseButtonDown(1))
{
mouseButtonDown1 = true;
allowCamera = true;
GameCursor.Instance.SetCursorLock(true);
}
}
}
else if(Input.GetMouseButton(1))

if(allowCamera)
{
bool mouseUp0 = Input.GetMouseButtonUp(0);
bool mouseUp1 = Input.GetMouseButtonUp(1);

if((mouseButtonDown0 || mouseButtonDown1) && (mouseUp0 || mouseUp1))
{
if(mouseUp0) mouseButtonDown0 = false;
if(mouseUp1) mouseButtonDown1 = false;

if(!mouseButtonDown0 && !mouseButtonDown1)
{
allowCamera = false;
GameCursor.Instance.SetCursorLock(false);
}
}
}

if(allowCamera)
{
CurrentFOV += Input.GetAxis("Mouse X");
if(mouseButtonDown0)
{
var x = Input.GetAxis("Mouse X") * MouseSens.Value;
var y = -Input.GetAxis("Mouse Y") * MouseSens.Value;
LookRotation += new Vector3(y, x, 0f);
}
else if(mouseButtonDown1)
{
CurrentFOV += Input.GetAxis("Mouse X");
}
}
}
}

internal virtual void EnablePOV()
protected virtual void EnablePov()
{
POVEnabled = true;
backupFOV = GameCamera.fieldOfView;
backupNearClip = GameCamera.nearClipPlane;

if(CurrentFOV == -1)
if(CurrentFOV == null)
CurrentFOV = DefaultFOV.Value;
}

internal virtual void DisablePOV()
protected virtual void DisablePov()
{
POVEnabled = false;
GameCamera.fieldOfView = backupFOV;
GameCamera.nearClipPlane = backupNearClip;
GameCursor.Instance.SetCursorLock(false);
}
}
}
43 changes: 27 additions & 16 deletions src/RealPOV.Koikatu/RealPOV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace RealPOV.Koikatu
[BepInDependency(KKAPI.KoikatuAPI.GUID)]
public class RealPOV : RealPOVCore
{
public const string Version = "1.1.0." + BuildNumber.Version;
public const string Version = "1.2.0." + BuildNumber.Version;

private ConfigEntry<bool> HideHead { get; set; }

Expand All @@ -27,13 +27,13 @@ public class RealPOV : RealPOVCore
private readonly bool isStudio = Paths.ProcessName == "CharaStudio";
private bool prevVisibleHeadAlways;
private HFlag hFlag;
internal static int currentCharaID = -1;
private static int currentCharaId = -1;
private static RealPOV plugin;

protected override void Awake()
{
plugin = this;
defaultFov = 90;
defaultFov = 90f;
defaultViewOffset = 0.001f;
base.Awake();

Expand All @@ -50,20 +50,32 @@ protected override void Awake()
SceneManager.sceneUnloaded += arg0 => charaQueue = null;
}

public static void EnablePov(SceneDataController.PovData povData)
public static void EnablePov(ScenePovData povData)
{
if(Studio.Studio.Instance.dicObjectCtrl.TryGetValue(povData.CharaId, out var chara))
{
var ociChar = (OCIChar)chara;
currentChara = ociChar.charInfo;
currentCharaID = ociChar.objectInfo.dicKey;
currentChara = ((OCIChar)chara).charInfo;
currentCharaId = chara.objectInfo.dicKey;
LookRotation = povData.Rotation;
CurrentFOV = povData.Fov;
plugin.EnablePOV();
plugin.EnablePov();
}
}

internal override void EnablePOV()
public static ScenePovData GetPovData()
{
if(currentCharaId == -1)
return null;

return new ScenePovData
{
CharaId = currentCharaId,
Fov = CurrentFOV.Value,
Rotation = LookRotation
};
}

protected override void EnablePov()
{
if(!currentChara)
{
Expand All @@ -74,7 +86,7 @@ internal override void EnablePOV()
{
var ociChar = selectedCharas.First();
currentChara = ociChar.charInfo;
currentCharaID = ociChar.objectInfo.dicKey;
currentCharaId = ociChar.objectInfo.dicKey;
}
else
{
Expand Down Expand Up @@ -133,23 +145,23 @@ ChaControl GetCurrentChara()

//LookRotation = currentChara.objHeadBone.transform.rotation.eulerAngles;

base.EnablePOV();
base.EnablePov();

backupLayer = GameCamera.gameObject.layer;
GameCamera.gameObject.layer = 0;
}
}

internal override void DisablePOV()
protected override void DisablePov()
{
currentChara.fileStatus.visibleHeadAlways = prevVisibleHeadAlways;
currentChara = null;
currentCharaID = -1;
currentCharaId = -1;

var cc = (MonoBehaviour)GameCamera.GetComponent<CameraControl_Ver2>() ?? GameCamera.GetComponent<Studio.CameraControl>();
if(cc) cc.enabled = true;

base.DisablePOV();
base.DisablePov();

GameCamera.gameObject.layer = backupLayer;
}
Expand All @@ -175,13 +187,12 @@ private static bool ApplyRotation(NeckLookControllerVer2 __instance)
GameCamera.transform.position = Vector3.Lerp(eyeObjs[0].eyeTransform.position, eyeObjs[1].eyeTransform.position, 0.5f);
GameCamera.transform.rotation = currentChara.objHeadBone.transform.rotation;
GameCamera.transform.Translate(Vector3.forward * ViewOffset.Value);
GameCamera.fieldOfView = CurrentFOV;
GameCamera.fieldOfView = CurrentFOV.Value;

return false;
}
}

//__instance.target = POVEnabled ? currentChara.eyeLookCtrl.transform : Camera.main.transform;
return true;
}
}
Expand Down
31 changes: 11 additions & 20 deletions src/RealPOV.Koikatu/SceneDataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,26 @@ protected override void OnSceneLoad(SceneOperationKind operation, ReadOnlyDictio
return;

if(operation == SceneOperationKind.Load && extData.data.TryGetValue(DictID, out var povRawData))
{
var povData = MessagePackSerializer.Deserialize<PovData>((byte[])povRawData);
RealPOV.EnablePov(povData);
}
RealPOV.EnablePov(MessagePackSerializer.Deserialize<ScenePovData>((byte[])povRawData));
}

protected override void OnSceneSave()
{
if(RealPOV.currentCharaID != -1)
var povData = RealPOV.GetPovData();
if(povData != null)
{
var povData = new PovData
{
CharaId = RealPOV.currentCharaID,
Fov = RealPOVCore.CurrentFOV,
Rotation = RealPOVCore.LookRotation
};

var pluginData = new PluginData();
pluginData.data.Add(DictID, MessagePackSerializer.Serialize(povData));
SetExtendedData(pluginData);
}
}

[MessagePackObject(true)]
public class PovData
{
public Vector3 Rotation;
public int CharaId;
public float Fov;
}
}

[MessagePackObject(true)]
public class ScenePovData
{
public Vector3 Rotation;
public int CharaId;
public float Fov;
}
}

0 comments on commit d4ce953

Please sign in to comment.