PlatformLayer in, except rendering

This commit is contained in:
Matt Thorson
2020-08-03 19:09:45 -07:00
parent d736ded07e
commit 823656d8e5
14 changed files with 554 additions and 163 deletions

15
src/Input/Axes.bf Normal file
View File

@ -0,0 +1,15 @@
namespace Strawberry
{
enum Axes : int32
{
case Invalid = -1;
case LeftX;
case LeftY;
case RightX;
case RightY;
case TriggerLeft;
case TriggerRight;
public int Count => (int)TriggerRight + 1;
}
}

24
src/Input/Buttons.bf Normal file
View File

@ -0,0 +1,24 @@
namespace Strawberry
{
enum Buttons : int32
{
case Invalid = -1;
case A;
case B;
case X;
case Y;
case Back;
case Guide;
case Start;
case LeftStick;
case RightStick;
case LeftShoulder;
case RightShoulder;
case DpadUp;
case DpadDown;
case DpadLeft;
case DpadRight;
public int Count => (int)DpadRight + 1;
}
}

View File

@ -1,4 +1,3 @@
using SDL2;
using System;
using System.Diagnostics;
@ -6,81 +5,59 @@ namespace Strawberry
{
static public class Input
{
static private bool* keyboard;
static private bool[] previousKeyboard;
static private float[] lastKeypressTimes;
static private SDL.SDL_GameController*[] gamepads;
static private void Init(int gamepadLimit)
static private void Init()
{
keyboard = SDL.GetKeyboardState(null);
previousKeyboard = new bool[(int)SDL.Scancode.NUMSCANCODES];
lastKeypressTimes = new float[(int)SDL.Scancode.NUMSCANCODES];
gamepads = new SDL.SDL_GameController*[gamepadLimit];
for (let i < gamepads.Count)
gamepads[i] = SDL.GameControllerOpen((int32)i);
previousKeyboard = new bool[Keys.Count];
lastKeypressTimes = new float[Keys.Count];
}
static private void Dispose()
{
delete previousKeyboard;
delete lastKeypressTimes;
delete gamepads;
}
static public void BeforeUpdate()
{
SDL.PumpEvents();
SDL.GameControllerUpdate();
}
static public void AfterUpdate()
{
for (let i < previousKeyboard.Count)
{
if (!previousKeyboard[i] && keyboard[i])
if (!previousKeyboard[i] && Game.PlatformLayer.PollKey((Keys)i))
lastKeypressTimes[i] = Time.Elapsed;
previousKeyboard[i] = keyboard[i];
previousKeyboard[i] = Game.PlatformLayer.PollKey((Keys)i);
}
}
static public bool Ctrl => SDL.GetModState() & .CTRL > 0;
static public bool Alt => SDL.GetModState() & .ALT > 0;
static public bool Shift => SDL.GetModState() & .SHIFT > 0;
static public bool CapsLock => SDL.GetModState() & .Caps > 0;
static public bool NumLock => SDL.GetModState() & .Num > 0;
static public bool Ctrl => KeyCheck(Keys.LCtrl) || KeyCheck(Keys.RCtrl);
static public bool Alt => KeyCheck(Keys.LCtrl) || KeyCheck(Keys.RCtrl);
static public bool Shift => KeyCheck(Keys.LCtrl) || KeyCheck(Keys.RCtrl);
static public bool CapsLock => Game.PlatformLayer.CapsLock;
static public bool NumLock => Game.PlatformLayer.NumLock;
static public bool KeyCheck(SDL.Scancode key)
static public bool KeyCheck(Keys key)
{
Debug.Assert(keyboard != null, "Polling keyboard before Input.Init");
return keyboard[(int)key];
return Game.PlatformLayer.PollKey(key);
}
static public bool KeyPressed(SDL.Scancode key)
static public bool KeyPressed(Keys key)
{
Debug.Assert(keyboard != null, "Polling keyboard before Input.Init");
return keyboard[(int)key] && !previousKeyboard[(int)key];
return Game.PlatformLayer.PollKey(key) && !previousKeyboard[(int)key];
}
static public bool KeyPressed(SDL.Scancode key, float repeatDelay, float repeatInterval)
static public bool KeyPressed(Keys key, float repeatDelay, float repeatInterval)
{
Debug.Assert(keyboard != null, "Polling keyboard before Input.Init");
let i = (int)key;
if (keyboard[i])
return !previousKeyboard[i] || Time.OnInterval(repeatInterval, lastKeypressTimes[i] + repeatDelay);
if (Game.PlatformLayer.PollKey(key))
return !previousKeyboard[(int)key] || Time.OnInterval(repeatInterval, lastKeypressTimes[(int)key] + repeatDelay);
else
return false;
}
static public bool KeyReleased(SDL.Scancode key)
static public bool KeyReleased(Keys key)
{
Debug.Assert(keyboard != null, "Polling keyboard before Input.Init");
return !keyboard[(int)key] && previousKeyboard[(int)key];
return !Game.PlatformLayer.PollKey(key) && previousKeyboard[(int)key];
}
static public void KeystrokesIntoString(String toString, float keyRepeatDelay, float keyRepeatInterval)
@ -93,11 +70,11 @@ namespace Strawberry
KeystrokesIntoStringUtil(toString, scope => Input.KeyPressed);
}
static private void KeystrokesIntoStringUtil(String toString, delegate bool(SDL.Scancode) pressed)
static private void KeystrokesIntoStringUtil(String toString, delegate bool(Keys) pressed)
{
for (let i < (int)SDL2.SDL.Scancode.NUMSCANCODES)
for (let i < (int)Keys.Count)
{
let key = (SDL2.SDL.Scancode)i;
let key = (Keys)i;
if (pressed(key))
{
if (key >= .A && key <= .Z)
@ -192,28 +169,14 @@ namespace Strawberry
}
}
static public bool GamepadButtonCheck(int gamepadID, SDL.SDL_GameControllerButton button)
static public bool GamepadButtonCheck(int gamepadID, Buttons button)
{
Debug.Assert(gamepads != null, "Polling gamepad before Input.Init");
Debug.Assert(gamepadID < gamepads.Count, "Gamepad index out of range (increase Game.gamepadLimit!");
Debug.Assert(gamepadID >= 0, "Negative gamepad index!");
return SDL.GameControllerGetButton(gamepads[gamepadID], button) == 1;
return Game.PlatformLayer.PollGamepadButton(gamepadID, button);
}
static public float GamepadAxisCheck(int gamepadID, SDL.SDL_GameControllerAxis axis)
static public float GamepadAxisCheck(int gamepadID, Axes axis)
{
Debug.Assert(gamepads != null, "Polling gamepad before Input.Init");
Debug.Assert(gamepadID < gamepads.Count, "Gamepad index out of range (increase Game.gamepadLimit!");
Debug.Assert(gamepadID >= 0, "Negative gamepad index!");
let val = SDL.GameControllerGetAxis(gamepads[gamepadID], axis);
if (val == 0)
return 0;
else if (val > 0)
return val / 32767f;
else
return val / 32768f;
return Game.PlatformLayer.PollGamepadAxis(gamepadID, axis);
}
}
}

