Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Gamepad changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Nov 18, 2023
1 parent c874cac commit b755586
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
8 changes: 4 additions & 4 deletions Client/ClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public static void Save() {

public class General {
[DataMember(Name = "deadzone_right")]
public float _deadzoneRight = 0;
public double _deadzoneRight = 0;
[DataMember(Name = "deadzone_left")]
public float _deadzoneLeft = 0;
public double _deadzoneLeft = 0;

[DataMember(Name = "fov")]
public float _fov = 45;
Expand All @@ -43,12 +43,12 @@ public class General {
[DataMember(Name = "chunk_build_thread_count")]
public int _chunkBuildThreadCount = 3;

public static float deadzoneRight {
public static double deadzoneRight {
get => instance.general._deadzoneRight;
set => instance.general._deadzoneRight = value;
}

public static float deadzoneLeft {
public static double deadzoneLeft {
get => instance.general._deadzoneLeft;
set => instance.general._deadzoneLeft = value;
}
Expand Down
51 changes: 43 additions & 8 deletions Client/Keybinding/Button.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using GlmSharp;
using RenderSurface.Input.Gamepad;
using Veldrid;
using VMouseButton = Veldrid.MouseButton;
Expand All @@ -15,7 +16,7 @@ public abstract class Button {
return first switch {
"Key" => KeyButton.FromString(second),
"Mouse" => MouseButton.FromString(second),
"Button" => ControllerNewButton.FromString(second),
"Button" => ControllerButton.FromString(second),
"Axis" => ControllerAxisButton.FromString(second),
_ => null,
};
Expand Down Expand Up @@ -78,13 +79,13 @@ public override string ToString()
=> "Mouse."+Button;
}

public class ControllerNewButton : Button {
private static readonly Dictionary<GamepadButton, ControllerNewButton> Cache = new();
public class ControllerButton : Button {
private static readonly Dictionary<GamepadButton, ControllerButton> Cache = new();

public new static ControllerNewButton? FromString(string value)
public new static ControllerButton? FromString(string value)
=> Enum.TryParse(value, out GamepadButton button) ? Get(button) : null;

public static ControllerNewButton Get(GamepadButton button) {
public static ControllerButton Get(GamepadButton button) {
if (!Cache.ContainsKey(button))
Cache[button] = new(button);

Expand All @@ -95,7 +96,7 @@ public static ControllerNewButton Get(GamepadButton button) {

public override bool isPressed => VoxelClient.Instance.InputManager.IsButtonPressed(Button);

public ControllerNewButton(GamepadButton button) {
public ControllerButton(GamepadButton button) {
Button = button;
}

Expand All @@ -118,14 +119,48 @@ public static ControllerAxisButton Get(GamepadAxis axis) {

public readonly GamepadAxis Axis;

public override double strength => VoxelClient.Instance.InputManager.GetAxisStrength(Axis);
public override double strength => GetAxisStrength(Axis);

public override bool isPressed => strength > 0.5;
public override bool isPressed => strength > 0.25;

public ControllerAxisButton(GamepadAxis axis) {
Axis = axis;
}

public override string ToString()
=> $"Axis.{Axis}";

private static double GetAxisStrength(GamepadAxis axis) {
var inputManager = VoxelClient.Instance.InputManager;
if (axis == GamepadAxis.RightX || axis == GamepadAxis.RightY) {
int i = (int)axis - 2;
var vec = new dvec2(
inputManager.GetAxisStrength(GamepadAxis.RightX),
inputManager.GetAxisStrength(GamepadAxis.RightY)
);
if (vec.Length < ClientConfig.General.deadzoneRight)
return 0;

if (vec[i] < ClientConfig.General.deadzoneRight * 0.5)
return 0;

return vec[i];
}
if (axis == GamepadAxis.LeftX || axis == GamepadAxis.LeftY) {
int i = (int)axis;
var vec = new dvec2(
inputManager.GetAxisStrength(GamepadAxis.LeftX),
inputManager.GetAxisStrength(GamepadAxis.LeftY)
);
if (vec.Length < ClientConfig.General.deadzoneLeft)
return 0;

if (vec[i] < ClientConfig.General.deadzoneLeft * 0.5)
return 0;

return vec[i];
}

return inputManager.GetAxisStrength(axis);
}
}
8 changes: 4 additions & 4 deletions Client/Keybinding/Keybinds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class Keybinds {
public static readonly Keybind Pause = new(
"pause",
KeyButton.Get(Key.Escape),
ControllerNewButton.Get(GamepadButton.Start)
ControllerButton.Get(GamepadButton.Start)
);

public static readonly Keybind Forward = new(
Expand Down Expand Up @@ -41,13 +41,13 @@ public static class Keybinds {
public static readonly Keybind Jump = new(
"movement.jump",
KeyButton.Get(Key.Space),
ControllerNewButton.Get(GamepadButton.A)
ControllerButton.Get(GamepadButton.A)
);

public static readonly Keybind Crouch = new(
"movement.crouch",
KeyButton.Get(Key.ShiftLeft),
ControllerNewButton.Get(GamepadButton.RightStick)
ControllerButton.Get(GamepadButton.RightStick)
);

public static readonly Keybind LookUp = new(
Expand Down Expand Up @@ -89,7 +89,7 @@ public static class Keybinds {
public static readonly Keybind Refresh = new(
"debug.refresh",
KeyButton.Get(Key.F3),
ControllerNewButton.Get(GamepadButton.Start)
ControllerButton.Get(GamepadButton.Start)
);

public static void ReadFromConfig() {
Expand Down

0 comments on commit b755586

Please sign in to comment.