Skip to content

Commit ab910fa

Browse files
committed
further pointer/target work and organization
1 parent ea3c5b9 commit ab910fa

28 files changed

Lines changed: 904 additions & 670 deletions

‎sources/Input/Input/DualReadOnlyList.cs‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ namespace Silk.NET.Input;
1212
/// <summary>
1313
/// Represents a list that has exactly two elements.
1414
/// </summary>
15-
/// <typeparam name="T">The element type.</typeparam>
1615

17-
public DualReadOnlyList(Func<T> left, Func<T> right)
16+
public DualReadOnlyList(Func<T> getLeft, Func<T> getRight)
1817
{
19-
_left = left;
20-
_right = right;
18+
_getLeft = getLeft;
19+
_getRight = getRight;
2120
}
2221

2322
/// <summary>
@@ -26,7 +25,7 @@ public DualReadOnlyList(Func<T> left, Func<T> right)
2625
public T Left
2726
{
2827
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29-
get => _left();
28+
get => _getLeft();
3029
}
3130

3231
/// <summary>
@@ -35,15 +34,15 @@ public T Left
3534
public T Right
3635
{
3736
[MethodImpl(MethodImplOptions.AggressiveInlining)]
38-
get => _right();
37+
get => _getRight();
3938
}
4039

4140

4241
/// <inheritdoc />
4342
public IEnumerator<T> GetEnumerator()
4443
{
45-
yield return _left();
46-
yield return _right();
44+
yield return _getLeft();
45+
yield return _getRight();
4746
}
4847

4948
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
@@ -55,12 +54,12 @@ public IEnumerator<T> GetEnumerator()
5554
public T this[int index] =>
5655
index switch
5756
{
58-
0 => _left(),
59-
1 => _right(),
57+
0 => _getLeft(),
58+
1 => _getRight(),
6059
_ => throw new IndexOutOfRangeException(),
6160
};
6261

6362

64-
private readonly Func<T> _left;
65-
private readonly Func<T> _right;
63+
private readonly Func<T> _getLeft;
64+
private readonly Func<T> _getRight;
6665
}

‎sources/Input/Input/GamepadState.cs‎

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,26 @@ namespace Silk.NET.Input;
77
/// </summary>
88
public class GamepadState
99
{
10-
/// <summary>
11-
/// The constructor for a new GamepadState object
12-
/// </summary>
13-
/// <param name="buttons">The list of buttons</param>
14-
/// <param name="axisStates">The list of states of the controllers axes that the triggers and joysticks will
15-
/// be read from via their specific indices in this array</param>
16-
/// <param name="joystickXAxisIndicesLR">The joystick X axes.</param>
17-
/// <param name="joystickYAxisIndicesDU">The Joystick Y axes. </param>
18-
/// <param name="joystickTriggerIndicesLR"></param>
19-
/// <remarks>
20-
/// For <see cref="joystickXAxisIndicesLR"/> and <see cref="joystickYAxisIndicesDU"/>, the must be either of length
21-
/// 2 or 4.
22-
///
23-
/// If two are provided, the first is assumed to be the left stick, and the second is assumed to be the right stick
24-
///
25-
/// if 4 are provided, it is assumed that the first two are - and + sides of the first axis, and so on.
26-
///<example>[leftX, rightX] OR [-leftX, +leftX, -rightX, +rightX]</example>
27-
/// </remarks>
28-
/// <exception cref="ArgumentException"></exception>
29-
/// <exception cref="ArgumentOutOfRangeException"></exception>
10+
/// <summary>
11+
/// The constructor for a new GamepadState object
12+
/// </summary>
13+
/// <param name="buttons">The list of buttons</param>
14+
/// <param name="axisStates">The list of states of the controllers axes that the triggers and joysticks will
15+
/// be read from via their specific indices in this array</param>
16+
/// <remarks>
17+
/// </remarks>
18+
/// <exception cref="ArgumentException"></exception>
19+
/// <exception cref="ArgumentOutOfRangeException"></exception>
3020
public GamepadState(IReadOnlyList<Button<JoystickButton>> buttons, IReadOnlyList<float> axisStates)
3121
{
3222
_axisStates = axisStates;
3323
Buttons = new ButtonReadOnlyList<JoystickButton>(buttons);
3424
Triggers = new DualReadOnlyList<float>(
35-
left: () => _axisStates[JoystickAxis.LeftTrigger.Index()],
36-
right: () =>_axisStates[JoystickAxis.RightTrigger.Index()]);
25+
getLeft: () => _axisStates[JoystickAxis.LeftTrigger.Index()],
26+
getRight: () =>_axisStates[JoystickAxis.RightTrigger.Index()]);
3727
Thumbsticks = new DualReadOnlyList<Vector2>(
38-
left: () => new Vector2(_axisStates[JoystickAxis.LeftX.Index()], _axisStates[JoystickAxis.LeftY.Index()]),
39-
right: () => new Vector2(_axisStates[JoystickAxis.RightX.Index()], _axisStates[JoystickAxis.RightY.Index()]));
28+
getLeft: () => new Vector2(_axisStates[JoystickAxis.LeftX.Index()], _axisStates[JoystickAxis.LeftY.Index()]),
29+
getRight: () => new Vector2(_axisStates[JoystickAxis.RightX.Index()], _axisStates[JoystickAxis.RightY.Index()]));
4030
}
4131