268
src/Input/Keys.bf Normal file
View File

@ -0,0 +1,268 @@
namespace Strawberry
{
public enum Keys : uint32
{
case Unknown = 0;
case A = 4;
case B = 5;
case C = 6;
case D = 7;
case E = 8;
case F = 9;
case G = 10;
case H = 11;
case I = 12;
case J = 13;
case K = 14;
case L = 15;
case M = 16;
case N = 17;
case O = 18;
case P = 19;
case Q = 20;
case R = 21;
case S = 22;
case T = 23;
case U = 24;
case V = 25;
case W = 26;
case X = 27;
case Y = 28;
case Z = 29;
case Key1 = 30;
case Key2 = 31;
case Key3 = 32;
case Key4 = 33;
case Key5 = 34;
case Key6 = 35;
case Key7 = 36;
case Key8 = 37;
case Key9 = 38;
case Key0 = 39;
case Return = 40;
case Escape = 41;
case BackSpace = 42;
case Tab = 43;
case Space = 44;
case Minus = 45;
case Equals = 46;
case LeftBracket = 47;
case RightBracket = 48;
case BackSlash = 49;
case NonUSHash = 50;
case Semicolon = 51;
case Apostrophe = 52;
case Grave = 53;
case Comma = 54;
case Period = 55;
case Slash = 56;
case CapsLock = 57;
case F1 = 58;
case F2 = 59;
case F3 = 60;
case F4 = 61;
case F5 = 62;
case F6 = 63;
case F7 = 64;
case F8 = 65;
case F9 = 66;
case F10 = 67;
case F11 = 68;
case F12 = 69;
case PrintScreen = 70;
case ScrollLock = 71;
case Pause = 72;
case Insert = 73;
case Home = 74;
case PageUp = 75;
case Delete = 76;
case End = 77;
case PageDown = 78;
case Right = 79;
case Left = 80;
case Down = 81;
case Up = 82;
case NumLockClear = 83;
case KpDivide = 84;
case KpMultiply = 85;
case KpMinus = 86;
case KpPlus = 87;
case KpEnter = 88;
case Kp1 = 89;
case Kp2 = 90;
case Kp3 = 91;
case Kp4 = 92;
case Kp5 = 93;
case Kp6 = 94;
case Kp7 = 95;
case Kp8 = 96;
case Kp9 = 97;
case Kp0 = 98;
case KpPeriod = 99;
case NonUSBackslash = 100;
case Application = 101;
case Power = 102;
case KpEquals = 103;
case F13 = 104;
case F14 = 105;
case F15 = 106;
case F16 = 107;
case F17 = 108;
case F18 = 109;
case F19 = 110;
case F20 = 111;
case F21 = 112;
case F22 = 113;
case F23 = 114;
case F24 = 115;
case Execute = 116;
case Help = 117;
case Menu = 118;
case Select = 119;
case Stop = 120;
case Again = 121;
case Undo = 122;
case Cut = 123;
case Copy = 124;
case Paste = 125;
case Find = 126;
case Mute = 127;
case VolumeUp = 128;
case VolumeDown = 129;
case LockingCapsLock = 130;
case LockingNumLock = 131;
case LockingScrollLock = 132;
case KpComma = 133;
case KpEqualsAS400 = 134;
case International1 = 135;
case International2 = 136;
case International3 = 137;
case International4 = 138;
case International5 = 139;
case International6 = 140;
case International7 = 141;
case International8 = 142;
case International9 = 143;
case Lang1 = 144;
case Lang2 = 145;
case Lang3 = 146;
case Lang4 = 147;
case Lang5 = 148;
case Lang6 = 149;
case Lang7 = 150;
case Lang8 = 151;
case Lang9 = 152;
case AltErase = 153;
case SysReq = 154;
case Cancel = 155;
case Clear = 156;
case Prior = 157;
case Return2 = 158;
case Separator = 159;
case Out = 160;
case Oper = 161;
case ClearAgain = 162;
case CrSel = 163;
case ExSel = 164;
case Kp00 = 176;
case Kp000 = 177;
case ThousandsSeparator = 178;
case DecimalSeparator = 179;
case CurrencyUnit = 180;
case CurrencySubUnit = 181;
case KpLeftParen = 182;
case KpRightParen = 183;
case KpLeftBrace = 184;
case KpRightBrace = 185;
case KpTab = 186;
case KpBackspace = 187;
case KPA = 188;
case KPB = 189;
case KPC = 190;
case KPD = 191;
case KPE = 192;
case KPF = 193;
case KpXor = 194;
case KpPower = 195;
case KpPercent = 196;
case KpLess = 197;
case KpGreater = 198;
case KpAmpersand = 199;
case KpdBlAmpersand = 200;
case KpVerticalBar = 201;
case KpDblVerticalBar = 202;
case KpColon = 203;
case KpHash = 204;
case KpSpace = 205;
case KpAt = 206;
case KpExclam = 207;
case KpMemstore = 208;
case KpMemRecall = 209;
case KpMemClear = 210;
case KpMemAdd = 211;
case KpMemSubtract = 212;
case KpMemMultiply = 213;
case KpMemDivide = 214;
case KpPlusMinus = 215;
case KpClear = 216;
case KpClearEntry = 217;
case KpBinary = 218;
case KpOctal = 219;
case KpDecimal = 220;
case KpHexadecimal = 221;
case LCtrl = 224;
case LShift = 225;
case LAlt = 226;
case LGui = 227;
case RCtrl = 228;
case RShift = 229;
case RAlt = 230;
case RGui = 231;
case Mode = 257;
// these come from the usb consumer page (0x0c)
case AudioNext = 258;
case AudioPrev = 259;
case AudioStop = 260;
case AudioPlay = 261;
case AudioMute = 262;
case MediaSelect = 263;
case Www = 264;
case Mail = 265;
case Calculator = 266;
case Computer = 267;
case AcSearch = 268;
case AcHome = 269;
case AcBack = 270;
case AcForward = 271;
case AcStop = 272;
case AcRefresh = 273;
case AcBookmarks = 274;
// these come from other sources; and are mostly mac related
case BrightnessDown = 275;
case BrightnessUp = 276;
case DisplaySwitch = 277;
case KbdIllumToggle = 278;
case KbdIllumDown = 279;
case KbdIllumUp = 280;
case Eject = 281;
case Sleep = 282;
case App1 = 283;
case App2 = 284;
static public int Count => 512;
}
}

