mirror of
https://github.com/MaddyThorson/StrawberryBF.git
synced 2025-08-17 05:40:40 +08:00
Separated Input into its own static class
This commit is contained in:
91
src/Input/Input.bf
Normal file
91
src/Input/Input.bf
Normal file
@ -0,0 +1,91 @@
|
||||
using SDL2;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Strawberry
|
||||
{
|
||||
static public class Input
|
||||
{
|
||||
static private bool* keyboard;
|
||||
static private bool[] previousKeyboard;
|
||||
static private SDL.SDL_GameController*[] gamepads;
|
||||
|
||||
static public void Init(int gamepadLimit)
|
||||
{
|
||||
keyboard = SDL.GetKeyboardState(null);
|
||||
previousKeyboard = new bool[(int)SDL.Scancode.NUMSCANCODES];
|
||||
|
||||
gamepads = new SDL.SDL_GameController*[gamepadLimit];
|
||||
for (let i < gamepads.Count)
|
||||
gamepads[i] = SDL.GameControllerOpen((int32)i);
|
||||
}
|
||||
|
||||
static public void Dispose()
|
||||
{
|
||||
delete previousKeyboard;
|
||||
delete gamepads;
|
||||
}
|
||||
|
||||
static public void BeforeUpdate()
|
||||
{
|
||||
SDL.PumpEvents();
|
||||
SDL.GameControllerUpdate();
|
||||
}
|
||||
|
||||
static public void AfterUpdate()
|
||||
{
|
||||
for (let i < previousKeyboard.Count)
|
||||
previousKeyboard[i] = keyboard[i];
|
||||
}
|
||||
|
||||
static public bool KeyCheck(SDL.Scancode key)
|
||||
{
|
||||
if (keyboard == null)
|
||||
Debug.FatalError("Polling keyboard before Input.Init");
|
||||
|
||||
return keyboard[(int)key];
|
||||
}
|
||||
|
||||
static public bool KeyPressed(SDL.Scancode key)
|
||||
{
|
||||
if (keyboard == null)
|
||||
Debug.FatalError("Polling keyboard before Input.Init");
|
||||
|
||||
return keyboard[(int)key] && !previousKeyboard[(int)key];
|
||||
}
|
||||
|
||||
static public bool KeyReleased(SDL.Scancode key)
|
||||
{
|
||||
if (keyboard == null)
|
||||
Debug.FatalError("Polling keyboard before Input.Init");
|
||||
|
||||
return !keyboard[(int)key] && previousKeyboard[(int)key];
|
||||
}
|
||||
|
||||
static public bool GamepadButtonCheck(int gamepadID, SDL.SDL_GameControllerButton button)
|
||||
{
|
||||
if (gamepads == null)
|
||||
Debug.FatalError("Polling gamepad before Input.Init");
|
||||
if (gamepadID >= gamepads.Count)
|
||||
Debug.FatalError("Gamepad index out of range (increase Game.gamepadLimit!");
|
||||
|
||||
return SDL.GameControllerGetButton(gamepads[gamepadID], button) == 1;
|
||||
}
|
||||
|
||||
static public float GamepadAxisCheck(int gamepadID, SDL.SDL_GameControllerAxis axis)
|
||||
{
|
||||
if (gamepads == null)
|
||||
Debug.FatalError("Polling gamepad before Input.Init");
|
||||
if (gamepadID >= gamepads.Count)
|
||||
Debug.FatalError("Gamepad index out of range (increase Game.gamepadLimit!");
|
||||
|
||||
let val = SDL.GameControllerGetAxis(gamepads[gamepadID], axis);
|
||||
if (val == 0)
|
||||
return 0;
|
||||
else if (val > 0)
|
||||
return val / 32767f;
|
||||
else
|
||||
return val / 32768f;
|
||||
}
|
||||
}
|
||||
}
|
@ -152,9 +152,9 @@ namespace Strawberry
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (Game.KeyCheck(PositiveKeycode))
|
||||
if (Input.KeyCheck(PositiveKeycode))
|
||||
{
|
||||
if (Game.KeyCheck(NegativeKeycode))
|
||||
if (Input.KeyCheck(NegativeKeycode))
|
||||
{
|
||||
switch (OverlapBehavior)
|
||||
{
|
||||
@ -181,7 +181,7 @@ namespace Strawberry
|
||||
value = 1;
|
||||
}
|
||||
}
|
||||
else if (Game.KeyCheck(NegativeKeycode))
|
||||
else if (Input.KeyCheck(NegativeKeycode))
|
||||
{
|
||||
turned = false;
|
||||
value = -1;
|
||||
@ -222,9 +222,9 @@ namespace Strawberry
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
if (Game.GamepadButtonCheck(GamepadID, PositiveButton))
|
||||
if (Input.GamepadButtonCheck(GamepadID, PositiveButton))
|
||||
{
|
||||
if (Game.GamepadButtonCheck(GamepadID, NegativeButton))
|
||||
if (Input.GamepadButtonCheck(GamepadID, NegativeButton))
|
||||
{
|
||||
switch (OverlapBehavior)
|
||||
{
|
||||
@ -251,7 +251,7 @@ namespace Strawberry
|
||||
value = 1;
|
||||
}
|
||||
}
|
||||
else if (Game.GamepadButtonCheck(GamepadID, NegativeButton))
|
||||
else if (Input.GamepadButtonCheck(GamepadID, NegativeButton))
|
||||
{
|
||||
turned = false;
|
||||
value = -1;
|
||||
@ -289,7 +289,7 @@ namespace Strawberry
|
||||
{
|
||||
get
|
||||
{
|
||||
let val = Game.GamepadAxisCheck(GamepadID, Axis);
|
||||
let val = Input.GamepadAxisCheck(GamepadID, Axis);
|
||||
if (Math.Abs(val) < Deadzone)
|
||||
return 0;
|
||||
else
|
||||
|
@ -142,7 +142,7 @@ namespace Strawberry
|
||||
{
|
||||
get
|
||||
{
|
||||
return Game.KeyCheck(Keycode);
|
||||
return Input.KeyCheck(Keycode);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -162,7 +162,7 @@ namespace Strawberry
|
||||
{
|
||||
get
|
||||
{
|
||||
return Game.GamepadButtonCheck(GamepadID, Button);
|
||||
return Input.GamepadButtonCheck(GamepadID, Button);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -187,9 +187,9 @@ namespace Strawberry
|
||||
get
|
||||
{
|
||||
if (Condition == .GreaterThan)
|
||||
return Game.GamepadAxisCheck(GamepadID, Axis) >= Threshold;
|
||||
return Input.GamepadAxisCheck(GamepadID, Axis) >= Threshold;
|
||||
else
|
||||
return Game.GamepadAxisCheck(GamepadID, Axis) <= Threshold;
|
||||
return Input.GamepadAxisCheck(GamepadID, Axis) <= Threshold;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user