public void Update() { if (Util.Glfw.JoystickIsGamepad(Index)) { return; } var axes = Axes; for (var i = 0; i < axes.Count; i++) { if (axes[i].Position != (_cachedAxes.Count > i ? _cachedAxes[i] : axes[i]).Position) { _cachedAxes[i] = axes[i]; AxisMoved?.Invoke(this, Util.ApplyDeadzone(axes[i], Deadzone)); } } var buttons = Buttons; for (var i = 0; i < buttons.Count; i++) { if (buttons[i].Pressed != (_cachedButtons.Count > i ? _cachedButtons[i] : buttons[i]).Pressed) { _cachedButtons[i] = buttons[i]; (buttons[i].Pressed ? ButtonDown : ButtonUp)?.Invoke(this, buttons[i]); } } var hats = Hats; for (var i = 0; i < hats.Count; i++) { if (hats[i].Position != (_cachedHats.Count > i ? _cachedHats[i] : hats[i]).Position) { _cachedHats[i] = hats[i]; HatMoved?.Invoke(this, hats[i]); } } }
public void DoEvent(Event @event) { switch ((EventType)@event.Type) { case EventType.Joyaxismotion: { if (_axes.Length < @event.Jaxis.Axis + 1) { Array.Resize(ref _axes, @event.Jaxis.Axis + 1); } AxisMoved?.Invoke ( this, _axes[@event.Jaxis.Axis] = new Axis (@event.Jaxis.Axis, (float)@event.Jaxis.Value / short.MaxValue) ); break; } case EventType.Joyballmotion: { // todo investigate adding balls to the input spec later down the line break; } case EventType.Joyhatmotion: { if (_hats.Length < @event.Jhat.Hat + 1) { Array.Resize(ref _hats, @event.Jhat.Hat + 1); } var val = @event.Jhat.Value; HatMoved?.Invoke ( this, _hats[@event.Jhat.Hat] = new Hat ( @event.Jhat.Hat, (Position2D)((val & 0x01) * (int)Position2D.Up + (val & 0x02) * (int)Position2D.Right + (val & 0x04) * (int)Position2D.Down + (val & 0x08) * (int)Position2D.Left) ) ); break; } case EventType.Joybuttondown: { if (_buttons.Length < @event.Jbutton.Button + 1) { Array.Resize(ref _buttons, @event.Jbutton.Button + 1); } ButtonDown?.Invoke ( this, _buttons[@event.Jbutton.Button] = new Button ((ButtonName)@event.Jbutton.Button, @event.Jbutton.Button, true) ); break; } case EventType.Joybuttonup: { if (_buttons.Length < @event.Jbutton.Button + 1) { Array.Resize(ref _buttons, @event.Jbutton.Button + 1); } ButtonUp?.Invoke ( this, _buttons[@event.Jbutton.Button] = new Button ((ButtonName)@event.Jbutton.Button, @event.Jbutton.Button, false) ); break; } case EventType.Joydeviceadded: { _ctx.ChangeConnection(this, true); break; } case EventType.Joydeviceremoved: { _ctx.ChangeConnection(this, false); break; } } }