View File

@ -1,6 +1,5 @@
using System.Collections;
using System;
using SDL2;
namespace Strawberry
{
@ -89,19 +88,19 @@ namespace Strawberry
// Setup Calls
public VirtualAxis AddKeys(SDL.Scancode negativeKey, SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
public VirtualAxis AddKeys(Keys negativeKey, Keys positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
{
nodes.Add(new KeyboardKeys(negativeKey, positiveKey, overlapBehavior));
return this;
}
public VirtualAxis AddButtons(int gamepadID, SDL.SDL_GameControllerButton negativeButton, SDL.SDL_GameControllerButton positiveButton, OverlapBehaviors overlapBehavior = .TakeNewer)
public VirtualAxis AddButtons(int gamepadID, Buttons negativeButton, Buttons positiveButton, OverlapBehaviors overlapBehavior = .TakeNewer)
{
nodes.Add(new GamepadButtons(gamepadID, negativeButton, positiveButton, overlapBehavior));
return this;
}
public VirtualAxis AddAxis(int gamepadID, SDL.SDL_GameControllerAxis axis, float deadzone)
public VirtualAxis AddAxis(int gamepadID, Axes axis, float deadzone)
{
nodes.Add(new GamepadAxis(gamepadID, axis, deadzone));
return this;
@ -137,24 +136,24 @@ namespace Strawberry
private class KeyboardKeys : Node
{
public OverlapBehaviors OverlapBehavior;
public SDL.Scancode NegativeKeycode;
public SDL.Scancode PositiveKeycode;
public Keys NegativeKey;
public Keys PositiveKey;
private float value;
private bool turned;
public this(SDL.Scancode negativeKey, SDL.Scancode positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
public this(Keys negativeKey, Keys positiveKey, OverlapBehaviors overlapBehavior = .TakeNewer)
{
NegativeKeycode = negativeKey;
PositiveKeycode = positiveKey;
NegativeKey = negativeKey;
PositiveKey = positiveKey;
OverlapBehavior = overlapBehavior;
}
public override void Update()
{
if (Input.KeyCheck(PositiveKeycode))
if (Input.KeyCheck(PositiveKey))
{
if (Input.KeyCheck(NegativeKeycode))
if (Input.KeyCheck(NegativeKey))
{
switch (OverlapBehavior)
{
@ -181,7 +180,7 @@ namespace Strawberry
value = 1;
}
}
else if (Input.KeyCheck(NegativeKeycode))
else if (Input.KeyCheck(NegativeKey))
{
turned = false;
value = -1;
@ -206,13 +205,13 @@ namespace Strawberry
{
public int GamepadID;
public OverlapBehaviors OverlapBehavior;
public SDL.SDL_GameControllerButton NegativeButton;
public SDL.SDL_GameControllerButton PositiveButton;
public Buttons NegativeButton;
public Buttons PositiveButton;
private float value;
private bool turned;
public this(int gamepadID, SDL.SDL_GameControllerButton negativeButton, SDL.SDL_GameControllerButton positiveButton, OverlapBehaviors overlapBehavior = .TakeNewer)
public this(int gamepadID, Buttons negativeButton, Buttons positiveButton, OverlapBehaviors overlapBehavior = .TakeNewer)
{
GamepadID = gamepadID;
NegativeButton = negativeButton;
@ -275,10 +274,10 @@ namespace Strawberry
private class GamepadAxis : Node
{
public int GamepadID;
public SDL.SDL_GameControllerAxis Axis;
public Axes Axis;
public float Deadzone;
public this(int gamepadID, SDL.SDL_GameControllerAxis axis, float deadzone)
public this(int gamepadID, Axes axis, float deadzone)
{
GamepadID = gamepadID;
Axis = axis;

View File

@ -1,7 +1,6 @@
using System.Collections;
using System;
using System.Diagnostics;
using SDL2;
namespace Strawberry
{
@ -85,19 +84,19 @@ namespace Strawberry
// Setup Calls
public VirtualButton AddKey(SDL.Scancode keycode)
public VirtualButton AddKey(Keys key)
{
nodes.Add(new KeyboardKey(keycode));
nodes.Add(new KeyboardKey(key));
return this;
}
public VirtualButton AddButton(int gamepadID, SDL.SDL_GameControllerButton button)
public VirtualButton AddButton(int gamepadID, Buttons button)
{
nodes.Add(new GamepadButton(gamepadID, button));
return this;
}
public VirtualButton AddAxis(int gamepadID, SDL.SDL_GameControllerAxis axis, float threshold, ThresholdConditions condition = .GreaterThan)
public VirtualButton AddAxis(int gamepadID, Axes axis, float threshold, ThresholdConditions condition = .GreaterThan)
{
nodes.Add(new GamepadAxis(gamepadID, axis, threshold, condition));
return this;
@ -131,18 +130,18 @@ namespace Strawberry
private class KeyboardKey : Node
{
public SDL.Scancode Keycode;
public Keys Key;
public this(SDL.Scancode keycode)
public this(Keys key)
{
Keycode = keycode;
Key = key;
}
override public bool Check
{
get
{
return Input.KeyCheck(Keycode);
return Input.KeyCheck(Key);
}
}
}
@ -150,9 +149,9 @@ namespace Strawberry
private class GamepadButton : Node
{
public int GamepadID;
public SDL.SDL_GameControllerButton Button;
public Buttons Button;
public this(int gamepadID, SDL.SDL_GameControllerButton button)
public this(int gamepadID, Buttons button)
{
GamepadID = gamepadID;
Button = button;
@ -170,11 +169,11 @@ namespace Strawberry
private class GamepadAxis : Node
{
public int GamepadID;
public SDL.SDL_GameControllerAxis Axis;
public Axes Axis;
public float Threshold;
public ThresholdConditions Condition;
public this(int gamepadID, SDL.SDL_GameControllerAxis axis, float threshold, ThresholdConditions condition = .GreaterThan)
public this(int gamepadID, Axes axis, float threshold, ThresholdConditions condition = .GreaterThan)
{
GamepadID = gamepadID;
Axis = axis;