Skip to content

Commit bbc9874

Browse files
committed
continued
1 parent 6fc626f commit bbc9874

File tree

13 files changed

+659
-366
lines changed

13 files changed

+659
-366
lines changed

sources/Input/Input/Implementations/SDL3/Devices/Joysticks/SdlGamepad.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ private void Remap(GamepadHandle gamepadHandle)
114114

115115
public override string Name => Joystick.Name;
116116

117+
public override void Initialize() => throw new NotImplementedException();
118+
117119
protected override void Release()
118120
{
119121
Joystick.RemoveDeviceMapping(this);

sources/Input/Input/Implementations/SDL3/Devices/Joysticks/SdlJoystick.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ internal static (float minus, float plus) SplitValue(float mappedValue)
171171
}
172172

173173

174+
public override void Initialize() => throw new NotImplementedException();
175+
174176
protected override void Release() => NativeBackend.CloseJoystick(JoystickHandle);
175177

176178
public override void FinalizeUpdate(SdlInputBackend.SilkEventQueues silkEvents)

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

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Numerics;
5+
using System.Runtime.CompilerServices;
6+
using Silk.NET.Maths;
57
using Silk.NET.SDL;
68

79
namespace Silk.NET.Input.SDL3.Devices.Pointers;
810

9-
internal class SdlMouse : SdlPointerDevice, IMouse, ISdlDevice<SdlMouse>
11+
internal sealed class SdlMouse : SdlPointerDevice, IMouse, ISdlDevice<SdlMouse>
1012
{
1113
public override PointerState State => _state;
1214
public ICursorConfiguration Cursor { get; }
@@ -20,7 +22,8 @@ private SdlMouse(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend, IPoi
2022
_state = new MouseState(Buttons, Points, Vector2.Zero);
2123
Cursor = cursor;
2224
float x = 0, y = 0;
23-
_ = NativeBackend.GetMouseState(x.AsRef(), y.AsRef());
25+
var mouseInputFlags = GetButtonMaskSdl(ref x, ref y);
26+
ApplyMouseButtonState(mouseInputFlags);
2427

2528
var window = NativeBackend.GetMouseFocus();
2629
uint windowId;
@@ -37,14 +40,28 @@ private SdlMouse(ulong sdlDeviceId, nint uniqueId, SdlInputBackend backend, IPoi
3740
}
3841
}
3942

40-
_mouseWindowId = windowId;
4143
var pressure = _state.Buttons[PointerButton.Primary].Pressure;
4244
SetTargetPoint(windowId, new Vector3(x, y, 0), pressure);
4345
// var point = _unboundedPointerTarget.GetPoint(this, 0);
4446
}
4547

46-
protected override uint GetButtonMaskSdl() => NativeBackend.GetMouseState(nullptr, nullptr);
48+
private void ApplyMouseButtonState(SdlMouseInputFlags mouseState)
49+
{
50+
foreach (var pointerButtonName in EnumInfo<PointerButton>.UniqueValues)
51+
{
52+
ref var button = ref GetButtonRef(pointerButtonName);
53+
var isDown = mouseState.Has(pointerButtonName);
54+
button = button with { IsDown = isDown, Pressure = isDown ? 1 : 0 };
55+
}
56+
}
57+
58+
public override void Initialize()
59+
{
4760

61+
}
62+
63+
private unsafe SdlMouseInputFlags GetButtonMaskSdl(ref float x, ref float y) =>
64+
(SdlMouseInputFlags)NativeBackend.GetMouseState((float*)Unsafe.AsPointer(ref x), (float*)Unsafe.AsPointer(ref y));
4865

