/// <summary> /// Initializes a new instance of the <see cref="KeyboardDevice"/> class. /// </summary> /// <param name="inpDeviceImp">The platform dependent connector to the underlying physical device.</param> public KeyboardDevice(IInputDeviceImp inpDeviceImp) : base(inpDeviceImp) { _leftRightAxis = RegisterTwoButtonAxis((int)KeyCodes.Left, (int)KeyCodes.Right, AxisDirection.X).Id; _upDownAxis = RegisterTwoButtonAxis((int)KeyCodes.Down, (int)KeyCodes.Up, AxisDirection.Y).Id; _adAxis = RegisterTwoButtonAxis((int)KeyCodes.A, (int)KeyCodes.D, AxisDirection.X).Id; _wsAxis = RegisterTwoButtonAxis((int)KeyCodes.S, (int)KeyCodes.W, AxisDirection.Y).Id; }
/// <summary> /// Registers the type of input device available. /// </summary> /// <param name="match"></param> /// <param name="creator"></param> public void RegisterInputDeviceType(MatchFunc match, CreatorFunc creator) { if (match == null) { throw new ArgumentNullException(nameof(match)); } if (creator == null) { throw new ArgumentNullException(nameof(creator)); } _specialDeviceCreators.Add(new SpecialDeviceCreator { Match = match, Creator = creator }); // Reconnect any existing devices matching the predicate // List<string> matchingDevices = (from device in _inputDevices.Values where device.DeviceImp != null && match(device.DeviceImp) select device.Id).ToList(); List <string> matchingDevices = new List <string>(); foreach (var device in _inputDevices.Values) { if (device.DeviceImp != null && match(device.DeviceImp)) { matchingDevices.Add(device.Id); } } foreach (var devId in matchingDevices) { InputDevice dev = _inputDevices[devId]; // Set device to disconnected state dev.Disconnect(); // Inform interested users about disconnection InputDeviceDisconnected?.Invoke(this, new DeviceConnectionArgs { InputDevice = dev }); IInputDeviceImp inputDeviceImp = dev.DeviceImp; // Remove device from the list _inputDevices.Remove(devId); dev = creator(inputDeviceImp); _inputDevices[devId] = dev; // no need to call reconnect since device is constructed from scratch // Inform interested users about the newly connected device. InputDeviceConnected?.Invoke(this, new DeviceConnectionArgs { InputDevice = dev }); } }
internal void Disconnect() { // The driver informed the input system that this device has disconnected. Set all axes to 0 and all buttons to false. foreach (var axisId in _axesToListen.Keys.ToArray()) { if (AxisValueChanged != null && _axesToListen[axisId] != 0) { // Inform axes-to-listen about the new value (the disconnected device won't do it)... // Do NOT inform axes-to-poll here since they will be informed in PreRender anyway. AxisValueChanged(this, new AxisValueChangedArgs { Axis = _axes[axisId], Value = 0 }); } _axesToListen[axisId] = 0; } foreach (var axisId in _axesToPoll.Keys.ToArray()) { _axesToPoll[axisId] = 0; } foreach (var buttonId in _buttonsToListen.Keys.ToArray()) { if (ButtonValueChanged != null && _buttonsToListen[buttonId]) { // Inform buttons-to-listen about the new value (the disconnected device won't do it)... // Do NOT inform buttons-to-poll here since they will be informed in PreRender anyway. ButtonValueChanged(this, new ButtonValueChangedArgs { Button = _buttons[buttonId], Pressed = false }); } _buttonsToListen[buttonId] = false; } foreach (var buttonId in _buttonsToPoll.Keys.ToArray()) { _buttonsToPoll[buttonId] = false; } // Actually disconnect by releasing any reference to the implementation object. _inpDevImp.AxisValueChanged -= OnImpAxisValueChanged; _inpDevImp.ButtonValueChanged -= OnImpButtonValueChanged; _inpDevImp = null; _isConnected = false; }
internal void Reconnect(IInputDeviceImp deviceImp) { if (_isConnected) { throw new InvalidOperationException($"Cannot reconnect already connected input device (connected to {_inpDevImp.Desc}). Disconnect first."); } if (deviceImp == null) { throw new ArgumentNullException(nameof(deviceImp)); } _inpDevImp = deviceImp; _inpDevImp.AxisValueChanged += OnImpAxisValueChanged; _inpDevImp.ButtonValueChanged += OnImpButtonValueChanged; _isConnected = true; }
private InputDevice CreateInputDevice(IInputDeviceImp imp) { // First see if a special device can be found to handle this foreach (var sdc in _specialDeviceCreators) { if (sdc.Match(imp)) { InputDevice ret = sdc.Creator(imp); if (ret != null) { return(ret); } } } // Fallback - we don't know any special device, create a generic one around the device implementation. return(new InputDevice(imp)); }
/// <summary> /// Initializes a new instance of the <see cref="TouchDevice"/> class. /// </summary> /// <param name="inpDeviceImp">The "driver".</param> public TouchDevice(IInputDeviceImp inpDeviceImp) : base(inpDeviceImp) { int nTouchpoints = ButtonCount; int nVelAxes = nTouchpoints * 2; _velocityIDs = new int[nVelAxes]; int axisId = (int)TouchAxes.Touchpoint_0_X; int buttonId = (int)TouchPoints.Touchpoint_0; for (int i = 0; i < nVelAxes; i++) { _velocityIDs[i] = RegisterVelocityAxis(axisId++, buttonId).Id; i++; _velocityIDs[i] = RegisterVelocityAxis(axisId++, buttonId).Id; } _tpDistance = RegisterTtpDistanceAxis(); _tpDistanceVel = RegisterVelocityAxis(_tpDistance, (int)TouchPoints.Touchpoint_1).Id; _tpAngle = RegisterTtpAngleAxis(); _tpAngleVel = RegisterVelocityAxis(_tpAngle, (int)TouchPoints.Touchpoint_1).Id; _tpMidPointX = RegisterTtpMidpointAxis((int)TouchAxes.Touchpoint_0_X, (int)TouchAxes.Touchpoint_1_X, (int)TouchAxes.MinX, (int)TouchAxes.MaxX, AxisDirection.X, "Double-Touch Midpoint X"); _tpMidPointVelX = RegisterVelocityAxis(_tpMidPointX, (int)TouchPoints.Touchpoint_1).Id; _tpMidPointY = RegisterTtpMidpointAxis((int)TouchAxes.Touchpoint_0_Y, (int)TouchAxes.Touchpoint_1_Y, (int)TouchAxes.MinY, (int)TouchAxes.MaxY, AxisDirection.Y, "Double-Touch Midpoint Y"); _tpMidPointVelY = RegisterVelocityAxis(_tpMidPointY, (int)TouchPoints.Touchpoint_1).Id; }
private InputDevice Creator(IInputDeviceImp device) { throw new NotImplementedException(); }
/// <summary> /// Initializes a new instance of the <see cref="GamePadDevice"/> class. /// </summary> /// <param name="inpDeviceImp">The platform dependent connector to the underlying physical device.</param> public GamePadDevice(IInputDeviceImp inpDeviceImp) : base(inpDeviceImp) { _xDPadId = RegisterTwoButtonAxis((int)Gamepad.DPadLeft, (int)Gamepad.DPadRight, AxisDirection.X).Id; _yDPadId = RegisterTwoButtonAxis((int)Gamepad.DPadUp, (int)Gamepad.DPadDown, AxisDirection.Y).Id; }
/// <summary> /// Initializes a new instance of the <see cref="SixDOFDevice"/> class. /// </summary> /// <param name="inpDeviceImp">The platform dependent connector to the underlying physical device.</param> public SixDOFDevice(IInputDeviceImp inpDeviceImp) : base(inpDeviceImp) { }
/// <summary> /// Initializes a new instance of the <see cref="InputDevice"/> class. /// </summary> /// <param name="inputDeviceImp">The input device imp.</param> public InputDevice(IInputDeviceImp inputDeviceImp) { _inputDeviceImp = inputDeviceImp; }
/// <summary> /// Initializes a new instance of the <see cref="MouseDevice"/> class. /// </summary> /// <param name="inpDeviceImp">The platform dependent connector to the underlying physical device.</param> public MouseDevice(IInputDeviceImp inpDeviceImp) : base(inpDeviceImp) { _xVelId = RegisterVelocityAxis((int)MouseAxes.X).Id; _yVelId = RegisterVelocityAxis((int)MouseAxes.Y).Id; _wheelVelId = RegisterVelocityAxis((int)MouseAxes.Wheel).Id; }
internal InputDevice(IInputDeviceImp inpDeviceImp) { if (inpDeviceImp == null) { throw new ArgumentNullException(nameof(inpDeviceImp)); } _inpDevImp = inpDeviceImp; _isConnected = true; #region Handle Axes _axes = new Dictionary <int, AxisDescription>(); _axesToPoll = new Dictionary <int, float>(); _axesToListen = new Dictionary <int, float>(); _calculatedAxes = new Dictionary <int, CalculatedAxisDescription>(); _nextAxisId = 0; // Look for each axis if its pollable or listenable. Prepare the axis to be accessed // the other way (poll a listenable axis / listen to a pollable axis). foreach (var axisImpDescription in _inpDevImp.AxisImpDesc) { // Keep track of IDs (to keep them uniqe) int axisId = axisImpDescription.AxisDesc.Id; if (axisId > _nextAxisId) { _nextAxisId = axisId; } // Store axis in the list of all axes _axes[axisId] = axisImpDescription.AxisDesc; // Prepare axis to be accessed either way (polled or listened) if (axisImpDescription.PollAxis) { _axesToPoll[axisId] = _inpDevImp.GetAxis(axisId); } else { _axesToListen[axisId] = 0.0f; } } _nextAxisId++; if (_axesToListen.Count > 0) { _inpDevImp.AxisValueChanged += OnImpAxisValueChanged; } #endregion #region Handle Buttons _buttons = new Dictionary <int, ButtonDescription>(); _buttonsToPoll = new Dictionary <int, bool>(); _buttonsToListen = new Dictionary <int, bool>(); _buttonsUp = new HashSet <int>(); _buttonsDown = new HashSet <int>(); _buttonsToListenJustChanged = new Dictionary <int, bool>(); // Do the same for buttons foreach (var buttonImpDescription in _inpDevImp.ButtonImpDesc) { int buttonId = buttonImpDescription.ButtonDesc.Id; _buttons[buttonId] = buttonImpDescription.ButtonDesc; if (buttonImpDescription.PollButton) { _buttonsToPoll[buttonId] = _inpDevImp.GetButton(buttonId); } else { _buttonsToListen[buttonId] = false; } } if (_buttonsToListen.Count > 0) { _inpDevImp.ButtonValueChanged += OnImpButtonValueChanged; } #endregion }