mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2025-04-12 03:16:05 +08:00
Gamepad axis support works
This commit is contained in:
parent
1bc750490a
commit
da320af175
@ -1,5 +1,6 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System;
|
using System;
|
||||||
|
using SDL2;
|
||||||
|
|
||||||
namespace Strawberry
|
namespace Strawberry
|
||||||
{
|
{
|
||||||
@ -88,12 +89,24 @@ namespace Strawberry
|
|||||||
|
|
||||||
// Setup Calls
|
// Setup Calls
|
||||||
|
|
||||||
public VirtualAxis Keys(SDL2.SDL.Scancode negativeKey, SDL2.SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
|
public VirtualAxis AddKeys(SDL.Scancode negativeKey, SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
|
||||||
{
|
{
|
||||||
nodes.Add(new KeyboardKeys(negativeKey, positiveKey, overlapBehavior));
|
nodes.Add(new KeyboardKeys(negativeKey, positiveKey, overlapBehavior));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public VirtualAxis AddButtons(SDL.SDL_GameControllerButton negativeButton, SDL.SDL_GameControllerButton positiveButton, OverlapBehaviors overlapBehavior = .TakeNewer)
|
||||||
|
{
|
||||||
|
nodes.Add(new GamepadButtons(negativeButton, positiveButton, overlapBehavior));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public VirtualAxis AddAxis(SDL.SDL_GameControllerAxis axis, float deadzone)
|
||||||
|
{
|
||||||
|
nodes.Add(new GamepadAxis(axis, deadzone));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public VirtualAxis PressBuffer(float time)
|
public VirtualAxis PressBuffer(float time)
|
||||||
{
|
{
|
||||||
pressBuffer = time;
|
pressBuffer = time;
|
||||||
@ -124,13 +137,13 @@ namespace Strawberry
|
|||||||
private class KeyboardKeys : Node
|
private class KeyboardKeys : Node
|
||||||
{
|
{
|
||||||
public OverlapBehaviors OverlapBehavior;
|
public OverlapBehaviors OverlapBehavior;
|
||||||
public SDL2.SDL.Scancode NegativeKeycode;
|
public SDL.Scancode NegativeKeycode;
|
||||||
public SDL2.SDL.Scancode PositiveKeycode;
|
public SDL.Scancode PositiveKeycode;
|
||||||
|
|
||||||
private float value;
|
private float value;
|
||||||
private bool turned;
|
private bool turned;
|
||||||
|
|
||||||
public this(SDL2.SDL.Scancode negativeKey, SDL2.SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
|
public this(SDL.Scancode negativeKey, SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
|
||||||
{
|
{
|
||||||
NegativeKeycode = negativeKey;
|
NegativeKeycode = negativeKey;
|
||||||
PositiveKeycode = positiveKey;
|
PositiveKeycode = positiveKey;
|
||||||
@ -188,5 +201,97 @@ namespace Strawberry
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class GamepadButtons : Node
|
||||||
|
{
|
||||||
|
public OverlapBehaviors OverlapBehavior;
|
||||||
|
public SDL.SDL_GameControllerButton NegativeButton;
|
||||||
|
public SDL.SDL_GameControllerButton PositiveButton;
|
||||||
|
|
||||||
|
private float value;
|
||||||
|
private bool turned;
|
||||||
|
|
||||||
|
public this(SDL.SDL_GameControllerButton negativeButton, SDL.SDL_GameControllerButton positiveButton, OverlapBehaviors overlapBehavior = .TakeNewer)
|
||||||
|
{
|
||||||
|
NegativeButton = negativeButton;
|
||||||
|
PositiveButton = positiveButton;
|
||||||
|
OverlapBehavior = overlapBehavior;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update()
|
||||||
|
{
|
||||||
|
if (Game.GamepadButtonCheck(PositiveButton))
|
||||||
|
{
|
||||||
|
if (Game.GamepadButtonCheck(NegativeButton))
|
||||||
|
{
|
||||||
|
switch (OverlapBehavior)
|
||||||
|
{
|
||||||
|
case OverlapBehaviors.CancelOut:
|
||||||
|
value = 0;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OverlapBehaviors.TakeNewer:
|
||||||
|
if (!turned)
|
||||||
|
{
|
||||||
|
value *= -1;
|
||||||
|
turned = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case OverlapBehaviors.TakeOlder:
|
||||||
|
//value stays the same
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
turned = false;
|
||||||
|
value = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (Game.GamepadButtonCheck(NegativeButton))
|
||||||
|
{
|
||||||
|
turned = false;
|
||||||
|
value = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
turned = false;
|
||||||
|
value = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override float Value
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class GamepadAxis : Node
|
||||||
|
{
|
||||||
|
public SDL.SDL_GameControllerAxis Axis;
|
||||||
|
public float Deadzone;
|
||||||
|
|
||||||
|
public this(SDL.SDL_GameControllerAxis axis, float deadzone)
|
||||||
|
{
|
||||||
|
Axis = axis;
|
||||||
|
Deadzone = deadzone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override float Value
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
let val = Game.GamepadAxisCheck(Axis);
|
||||||
|
if (Math.Abs(val) < Deadzone)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,13 +85,13 @@ namespace Strawberry
|
|||||||
|
|
||||||
// Setup Calls
|
// Setup Calls
|
||||||
|
|
||||||
public VirtualButton Key(SDL.Scancode keycode)
|
public VirtualButton AddKey(SDL.Scancode keycode)
|
||||||
{
|
{
|
||||||
nodes.Add(new KeyboardKey(keycode));
|
nodes.Add(new KeyboardKey(keycode));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public VirtualButton Button(SDL.SDL_GameControllerButton button)
|
public VirtualButton AddButton(SDL.SDL_GameControllerButton button)
|
||||||
{
|
{
|
||||||
nodes.Add(new GamepadButton(button));
|
nodes.Add(new GamepadButton(button));
|
||||||
return this;
|
return this;
|
||||||
|
Loading…
Reference in New Issue
Block a user