Skip to content

Commit 9abcba2

Browse files
committed
fix mouse scrolling
1 parent 564d0b1 commit 9abcba2

2 files changed

Lines changed: 29 additions & 20 deletions

File tree

sources/Input/Input/Implementations/SDL3/Devices/Pointers/SdlMouse.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,26 @@ public void AddButtonEvent(in MouseButtonEvent evtButton, long timestamp)
208208

209209
public void AddWheelEvent(in MouseWheelEvent evtWheel, long timestamp)
210210
{
211-
var wheelState = _state.WheelPosition = new Vector2(evtWheel.X, evtWheel.Y);
211+
var pWheelPosition = _state.WheelPosition;
212+
const float max = 100f;
213+
var delta = new Vector2(evtWheel.X, evtWheel.Y);
214+
if (delta.X != 0 && pWheelPosition.X is > max or < -max)
215+
{
216+
pWheelPosition.X = 0;
217+
}
218+
219+
if (delta.Y != 0 && pWheelPosition.Y is > max or < -max)
220+
{
221+
pWheelPosition.Y = 0;
222+
}
223+
212224
AddMouseScrollEvent(
213-
scrollWheelPosition: wheelState,
225+
scrollWheelPosition: _state.WheelPosition = pWheelPosition + delta,
226+
scrollWheelDelta: delta,
214227
windowId: evtWheel.WindowID,
215-
position: new Vector3(evtWheel.MouseX, evtWheel.MouseY, 0),
216-
isMouseRelative: evtWheel.WindowID != 0,
217228
sdlTimestamp: evtWheel.Timestamp,
229+
mousePos: new Vector3(evtWheel.X, evtWheel.Y, 0),
218230
timestamp: timestamp);
219231
}
232+
220233
}

sources/Input/Input/Implementations/SDL3/Devices/Pointers/SdlPointerDevice.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics;
54
using System.Diagnostics.CodeAnalysis;
65
using System.Numerics;
76
using System.Runtime.CompilerServices;
@@ -28,8 +27,8 @@ protected SdlPointerDevice(SdlInputBackend backend, nint silkId,
2827
}
2928
}
3029

31-
32-
protected void AddButtonEvent(PointerButton button, long timestamp, ulong sdlTimestamp, bool isDown, float? pressure = null)
30+
protected void AddButtonEvent(PointerButton button, long timestamp, ulong sdlTimestamp, bool isDown,
31+
float? pressure = null)
3332
{
3433
pressure ??= isDown ? 1.0f : 0.0f;
3534
var idx = EnumInfo<PointerButton>.ValueIndexOfUnnamed(button);
@@ -46,7 +45,8 @@ protected void AddButtonEvent(PointerButton button, long timestamp, ulong sdlTim
4645

4746
if (myButton != original)
4847
{
49-
ButtonEvents.Enqueue(new ButtonChangedEvent<PointerButton>(this,timestamp, myButton, original), sdlTimestamp);
48+
ButtonEvents.Enqueue(new ButtonChangedEvent<PointerButton>(this, timestamp, myButton, original),
49+
sdlTimestamp);
5050
}
5151
}
5252

@@ -79,6 +79,7 @@ private ref Button<PointerButton> GetButtonRef(PointerButton button)
7979
public void FinalizeUpdate()
8080
{
8181
RepopulateActiveTargets();
82+
8283
return;
8384

8485
void RepopulateActiveTargets()
@@ -163,7 +164,6 @@ private unsafe ref TargetPoint CreateOrUpdateTargetPoint(IPointerTarget? target,
163164
}
164165

165166

166-
167167
[MethodImpl(MethodImplOptions.AggressiveInlining)]
168168
private static Ray3D<float> ConstructRay(in Vector3D<float> origin, Vector3D<float>? direction = null) =>
169169
new(origin, direction ?? Vector3D<float>.UnitZ);
@@ -300,8 +300,7 @@ private TargetPoint ToTargetPoint(in Vector3 posOnTarget, float pressure, IPoint
300300
);
301301
}
302302

303-
protected void AddMouseScrollEvent(Vector2 scrollWheelPosition, uint? windowId, Vector3? position,
304-
bool isMouseRelative, ulong sdlTimestamp, long timestamp)
303+
protected void AddMouseScrollEvent(Vector2 scrollWheelPosition, Vector2 scrollWheelDelta, Vector3 mousePos, uint? windowId, ulong sdlTimestamp, long timestamp)
305304
{
306305
if (this is not IMouse mouse)
307306
{
@@ -311,23 +310,19 @@ protected void AddMouseScrollEvent(Vector2 scrollWheelPosition, uint? windowId,
311310
uint? touchId = null;
312311
GetPointIdentifiers(ref touchId, windowId ?? _previousWindowId, out var windowTarget);
313312

314-
ref var point = ref CreateOrUpdateTargetPoint(windowTarget, touchId.Value, null, null, null, out _);
315-
316-
var previousScroll = _previousScrollWheelPosition ?? Vector2.Zero;
313+
ref var point = ref CreateOrUpdateTargetPoint(windowTarget, touchId.Value, mousePos, null, null, out _);
317314

318315
ScrollEvents.Enqueue(new MouseScrollEvent(
319316
Mouse: mouse,
320317
Timestamp: timestamp,
321318
Point: point,
322319
WheelPosition: scrollWheelPosition,
323-
Delta: scrollWheelPosition - previousScroll), sdlTimestamp);
320+
Delta: scrollWheelDelta), sdlTimestamp);
324321

325-
_previousScrollWheelPosition = scrollWheelPosition;
326322
}
327323

328-
private Vector2? _previousScrollWheelPosition;
329-
330-
protected void UpdatePointRay(uint? touchId, float? xTilt, float? yTilt, float? zTwist, float? distance, ulong sdlTimestamp, long timestamp)
324+
protected void UpdatePointRay(uint? touchId, float? xTilt, float? yTilt, float? zTwist, float? distance,
325+
ulong sdlTimestamp, long timestamp)
331326
{
332327
if (xTilt == null && yTilt == null && zTwist == null && distance == null)
333328
{
@@ -358,7 +353,8 @@ protected void SetGripPressure(float pressure, ulong sdlTimestamp, long timestam
358353
{
359354
// todo - use only the given events to update the state? is that possible? keyboard character input would probably be a problem..
360355
State.GripPressure = pressure;
361-
GripEvents.Enqueue(new PointerGripChangedEvent(this, timestamp, pressure, pressure - State.GripPressure), sdlTimestamp);
356+
GripEvents.Enqueue(new PointerGripChangedEvent(this, timestamp, pressure, pressure - State.GripPressure),
357+
sdlTimestamp);
362358
}
363359

364360

0 commit comments

Comments
 (0)