From 8fb2ba6afa59375264d96f215402a7e7ecf45d72 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Thu, 6 Jun 2024 15:18:45 -0700 Subject: [PATCH] Fix TIC80 mouse inputs resolves #3938 --- .../Computers/TIC80/LibTIC80.cs | 4 +- .../Computers/TIC80/TIC80.cs | 70 +++++++++++-------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/BizHawk.Emulation.Cores/Computers/TIC80/LibTIC80.cs b/src/BizHawk.Emulation.Cores/Computers/TIC80/LibTIC80.cs index 8b93797a01..e39fc60044 100644 --- a/src/BizHawk.Emulation.Cores/Computers/TIC80/LibTIC80.cs +++ b/src/BizHawk.Emulation.Cores/Computers/TIC80/LibTIC80.cs @@ -128,8 +128,8 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 public TIC80Gamepad P3Gamepad; public TIC80Gamepad P4Gamepad; - public sbyte MouseX; - public sbyte MouseY; + public byte MouseX; + public byte MouseY; public ushort MouseButtons; public TIC80Keys Key1; diff --git a/src/BizHawk.Emulation.Cores/Computers/TIC80/TIC80.cs b/src/BizHawk.Emulation.Cores/Computers/TIC80/TIC80.cs index 8bd6843335..46b46d3d45 100644 --- a/src/BizHawk.Emulation.Cores/Computers/TIC80/TIC80.cs +++ b/src/BizHawk.Emulation.Cores/Computers/TIC80/TIC80.cs @@ -11,7 +11,7 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 { [PortedCore(CoreNames.TIC80, "nesbox", "v1.0.2164", "https://tic80.com/")] [ServiceNotApplicable(new[] { typeof(IDriveLight), })] - public partial class TIC80 : WaterboxCore + public sealed partial class TIC80 : WaterboxCore { private readonly LibTIC80 _core; @@ -51,7 +51,7 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 }); var rom = lp.Roms[0].FileData; - var inputsActive = new bool[6] + var inputsActive = new[] { _syncSettings.Gamepad1, _syncSettings.Gamepad2, @@ -72,7 +72,7 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 InputsActive = Array.AsReadOnly(inputsActive); PostInit(); - DeterministicEmulation = lp.DeterministicEmulationRequested || (!_syncSettings.UseRealTime); + DeterministicEmulation = lp.DeterministicEmulationRequested || !_syncSettings.UseRealTime; InitializeRtc(_syncSettings.InitialTime); } @@ -84,10 +84,10 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 { var enumValues = Enum.GetValues(typeof(LibTIC80.TIC80Keys)); var ret = new KeyValuePair[enumValues.Length - 1]; - for (int i = 0; i < ret.Length; i++) + for (var i = 0; i < ret.Length; i++) { var val = enumValues.GetValue(i + 1); - var name = Enum.GetName(typeof(LibTIC80.TIC80Keys), val).TrimStart('_').Replace('_', ' '); + var name = Enum.GetName(typeof(LibTIC80.TIC80Keys), val)!.TrimStart('_').Replace('_', ' '); ret[i] = new(name, (LibTIC80.TIC80Keys)val); } @@ -98,7 +98,7 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 { var ret = new ControllerDefinition("TIC-80 Controller"); - for (int i = 0; i < 4; i++) + for (var i = 0; i < 4; i++) { if (inputsActive[i]) { @@ -111,7 +111,7 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 if (inputsActive[4]) { - ret.AddXYPair("Mouse Position {0}", AxisPairOrientation.RightAndUp, (-128).RangeTo(127), 0); + ret.AddXYPair("Mouse Position {0}", AxisPairOrientation.RightAndUp, 0.RangeTo(255), 128); ret.BoolButtons.Add("Mouse Left Click"); ret.BoolButtons.Add("Mouse Middle Click"); ret.BoolButtons.Add("Mouse Right Click"); @@ -139,7 +139,7 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 { foreach (var k in Enum.GetValues(typeof(LibTIC80.TIC80Keys))) { - var name = Enum.GetName(typeof(LibTIC80.TIC80Keys), k).TrimStart('_').Replace('_', ' '); + var name = Enum.GetName(typeof(LibTIC80.TIC80Keys), k)!.TrimStart('_').Replace('_', ' '); if (name is "Unknown") continue; ret.BoolButtons.Add(name); ret.CategoryLabels[name] = "Keyboard"; @@ -154,7 +154,7 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 private static void GetGamepads(IController controller, ref LibTIC80.TIC80Inputs inputs) { var gamepads = new LibTIC80.TIC80Gamepad[4]; - for (int i = 0; i < 4; i++) + for (var i = 0; i < 4; i++) { gamepads[i] = 0; foreach (var b in Enum.GetValues(typeof(LibTIC80.TIC80Gamepad))) @@ -175,33 +175,40 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 private static ushort GetMouseButtons(IController controller) { ushort ret = 0; + if (controller.IsPressed("Mouse Left Click")) - { - ret |= 0x8000; - } - if (controller.IsPressed("Mouse Middle Click")) - { - ret |= 0x4000; - } - if (controller.IsPressed("Mouse Right Click")) - { - ret |= 0x2000; - } - var x = (ushort)((sbyte)controller.AxisValue("Mouse Scroll X") + 32); - ret |= (ushort)(x << 7); - var y = (ushort)((sbyte)controller.AxisValue("Mouse Scroll Y") + 32); - ret |= (ushort)(y << 1); - if (controller.IsPressed("Mouse Relative Toggle")) { ret |= 0x0001; } + + if (controller.IsPressed("Mouse Middle Click")) + { + ret |= 0x0002; + } + + if (controller.IsPressed("Mouse Right Click")) + { + ret |= 0x0004; + } + + var x = (ushort)((sbyte)controller.AxisValue("Mouse Scroll X") + 32); + ret |= (ushort)(x << 3); + + var y = (ushort)((sbyte)controller.AxisValue("Mouse Scroll Y") + 32); + ret |= (ushort)(y << (3 + 6)); + + if (controller.IsPressed("Mouse Relative Toggle")) + { + ret |= 0x8000; + } + return ret; } private static void GetKeys(IController controller, ref LibTIC80.TIC80Inputs inputs) { var keys = new LibTIC80.TIC80Keys[4]; - int i = 0; + var i = 0; foreach (var kvp in KeyMap) { if (controller.IsPressed(kvp.Key)) @@ -224,11 +231,18 @@ namespace BizHawk.Emulation.Cores.Computers.TIC80 { var inputs = new LibTIC80.TIC80Inputs { - MouseX = (sbyte)controller.AxisValue("Mouse Position X"), - MouseY = (sbyte)controller.AxisValue("Mouse Position Y"), + MouseX = (byte)controller.AxisValue("Mouse Position X"), + MouseY = (byte)controller.AxisValue("Mouse Position Y"), MouseButtons = GetMouseButtons(controller), }; + if (controller.IsPressed("Mouse Relative Toggle")) + { + // turn (0, 255) to (-128, 127) + inputs.MouseX -= 128; + inputs.MouseY -= 128; + } + GetGamepads(controller, ref inputs); GetKeys(controller, ref inputs); _core.SetInputs(ref inputs);