Skip to content

Commit 6b0c711

Browse files
authored
possible fix for rare(ish) mouselook subpixel bug (#64)
1 parent 2d40491 commit 6b0c711

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

ACViewer/Extensions/MouseEx.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Runtime.InteropServices;
2+
using System.Windows;
3+
using System.Windows.Media;
4+
5+
namespace ACViewer.Extensions
6+
{
7+
public class MouseEx
8+
{
9+
public struct POINT
10+
{
11+
public int X;
12+
public int Y;
13+
}
14+
15+
[DllImport("user32.dll")]
16+
public static extern bool GetCursorPos(out POINT lpPoint);
17+
18+
[DllImport("user32.dll")]
19+
public static extern bool SetCursorPos(int X, int Y);
20+
21+
public static Point SetCursor(Visual visual, int x, int y)
22+
{
23+
var p = visual.PointToScreen(new Point(x, y));
24+
SetCursorPos((int)p.X, (int)p.Y);
25+
return p;
26+
}
27+
}
28+
}

ACViewer/Player.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ public static bool GetKeyState(KeyboardState keyboardState, List<Keys> keys)
147147

148148
private static readonly float MouseSpeedBase = 0.5f / 6.0f;
149149

150+
public System.Windows.Point LastSetPoint { get; set; }
151+
150152
public void Update(GameTime time)
151153
{
152154
if (!FullSim)
@@ -283,8 +285,10 @@ public void Update(GameTime time)
283285
{
284286
if (wasRightClick)
285287
{
286-
var diffX = mouseState.X - Camera.centerX;
287-
var diffY = mouseState.Y - Camera.centerY;
288+
MouseEx.GetCursorPos(out var cursorPos);
289+
290+
var diffX = cursorPos.X - (int)LastSetPoint.X;
291+
var diffY = cursorPos.Y - (int)LastSetPoint.Y;
288292

289293
if (ConfigManager.Config.Mouse.AltMethod)
290294
{
@@ -324,7 +328,7 @@ public void Update(GameTime time)
324328
}
325329

326330
if (!ConfigManager.Config.Mouse.AltMethod)
327-
Mouse.SetCursor(Camera.centerX, Camera.centerY);
331+
LastSetPoint = MouseEx.SetCursor(GameView.Instance, Camera.centerX, Camera.centerY);
328332
}
329333
else if (wasRightClick)
330334
{

ACViewer/Render/Camera.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using ACViewer.Config;
1313
using ACViewer.Enum;
14+
using ACViewer.Extensions;
1415
using ACViewer.Render;
1516
using ACViewer.View;
1617

@@ -273,6 +274,8 @@ public Matrix CreateProjection()
273274

274275
public bool Locked { get; set; }
275276

277+
public System.Windows.Point LastSetPoint { get; set; }
278+
276279
public void Update(GameTime gameTime)
277280
{
278281
if (Mouse == null || Locked) return;
@@ -314,8 +317,10 @@ public void Update(GameTime gameTime)
314317
{
315318
if (PrevMouseState.RightButton == ButtonState.Pressed)
316319
{
317-
var xDiff = mouseState.X - centerX;
318-
var yDiff = mouseState.Y - centerY;
320+
MouseEx.GetCursorPos(out var cursorPos);
321+
322+
var xDiff = cursorPos.X - (int)LastSetPoint.X;
323+
var yDiff = cursorPos.Y - (int)LastSetPoint.Y;
319324

320325
if (ConfigManager.Config.Mouse.AltMethod)
321326
{
@@ -345,7 +350,7 @@ public void Update(GameTime gameTime)
345350
}
346351
// there is a delay here, so Mouse.GetState() won't be immediately affected
347352
if (!ConfigManager.Config.Mouse.AltMethod)
348-
Mouse.SetCursor(centerX, centerY);
353+
LastSetPoint = MouseEx.SetCursor(GameView.Instance, centerX, centerY);
349354
}
350355
else if (PrevMouseState.RightButton == ButtonState.Pressed)
351356
{

0 commit comments

Comments
 (0)