4966
public static SdlMouse CreateDevice(ulong sdlDeviceId, SdlInputBackend backend)
5067
{
@@ -101,7 +118,7 @@ public bool TrySetPosition(Vector2 position)
101118
{
102119
if (NativeBackend.WarpMouseGlobal(position.X, position.Y))
103120
{
104-
SetTargetPoint(_mouseWindowId, new Vector3(position.X, position.Y, 0), 0);
121+
SetTargetPoint(null, new Vector3(position.X, position.Y, 0), 0);
105122
return true;
106123
}
107124

@@ -116,9 +133,12 @@ public void AddMotion(in MouseMotionEvent evtMotion)
116133
_accumulatedMotion += movementRelative;
117134
// todo - test against evtMotion state values
118135

119-
SetTargetPoint(mouseWindowId, _accumulatedMotion, 0);
136+
//SetTargetPoint(mouseWindowId, _accumulatedMotion, 0, 0);
137+
SetTargetPoint(mouseWindowId, new Vector3(evtMotion.X, evtMotion.Y, 0), 0);
120138
}
121139

140+
141+
122142
public void AddButtonEvent(in MouseButtonEvent evtButton)
123143
{
124144
var button = PointerButton.Primary + evtButton.Button;
@@ -128,12 +148,14 @@ public void AddButtonEvent(in MouseButtonEvent evtButton)
128148

129149
public void AddWheelEvent(in MouseWheelEvent evtWheel)
130150
{
131-
_mouseScroll[0] += evtWheel.X;
132-
_mouseScroll[1] += evtWheel.Y;
151+
ref var x = ref _mouseScroll.X;
152+
ref var y = ref _mouseScroll.Y;
153+
x += evtWheel.X;
154+
y += evtWheel.Y;
133155

134156
// todo - evt.Which?
135-
var hMagnitude = MathF.Abs(_mouseScroll[0]);
136-
var vMagnitude = MathF.Abs(_mouseScroll[1]);
157+
var hMagnitude = MathF.Abs(x);
158+
var vMagnitude = MathF.Abs(y);
137159

138160
if (hMagnitude >= 1)
139161
{
@@ -151,31 +173,6 @@ public void AddWheelEvent(in MouseWheelEvent evtWheel)
151173
}
152174

153175

154-
private void SetTargetPoint(uint windowId, in Vector3 pos, float pressure)
155-
{
156-
_ = Backend.TryGetPointerTargetForWindow(windowId, out var windowTarget);
157-
158-
if (TryGetPointIndexForTarget(windowTarget, out var index))
159-
{
160-
UpdatePoint(ToTargetPoint(pos, pressure, windowTarget), index);
161-
}
162-
else
163-
{
164-
AddPoint(ToTargetPoint(pos, pressure, windowTarget));
165-
}
166-
167-
#if DEBUG
168-
if (_mouseWindowId != windowId)
169-
{
170-
InputLog.Warn($"Mouse window changed from {_mouseWindowId} to {windowId}");
171-
}
172-
#endif
173-
174-
_mouseWindowId = windowId;
175-
}
176-
177-
178-
private uint _mouseWindowId;
179176
private Vector2 _mouseScroll;
180177
private Vector3 _accumulatedMotion;
181178
}

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

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
3-
4-
using System.Diagnostics.CodeAnalysis;
53
using System.Numerics;
64
using Silk.NET.SDL;
75

@@ -13,7 +11,7 @@ public SdlPen(SdlInputBackend backend, nint silkId, ulong sdlDeviceId, string na
1311
base(backend, silkId, sdlDeviceId, unbounded)
1412
{
1513
Name = name;
16-
State = new PointerState(_buttons, _points);
14+
State = new PointerState(Buttons, Points);
1715
}
1816

1917
public static SdlPen CreateDevice(ulong sdlDeviceId, SdlInputBackend backend)
@@ -48,36 +46,93 @@ public static SdlPen CreateDevice(ulong sdlDeviceId, SdlInputBackend backend)
4846
SdlPen Create() => new(backend, uniqueId, sdlDeviceId, name.ReadToString(), backend.UnboundedPointerTarget);
4947
}
5048

51-
protected override uint GetButtonMaskSdl()
49+
private void ApplyPenInputState(SdlPenInputFlags penState)
5250
{
53-
throw new NotImplementedException();
51+
foreach (var pointerButtonName in EnumInfo<PointerButton>.UniqueValues)
52+
{
53+
ref var button = ref GetButtonRef(pointerButtonName);
54+
var isDown = penState.Has(pointerButtonName);
55+
button = button with { IsDown = isDown, Pressure = isDown ? 1 : 0 };
56+
}
5457
}
5558

56-
private readonly List<Button<PointerButton>> _buttons = [];
57-
private readonly List<TargetPoint> _points = [];
59+
60+
61+
public override void Initialize()
62+
{
63+
64+
}
5865

5966
public override PointerState State { get; }
6067

6168
protected override bool OnePointOnly => true;
6269

63-
6470
public override string Name { get; }
6571
protected override void Release() => throw new NotImplementedException();
6672

73+
public void UpDownEvent(in PenTouchEvent evt)
74+
{
6775

68-
private void SetWindowTarget(IPointerTarget? target) => _windowTarget = target;
76+
}
6977

70-
private bool _isPenClose;
71-
private IPointerTarget? _windowTarget;
7278

73-
public void Event(IPointerTarget? target, in Vector2? position, SdlInputBackend.SdlPenInputFlags state,
74-
PenAxis? axis = null, [NotNullIfNotNull(nameof(axis))] float? axisValue = null)
79+
public void MotionEvent(in PenMotionEvent evt)
7580
{
81+
if (!Backend.TryGetPointerTargetForWindow(evt.WindowID, out var target))
82+
{
83+
return;
84+
}
85+
86+
UpdatePoint(ToTargetPoint(new Vector3(evt.X, evt.Y, 0), 0, target, 0), 0);
7687
}
7788

78-
public void SetProximity(IPointerTarget? target, bool inProximity)
89+
90+
public void ButtonEvent(in PenButtonEvent evt) => ApplyPenInputState((SdlPenInputFlags)evt.PenState);
91+
92+
public void AxisEvent(in PenAxisEvent evt)
7993
{
80-
_isPenClose = inProximity;
81-
SetWindowTarget(target);
94+
switch (evt.Axis)
95+
{
96+
case PenAxis.Pressure:
97+
{
98+
SetPointPressure(0, evt.Value);
99+
break;;
100+
}
101+
case PenAxis.Xtilt:
102+
{
103+
SetPointXTilt(0, evt.Value);
104+
break;
105+
}
106+
case PenAxis.Ytilt:
107+
{
108+
SetPointYTilt(0, evt.Value);
109+
break;
110+
}
111+
case PenAxis.Distance:
112+
{
113+
SetPointDistance(0, evt.Value);
114+
break;
115+
}
116+
case PenAxis.Rotation: // barrel rotation
117+
{
118+
SetPointTwist(0, evt.Value);
119+
break;
120+
}
121+
case PenAxis.Slider:
122+
{
123+
// additional "button" or additional "axis" or "pressure"?
124+
// SetPointSlider(0, evt.Value);
125+
break;
126+
}
127+
case PenAxis.TangentialPressure:
128+
{
129+
SetGripPressure(evt.Value);
130+
break;
131+
}
132+
default:
133+
{
134+
return;
135+
}
136+
}
82137
}
83138
}

0 commit comments

Comments
 (0)