Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 75 additions & 82 deletions DFUVR/SPC.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@

using DaggerfallWorkshop.Game;
using DaggerfallWorkshop.Game.Utility;
using HarmonyLib;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SpatialTracking;
using UnityEngine.UI;
using uWindowCapture;
Expand All @@ -28,6 +21,8 @@ public class SPC : MonoBehaviour
public int windowWidth;
public int windowHeight;

private bool isClicking = false;

private UwcWindowTexture wTexture;

[DllImport("user32.dll")]
Expand All @@ -39,10 +34,6 @@ public class SPC : MonoBehaviour
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int X, int Y);

void Start()
{

}
public void Initialize(LineRenderer line, GraphicRaycaster graphicRaycaster, TrackedPoseDriver tracked)
{
if (!Var.fStartMenu)
Expand All @@ -58,17 +49,16 @@ public void Initialize(LineRenderer line, GraphicRaycaster graphicRaycaster, Tra
//wTexture = GameObject.Find("VRUI").GetComponent<UwcWindowTexture>();
windowHeight = Var.windowHeight;
windowWidth = Var.windowWidth;

}
}

lineRenderer = line;
trackedPoseDriver = tracked;
this.graphicRaycaster = graphicRaycaster;
}

void Update()
{

//foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode)))
//{
// if (Input.GetKey(kcode))
Expand Down Expand Up @@ -99,8 +89,9 @@ void Update()
// windowHeight = wTexture.window.height;
// Plugin.LoggerInstance.LogInfo(windowWidth+", "+windowHeight);
//}

TriggerProvider.CheckPressedRight();
bool clicked = Input.GetKeyDown(Var.acceptButton) || TriggerProvider.pressedDone;

if (trackedPoseDriver != null && lineRenderer != null)
{
Vector3 controllerPosition = trackedPoseDriver.transform.localPosition;
Expand All @@ -111,11 +102,12 @@ void Update()

lineRenderer.SetPosition(0, lineStart);
lineRenderer.SetPosition(1, lineEnd);
if (Input.GetKeyDown(Var.acceptButton) || Var.rTriggerDone || Var.lTriggerDone)

if (!Var.fStartMenu)
{
if (!Var.fStartMenu)
if (!Var.isCalibrated)
{
if (!Var.isCalibrated)
if (clicked)
{
var result = UwcWindowTexture.RayCast(lineStart, trackedPoseDriver.transform.forward, raycastDistance, -1);
if (result.hit)
Expand All @@ -128,88 +120,77 @@ void Update()
mouse_event(MOUSEEVENTF_LEFTUP, (uint)desktopCoord.x, (uint)desktopCoord.y, 0, 0);
}
}
else
{
//RaycastHit hit;

//if (Physics.Raycast(lineStart, trackedPoseDriver.transform.forward, out hit, raycastDistance))
//{
// if (hit.collider.gameObject.name == "VRUI")
// {
// if (Input.GetKeyDown(Var.acceptButton))
// {
// Plugin.LoggerInstance.LogInfo("Raycast Hit: " + hit.point);
// SimulateMouseClick(hit.point);
// }
// }
// if (Input.GetKeyDown(Var.acceptButton))
// {
// if (hit.collider.gameObject.name != "VRUI")
// { Plugin.LoggerInstance.LogInfo("Hit " + hit.collider.gameObject.name); }
// }
//}
RaycastHit[] hits = Physics.RaycastAll(lineStart, trackedPoseDriver.transform.forward, raycastDistance);

foreach (var hit in hits)
{
Plugin.LoggerInstance.LogInfo("clicked");
if (hit.collider.gameObject.name == "VRUI")
{
Plugin.LoggerInstance.LogInfo("Raycast Hit: " + hit.point);
SimulateMouseClick(hit.point);
break;
}

if (hit.collider.gameObject.GetComponent<Button>() != null)
{
hit.collider.gameObject.GetComponent<Button>().onClick.Invoke();
}

}
}
}
else
{
//RaycastHit hit;

//if (Physics.Raycast(lineStart, trackedPoseDriver.transform.forward, out hit, raycastDistance))
//{
// if (hit.collider.gameObject.name == "VRUI")
// {
// if (Input.GetKeyDown(Var.acceptButton))
// {
// Plugin.LoggerInstance.LogInfo("Raycast Hit: " + hit.point);
// SimulateMouseClick(hit.point);
// }
// }
// if (Input.GetKeyDown(Var.acceptButton))
// {
// if (hit.collider.gameObject.name != "VRUI")
// { Plugin.LoggerInstance.LogInfo("Hit " + hit.collider.gameObject.name); }
// }
//}
RaycastHit[] hits = Physics.RaycastAll(lineStart, trackedPoseDriver.transform.forward, raycastDistance);

foreach (var hit in hits)
{
Plugin.LoggerInstance.LogInfo("clicked");
if (hit.collider.gameObject.GetComponent<Button>() != null)
if (hit.collider.gameObject.name == "VRUI")
{
hit.collider.gameObject.GetComponent<Button>().onClick.Invoke();
Plugin.LoggerInstance.LogInfo("Raycast Hit: " + hit.point);
SimulateMouseClick(hit.point);
break;
}
else if (hit.collider.gameObject.GetComponent<Dropdown>() != null)
{
Dropdown dropdown = hit.collider.gameObject.GetComponent<Dropdown>();

if (Input.GetKeyDown(Var.acceptButton) || Var.rTriggerDone || Var.lTriggerDone)
hit.collider.gameObject.GetComponent<Button>()?.onClick.Invoke();
}
}
}
else if (clicked)
{
RaycastHit[] hits = Physics.RaycastAll(lineStart, trackedPoseDriver.transform.forward, raycastDistance);

dropdown.value = (dropdown.value + 1) % dropdown.options.Count;
dropdown.RefreshShownValue();
}
foreach (var hit in hits)
{
Plugin.LoggerInstance.LogInfo("clicked");
if (hit.collider.gameObject.GetComponent<Button>() != null)
{
hit.collider.gameObject.GetComponent<Button>().onClick.Invoke();
}
else if (hit.collider.gameObject.GetComponent<Dropdown>() != null)
{
Dropdown dropdown = hit.collider.gameObject.GetComponent<Dropdown>();

dropdown.value = (dropdown.value + 1) % dropdown.options.Count;
dropdown.RefreshShownValue();
}
}
}
}


