-
Notifications
You must be signed in to change notification settings - Fork 3
Tutorials
This page contains tutorials about how to implement common functionality related to gamepads on your application, using XInputium. Because different game or application frameworks use different means for performing the same tasks, the tutorials in this page will assume a generic fictional application framework, instead of using specific existing frameworks, unless specified otherwise.
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
A common ability game characters have in many games is the ability to jump. It is pretty common to make these jumps dynamic, by allowing the user to tap a button to make a short jump, or holding the button for longer to make a higher jump. In the following example, we will see how one could implement this functionality in a game. There are several ways for doing this, but we will use the versatility of ActivationInputEvent to achieve it.
using System;
using System.Diagnostics;
using XInputium;
using XInputium.ModifierFunctions;
using XInputium.XInput;
namespace MyNamespace;
public class MyGameOrApplication
{
private readonly XGamepad _gamepad;
private XButtons _jumpButton = XButtons.A; // The jump button.
private float _maxJumpHoldTime = 750; // Button hold time for the highest jump, in milliseconds.
public MyGameOrApplication()
{
_gamepad = new(); // This uses the first connected device it finds.
// Register a dynamic event that fires when the button is released or when the
// button is held for the `_maxJumpHoldTime`, whatever happens first.
_gamepad.RegisterActivationInputEvent(
() => _gamepad.Buttons.IsPressed(_jumpButton),
TimeSpan.Zero, TimeSpan.Zero,
TimeSpan.FromMilliseconds(_maxJumpHoldTime),
ActivationInputEventTriggerMode.OnDeactivation,
(s, e) =>
{
// Determine the jump force, using the duration of the hold.
float jumpForce = (float)e.PreviousStateDuration.TotalMilliseconds / _maxJumpHoldTime;
// Make the jump force non-linear, so pressing the button shortly will not jump
// with a too low force. Optionally, we could also set a minimum jump force.
jumpForce = NonLinearFunctions.QuadraticEaseOut(jumpForce);
// Call our method that will make the game character jump.
Jump(jumpForce);
});
}
// This method is hypothetically called by the game/application framework on every frame render.
protected void OnRender()
{
// Call this on every game/application frame.
_gamepad.Update(); // Input events will be triggered now.
}
// Make the character jump, with a force within the 0-1 range.
public void Jump(float force)
{
// Your jump code here.
Debug.WriteLine($"Jump! (force: {force:P0})");
}
}On our example above, if the user holds A button for less than 750 milliseconds, a fraction of the max jump force is used to jump. Once that time has passed, if the user is still holding the button, the character jumps anyway, with the maximum jump force. The jump force depends on how long the user holds the button, and that time affects the jump force non-linearly.
TODO
TODO
Games with press any button screens are very common. To determine if any digital button is pressed, you simply need to subscribe to XGamepad.ButtonPressed event. The following example, shows how you could subscribe to that event.
XGamepad _gamepad = new();
_gamepad.ButtonPressed += (s, e) => Debug.WriteLine("Some button was pressed.");Note that the left and right triggers are not buttons and will not fire the ButtonPressed event. To also handle the triggers as digital buttons, you can add the following code:
_gamepad.LeftTrigger.ToDigitalButton(0.5f).Pressed +=
(s, e) => Debug.WriteLine("Some button was pressed.");
_gamepad.RightTrigger.ToDigitalButton(0.5f).Pressed +=
(s, e) => Debug.WriteLine("Some button was pressed.");Alternatively, you could also use ActivationInputEvent to achieve the same result as the previous two examples:
XGamepad _gamepad = new();
_gamepad.RegisterActivationInputEvent(
() => _gamepad.Buttons.IsAnyButtonPressed
|| _gamepad.LeftTrigger.Value >= 0.5f || _gamepad.RightTrigger.Value >= 0.5f,
ActivationInputEventTriggerMode.OnActivation,
(s, e) => Debug.WriteLine("Some button was pressed."));TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
TODO
The wiki is currently a work in progress, and some pages may be missing or incomplete. Thank you for your understanding!