private void HandleMouseButtons(InputMapping mapping) { if (mapping.PositiveMouseButton == null && mapping.NegativeMouseButton == null) { return; } if (mapping.PressType == PressType.Down) { if (mapping.PositiveMouseButton != null && InputService.IsDown(mapping.PositiveMouseButton.Value)) { Value++; } if (mapping.NegativeMouseButton != null && InputService.IsDown(mapping.NegativeMouseButton.Value)) { Value--; } } else if (mapping.PressType == PressType.Press) { if (mapping.PositiveMouseButton != null && InputService.IsPressed(mapping.PositiveMouseButton.Value, false)) { Value++; } if (mapping.NegativeMouseButton != null && InputService.IsPressed(mapping.NegativeMouseButton.Value, false)) { Value--; } } else { Debug.Assert(mapping.PressType == PressType.DoubleClick, "Unhandled press type."); if (mapping.PositiveMouseButton != null && InputService.IsDoubleClick(mapping.PositiveMouseButton.Value)) { Value++; } if (mapping.NegativeMouseButton != null && InputService.IsDoubleClick(mapping.NegativeMouseButton.Value)) { Value--; } } }
private void HandleMapping(InputMapping mapping) { if (mapping == null) { return; } if (mapping.ModifierKeys == ModifierKeys.None || (InputService.ModifierKeys & mapping.ModifierKeys) == mapping.ModifierKeys) { // No modifiers necessary, or all modifiers are down. HandleKeys(mapping); HandleMouseButtons(mapping); } #if !SILVERLIGHT if (mapping.ModifierButtons == null || InputService.IsDown(mapping.ModifierButtons.Value, LogicalPlayerIndex)) { // No modifiers necessary, or all modifiers are down. HandleButtons(mapping); } #endif Value = MathHelper.Clamp(Value, -1, 1); float axisValue = HandleAxis(mapping); // Combine Value and axisValue. // The normal value limit is 1, but mouse axis can give a value beyond 1. We take the // max of these values as the limit. The Value must not increase above the limit when // the user uses axis + keys/buttons. (For instance, concurrent presses should not // make a player faster.) float limit = Math.Max(1, Math.Abs(axisValue)); Value = MathHelper.Clamp(Value + axisValue, -limit, limit); if (mapping.Invert) { Value = -Value; } }
private float HandleAxis(InputMapping mapping) { if (mapping.Axis == null) { return(0); } float axisValue = 0; switch (mapping.Axis.Value) { case DeviceAxis.MouseXAbsolute: axisValue = InputService.MousePosition.X; break; case DeviceAxis.MouseYAbsolute: axisValue = InputService.MousePosition.Y; break; case DeviceAxis.MouseXRelative: axisValue = InputService.MousePositionDelta.X; break; case DeviceAxis.MouseYRelative: axisValue = InputService.MousePositionDelta.Y; break; case DeviceAxis.MouseWheel: axisValue = InputService.MouseWheelDelta; break; #if !SILVERLIGHT case DeviceAxis.GamePadStickLeftX: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Left.X; break; case DeviceAxis.GamePadStickLeftY: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Left.Y; break; case DeviceAxis.GamePadStickRightX: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Right.X; break; case DeviceAxis.GamePadStickRightY: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Right.Y; break; case DeviceAxis.GamePadTriggerLeft: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).Triggers.Left; break; case DeviceAxis.GamePadTriggerRight: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).Triggers.Right; break; #endif default: Debug.Assert(false, "Unhandled device axis."); break; } if (Sensitivity != 1.0f) { axisValue = Math.Sign(axisValue) * (float)Math.Pow(Math.Abs(axisValue), 1.0f / Sensitivity); } return(axisValue); }
// Notes: // - We could turn digital input (buttons and keys) into a continuous input: When the // user presses a button, the value does not immediately jump to 1. Instead, the user // has to press the button for a longer time until 1 is reached. - But this is // application dependent and is also relevant for analog input (axes). // --> Do not handle this case here. // TODO: // - Integrate handling of IsHandled flags. //-------------------------------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- //-------------------------------------------------------------- #endregion Other #if !SILVERLIGHT private void HandleButtons(InputMapping mapping) { if (mapping.PositiveButton == null && mapping.NegativeButton == null) return; if (mapping.PressType == PressType.Down) { if (mapping.PositiveButton != null && InputService.IsDown(mapping.PositiveButton.Value, LogicalPlayerIndex)) Value++; if (mapping.NegativeButton != null && InputService.IsDown(mapping.NegativeButton.Value, LogicalPlayerIndex)) Value--; } else if (mapping.PressType == PressType.Press) { if (mapping.PositiveButton != null && InputService.IsPressed(mapping.PositiveButton.Value, false, LogicalPlayerIndex)) Value++; if (mapping.NegativeButton != null && InputService.IsPressed(mapping.NegativeButton.Value, false, LogicalPlayerIndex)) Value--; } else { Debug.Assert(mapping.PressType == PressType.DoubleClick, "Unhandled press type."); if (mapping.PositiveButton != null && InputService.IsDoubleClick(mapping.PositiveButton.Value, LogicalPlayerIndex)) Value++; if (mapping.NegativeButton != null && InputService.IsDoubleClick(mapping.NegativeButton.Value, LogicalPlayerIndex)) Value--; } }
private void HandleMapping(InputMapping mapping) { if (mapping == null) return; if (mapping.ModifierKeys == ModifierKeys.None || (InputService.ModifierKeys & mapping.ModifierKeys) == mapping.ModifierKeys) { // No modifiers necessary, or all modifiers are down. HandleKeys(mapping); HandleMouseButtons(mapping); } #if !SILVERLIGHT if (mapping.ModifierButtons == null || InputService.IsDown(mapping.ModifierButtons.Value, LogicalPlayerIndex)) { // No modifiers necessary, or all modifiers are down. HandleButtons(mapping); } #endif Value = MathHelper.Clamp(Value, -1, 1); float axisValue = HandleAxis(mapping); // Combine Value and axisValue. // The normal value limit is 1, but mouse axis can give a value beyond 1. We take the // max of these values as the limit. The Value must not increase above the limit when // the user uses axis + keys/buttons. (For instance, concurrent presses should not // make a player faster.) float limit = Math.Max(1, Math.Abs(axisValue)); Value = MathHelper.Clamp(Value + axisValue, -limit, limit); if (mapping.Invert) Value = -Value; }
private void HandleKeys(InputMapping mapping) { if (mapping.PositiveKey == null && mapping.NegativeKey == null) return; if (mapping.PressType == PressType.Down) { if (mapping.PositiveKey != null && InputService.IsDown(mapping.PositiveKey.Value)) Value++; if (mapping.NegativeKey != null && InputService.IsDown(mapping.NegativeKey.Value)) Value--; } else if (mapping.PressType == PressType.Press) { if (mapping.PositiveKey != null && InputService.IsPressed(mapping.PositiveKey.Value, false)) Value++; if (mapping.NegativeKey != null && InputService.IsPressed(mapping.NegativeKey.Value, false)) Value--; } else { Debug.Assert(mapping.PressType == PressType.DoubleClick, "Unhandled press type."); if (mapping.PositiveKey != null && InputService.IsDoubleClick(mapping.PositiveKey.Value)) Value++; if (mapping.NegativeKey != null && InputService.IsDoubleClick(mapping.NegativeKey.Value)) Value--; } }
private float HandleAxis(InputMapping mapping) { if (mapping.Axis == null) return 0; float axisValue = 0; switch (mapping.Axis.Value) { case DeviceAxis.MouseXAbsolute: axisValue = InputService.MousePosition.X; break; case DeviceAxis.MouseYAbsolute: axisValue = InputService.MousePosition.Y; break; case DeviceAxis.MouseXRelative: axisValue = InputService.MousePositionDelta.X; break; case DeviceAxis.MouseYRelative: axisValue = InputService.MousePositionDelta.Y; break; case DeviceAxis.MouseWheel: axisValue = InputService.MouseWheelDelta; break; #if !SILVERLIGHT case DeviceAxis.GamePadStickLeftX: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Left.X; break; case DeviceAxis.GamePadStickLeftY: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Left.Y; break; case DeviceAxis.GamePadStickRightX: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Right.X; break; case DeviceAxis.GamePadStickRightY: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).ThumbSticks.Right.Y; break; case DeviceAxis.GamePadTriggerLeft: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).Triggers.Left; break; case DeviceAxis.GamePadTriggerRight: axisValue = InputService.GetGamePadState(LogicalPlayerIndex).Triggers.Right; break; #endif default: Debug.Assert(false, "Unhandled device axis."); break; } if (Sensitivity != 1.0f) axisValue = Math.Sign(axisValue) * (float)Math.Pow(Math.Abs(axisValue), 1.0f / Sensitivity); return axisValue; }