Input - Polished up UI

Input - Added events to service
This commit is contained in:
SpoinkyNL committed 2020-11-23 19:41:48 +01:00
1 parent 6ee5cbd09c
commit f1f0abfec5
40 files changed
+552 -203

No files matched your search

@@ -23,8 +23,10 @@ namespace Artemis.Core
RgbDevice = rgbDevice;
DeviceProvider = deviceProvider;
Surface = surface;
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
DeviceEntity = new DeviceEntity();
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
_leds = rgbDevice.Select(l => new ArtemisLed(l, this)).ToList().AsReadOnly();
Rotation = 0;
@@ -40,8 +42,12 @@ namespace Artemis.Core
RgbDevice = rgbDevice;
DeviceProvider = deviceProvider;
Surface = surface;
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
DeviceEntity = deviceEntity;
InputIdentifiers = new List<ArtemisDeviceInputIdentifier>();
foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifiers)
InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(identifierEntity.InputProvider, identifierEntity.Identifier));
_leds = rgbDevice.Select(l => new ArtemisLed(l, this)).ToList().AsReadOnly();
}
@@ -183,10 +189,10 @@ namespace Artemis.Core
// Other properties are computed
DeviceEntity.DeviceIdentifier = RgbDevice.GetDeviceIdentifier();
DeviceEntity.InputIdentifier.Clear();
DeviceEntity.InputIdentifiers.Clear();
foreach (ArtemisDeviceInputIdentifier identifier in InputIdentifiers)
{
DeviceEntity.InputIdentifier.Add(new DeviceInputIdentifierEntity
DeviceEntity.InputIdentifiers.Add(new DeviceInputIdentifierEntity
{
InputProvider = identifier.InputProvider,
Identifier = identifier.Identifier
@@ -205,7 +211,7 @@ namespace Artemis.Core
RgbDevice.Location = new Point(DeviceEntity.X, DeviceEntity.Y);
InputIdentifiers.Clear();
foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifier)
foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifiers)
InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(identifierEntity.InputProvider, identifierEntity.Identifier));
CalculateRenderProperties();
@@ -3,7 +3,7 @@
/// <summary>
/// Represents a device that provides input to the <see cref="IInputService" />
/// </summary>
public enum InputFallbackDeviceType
public enum InputDeviceType
{
/// <summary>
/// None
@@ -6,7 +6,7 @@ namespace Artemis.Core.Services
/// Specifies the set of modifier keys.
/// </summary>
[Flags]
public enum KeyboardModifierKeys
public enum KeyboardModifierKey
{
/// <summary>No modifiers are pressed.</summary>
None = 0,
@@ -0,0 +1,31 @@
using System;
namespace Artemis.Core.Services
{
/// <summary>
/// Contains data for input provider identifier events
/// </summary>
public class InputProviderIdentifierEventArgs : EventArgs
{
/// <summary>
/// Creates a new instance of the <see cref="InputProviderIdentifierEventArgs" /> class
/// </summary>
/// <param name="identifier">A value that can be used to identify this device</param>
/// <param name="deviceType">The type of device this identifier belongs to</param>
public InputProviderIdentifierEventArgs(object identifier, InputDeviceType deviceType)
{
Identifier = identifier;
DeviceType = deviceType;
}
/// <summary>
/// Gets a value that can be used to identify this device
/// </summary>
public object Identifier { get; }
/// <summary>
/// Gets the type of device this identifier belongs to
/// </summary>
public InputDeviceType DeviceType { get; }
}
}
@@ -29,22 +29,22 @@ namespace Artemis.Core.Services
public ArtemisDevice? Device { get; }
/// <summary>
/// Gets the X position of the mouse cursor (not necessarily tied to the specific device)
/// Gets the X position of the mouse cursor in pixels (not necessarily tied to the specific device)
/// </summary>
public int CursorX { get; }
/// <summary>
/// Gets the Y position of the mouse cursor (not necessarily tied to the specific device)
/// Gets the Y position of the mouse cursor in pixels (not necessarily tied to the specific device)
/// </summary>
public int CursorY { get; }
/// <summary>
/// Gets the movement delta in the horizontal direction
/// Gets the movement delta in the horizontal direction in pixels
/// </summary>
public int DeltaX { get; }
/// <summary>
/// Gets the movement delta in the vertical direction
/// Gets the movement delta in the vertical direction in pixels
/// </summary>
public int DeltaY { get; }
}
@@ -5,9 +5,9 @@ namespace Artemis.Core.Services
/// <summary>
/// Contains data for keyboard input events
/// </summary>
public class KeyboardEventArgs : EventArgs
public class KeyboardKeyEventArgs : EventArgs
{
internal KeyboardEventArgs(ArtemisDevice? device, ArtemisLed? led, KeyboardKey key, KeyboardModifierKeys modifiers)
internal KeyboardKeyEventArgs(ArtemisDevice? device, ArtemisLed? led, KeyboardKey key, KeyboardModifierKey modifiers)
{
Device = device;
Led = led;
@@ -33,22 +33,6 @@ namespace Artemis.Core.Services
/// <summary>
/// Gets the modifiers that are pressed
/// </summary>
public KeyboardModifierKeys Modifiers { get; }
}
/// <summary>
/// Contains data for keyboard input events
/// </summary>
public class KeyboardKeyUpDownEventArgs : KeyboardEventArgs
{
internal KeyboardKeyUpDownEventArgs(ArtemisDevice? device, ArtemisLed? led, KeyboardKey key, KeyboardModifierKeys modifiers, bool isDown) : base(device, led, key, modifiers)
{
IsDown = isDown;
}
/// <summary>
/// Whether the key is being pressed down, if <see langword="false"/> the key is being released
/// </summary>
public bool IsDown { get; set; }
public KeyboardModifierKey Modifiers { get; }
}
}
@@ -0,0 +1,18 @@
namespace Artemis.Core.Services
{
/// <summary>
/// Contains data for keyboard input events
/// </summary>
public class KeyboardKeyUpDownEventArgs : KeyboardKeyEventArgs
{
internal KeyboardKeyUpDownEventArgs(ArtemisDevice? device, ArtemisLed? led, KeyboardKey key, KeyboardModifierKey modifiers, bool isDown) : base(device, led, key, modifiers)
{
IsDown = isDown;
}
/// <summary>
/// Whether the key is being pressed down, if <see langword="false"/> the key is being released
/// </summary>
public bool IsDown { get; set; }
}
}
@@ -0,0 +1,32 @@
using System;
namespace Artemis.Core.Services
{
/// <summary>
/// Contains data for mouse input events
/// </summary>
public class MouseButtonEventArgs : EventArgs
{
internal MouseButtonEventArgs(ArtemisDevice? device, ArtemisLed? led, MouseButton button)
{
Device = device;
Led = led;
Button = button;
}
/// <summary>
/// Gets the device that triggered the event
/// </summary>
public ArtemisDevice? Device { get; }
/// <summary>
/// Gets the LED that corresponds to the pressed key
/// </summary>
public ArtemisLed? Led { get; }
/// <summary>
/// Gets the button that triggered the event
/// </summary>
public MouseButton Button { get; }
}
}
@@ -0,0 +1,18 @@
namespace Artemis.Core.Services
{
/// <summary>
/// Contains data for mouse input events
/// </summary>
public class MouseButtonUpDownEventArgs : MouseButtonEventArgs
{
internal MouseButtonUpDownEventArgs(ArtemisDevice? device, ArtemisLed? led, MouseButton button, bool isDown) : base(device, led, button)
{
IsDown = isDown;
}
/// <summary>
/// Whether the button is being pressed down, if <see langword="false"/> the button is being released
/// </summary>
public bool IsDown { get; set; }
}
}
@@ -0,0 +1,51 @@
using System;
namespace Artemis.Core.Services
{
/// <summary>
/// Contains data for mouse movement events
/// </summary>
public class MouseMoveEventArgs : EventArgs
{
/// <summary>
/// </summary>
/// <param name="device">The device that triggered the event</param>
/// <param name="cursorX">The X position of the mouse cursor (not necessarily tied to the specific device)</param>
/// <param name="cursorY">The Y position of the mouse cursor (not necessarily tied to the specific device)</param>
/// <param name="deltaX">The movement delta in the horizontal direction</param>
/// <param name="deltaY">The movement delta in the vertical direction</param>
internal MouseMoveEventArgs(ArtemisDevice? device, int cursorX, int cursorY, int deltaX, int deltaY)
{
Device = device;
CursorX = cursorX;
CursorY = cursorY;
DeltaX = deltaX;
DeltaY = deltaY;
}
/// <summary>
/// Gets the device that triggered the event
/// </summary>
public ArtemisDevice? Device { get; }
/// <summary>
/// Gets the X position of the mouse cursor (not necessarily tied to the specific device)
/// </summary>
public int CursorX { get; }
/// <summary>
/// Gets the Y position of the mouse cursor (not necessarily tied to the specific device)
/// </summary>
public int CursorY { get; }
/// <summary>
/// Gets the movement delta in the horizontal direction
/// </summary>
public int DeltaX { get; }
/// <summary>
/// Gets the movement delta in the vertical direction
/// </summary>
public int DeltaY { get; }
}
}
@@ -0,0 +1,57 @@
using System;
namespace Artemis.Core.Services
{
/// <summary>
/// Contains data for mouse scroll events
/// </summary>
public class MouseScrollEventArgs : EventArgs
{
/// <summary>
/// </summary>
/// <param name="device">The device that triggered the event</param>
/// <param name="direction">The direction in which was scrolled</param>
/// <param name="delta">The scroll delta (can positive or negative)</param>
internal MouseScrollEventArgs(ArtemisDevice? device, MouseScrollDirection direction, int delta)
{
Device = device;
Direction = direction;
Delta = delta;
}
/// <summary>
/// Gets the device that triggered the event
/// </summary>
public ArtemisDevice? Device { get; }
/// <summary>
/// Gets the direction in which was scrolled
/// </summary>
public MouseScrollDirection Direction { get; }
/// <summary>
/// Gets the scroll delta (can positive or negative)
/// </summary>
public int Delta { get; }
/// <summary>
/// Gets a boolean indicating whether the mouse scrolled up
/// </summary>
public bool IsScrollingUp => Direction == MouseScrollDirection.Vertical && Delta > 0;
/// <summary>
/// Gets a boolean indicating whether the mouse scrolled down
/// </summary>
public bool IsScrollingDown => Direction == MouseScrollDirection.Vertical && Delta < 0;
/// <summary>
/// Gets a boolean indicating whether the mouse scrolled right
/// </summary>
public bool IsScrollingRight => Direction == MouseScrollDirection.Horizontal && Delta > 0;
/// <summary>
/// Gets a boolean indicating whether the mouse scrolled left
/// </summary>
public bool IsScrollingLeft => Direction == MouseScrollDirection.Horizontal && Delta < 0;
}
}
@@ -31,51 +31,68 @@ namespace Artemis.Core.Services
/// <summary>
/// Occurs when the input provided received a device identifier
/// </summary>
public event EventHandler<object>? IdentifierReceived;
public event EventHandler<InputProviderIdentifierEventArgs>? IdentifierReceived;
/// <summary>
/// Invokes the <see cref="KeyboardDataReceived" /> event which the <see cref="IInputService" /> listens to as long as
/// this provider is registered
/// </summary>
protected virtual void OnKeyboardDataReceived(InputProviderKeyboardEventArgs e)
/// <param name="device">The device that triggered the event</param>
/// <param name="key">The key that triggered the event</param>
/// <param name="isDown">Whether the key is pressed down</param>
protected virtual void OnKeyboardDataReceived(ArtemisDevice? device, KeyboardKey key, bool isDown)
{
KeyboardDataReceived?.Invoke(this, e);
KeyboardDataReceived?.Invoke(this, new InputProviderKeyboardEventArgs(device, key, isDown));
}
/// <summary>
/// Invokes the <see cref="MouseButtonDataReceived" /> event which the <see cref="IInputService" /> listens to as long
/// as this provider is registered
/// </summary>
protected virtual void OnMouseButtonDataReceived(InputProviderMouseButtonEventArgs e)
/// <param name="device">The device that triggered the event</param>
/// <param name="button">The button that triggered the event</param>
/// <param name="isDown">Whether the button is pressed down</param>
protected virtual void OnMouseButtonDataReceived(ArtemisDevice? device, MouseButton button, bool isDown)
{
MouseButtonDataReceived?.Invoke(this, e);
MouseButtonDataReceived?.Invoke(this, new InputProviderMouseButtonEventArgs(device, button, isDown));
}
/// <summary>
/// Invokes the <see cref="MouseScrollDataReceived" /> event which the <see cref="IInputService" /> listens to as long
/// as this provider is registered
/// </summary>
protected virtual void OnMouseScrollDataReceived(InputProviderMouseScrollEventArgs e)
/// <param name="device">The device that triggered the event</param>
/// <param name="direction">The direction in which was scrolled</param>
/// <param name="delta">The scroll delta (can positive or negative)</param>
protected virtual void OnMouseScrollDataReceived(ArtemisDevice? device, MouseScrollDirection direction, int delta)
{
MouseScrollDataReceived?.Invoke(this, e);
MouseScrollDataReceived?.Invoke(this, new InputProviderMouseScrollEventArgs(device, direction, delta));
}
/// <summary>
/// Invokes the <see cref="MouseMoveDataReceived" /> event which the <see cref="IInputService" /> listens to as long as
/// this provider is registered
/// </summary>
protected virtual void OnMouseMoveDataReceived(InputProviderMouseMoveEventArgs e)
/// <param name="device">The device that triggered the event</param>
/// <param name="cursorX">The X position of the mouse cursor (not necessarily tied to the specific device)</param>
/// <param name="cursorY">The Y position of the mouse cursor (not necessarily tied to the specific device)</param>
/// <param name="deltaX">The movement delta in the horizontal direction</param>
/// <param name="deltaY">The movement delta in the vertical direction</param>
protected virtual void OnMouseMoveDataReceived(ArtemisDevice? device, int cursorX, int cursorY, int deltaX, int deltaY)
{
MouseMoveDataReceived?.Invoke(this, e);
MouseMoveDataReceived?.Invoke(this, new InputProviderMouseMoveEventArgs(device, cursorX, cursorY, deltaX, deltaY));
}
/// <summary>
/// Invokes the <see cref="IdentifierReceived" /> event which the <see cref="IInputService" /> listens to as long as
/// this provider is registered
/// <para>Except for on mouse movement, call this whenever you have an identifier ready for the core to process</para>
/// </summary>
protected virtual void OnIdentifierReceived(object identifier)
/// <param name="identifier">A value that can be used to identify this device</param>
/// <param name="deviceType">The type of device this identifier belongs to</param>
protected virtual void OnIdentifierReceived(object identifier, InputDeviceType deviceType)
{
IdentifierReceived?.Invoke(this, identifier);
IdentifierReceived?.Invoke(this, new InputProviderIdentifierEventArgs(identifier, deviceType));
}
#region IDisposable
+98 -42
View File
@@ -10,14 +10,13 @@ namespace Artemis.Core.Services
{
private readonly ILogger _logger;
private readonly ISurfaceService _surfaceService;
private readonly List<InputProvider> _inputProviders;
public InputService(ILogger logger, ISurfaceService surfaceService)
{
_logger = logger;
_surfaceService = surfaceService;
_inputProviders = new List<InputProvider>();
_keyboardModifier = new Dictionary<ArtemisDevice, KeyboardModifierKeys>();
_keyboardModifier = new Dictionary<ArtemisDevice, KeyboardModifierKey>();
_deviceCache = new Dictionary<Tuple<InputProvider, object>, ArtemisDevice>();
_devices = new List<ArtemisDevice>();
@@ -26,6 +25,21 @@ namespace Artemis.Core.Services
BustIdentifierCache();
}
#region IDisposable
/// <inheritdoc />
public void Dispose()
{
while (_inputProviders.Any())
RemoveInputProvider(_inputProviders.First());
}
#endregion
#region Providers
private readonly List<InputProvider> _inputProviders;
public void AddInputProvider(InputProvider inputProvider)
{
inputProvider.IdentifierReceived += InputProviderOnIdentifierReceived;
@@ -49,6 +63,8 @@ namespace Artemis.Core.Services
inputProvider.MouseMoveDataReceived -= InputProviderOnMouseMoveDataReceived;
}
#endregion
#region Identification
private readonly Dictionary<Tuple<InputProvider, object>, ArtemisDevice> _deviceCache;
@@ -59,6 +75,10 @@ namespace Artemis.Core.Services
public void IdentifyDevice(ArtemisDevice device)
{
if (device.RgbDevice.DeviceInfo.DeviceType != RGBDeviceType.Keyboard && device.RgbDevice.DeviceInfo.DeviceType != RGBDeviceType.Mouse)
throw new ArtemisCoreException($"Cannot initialize input-identification for a device of type {device.RgbDevice.DeviceInfo.DeviceType}. \r\n" +
"Only keyboard and mouse is supported.");
_identifyingDevice = device;
_logger.Debug("Start identifying device {device}", device);
}
@@ -73,7 +93,7 @@ namespace Artemis.Core.Services
BustIdentifierCache();
}
public ArtemisDevice? GetDeviceByIdentifier(InputProvider provider, object identifier, InputFallbackDeviceType fallbackType)
public ArtemisDevice? GetDeviceByIdentifier(InputProvider provider, object identifier, InputDeviceType type)
{
if (provider == null) throw new ArgumentNullException(nameof(provider));
if (identifier == null) throw new ArgumentNullException(nameof(identifier));
@@ -84,7 +104,7 @@ namespace Artemis.Core.Services
return cacheMatch;
string providerName = provider.GetType().FullName!;
ArtemisDevice? match = _devices.FirstOrDefault(m => m.InputIdentifiers.Any(i => Equals(i.InputProvider,providerName) && Equals(i.Identifier, identifier)));
ArtemisDevice? match = _devices.FirstOrDefault(m => m.InputIdentifiers.Any(i => Equals(i.InputProvider, providerName) && Equals(i.Identifier, identifier)));
// If a match was found cache it to speed up the next event and return the match
if (match != null)
@@ -94,9 +114,9 @@ namespace Artemis.Core.Services
}
// If there is no match, apply our fallback type
if (fallbackType == InputFallbackDeviceType.None)
if (type == InputDeviceType.None)
return null;
if (fallbackType == InputFallbackDeviceType.Keyboard)
if (type == InputDeviceType.Keyboard)
{
if (_cachedFallbackKeyboard != null)
return _cachedFallbackKeyboard;
@@ -104,7 +124,7 @@ namespace Artemis.Core.Services
return _cachedFallbackKeyboard;
}
if (fallbackType == InputFallbackDeviceType.Mouse)
if (type == InputDeviceType.Mouse)
{
if (_cachedFallbackMouse != null)
return _cachedFallbackMouse;
@@ -140,18 +160,21 @@ namespace Artemis.Core.Services
BustIdentifierCache();
}
private void InputProviderOnIdentifierReceived(object? sender, object identifier)
private void InputProviderOnIdentifierReceived(object? sender, InputProviderIdentifierEventArgs e)
{
if (_identifyingDevice == null)
// Don't match if there is no device or if the device type differs from the event device type
if (_identifyingDevice == null ||
_identifyingDevice.RgbDevice.DeviceInfo.DeviceType == RGBDeviceType.Keyboard && e.DeviceType == InputDeviceType.Mouse ||
_identifyingDevice.RgbDevice.DeviceInfo.DeviceType == RGBDeviceType.Mouse && e.DeviceType == InputDeviceType.Keyboard)
return;
if (!(sender is InputProvider inputProvider))
if (!(sender is InputProvider inputProvider))
return;
string providerName = inputProvider.GetType().FullName!;
// Remove existing identification
_identifyingDevice.InputIdentifiers.RemoveAll(i => i.InputProvider == providerName);
_identifyingDevice.InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(providerName, identifier));
_identifyingDevice.InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(providerName, e.Identifier));
StopIdentify();
OnDeviceIdentified();
@@ -161,12 +184,12 @@ namespace Artemis.Core.Services
#region Keyboard
private readonly Dictionary<ArtemisDevice, KeyboardModifierKeys> _keyboardModifier;
private KeyboardModifierKeys _globalModifiers;
private readonly Dictionary<ArtemisDevice, KeyboardModifierKey> _keyboardModifier;
private KeyboardModifierKey _globalModifiers;
private void InputProviderOnKeyboardDataReceived(object? sender, InputProviderKeyboardEventArgs e)
{
KeyboardModifierKeys keyboardModifierKeys = UpdateModifierKeys(e.Device, e.Key, e.IsDown);
KeyboardModifierKey keyboardModifierKey = UpdateModifierKeys(e.Device, e.Key, e.IsDown);
// Get the LED - TODO: leverage a lookup
bool foundLedId = InputKeyUtilities.KeyboardKeyLedIdMap.TryGetValue(e.Key, out LedId ledId);
@@ -175,49 +198,49 @@ namespace Artemis.Core.Services
led = e.Device.Leds.FirstOrDefault(l => l.RgbLed.Id == ledId);
// Create the UpDown event args because it can be used for every event
KeyboardKeyUpDownEventArgs eventArgs = new KeyboardKeyUpDownEventArgs(e.Device, led, e.Key, keyboardModifierKeys, e.IsDown);
KeyboardKeyUpDownEventArgs eventArgs = new KeyboardKeyUpDownEventArgs(e.Device, led, e.Key, keyboardModifierKey, e.IsDown);
OnKeyboardKeyUpDown(eventArgs);
if (e.IsDown)
OnKeyboardKeyDown(eventArgs);
else
OnKeyboardKeyUp(eventArgs);
_logger.Verbose("Keyboard data: LED ID: {ledId}, key: {key}, is down: {isDown}, modifiers: {modifiers}, device: {device} ", ledId, e.Key, e.IsDown, keyboardModifierKeys, e.Device);
// _logger.Verbose("Keyboard data: LED ID: {ledId}, key: {key}, is down: {isDown}, modifiers: {modifiers}, device: {device} ", ledId, e.Key, e.IsDown, keyboardModifierKey, e.Device);
}
private KeyboardModifierKeys UpdateModifierKeys(ArtemisDevice? device, KeyboardKey key, in bool isDown)
private KeyboardModifierKey UpdateModifierKeys(ArtemisDevice? device, KeyboardKey key, in bool isDown)
{
KeyboardModifierKeys modifiers = _globalModifiers;
KeyboardModifierKey modifiers = _globalModifiers;
if (device != null)
_keyboardModifier.TryGetValue(device, out modifiers);
if (key == KeyboardKey.LeftAlt || key == KeyboardKey.RightAlt)
{
if (isDown)
modifiers = modifiers | KeyboardModifierKeys.Alt;
modifiers |= KeyboardModifierKey.Alt;
else
modifiers = modifiers & ~KeyboardModifierKeys.Alt;
modifiers &= ~KeyboardModifierKey.Alt;
}
else if (key == KeyboardKey.LeftCtrl || key == KeyboardKey.RightCtrl)
{
if (isDown)
modifiers = modifiers | KeyboardModifierKeys.Control;
modifiers |= KeyboardModifierKey.Control;
else
modifiers = modifiers & ~KeyboardModifierKeys.Control;
modifiers &= ~KeyboardModifierKey.Control;
}
else if (key == KeyboardKey.LeftShift || key == KeyboardKey.RightShift)
{
if (isDown)
modifiers = modifiers | KeyboardModifierKeys.Shift;
modifiers |= KeyboardModifierKey.Shift;
else
modifiers = modifiers & ~KeyboardModifierKeys.Shift;
modifiers &= ~KeyboardModifierKey.Shift;
}
else if (key == KeyboardKey.LWin || key == KeyboardKey.RWin)
{
if (isDown)
modifiers = modifiers | KeyboardModifierKeys.Windows;
modifiers |= KeyboardModifierKey.Windows;
else
modifiers = modifiers & ~KeyboardModifierKeys.Windows;
modifiers &= ~KeyboardModifierKey.Windows;
}
if (device != null)
@@ -235,16 +258,30 @@ namespace Artemis.Core.Services
private void InputProviderOnMouseButtonDataReceived(object? sender, InputProviderMouseButtonEventArgs e)
{
bool foundLedId = InputKeyUtilities.MouseButtonLedIdMap.TryGetValue(e.Button, out LedId ledId);
ArtemisLed? led = null;
if (foundLedId && e.Device != null)
led = e.Device.Leds.FirstOrDefault(l => l.RgbLed.Id == ledId);
// Create the UpDown event args because it can be used for every event
MouseButtonUpDownEventArgs eventArgs = new MouseButtonUpDownEventArgs(e.Device, led, e.Button, e.IsDown);
OnMouseButtonUpDown(eventArgs);
if (e.IsDown)
OnMouseButtonDown(eventArgs);
else
OnMouseButtonUp(eventArgs);
// _logger.Verbose("Mouse button data: LED ID: {ledId}, button: {button}, is down: {isDown}, device: {device} ", ledId, e.Button, e.IsDown, e.Device);
}
private void InputProviderOnMouseScrollDataReceived(object? sender, InputProviderMouseScrollEventArgs e)
{
OnMouseScroll(new MouseScrollEventArgs(e.Device, e.Direction, e.Delta));
// _logger.Verbose("Mouse scroll data: Direction: {direction}, delta: {delta}, device: {device} ", e.Direction, e.Delta, e.Device);
}
private void InputProviderOnMouseMoveDataReceived(object? sender, InputProviderMouseMoveEventArgs e)
{
OnMouseMove(new MouseMoveEventArgs(e.Device, e.CursorX, e.CursorY, e.DeltaX, e.DeltaY));
// _logger.Verbose("Mouse move data: XY: {X},{Y} - delta XY: {deltaX},{deltaY} - device: {device} ", e.CursorX, e.CursorY, e.DeltaX, e.DeltaY, e.Device);
}
@@ -253,8 +290,13 @@ namespace Artemis.Core.Services
#region Events
public event EventHandler<KeyboardKeyUpDownEventArgs>? KeyboardKeyUpDown;
public event EventHandler<KeyboardEventArgs>? KeyboardKeyDown;
public event EventHandler<KeyboardEventArgs>? KeyboardKeyUp;
public event EventHandler<KeyboardKeyEventArgs>? KeyboardKeyDown;
public event EventHandler<KeyboardKeyEventArgs>? KeyboardKeyUp;
public event EventHandler<MouseButtonUpDownEventArgs>? MouseButtonUpDown;
public event EventHandler<MouseButtonEventArgs>? MouseButtonDown;
public event EventHandler<MouseButtonEventArgs>? MouseButtonUp;
public event EventHandler<MouseScrollEventArgs>? MouseScroll;
public event EventHandler<MouseMoveEventArgs>? MouseMove;
public event EventHandler? DeviceIdentified;
protected virtual void OnKeyboardKeyUpDown(KeyboardKeyUpDownEventArgs e)
@@ -262,32 +304,46 @@ namespace Artemis.Core.Services
KeyboardKeyUpDown?.Invoke(this, e);
}
protected virtual void OnKeyboardKeyDown(KeyboardEventArgs e)
protected virtual void OnKeyboardKeyDown(KeyboardKeyEventArgs e)
{
KeyboardKeyDown?.Invoke(this, e);
}
protected virtual void OnKeyboardKeyUp(KeyboardEventArgs e)
protected virtual void OnKeyboardKeyUp(KeyboardKeyEventArgs e)
{
KeyboardKeyUp?.Invoke(this, e);
}
protected virtual void OnMouseButtonUpDown(MouseButtonUpDownEventArgs e)
{
MouseButtonUpDown?.Invoke(this, e);
}
protected virtual void OnMouseButtonDown(MouseButtonEventArgs e)
{
MouseButtonDown?.Invoke(this, e);
}
protected virtual void OnMouseButtonUp(MouseButtonEventArgs e)
{
MouseButtonUp?.Invoke(this, e);
}
protected virtual void OnMouseScroll(MouseScrollEventArgs e)
{
MouseScroll?.Invoke(this, e);
}
protected virtual void OnMouseMove(MouseMoveEventArgs e)
{
MouseMove?.Invoke(this, e);
}
protected virtual void OnDeviceIdentified()
{
DeviceIdentified?.Invoke(this, EventArgs.Empty);
}
#endregion
#region IDisposable
/// <inheritdoc />
public void Dispose()
{
while (_inputProviders.Any())
RemoveInputProvider(_inputProviders.First());
}
#endregion
}
}
@@ -22,7 +22,7 @@ namespace Artemis.Core.Services
/// <summary>
/// Identifies the provided <paramref name="device" /> by assigning the next received device identification to it
/// </summary>
/// <param name="device">The device to identify</param>
/// <param name="device">The device to identify - Must be a keyboard or mouse</param>
void IdentifyDevice(ArtemisDevice device);
/// <summary>
@@ -40,12 +40,37 @@ namespace Artemis.Core.Services
/// <summary>
/// Occurs whenever a key on a keyboard was pressed
/// </summary>
event EventHandler<KeyboardEventArgs> KeyboardKeyDown;
event EventHandler<KeyboardKeyEventArgs> KeyboardKeyDown;
/// <summary>
/// Occurs whenever a key on a keyboard was released
/// </summary>
event EventHandler<KeyboardEventArgs> KeyboardKeyUp;
event EventHandler<KeyboardKeyEventArgs> KeyboardKeyUp;
/// <summary>
/// Occurs whenever a button on a mouse was pressed or released
/// </summary>
event EventHandler<MouseButtonUpDownEventArgs> MouseButtonUpDown;
/// <summary>
/// Occurs whenever a button on a mouse was pressed
/// </summary>
event EventHandler<MouseButtonEventArgs> MouseButtonDown;
/// <summary>
/// Occurs whenever a button on a mouse was released
/// </summary>
event EventHandler<MouseButtonEventArgs> MouseButtonUp;
/// <summary>
/// Occurs whenever a the scroll wheel of a mouse is turned
/// </summary>
event EventHandler<MouseScrollEventArgs> MouseScroll;
/// <summary>
/// Occurs whenever a a mouse is moved
/// </summary>
event EventHandler<MouseMoveEventArgs> MouseMove;
/// <summary>
/// Occurs when a device has been identified after calling <see cref="IdentifyDevice" />
@@ -61,9 +86,9 @@ namespace Artemis.Core.Services
/// </summary>
/// <param name="provider">The input provider to identify the device for</param>
/// <param name="identifier">The value to use to identify the device</param>
/// <param name="fallbackType">A device type to fall back to if no match is found</param>
/// <param name="type">A device type to fall back to if no match is found</param>
/// <returns>If found, the Artemis device matching the provider and identifier</returns>
ArtemisDevice? GetDeviceByIdentifier(InputProvider provider, object identifier, InputFallbackDeviceType fallbackType);
ArtemisDevice? GetDeviceByIdentifier(InputProvider provider, object identifier, InputDeviceType type);
/// <summary>
/// Clears the identifier cache