4232
/// <summary>
@@ -47,12 +37,12 @@ public GamepadState(IReadOnlyList<Button<JoystickButton>> buttons, IReadOnlyList
4737
/// <summary>
4838
/// Gets the state of the twin sticks on the gamepad.
4939
/// </summary>
50-
public DualReadOnlyList<Vector2> Thumbsticks { get; internal set; }
40+
public DualReadOnlyList<Vector2> Thumbsticks { get; }
5141

5242
/// <summary>
5343
/// Gets the state of the triggers on the gamepad.
5444
/// </summary>
55-
public DualReadOnlyList<float> Triggers { get; internal set; }
45+
public DualReadOnlyList<float> Triggers { get; }
5646

5747
// ReSharper disable PrivateFieldCanBeConvertedToLocalVariable <- keeps closures consistent
5848
private readonly IReadOnlyList<float> _axisStates;

‎sources/Input/Input/Handlers/Pointers.cs‎

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Diagnostics;
22
using System.Diagnostics.CodeAnalysis;
33
using System.Numerics;
4-
using System.Runtime.CompilerServices;
54
using System.Runtime.InteropServices;
65

76
namespace Silk.NET.Input;
@@ -103,14 +102,11 @@ internal void HandleButtonChanged(ButtonChangedEvent<PointerButton> @event)
103102
}
104103
}
105104

106-
void IMouseInputHandler.HandleScroll(MouseScrollEvent @event) => HandleScroll(@event);
105+
/// <inheritdoc/>
106+
public void HandleScroll(MouseScrollEvent @event) => MouseScroll?.Invoke(@event);
107107

108-
internal void HandleScroll(MouseScrollEvent @event) => MouseScroll?.Invoke(@event);
109-
110-
void IPointerInputHandler.HandleTargetChanged(PointerTargetChangedEvent @event) =>
111-
HandleTargetChanged(@event);
112-
113-
internal void HandleTargetChanged(PointerTargetChangedEvent @event)
108+
/// <inheritdoc/>
109+
public void HandleTargetChanged(PointerTargetChangedEvent @event)
114110
{
115111
TargetChanged?.Invoke(@event);
116112
if (_clicks is null || @event.IsAdded is not false)
@@ -136,10 +132,8 @@ internal void HandleTargetChanged(PointerTargetChangedEvent @event)
136132
}
137133
}
138134

139-
void IPointerInputHandler.HandlePointChanged(PointChangedEvent @event) =>
140-
HandlePointChanged(@event);
141-
142-
internal void HandlePointChanged(PointChangedEvent @event)
135+
/// <inheritdoc/>
136+
public void HandlePointChanged(PointChangedEvent @event)
143137
{
144138
try
145139
{
@@ -175,10 +169,8 @@ internal void HandlePointChanged(PointChangedEvent @event)
175169
}
176170
}
177171

178-
void IPointerInputHandler.HandleGripChanged(PointerGripChangedEvent @event) =>
179-
HandleGripChanged(@event);
180-
181-
internal void HandleGripChanged(PointerGripChangedEvent @event) => GripChanged?.Invoke(@event);
172+
/// <inheritdoc/>
173+
public void HandleGripChanged(PointerGripChangedEvent @event) => GripChanged?.Invoke(@event);
182174

183175
private record struct ClickData(
184176
IPointerDevice Device,
@@ -365,15 +357,6 @@ private void InvokeClick(PointerClickEvent evt)
365357
}
366358
}
367359

368-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
369-
void IInputHandler<PointerTargetChangedEvent>.Handle(PointerTargetChangedEvent @event) => HandleTargetChanged(@event);
370-
371-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
372-
void IInputHandler<PointChangedEvent>.Handle(PointChangedEvent @event) => HandlePointChanged(@event);
373-
374-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
375-
void IInputHandler<PointerGripChangedEvent>.Handle(PointerGripChangedEvent @event) => HandleGripChanged(@event);
376-
377360
/// <inheritdoc />
378361
protected internal override void HandleDeviceConnectionChanged(ConnectionEvent @event)
379362
{

‎sources/Input/Input/IPointerInputHandler.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.CompilerServices;
2+
13
namespace Silk.NET.Input;
24

35
/// <summary>
@@ -23,4 +25,14 @@ public interface IPointerInputHandler : IButtonInputHandler<PointerButton>, IInp
2325
/// </summary>
2426
/// <param name="event">The event details.</param>
2527
void HandleGripChanged(PointerGripChangedEvent @event);
28+
29+
30+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31+
void IInputHandler<PointerTargetChangedEvent>.Handle(PointerTargetChangedEvent @event) => HandleTargetChanged(@event);
32+
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
void IInputHandler<PointChangedEvent>.Handle(PointChangedEvent @event) => HandlePointChanged(@event);
35+
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37+
void IInputHandler<PointerGripChangedEvent>.Handle(PointerGripChangedEvent @event) => HandleGripChanged(@event);
2638
}

‎sources/Input/Input/IPointerTarget.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ public interface IPointerTarget
3838
/// </param>
3939
/// <returns>The point at the given index with which the given pointer device is pointing at the target.</returns>
4040
TargetPoint GetPoint(IPointerDevice pointer, int point);
41-
}
41+
}

‎sources/Input/Input/Implementations/SDL3/DataStructures/SdlArray.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace Silk.NET.Input.SDL3.DataStructures;
2727

2828
public static SdlArray<T> Null => default;
2929

30-
public SdlArray(Ptr<T> ptr, int count, ISdl? sdl, bool consumerCanDispose = true)
30+
public SdlArray(Ptr<T> ptr, int count, ISdl? sdl, bool consumerCanDispose)
3131
{
3232
if (consumerCanDispose)
3333
{

‎sources/Input/Input/Implementations/SDL3/DataStructures/SdlInputEventQueue.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void Enqueue(in T item, ulong sdlTimestamp)
4141
}
4242

4343

44-
/// <Summary>
44+
/// <summary>
4545
/// Returns a reference to our data as two spans and clears the inner count of our buffers
4646
/// (the functional equivalent of calling <see cref="List{T}.Clear()"/>)
4747
/// </summary>

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Numerics;
88
using System.Runtime.CompilerServices;
99
using Silk.NET.Input.SDL3.DataStructures;
10-
using Silk.NET.Input.SDL3.Devices.Pointers;
10+
using Silk.NET.Input.SDL3.Extensions;
1111
using Silk.NET.Maths;
1212
using Silk.NET.SDL;
1313

@@ -640,10 +640,6 @@ typedef struct SDL_GamepadTouchpadEvent
640640
return;
641641
}
642642

643-
Debug.Assert(surface.IsSimulated);
644-
Debug.Assert(surface.Targets.Count == 1);
645-
Debug.Assert(surface.Targets[0] == target);
646-
647643
target.SetBounds(bounds);
648644
var fingerId = evt.Finger;
649645

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Numerics;
55
using System.Runtime.CompilerServices;
66
using Silk.NET.Input.SDL3.DataStructures;
7+
using Silk.NET.Input.SDL3.Extensions;
78
using Silk.NET.SDL;
89

910
namespace Silk.NET.Input.SDL3.Devices.Joysticks;

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Numerics;
55
using System.Runtime.CompilerServices;
6+
using Silk.NET.Input.SDL3.Extensions;
67
using Silk.NET.SDL;
78

89
namespace Silk.NET.Input.SDL3.Devices.Pointers;
@@ -32,24 +33,10 @@ protected internal override void Initialize(long timestamp, ulong sdlTimestamp)
3233
ApplyMouseButtonState(mouseInputFlags, sdlTimestamp, timestamp);
3334

3435
var window = NativeBackend.GetMouseFocus();
35-
uint windowId;
36-
if (window == nullptr)
36+
if (Backend.TryGetOrCreatePointerTargetForWindow(window, out var target))
3737
{
38-
windowId = 0;
38+
AddOrUpdatePoint(null, target, new Vector3(x, y, 0), null, DownState, null, true, sdlTimestamp, timestamp);
3939
}
40-
else
41-
{
42-
windowId = NativeBackend.GetWindowID(window);
43-
if (windowId == 0)
44-
{
45-
SdlLog.Error("Mouse has no window");
46-
}
47-
}
48-
49-
50-
// var pressure = _state.Buttons[PointerButton.Primary].Pressure;
51-
Backend.TryGetPointerTargetForWindow(windowId, out var target);
52-
AddOrUpdatePoint(null, target, new Vector3(x, y, 0), null, DownState, null, true, sdlTimestamp, timestamp);
5340
// var point = _unboundedPointerTarget.GetPoint(this, 0);
5441
}
5542

@@ -184,13 +171,8 @@ public bool TrySetPosition(Vector2 position)
184171
/// </summary>
185172
public bool NeedsPump { get; private set; }
186173

187-
public void AddMotion(in MouseMotionEvent evtMotion, long timestamp)
174+
public void AddMotion(in MouseMotionEvent evtMotion, IPointerTarget target, long timestamp)
188175
{
189-
if (!Backend.TryGetPointerTargetForWindow(evtMotion.WindowID, out var target))
190-
{
191-
throw new InvalidOperationException("Failed to get pointer target for window");
192-
}
193-
194176
AddOrUpdatePoint(null, target, new Vector3(evtMotion.X, evtMotion.Y, 0), 1, null, null,
195177
evtMotion.WindowID != 0, evtMotion.Timestamp, timestamp);
196178
}
@@ -211,7 +193,7 @@ public void AddButtonEvent(in MouseButtonEvent evtButton, long timestamp)
211193
AddButtonEvent(button, timestamp, evtButton.Timestamp, evtButton.Down > 0, evtButton.Down * mult);
212194
}
213195

214-
public void AddWheelEvent(in MouseWheelEvent evtWheel, long timestamp)
196+
public void AddWheelEvent(in MouseWheelEvent evtWheel, IPointerTarget target, long timestamp)
215197
{
216198
var pWheelPosition = _state.WheelPosition;
217199
const float max = 100f;
@@ -230,7 +212,7 @@ public void AddWheelEvent(in MouseWheelEvent evtWheel, long timestamp)
230212
AddMouseScrollEvent(
231213
scrollWheelPosition: _state.WheelPosition = pWheelPosition + delta,
232214
scrollWheelDelta: delta,
233-
windowId: evtWheel.WindowID,
215+
target: target,
234216
sdlTimestamp: evtWheel.Timestamp,
235217
mousePos: new Vector3(evtWheel.X, evtWheel.Y, 0),
236218
timestamp: timestamp);

0 commit comments

Comments
 (0)