Skip to content

Commit

Permalink
Fix TIC80 mouse inputs
Browse files Browse the repository at this point in the history
resolves #3938
  • Loading branch information
CasualPokePlayer committed Jun 6, 2024
1 parent a7c8156 commit 8fb2ba6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/BizHawk.Emulation.Cores/Computers/TIC80/LibTIC80.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public struct TIC80Inputs
public TIC80Gamepad P3Gamepad;
public TIC80Gamepad P4Gamepad;

public sbyte MouseX;
public sbyte MouseY;
public byte MouseX;
public byte MouseY;
public ushort MouseButtons;

public TIC80Keys Key1;
Expand Down
50 changes: 32 additions & 18 deletions src/BizHawk.Emulation.Cores/Computers/TIC80/TIC80.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -51,7 +51,7 @@ public TIC80(CoreLoadParameters<TIC80Settings, TIC80SyncSettings> lp)
});

var rom = lp.Roms[0].FileData;
var inputsActive = new bool[6]
var inputsActive = new[]
{
_syncSettings.Gamepad1,
_syncSettings.Gamepad2,
Expand All @@ -72,7 +72,7 @@ public TIC80(CoreLoadParameters<TIC80Settings, TIC80SyncSettings> lp)
InputsActive = Array.AsReadOnly(inputsActive);
PostInit();

DeterministicEmulation = lp.DeterministicEmulationRequested || (!_syncSettings.UseRealTime);
DeterministicEmulation = lp.DeterministicEmulationRequested || !_syncSettings.UseRealTime;
InitializeRtc(_syncSettings.InitialTime);
}

Expand All @@ -84,10 +84,10 @@ public TIC80(CoreLoadParameters<TIC80Settings, TIC80SyncSettings> lp)
{
var enumValues = Enum.GetValues(typeof(LibTIC80.TIC80Keys));
var ret = new KeyValuePair<string, LibTIC80.TIC80Keys>[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);
}

Expand All @@ -98,7 +98,7 @@ private static ControllerDefinition CreateControllerDefinition(bool[] inputsActi
{
var ret = new ControllerDefinition("TIC-80 Controller");

for (int i = 0; i < 4; i++)
for (var i = 0; i < 4; i++)
{
if (inputsActive[i])
{
Expand All @@ -111,7 +111,7 @@ private static ControllerDefinition CreateControllerDefinition(bool[] inputsActi

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");
Expand Down Expand Up @@ -139,7 +139,7 @@ private static ControllerDefinition CreateControllerDefinition(bool[] inputsActi
{
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";
Expand All @@ -154,7 +154,7 @@ private static ControllerDefinition CreateControllerDefinition(bool[] inputsActi
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)))
Expand All @@ -175,33 +175,40 @@ private static void GetGamepads(IController controller, ref LibTIC80.TIC80Inputs
private static ushort GetMouseButtons(IController controller)
{
ushort ret = 0;

if (controller.IsPressed("Mouse Left Click"))
{
ret |= 0x8000;
ret |= 0x0001;
}

if (controller.IsPressed("Mouse Middle Click"))
{
ret |= 0x4000;
ret |= 0x0002;
}

if (controller.IsPressed("Mouse Right Click"))
{
ret |= 0x2000;
ret |= 0x0004;
}

var x = (ushort)((sbyte)controller.AxisValue("Mouse Scroll X") + 32);
ret |= (ushort)(x << 7);
ret |= (ushort)(x << 3);

var y = (ushort)((sbyte)controller.AxisValue("Mouse Scroll Y") + 32);
ret |= (ushort)(y << 1);
ret |= (ushort)(y << (3 + 6));

if (controller.IsPressed("Mouse Relative Toggle"))
{
ret |= 0x0001;
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))
Expand All @@ -224,11 +231,18 @@ protected override LibWaterboxCore.FrameInfo FrameAdvancePrep(IController contro
{
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);
Expand Down

0 comments on commit 8fb2ba6

Please sign in to comment.