Skip to content

Commit

Permalink
Merge pull request CosmosOS#2867 from GoldenretriverYT/support-scroll…
Browse files Browse the repository at this point in the history
…ing-properly

Support scrolling properly
  • Loading branch information
quajak authored Jan 2, 2024
2 parents 2d1f688 + f12ca25 commit 1905e79
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
19 changes: 10 additions & 9 deletions source/Cosmos.HAL2/PS2Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum Command : byte

#region Properties

private bool HasScrollWheel => mouseID == 3 || mouseID == 4;
public bool HasScrollWheel => mouseID == 3 || mouseID == 4;

public byte PS2Port { get; }

Expand Down Expand Up @@ -132,13 +132,13 @@ public void HandleMouse(ref INTs.IRQContext context)
mouseCycle++;
}
}
else if (mouseCycle == 3)
{
mouseByte[3] = IOPort.Read8(Core.IOGroup.PS2Controller.Data);
mouseCycle++;
}

// TODO: move conditions to the if statement when stack corruption detection
// works better for complex conditions
var xTest1 = mouseCycle == 2 && !HasScrollWheel;
var xTest2 = mouseCycle == 3 && HasScrollWheel;

if (xTest1 || xTest2)
if ((mouseCycle == 2 && !HasScrollWheel) || (mouseCycle == 4 && HasScrollWheel))
{
int xDeltaX = 0;
int xDeltaY = 0;
Expand Down Expand Up @@ -166,7 +166,8 @@ public void HandleMouse(ref INTs.IRQContext context)

if (HasScrollWheel)
{
var xScrollWheelByte = mouseByte[3] & 0x0F;
var xScrollWheelByte = mouseByte[3];

xScrollWheel = (xScrollWheelByte & 0b1000) == 0 ? xScrollWheelByte : xScrollWheelByte | ~0x0F;

if (mouseID == 4)
Expand Down Expand Up @@ -231,4 +232,4 @@ private void SendCommand(Command aCommand, byte? aByte = null)

#endregion
}
}
}
23 changes: 23 additions & 0 deletions source/Cosmos.System2/MouseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static class MouseManager
private static int deltaX;
private static int deltaY;

private static int scrollDelta;

/// <summary>
/// The X location of the mouse.
/// </summary>
Expand Down Expand Up @@ -144,6 +146,18 @@ internal set
}
}

/// <summary>
/// The 'delta' for the mouse scroll wheel. Needs to be manually reset.
/// </summary>
public static int ScrollDelta {
get {
return scrollDelta;
}
internal set => scrollDelta = value;
}

public static bool ScrollWheelPresent => mouseList.Exists(x => (x is PS2Mouse xPs2 && xPs2.HasScrollWheel));

#endregion

#region Methods
Expand All @@ -167,12 +181,21 @@ public static void HandleMouse(int deltaX, int deltaY, int mouseState, int scrol
DeltaX = deltaX;
DeltaY = deltaY;

ScrollDelta += scrollWheel;

X = (uint)Math.Clamp(X + (MouseSensitivity * deltaX), 0, ScreenWidth - 1);
Y = (uint)Math.Clamp(Y + (MouseSensitivity * deltaY), 0, ScreenHeight - 1);
LastMouseState = MouseState;
MouseState = (MouseState)mouseState;
}

/// <summary>
/// Reset the scroll delta to 0.
/// </summary>
public static void ResetScrollDelta() {
ScrollDelta = 0;
}

/// <summary>
/// Add mouse to the mouse list.
/// </summary>
Expand Down

0 comments on commit 1905e79

Please sign in to comment.