Skip to content

Commit

Permalink
VR POV v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tony246642 committed Jun 18, 2024
1 parent a45f616 commit e550b06
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 30 deletions.
83 changes: 57 additions & 26 deletions KoikatuVR/POV.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class POV : ProtectedBehaviour

private Quaternion _tmp_head_y; // for POV_MODE.EYE

private SteamVR_Controller.Device L_device;
private SteamVR_Controller.Device R_device;

protected override void OnAwake()
{
base.OnAwake();
Expand All @@ -45,6 +48,10 @@ protected override void OnAwake()
_settings = VR.Context.Settings as KoikatuSettings;

_tmp_head_y = Quaternion.identity;

L_device = SteamVR_Controller.Input((int)VR.Mode.Left.Tracking.index);

R_device = SteamVR_Controller.Input((int)VR.Mode.Right.Tracking.index);
}

private void SetCameraToCharEye(ChaControl target)
Expand Down Expand Up @@ -106,7 +113,8 @@ private int getCurrChaIdx(ChaControl[] targets)
{
for(int i = 0; i < targets.Length; i++)
{
if(ChaControl.ReferenceEquals(targets[i], _currentTarget) && _currentTarget.sex != 1)
if(ChaControl.ReferenceEquals(targets[i], _currentTarget) &&
((_currentTarget.sex == (int)POVConfig.targetGender.Value) || (POVConfig.targetGender.Value == POVConfig.Gender.All)))
{
return i;
}
Expand All @@ -131,7 +139,7 @@ private void nextChar(ChaControl[] targets, int currentTargetIndex, bool keep_ch

if (_currentTarget)
{
if (_currentTarget.sex != 1) // 1 is female, only choose male as target
if ((POVConfig.targetGender.Value == POVConfig.Gender.All) || (_currentTarget.sex == (int)POVConfig.targetGender.Value)) // 1 is female
{
if (_povMode == POV_MODE.EYE || _povMode == POV_MODE.HEAD || _povMode == POV_MODE.TELEPORT)
{
Expand Down Expand Up @@ -201,32 +209,49 @@ protected override void OnUpdate()
{
if (_settings.EnablePOV == false) return;
// Press Key (default: Y) to change POV Mode, configurable in BepInEX Plugin Settings (F1)
if (POVConfig.switchPOVModeKey.Value.IsDown())
if (POVConfig.switchPOVModeKey.Value == POVConfig.POVKeyList.VR_TRIGGER || POVConfig.switchPOVModeKey.Value == POVConfig.POVKeyList.VR_BUTTON2) // Use VR button
{
var button_id = (POVConfig.switchPOVModeKey.Value == POVConfig.POVKeyList.VR_TRIGGER) ? Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger : Valve.VR.EVRButtonId.k_EButton_A;
if (L_device.GetPressDown(button_id) || R_device.GetPressDown(button_id))
{
if (_povMode == POV_MODE.EYE) {resetRotationXZ();}
_povMode = (POV_MODE)(((int)(_povMode + 1)) % (int)(POV_MODE.TOTAL));
}
}
else if (Input.GetKeyDown((KeyCode)POVConfig.switchPOVModeKey.Value)) // Use Keyboard Key
{
if (_povMode == POV_MODE.EYE) {resetRotationXZ();}
_povMode = (POV_MODE)(((int)(_povMode + 1)) % (int)(POV_MODE.TOTAL));
}

// When left hand controller is Hand Tool and visible, Press VR Trigger button to set _active
if ((!_active) && VR.Mode.Left.ToolIndex == 2 && VR.Mode.Left.ActiveTool.isActiveAndEnabled)
if (!_active) // Activate POV under Hand Tool if user press VR Trigger button / Key
{
var device = SteamVR_Controller.Input((int)VR.Mode.Left.Tracking.index);
if (device.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))
if (POVConfig.POVKey.Value == POVConfig.POVKeyList.VR_TRIGGER || POVConfig.POVKey.Value == POVConfig.POVKeyList.VR_BUTTON2) // Use VR button
{
_active = true;
initPOVTarget();
return;
var button_id = (POVConfig.POVKey.Value == POVConfig.POVKeyList.VR_TRIGGER) ? Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger : Valve.VR.EVRButtonId.k_EButton_A;
// When left hand controller is Hand Tool and visible, Press VR button to set _active
if (VR.Mode.Left.ToolIndex == 2 && VR.Mode.Left.ActiveTool.isActiveAndEnabled && L_device.GetPressDown(button_id))
{
_active = true;
initPOVTarget();
return;
}
// When right hand controller is Hand Tool and visible, Press VR button to set _active
else if (VR.Mode.Right.ToolIndex == 2 && VR.Mode.Right.ActiveTool.isActiveAndEnabled && R_device.GetPressDown(button_id))
{
_active = true;
initPOVTarget();
return;
}
}
}
// When right hand controller is Hand Tool and visible, Press VR Trigger button to set _active
else if ((!_active) && VR.Mode.Right.ToolIndex == 2 && VR.Mode.Right.ActiveTool.isActiveAndEnabled)
{
var device = SteamVR_Controller.Input((int)VR.Mode.Right.Tracking.index);
if (device.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))
else // Use Keyboard Key
{
_active = true;
initPOVTarget();
return;
if ((VR.Mode.Left.ToolIndex == 2 || VR.Mode.Right.ToolIndex == 2) && Input.GetKeyDown((KeyCode)POVConfig.POVKey.Value))
{
_active = true;
initPOVTarget();
return;
}
}
}
// When there is no Hand Tool, deactive
Expand All @@ -240,16 +265,22 @@ protected override void OnUpdate()

if (_active)
{
// Press VR Trigger button to change target POV character
if (VR.Mode.Left.ToolIndex == 2 && VR.Mode.Left.ActiveTool.isActiveAndEnabled)
if (POVConfig.POVKey.Value == POVConfig.POVKeyList.VR_TRIGGER || POVConfig.POVKey.Value == POVConfig.POVKeyList.VR_BUTTON2) // Use VR button
{
var device = SteamVR_Controller.Input((int)VR.Mode.Left.Tracking.index);
if (device.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger)) {SwitchToNextChar();}
var button_id = (POVConfig.POVKey.Value == POVConfig.POVKeyList.VR_TRIGGER) ? Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger : Valve.VR.EVRButtonId.k_EButton_A;
// Press VR button to change target POV character
if (VR.Mode.Left.ToolIndex == 2 && VR.Mode.Left.ActiveTool.isActiveAndEnabled && L_device.GetPressDown(button_id))
{
SwitchToNextChar();
}
else if (VR.Mode.Right.ToolIndex == 2 && VR.Mode.Right.ActiveTool.isActiveAndEnabled && R_device.GetPressDown(button_id))
{
SwitchToNextChar();
}
}
else if (VR.Mode.Right.ToolIndex == 2 && VR.Mode.Right.ActiveTool.isActiveAndEnabled)
else // Use Keyboard Key
{
var device = SteamVR_Controller.Input((int)VR.Mode.Right.Tracking.index);
if (device.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger)) {SwitchToNextChar();}
if (Input.GetKeyDown((KeyCode)POVConfig.POVKey.Value)) {SwitchToNextChar();}
}
}

Expand Down
197 changes: 194 additions & 3 deletions KoikatuVR/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,210 @@ public KeySet CurrentKeySet()
public class POVConfig
{
private const string sectionPOV = "4. POV between characters (Hand Tool in free H)";
public static ConfigEntry<KeyboardShortcut> switchPOVModeKey { get; private set; }
public static ConfigEntry<POVKeyList> switchPOVModeKey { get; private set; }
public static ConfigEntry<POVKeyList> POVKey { get; private set; }
public static ConfigEntry<Gender> targetGender { get; private set; }

public enum POVKeyList
{
[Description("VR Trigger Button")] // k_EButton_SteamVR_Trigger
VR_TRIGGER = KeyCode.None,
[Description("VR Button2(A/X).Depend on device")] // k_EButton_A
VR_BUTTON2 = 1, // Any number, not in KeyCode
[Description("Left mouse(=VR Trigger Button)")]
LBUTTON = KeyCode.Mouse0,
[Description("Right mouse(=VR Right Touchpad Click)")]
RBUTTON = KeyCode.Mouse1,
[Description("Middle mouse(=VR Touchpad Click)")]
MBUTTON = KeyCode.Mouse2,
[Description("Keyboard A")]
A = KeyCode.A,
[Description("Keyboard B")]
B = KeyCode.B,
[Description("Keyboard C")]
C = KeyCode.C,
[Description("Keyboard D")]
D = KeyCode.D,
[Description("Keyboard E")]
E = KeyCode.E,
[Description("Keyboard F")]
F = KeyCode.F,
[Description("Keyboard G")]
G = KeyCode.G,
[Description("Keyboard H")]
H = KeyCode.H,
[Description("Keyboard I")]
I = KeyCode.I,
[Description("Keyboard J")]
J = KeyCode.J,
[Description("Keyboard K")]
K = KeyCode.K,
[Description("Keyboard L")]
L = KeyCode.L,
[Description("Keyboard M")]
M = KeyCode.M,
[Description("Keyboard N")]
N = KeyCode.N,
[Description("Keyboard O")]
O = KeyCode.O,
[Description("Keyboard P")]
P = KeyCode.P,
[Description("Keyboard Q")]
Q = KeyCode.Q,
[Description("Keyboard R")]
R = KeyCode.R,
[Description("Keyboard S")]
S = KeyCode.S,
[Description("Keyboard T")]
T = KeyCode.T,
[Description("Keyboard U")]
U = KeyCode.U,
[Description("Keyboard V")]
V = KeyCode.V,
[Description("Keyboard W")]
W = KeyCode.W,
[Description("Keyboard X")]
X = KeyCode.X,
[Description("Keyboard Y")]
Y = KeyCode.Y,
[Description("Keyboard Z")]
Z = KeyCode.Z,
[Description("Keyboard 0")]
ALPHA0 = KeyCode.Alpha0,
[Description("Keyboard 1")]
ALPHA1 = KeyCode.Alpha1,
[Description("Keyboard 2")]
ALPHA2 = KeyCode.Alpha2,
[Description("Keyboard 3")]
ALPHA3 = KeyCode.Alpha3,
[Description("Keyboard 4")]
ALPHA4 = KeyCode.Alpha4,
[Description("Keyboard 5")]
ALPHA5 = KeyCode.Alpha5,
[Description("Keyboard 6")]
ALPHA6 = KeyCode.Alpha6,
[Description("Keyboard 7")]
ALPHA7 = KeyCode.Alpha7,
[Description("Keyboard 8")]
ALPHA8 = KeyCode.Alpha8,
[Description("Keyboard 9")]
ALPHA9 = KeyCode.Alpha9,
[Description("Keyboard Numpad 0")]
NUMPAD0 = KeyCode.Keypad0,
[Description("Keyboard Numpad 1")]
NUMPAD1 = KeyCode.Keypad1,
[Description("Keyboard Numpad 2")]
NUMPAD2 = KeyCode.Keypad2,
[Description("Keyboard Numpad 3")]
NUMPAD3 = KeyCode.Keypad3,
[Description("Keyboard Numpad 4")]
NUMPAD4 = KeyCode.Keypad4,
[Description("Keyboard Numpad 5")]
NUMPAD5 = KeyCode.Keypad5,
[Description("Keyboard Numpad 6")]
NUMPAD6 = KeyCode.Keypad6,
[Description("Keyboard Numpad 7")]
NUMPAD7 = KeyCode.Keypad7,
[Description("Keyboard Numpad 8")]
NUMPAD8 = KeyCode.Keypad8,
[Description("Keyboard Numpad 9")]
NUMPAD9 = KeyCode.Keypad9,
[Description("Keyboard F1")]
F1 = KeyCode.F1,
[Description("Keyboard F2")]
F2 = KeyCode.F2,
[Description("Keyboard F3")]
F3 = KeyCode.F3,
[Description("Keyboard F4")]
F4 = KeyCode.F4,
[Description("Keyboard F5")]
F5 = KeyCode.F5,
[Description("Keyboard F6")]
F6 = KeyCode.F6,
[Description("Keyboard F7")]
F7 = KeyCode.F7,
[Description("Keyboard F8")]
F8 = KeyCode.F8,
[Description("Keyboard F9")]
F9 = KeyCode.F9,
[Description("Keyboard F10")]
F10 = KeyCode.F10,
[Description("Keyboard F11")]
F11 = KeyCode.F11,
[Description("Keyboard F12")]
F12 = KeyCode.F12,
[Description("Keyboard Tab")]
TAB = KeyCode.Tab,
[Description("Keyboard Enter")]
RETURN = KeyCode.Return,
[Description("Keyboard Esc")]
ESC = KeyCode.Escape,
[Description("Keyboard Space")]
SPACE = KeyCode.Space,
[Description("Keyboard Home")]
HOME = KeyCode.Home,
[Description("Keyboard End")]
END = KeyCode.End,
[Description("Keyboard arrow left")]
LEFT = KeyCode.LeftArrow,
[Description("Keyboard arrow up")]
UP = KeyCode.UpArrow,
[Description("Keyboard arrow right")]
RIGHT = KeyCode.RightArrow,
[Description("Keyboard arrow down")]
DOWN = KeyCode.DownArrow,
[Description("Keyboard Ins")]
INSERT = KeyCode.Insert,
[Description("Keyboard Del")]
DELETE = KeyCode.Delete,
[Description("Keyboard Page Up")]
PAGEUP = KeyCode.PageUp,
[Description("Keyboard Page Down")]
PAGEDOWN = KeyCode.PageDown,
[Description("Keyboard Backspace")]
BACK = KeyCode.Backspace,
[Description("Keyboard Left Shift")]
LEFTSHIFT = KeyCode.LeftShift,
[Description("Keyboard Right Shift")]
RIGHTSHIFT = KeyCode.RightShift,
[Description("Keyboard Left Ctrl")]
LEFTCTRL = KeyCode.LeftControl,
[Description("Keyboard Right Ctrl")]
RIGHTCTRL = KeyCode.RightControl,
[Description("Keyboard Left Alt")]
LEFTALT = KeyCode.LeftAlt,
[Description("Keyboard Right Alt")]
RIGHTALT = KeyCode.RightAlt
}
public enum Gender
{
Male = 0, // must same as Koikatsu Male ChaControl.sex = 0
Female = 1, // must same as Koikatsu Female ChaControl.sex = 1
All
}
public POVConfig(ConfigFile config, KoikatuSettings settings)
{
var enablePOV = config.Bind(sectionPOV, "POV", true,
"Switch POV between characters in free H scenes (only works in Hand Tool)");
Tie(enablePOV, v => settings.EnablePOV = v);

switchPOVModeKey = config.Bind(sectionPOV, "Switch POV Mode", new KeyboardShortcut(KeyCode.Y),
switchPOVModeKey = config.Bind(sectionPOV, "Switch POV Mode (1~3)", POVKeyList.Y,
new ConfigDescription(
"Use VR Trigger Button to switch POV between characters. Three Modes:\r\n1: Camera is fixed at character's eye.\r\n2: Camera moves when character's head moves.\r\n3: Camera doesn't move when character's head moves (jump to character).",
"Use VR Button/Key to switch POV between characters. Three Modes:\r\n1: Camera is fixed at character's eye.\r\n2: Camera moves when character's head moves. (Default)\r\n3: Camera doesn't move when character's head moves (jump to character).",
null,
new ConfigurationManagerAttributes { Order = -1 }));

POVKey = config.Bind(sectionPOV, "Switch POV Camera", POVKeyList.VR_TRIGGER,
new ConfigDescription(
"Use this setting to switch the POV camera between target characters.",
null,
new ConfigurationManagerAttributes { Order = -2 }));

targetGender = config.Bind(sectionPOV, "Target", Gender.Male,
new ConfigDescription(
"The Gender of the POV targets.",
null,
new ConfigurationManagerAttributes { Order = -3 }));
}
private static void Tie<T>(ConfigEntry<T> entry, Action<T> set)
{
Expand Down
4 changes: 3 additions & 1 deletion KoikatuVR/VRMale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ class ChaControlPatches
[HarmonyPostfix]
static void PostInitialize(ChaControl __instance)
{
if (__instance.sex == 0)
if ((__instance.sex == 0 && POVConfig.targetGender.Value == POVConfig.Gender.Male) ||
(__instance.sex == 1 && POVConfig.targetGender.Value == POVConfig.Gender.Female) ||
(POVConfig.targetGender.Value == POVConfig.Gender.All))
{
__instance.GetOrAddComponent<VRMale>();
}
Expand Down

0 comments on commit e550b06

Please sign in to comment.