From 63c2ed54b3fad2dcb2bbf71a5f35f3a6e81c8d04 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 6 Mar 2023 12:28:43 -0500 Subject: [PATCH] trackball movement --- namedropper/Assets/Scenes/Game.unity | 84 +++++++-- .../Assets/Scenes/MouseInputManager.cs | 4 - namedropper/Assets/Scenes/TopicSelect.unity | 62 +++++++ namedropper/Assets/Scripts/Player.cs | 3 +- namedropper/Assets/TrackballInputManager.cs | 166 ++++++++++++++++++ .../Assets/TrackballInputManager.cs.meta | 11 ++ 6 files changed, 306 insertions(+), 24 deletions(-) create mode 100644 namedropper/Assets/TrackballInputManager.cs create mode 100644 namedropper/Assets/TrackballInputManager.cs.meta diff --git a/namedropper/Assets/Scenes/Game.unity b/namedropper/Assets/Scenes/Game.unity index 6ef2080..4b358c8 100644 --- a/namedropper/Assets/Scenes/Game.unity +++ b/namedropper/Assets/Scenes/Game.unity @@ -224,6 +224,17 @@ GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 76032778} m_PrefabAsset: {fileID: 0} +--- !u!114 &23812154 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 76032778} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 23812149} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &64627487 GameObject: m_ObjectHideFlags: 0 @@ -1037,6 +1048,17 @@ GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 2050475643} m_PrefabAsset: {fileID: 0} +--- !u!114 &707823890 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 2050475643} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 707823885} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &714773755 PrefabInstance: m_ObjectHideFlags: 0 @@ -3557,6 +3579,7 @@ GameObject: - component: {fileID: 1641253473} - component: {fileID: 1641253472} - component: {fileID: 1641253474} + - component: {fileID: 1641253475} m_Layer: 0 m_Name: Game m_TagString: Untagged @@ -3617,6 +3640,26 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d9f0573f6651ecd4b900ae5fee7f7cb6, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &1641253475 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1641253471} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 70de4eb8e48bea54b864b5d20448ee50, type: 3} + m_Name: + m_EditorClassIdentifier: + defaultMiceSensitivity: 1 + accelerationThreshold: 40 + accelerationMultiplier: 2 + _players: + - {fileID: 1791992025} + - {fileID: 23812154} + - {fileID: 707823890} + - {fileID: 1777639907} --- !u!1 &1666960087 GameObject: m_ObjectHideFlags: 0 @@ -3629,7 +3672,6 @@ GameObject: - component: {fileID: 1666960091} - component: {fileID: 1666960090} - component: {fileID: 1666960089} - - component: {fileID: 1666960092} m_Layer: 5 m_Name: Canvas m_TagString: Untagged @@ -3724,24 +3766,6 @@ Canvas: m_SortingLayerID: 0 m_SortingOrder: 0 m_TargetDisplay: 0 ---- !u!114 &1666960092 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1666960087} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: c1935701df9467a4697e65458c3ecd11, type: 3} - m_Name: - m_EditorClassIdentifier: - cursor: {fileID: 23812149} - colors: [] - defaultMiceSensitivity: 1 - accelerationThreshold: 40 - accelerationMultiplier: 2 - screenBorderPixels: 16 --- !u!1001 &1680381204 PrefabInstance: m_ObjectHideFlags: 0 @@ -4114,11 +4138,33 @@ GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 881809258} m_PrefabAsset: {fileID: 0} +--- !u!114 &1777639907 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 881809258} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1777639902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1791992020 stripped GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 9049015332794659158} m_PrefabAsset: {fileID: 0} +--- !u!114 &1791992025 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 9049015332794659158} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1791992020} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &1801753663 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/namedropper/Assets/Scenes/MouseInputManager.cs b/namedropper/Assets/Scenes/MouseInputManager.cs index 9b559e8..7d3a74a 100644 --- a/namedropper/Assets/Scenes/MouseInputManager.cs +++ b/namedropper/Assets/Scenes/MouseInputManager.cs @@ -11,8 +11,6 @@ using UnityEngine.UI; public class MouseInputManager : MonoBehaviour { - public static MouseInputManager instance; - [DllImport("LibRawInput")] private static extern bool init(); @@ -72,7 +70,6 @@ public class MouseInputManager : MonoBehaviour void Start() { - instance = this; bool res = init(); Debug.Log("Init() ==> " + res); Debug.Log(Marshal.SizeOf(typeof(RawInputEvent))); @@ -83,7 +80,6 @@ public class MouseInputManager : MonoBehaviour public void OnDestroy() { - instance = null; } int addCursor(int deviceId) diff --git a/namedropper/Assets/Scenes/TopicSelect.unity b/namedropper/Assets/Scenes/TopicSelect.unity index 00bd8b2..870283f 100644 --- a/namedropper/Assets/Scenes/TopicSelect.unity +++ b/namedropper/Assets/Scenes/TopicSelect.unity @@ -359,6 +359,17 @@ GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 261236785} m_PrefabAsset: {fileID: 0} +--- !u!114 &261236788 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 261236785} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 261236787} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &277092716 GameObject: m_ObjectHideFlags: 0 @@ -676,6 +687,17 @@ GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 1003872900} m_PrefabAsset: {fileID: 0} +--- !u!114 &1003872903 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 1003872900} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1003872902} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &1192266242 PrefabInstance: m_ObjectHideFlags: 0 @@ -871,6 +893,17 @@ GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 1192266242} m_PrefabAsset: {fileID: 0} +--- !u!114 &1192266245 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 1192266242} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1192266244} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1225785571 GameObject: m_ObjectHideFlags: 0 @@ -1211,6 +1244,17 @@ GameObject: m_CorrespondingSourceObject: {fileID: 9049015333349338381, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} m_PrefabInstance: {fileID: 1272407093} m_PrefabAsset: {fileID: 0} +--- !u!114 &1272407096 stripped +MonoBehaviour: + m_CorrespondingSourceObject: {fileID: 9049015333349338377, guid: 56b5ab1b6ee02794ab9664e282d1930a, type: 3} + m_PrefabInstance: {fileID: 1272407093} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1272407095} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 27913ac4da04ed24db893ba625314f05, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &1439945299 GameObject: m_ObjectHideFlags: 0 @@ -1321,6 +1365,7 @@ GameObject: - component: {fileID: 1641253473} - component: {fileID: 1641253472} - component: {fileID: 1641253474} + - component: {fileID: 1641253475} m_Layer: 0 m_Name: TopicSelect m_TagString: Untagged @@ -1375,6 +1420,23 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d9f0573f6651ecd4b900ae5fee7f7cb6, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &1641253475 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1641253471} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 70de4eb8e48bea54b864b5d20448ee50, type: 3} + m_Name: + m_EditorClassIdentifier: + _players: + - {fileID: 261236788} + - {fileID: 1272407096} + - {fileID: 1192266245} + - {fileID: 1003872903} --- !u!1 &1666960087 GameObject: m_ObjectHideFlags: 0 diff --git a/namedropper/Assets/Scripts/Player.cs b/namedropper/Assets/Scripts/Player.cs index 2e07ee3..d02590b 100644 --- a/namedropper/Assets/Scripts/Player.cs +++ b/namedropper/Assets/Scripts/Player.cs @@ -29,6 +29,7 @@ public class Player : MonoBehaviour Vector3 _originalScale; Vector3 _originalTextScale; string _categoryName; + public int? DeviceId = null; public enum Team { @@ -36,7 +37,7 @@ public class Player : MonoBehaviour BLUE } - Rigidbody2D _rigidbody; + public Rigidbody2D _rigidbody; private void Awake() { diff --git a/namedropper/Assets/TrackballInputManager.cs b/namedropper/Assets/TrackballInputManager.cs new file mode 100644 index 0000000..a8879e5 --- /dev/null +++ b/namedropper/Assets/TrackballInputManager.cs @@ -0,0 +1,166 @@ +using UnityEngine; +using System.Collections; +using System.Runtime.InteropServices; +using System; +using System.Collections.Generic; +using UnityEngine.UI; + + +public class TrackballInputManager : MonoBehaviour +{ + const float defaultMiceSensitivity = 1f; + const float upMultiplier = .5f; + const float accelerationThreshold = 40; + const float accelerationMultiplier = 2; + + [DllImport("LibRawInput")] + private static extern bool init(); + + [DllImport("LibRawInput")] + private static extern bool kill(); + + [DllImport("LibRawInput")] + private static extern IntPtr poll(); + + public const byte RE_DEVICE_CONNECT = 0; + public const byte RE_MOUSE = 2; + public const byte RE_DEVICE_DISCONNECT = 1; + + [StructLayout(LayoutKind.Sequential)] + public struct RawInputEvent + { + public int devHandle; + public int x, y, wheel; + public byte press; + public byte release; + public byte type; + } + + public class MousePointer + { + public GameObject obj; + public Vector2 position; + public int deviceID; + public int playerID; + public float sensitivity; + } + public static TrackballInputManager instance; + + [SerializeField] List _players; + // Start is called before the first frame update + void Start() + { + instance = this; + bool res = init(); + Debug.Log("Init() ==> " + res); + //Debug.Log(Marshal.SizeOf(typeof(RawInputEvent))); + //canvas = GetComponent(); + //canvasRect = GetComponent(); + Cursor.lockState = CursorLockMode.Locked; + Cursor.visible = false; + } + + public void OnDestroy() + { + instance = null; + } + + int lastEvents = 0; + bool isInit = true; + + void Update() + { + + + // Poll the events and properly update whatever we need + IntPtr data = poll(); + int numEvents = Marshal.ReadInt32(data); + if (numEvents > 0) lastEvents = numEvents; + for (int i = 0; i < numEvents; ++i) + { + var ev = new RawInputEvent(); + long offset = data.ToInt64() + sizeof(int) + i * Marshal.SizeOf(ev); + ev.devHandle = Marshal.ReadInt32(new IntPtr(offset + 0)); + ev.x = Marshal.ReadInt32(new IntPtr(offset + 4)); + ev.y = Marshal.ReadInt32(new IntPtr(offset + 8)); + ev.wheel = Marshal.ReadInt32(new IntPtr(offset + 12)); + ev.press = Marshal.ReadByte(new IntPtr(offset + 16)); + ev.release = Marshal.ReadByte(new IntPtr(offset + 17)); + ev.type = Marshal.ReadByte(new IntPtr(offset + 18)); + //Debug.Log(getEventName(ev.type) + ": H=" + ev.devHandle + "; (" + ev.x + ";" + ev.y + ") Down=" + ev.press + " Up=" + ev.release); + + if (ev.type == RE_DEVICE_CONNECT) AddPlayer(ev.devHandle); + else if (ev.type == RE_DEVICE_DISCONNECT) RemovePlayer(ev.devHandle); + else if (ev.type == RE_MOUSE) + { + + Player player = null; + if (_playersByDeviceId.TryGetValue(ev.devHandle, out player)) + { + float dx = ev.x * defaultMiceSensitivity; + float dy = ev.y * defaultMiceSensitivity; + if (Mathf.Abs(dx) > accelerationThreshold) dx *= accelerationMultiplier; + if (Mathf.Abs(dy) > accelerationThreshold) dy *= accelerationMultiplier; + + if (dy < 0) + dy *= upMultiplier; + + //use this method to move using position + //player.transform.position = new Vector2(player.transform.position.x + dx, player.transform.position.y - dy); + //use this method to move using velocity + player._rigidbody.velocity += new Vector2(dx, -dy); + } + else + { + Debug.Log("Unknown device found"); + AddPlayer(ev.devHandle); + } + } + } + Marshal.FreeCoTaskMem(data); + + } + + void RemovePlayer(int deviceId) + { + + Debug.Log("Removing DeviceID " + deviceId); + _playersByDeviceId[deviceId].DeviceId = null; + _playersByDeviceId.Remove(deviceId); + + } + Dictionary _playersByDeviceId = new Dictionary(); + int AddPlayer(int deviceId) + { + + if (!isInit) + { + Debug.LogError("Not initialized"); + return -1; + } + + Player player = null; + _playersByDeviceId.TryGetValue(deviceId, out player); + if (player != null) + { + Debug.LogError("This device already has a player"); + return -1; + } + + Debug.Log("Adding DeviceID " + deviceId); + + foreach (Player p in _players) + { + if (p.DeviceId == null) + { + p.DeviceId = deviceId; + _playersByDeviceId.Add(deviceId, p); + return deviceId; + } + } + + Debug.Log("no players left to add"); + return -1; + } + +} diff --git a/namedropper/Assets/TrackballInputManager.cs.meta b/namedropper/Assets/TrackballInputManager.cs.meta new file mode 100644 index 0000000..12c8849 --- /dev/null +++ b/namedropper/Assets/TrackballInputManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 70de4eb8e48bea54b864b5d20448ee50 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: