|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | + |
| 7 | +namespace Silk.NET.Input; |
| 8 | + |
| 9 | +public partial class InputContext |
| 10 | + : IJoystickInputHandler, |
| 11 | + IGamepadInputHandler, |
| 12 | + IMouseInputHandler, |
| 13 | + IPointerInputHandler, |
| 14 | + IKeyboardInputHandler, |
| 15 | + IInputHandler<ConnectionEvent>, |
| 16 | + IList<IInputBackend>, |
| 17 | + IReadOnlyList<IInputBackend> |
| 18 | +{ |
| 19 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 20 | + void IInputHandler<ConnectionEvent>.Handle(ConnectionEvent e) => HandleDeviceConnectionChanged(e); |
| 21 | + |
| 22 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 23 | + void IInputHandler<PointerTargetChangedEvent>.Handle(PointerTargetChangedEvent @event) => |
| 24 | + _pointers?.HandleTargetChanged(@event); |
| 25 | + |
| 26 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 27 | + void IInputHandler<PointChangedEvent>.Handle(PointChangedEvent @event) => |
| 28 | + _pointers?.HandlePointChanged(@event); |
| 29 | + |
| 30 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 31 | + void IInputHandler<PointerGripChangedEvent>.Handle(PointerGripChangedEvent @event) => |
| 32 | + _pointers?.HandleGripChanged(@event); |
| 33 | + |
| 34 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 35 | + void IButtonInputHandler<JoystickButton>.HandleButtonChanged(ButtonChangedEvent<JoystickButton> @event) => |
| 36 | + _joysticks?.HandleButtonChanged(@event); |
| 37 | + |
| 38 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 39 | + void IJoystickInputHandler.HandleAxisMove(JoystickAxisMoveEvent @event) => |
| 40 | + _joysticks?.HandleAxisMove(@event); |
| 41 | + |
| 42 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 43 | + void IJoystickInputHandler.HandleHatMove(JoystickHatMoveEvent @event) => |
| 44 | + _joysticks?.HandleHatMove(@event); |
| 45 | + |
| 46 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 47 | + void IGamepadInputHandler.HandleThumbstickMove(GamepadThumbstickMoveEvent @event) => |
| 48 | + _gamepads?.HandleThumbstickMove(@event); |
| 49 | + |
| 50 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 51 | + void IGamepadInputHandler.HandleTriggerMove(GamepadTriggerMoveEvent @event) => |
| 52 | + _gamepads?.HandleTriggerMove(@event); |
| 53 | + |
| 54 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 55 | + void IButtonInputHandler<PointerButton>.HandleButtonChanged(ButtonChangedEvent<PointerButton> @event) => |
| 56 | + _pointers?.HandleButtonChanged(@event); |
| 57 | + |
| 58 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 59 | + void IMouseInputHandler.HandleScroll(MouseScrollEvent @event) => |
| 60 | + _pointers?.HandleScroll(@event); |
| 61 | + |
| 62 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 63 | + void IPointerInputHandler.HandleTargetChanged(PointerTargetChangedEvent @event) => |
| 64 | + _pointers?.HandleTargetChanged(@event); |
| 65 | + |
| 66 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 67 | + void IPointerInputHandler.HandlePointChanged(PointChangedEvent @event) => |
| 68 | + _pointers?.HandlePointChanged(@event); |
| 69 | + |
| 70 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 71 | + void IPointerInputHandler.HandleGripChanged(PointerGripChangedEvent @event) => |
| 72 | + _pointers?.HandleGripChanged(@event); |
| 73 | + |
| 74 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 75 | + void IButtonInputHandler<KeyName>.HandleButtonChanged(ButtonChangedEvent<KeyName> @event) => |
| 76 | + _keyboards?.HandleButtonChanged(@event); |
| 77 | + |
| 78 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 79 | + void IKeyboardInputHandler.HandleKeyChanged(KeyChangedEvent @event) => |
| 80 | + _keyboards?.HandleKeyChanged(@event); |
| 81 | + |
| 82 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 83 | + void IKeyboardInputHandler.HandleKeyChar(KeyCharEvent @event) => |
| 84 | + _keyboards?.HandleKeyChar(@event); |
| 85 | + |
| 86 | + void IInputHandler.HandleDeviceConnectionChanged(ConnectionEvent @event) |
| 87 | + { |
| 88 | + HandleDeviceConnectionChanged(@event); |
| 89 | + |
| 90 | + try |
| 91 | + { |
| 92 | + ConnectionChanged?.Invoke(@event); |
| 93 | + } |
| 94 | + catch (Exception e) |
| 95 | + { |
| 96 | + InputLog.Error(e.ToString()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + IEnumerator<IInputBackend> IEnumerable<IInputBackend>.GetEnumerator() => |
| 101 | + _backends.GetEnumerator(); |
| 102 | + |
| 103 | + IEnumerator IEnumerable.GetEnumerator() => _backends.GetEnumerator(); |
| 104 | + |
| 105 | + void ICollection<IInputBackend>.Add(IInputBackend item) |
| 106 | + { |
| 107 | + HandleBackendAddition(item); |
| 108 | + _backends.Add(item); |
| 109 | + } |
| 110 | + |
| 111 | + void ICollection<IInputBackend>.Clear() |
| 112 | + { |
| 113 | + foreach (var backend in Backends) |
| 114 | + { |
| 115 | + HandleBackendRemoval(backend); |
| 116 | + } |
| 117 | + |
| 118 | + _backends.Clear(); |
| 119 | + } |
| 120 | + |
| 121 | + bool ICollection<IInputBackend>.Contains(IInputBackend item) => _backends.Contains(item); |
| 122 | + |
| 123 | + void ICollection<IInputBackend>.CopyTo(IInputBackend[] array, int arrayIndex) => |
| 124 | + _backends.CopyTo(array, arrayIndex); |
| 125 | + |
| 126 | + bool ICollection<IInputBackend>.Remove(IInputBackend item) |
| 127 | + { |
| 128 | + HandleBackendRemoval(item); |
| 129 | + return _backends.Remove(item); |
| 130 | + } |
| 131 | + |
| 132 | + int ICollection<IInputBackend>.Count => _backends.Count; |
| 133 | + bool ICollection<IInputBackend>.IsReadOnly => false; |
| 134 | + int IList<IInputBackend>.IndexOf(IInputBackend item) => _backends.IndexOf(item); |
| 135 | + |
| 136 | + void IList<IInputBackend>.Insert(int index, IInputBackend item) |
| 137 | + { |
| 138 | + HandleBackendAddition(item); |
| 139 | + _backends.Insert(index, item); |
| 140 | + } |
| 141 | + |
| 142 | + void IList<IInputBackend>.RemoveAt(int index) |
| 143 | + { |
| 144 | + var backend = _backends[index]; |
| 145 | + HandleBackendRemoval(backend); |
| 146 | + _backends.RemoveAt(index); |
| 147 | + } |
| 148 | + |
| 149 | + IInputBackend IList<IInputBackend>.this[int index] |
| 150 | + { |
| 151 | + get => _backends[index]; |
| 152 | + set |
| 153 | + { |
| 154 | + ArgumentNullException.ThrowIfNull(value); |
| 155 | + |
| 156 | + var existing = _backends[index]; |
| 157 | + if (existing == value) |
| 158 | + { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + HandleBackendRemoval(existing); |
| 163 | + HandleBackendAddition(value); |
| 164 | + _backends[index] = value; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + int IReadOnlyCollection<IInputBackend>.Count => _backends.Count; |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// Returns the <see cref="IInputBackend"/> at the specified index. |
| 172 | + /// </summary> |
| 173 | + /// <param name="index"></param> |
| 174 | + public IInputBackend this[int index] => _backends[index]; |
| 175 | +} |
0 commit comments