if (isClicking && !clicked)
{
Plugin.LoggerInstance.LogInfo("clicked false inside!");
isClicking = false;
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
}
//void LateUpdate()
//{
// string[] joystickNames = Input.GetJoystickNames();
// foreach (string name in joystickNames)
// {
// Plugin.LoggerInstance.LogInfo("Joystick: " + name);
// }
//}



//deprecated
private void SimulateMouseClick(Vector3 hitPoint)
{


GameObject vrui = GameObject.Find("VRUI");
Vector3 localHitPoint = vrui.transform.InverseTransformPoint(hitPoint);
Plugin.LoggerInstance.LogInfo($"Local Hit Point: {localHitPoint}");
Expand All @@ -224,17 +205,29 @@ private void SimulateMouseClick(Vector3 hitPoint)
//double normalizedY = (localHitPoint.y *0.9f + (vrui.transform.localScale.y * 0.5f)) / vrui.transform.localScale.y;
Plugin.LoggerInstance.LogInfo($"Normalized Hit Point: ({normalizedX}, {normalizedY})");


int screenX = (int)(normalizedX * windowWidth); //+ windowPosX;
int screenY = (int)((1 - normalizedY) * windowHeight);// + windowPosY;
Plugin.LoggerInstance.LogInfo($"Screen Coordinates: ({screenX}, {screenY})");


mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)screenX, (uint)screenY, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, (uint)screenX, (uint)screenY, 0, 0);
MouseClick((uint)screenX, (uint)screenY);
SetCursorPos((int)screenX, (int)screenY);
//Plugin.LoggerInstance.LogInfo($"Simulated mouse click at ({screenX}, {screenY})");
}

private bool MouseClick(uint x, uint y)
{
if (!isClicking && (Input.GetKeyDown(Var.acceptButton) || TriggerProvider.pressedDone))
{
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
isClicking = true;
}
else if (isClicking && (!Input.GetKeyDown(Var.acceptButton) && !TriggerProvider.pressedDone))
{
isClicking = false;
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
return isClicking;
}
}
}

Expand Down