diff --git a/src/flixel/FlxG.hx b/src/flixel/FlxG.hx index 9ef5950..f32ded5 100644 --- a/src/flixel/FlxG.hx +++ b/src/flixel/FlxG.hx @@ -1,5 +1,6 @@ package flixel; +import flixel.input.FlxKeyManager; import raylib.Raylib; import flixel.system.FlxMemory; import flixel.FlxCamera; @@ -30,6 +31,8 @@ class FlxG { public static var sound(default, null):SoundFrontEnd = new SoundFrontEnd(); + public static var keys(default, null):FlxKeyManager; + public static function switchState(nextState:FlxState):Void { cameras.reset(); sound.destroy(true); @@ -57,6 +60,8 @@ class FlxG { initialWidth = width; initialHeight = height; cameras.reset(); + + keys = new FlxKeyManager(); } @:noCompletion diff --git a/src/flixel/input/FlxBaseKeyList.hx b/src/flixel/input/FlxBaseKeyList.hx new file mode 100644 index 0000000..0608501 --- /dev/null +++ b/src/flixel/input/FlxBaseKeyList.hx @@ -0,0 +1,26 @@ +package flixel.input; + +import raylib.KeyboardKey; + +class FlxBaseKeyList { + + public var state(default, null):FlxInputState; + + public function new(state:FlxInputState) { + this.state = state; + } + + public function check(key:KeyboardKey):Bool { + switch (this.state) { + case JUST_RELEASED: + return isKeyReleased(key); + case RELEASED: + return !isKeyDown(key); + case PRESSED: + return isKeyDown(key); + case JUST_PRESSED: + return isKeyPressed(key); + } + return false; + } +} \ No newline at end of file diff --git a/src/flixel/input/FlxInputState.hx b/src/flixel/input/FlxInputState.hx new file mode 100644 index 0000000..d723fb0 --- /dev/null +++ b/src/flixel/input/FlxInputState.hx @@ -0,0 +1,8 @@ +package flixel.input; + +enum abstract FlxInputState(Int) from Int { + var JUST_RELEASED:Int = -1; + var RELEASED:Int = 0; + var PRESSED:Int = 1; + var JUST_PRESSED:Int = 2; +} diff --git a/src/flixel/input/FlxKeyManager.hx b/src/flixel/input/FlxKeyManager.hx new file mode 100644 index 0000000..ab9eb1e --- /dev/null +++ b/src/flixel/input/FlxKeyManager.hx @@ -0,0 +1,20 @@ +package flixel.input; + +import flixel.input.keyboard.FlxKeyList; + +class FlxKeyManager { + public var pressed(default, null):FlxKeyList; + + public var justPressed(default, null):FlxKeyList; + + public var released(default, null):FlxKeyList; + + public var justReleased(default, null):FlxKeyList; + + public function new() { + pressed = new FlxKeyList(PRESSED); + justPressed = new FlxKeyList(JUST_PRESSED); + released = new FlxKeyList(RELEASED); + justReleased = new FlxKeyList(JUST_RELEASED); + } +} diff --git a/src/flixel/input/keyboard/FlxKeyList.hx b/src/flixel/input/keyboard/FlxKeyList.hx new file mode 100644 index 0000000..56b43a5 --- /dev/null +++ b/src/flixel/input/keyboard/FlxKeyList.hx @@ -0,0 +1,508 @@ +package flixel.input.keyboard; + +class FlxKeyList extends FlxBaseKeyList { + public var A(get, never):Bool; + + inline function get_A() + return check(KEY_A); + + public var B(get, never):Bool; + + inline function get_B() + return check(KEY_B); + + public var C(get, never):Bool; + + inline function get_C() + return check(KEY_C); + + public var D(get, never):Bool; + + inline function get_D() + return check(KEY_D); + + public var E(get, never):Bool; + + inline function get_E() + return check(KEY_E); + + public var F(get, never):Bool; + + inline function get_F() + return check(KEY_F); + + public var G(get, never):Bool; + + inline function get_G() + return check(KEY_G); + + public var H(get, never):Bool; + + inline function get_H() + return check(KEY_H); + + public var I(get, never):Bool; + + inline function get_I() + return check(KEY_I); + + public var J(get, never):Bool; + + inline function get_J() + return check(KEY_J); + + public var K(get, never):Bool; + + inline function get_K() + return check(KEY_K); + + public var L(get, never):Bool; + + inline function get_L() + return check(KEY_L); + + public var M(get, never):Bool; + + inline function get_M() + return check(KEY_M); + + public var N(get, never):Bool; + + inline function get_N() + return check(KEY_N); + + public var O(get, never):Bool; + + inline function get_O() + return check(KEY_O); + + public var P(get, never):Bool; + + inline function get_P() + return check(KEY_P); + + public var Q(get, never):Bool; + + inline function get_Q() + return check(KEY_Q); + + public var R(get, never):Bool; + + inline function get_R() + return check(KEY_R); + + public var S(get, never):Bool; + + inline function get_S() + return check(KEY_S); + + public var T(get, never):Bool; + + inline function get_T() + return check(KEY_T); + + public var U(get, never):Bool; + + inline function get_U() + return check(KEY_U); + + public var V(get, never):Bool; + + inline function get_V() + return check(KEY_V); + + public var W(get, never):Bool; + + inline function get_W() + return check(KEY_W); + + public var X(get, never):Bool; + + inline function get_X() + return check(KEY_X); + + public var Y(get, never):Bool; + + inline function get_Y() + return check(KEY_Y); + + public var Z(get, never):Bool; + + inline function get_Z() + return check(KEY_Z); + + public var ZERO(get, never):Bool; + + inline function get_ZERO() + return check(KEY_ZERO); + + public var ONE(get, never):Bool; + + inline function get_ONE() + return check(KEY_ONE); + + public var TWO(get, never):Bool; + + inline function get_TWO() + return check(KEY_TWO); + + public var THREE(get, never):Bool; + + inline function get_THREE() + return check(KEY_THREE); + + public var FOUR(get, never):Bool; + + inline function get_FOUR() + return check(KEY_FOUR); + + public var FIVE(get, never):Bool; + + inline function get_FIVE() + return check(KEY_FIVE); + + public var SIX(get, never):Bool; + + inline function get_SIX() + return check(KEY_SIX); + + public var SEVEN(get, never):Bool; + + inline function get_SEVEN() + return check(KEY_SEVEN); + + public var EIGHT(get, never):Bool; + + inline function get_EIGHT() + return check(KEY_EIGHT); + + public var NINE(get, never):Bool; + + inline function get_NINE() + return check(KEY_NINE); + + public var PAGEUP(get, never):Bool; + + inline function get_PAGEUP() + return check(KEY_PAGE_UP); + + public var PAGEDOWN(get, never):Bool; + + inline function get_PAGEDOWN() + return check(KEY_PAGE_DOWN); + + public var HOME(get, never):Bool; + + inline function get_HOME() + return check(KEY_HOME); + + public var END(get, never):Bool; + + inline function get_END() + return check(KEY_END); + + public var INSERT(get, never):Bool; + + inline function get_INSERT() + return check(KEY_INSERT); + + public var ESCAPE(get, never):Bool; + + inline function get_ESCAPE() + return check(KEY_ESCAPE); + + public var MINUS(get, never):Bool; + + inline function get_MINUS() + return check(KEY_MINUS); + + public var PLUS(get, never):Bool; + + inline function get_PLUS() + return check(KEY_EQUAL); + + public var DELETE(get, never):Bool; + + inline function get_DELETE() + return check(KEY_DELETE); + + public var BACKSPACE(get, never):Bool; + + inline function get_BACKSPACE() + return check(KEY_BACKSPACE); + + public var LBRACKET(get, never):Bool; + + inline function get_LBRACKET() + return check(KEY_LEFT_BRACKET); + + public var RBRACKET(get, never):Bool; + + inline function get_RBRACKET() + return check(KEY_RIGHT_BRACKET); + + public var BACKSLASH(get, never):Bool; + + inline function get_BACKSLASH() + return check(KEY_BACKSLASH); + + public var CAPSLOCK(get, never):Bool; + + inline function get_CAPSLOCK() + return check(KEY_CAPS_LOCK); + + public var SCROLL_LOCK(get, never):Bool; + + inline function get_SCROLL_LOCK() + return check(KEY_SCROLL_LOCK); + + public var NUMLOCK(get, never):Bool; + + inline function get_NUMLOCK() + return check(KEY_NUM_LOCK); + + public var SEMICOLON(get, never):Bool; + + inline function get_SEMICOLON() + return check(KEY_SEMICOLON); + + public var QUOTE(get, never):Bool; + + inline function get_QUOTE() + return check(KEY_APOSTROPHE); + + public var ENTER(get, never):Bool; + + inline function get_ENTER() + return check(KEY_ENTER); + + public var SHIFT(get, never):Bool; + + inline function get_SHIFT() + return check(KEY_LEFT_SHIFT) || check(KEY_RIGHT_SHIFT); + + public var COMMA(get, never):Bool; + + inline function get_COMMA() + return check(KEY_COMMA); + + public var PERIOD(get, never):Bool; + + inline function get_PERIOD() + return check(KEY_PERIOD); + + public var SLASH(get, never):Bool; + + inline function get_SLASH() + return check(KEY_SLASH); + + public var GRAVEACCENT(get, never):Bool; + + inline function get_GRAVEACCENT() + return check(KEY_GRAVE); + + public var CONTROL(get, never):Bool; + + inline function get_CONTROL() + return check(KEY_LEFT_CONTROL) || check(KEY_RIGHT_CONTROL); + + public var ALT(get, never):Bool; + + inline function get_ALT() + return check(KEY_LEFT_ALT) || check(KEY_RIGHT_ALT); + + public var SPACE(get, never):Bool; + + inline function get_SPACE() + return check(KEY_SPACE); + + public var UP(get, never):Bool; + + inline function get_UP() + return check(KEY_UP); + + public var DOWN(get, never):Bool; + + inline function get_DOWN() + return check(KEY_DOWN); + + public var LEFT(get, never):Bool; + + inline function get_LEFT() + return check(KEY_LEFT); + + public var RIGHT(get, never):Bool; + + inline function get_RIGHT() + return check(KEY_RIGHT); + + public var TAB(get, never):Bool; + + inline function get_TAB() + return check(KEY_TAB); + + public var WINDOWS(get, never):Bool; + + inline function get_WINDOWS() + return check(KEY_KB_MENU); + + public var MENU(get, never):Bool; + + inline function get_MENU() + return check(KEY_MENU); + + public var PRINTSCREEN(get, never):Bool; + + inline function get_PRINTSCREEN() + return check(KEY_PRINT_SCREEN); + + public var BREAK(get, never):Bool; + + inline function get_BREAK() + return check(KEY_PAUSE); + + public var F1(get, never):Bool; + + inline function get_F1() + return check(KEY_F1); + + public var F2(get, never):Bool; + + inline function get_F2() + return check(KEY_F2); + + public var F3(get, never):Bool; + + inline function get_F3() + return check(KEY_F3); + + public var F4(get, never):Bool; + + inline function get_F4() + return check(KEY_F4); + + public var F5(get, never):Bool; + + inline function get_F5() + return check(KEY_F5); + + public var F6(get, never):Bool; + + inline function get_F6() + return check(KEY_F6); + + public var F7(get, never):Bool; + + inline function get_F7() + return check(KEY_F7); + + public var F8(get, never):Bool; + + inline function get_F8() + return check(KEY_F8); + + public var F9(get, never):Bool; + + inline function get_F9() + return check(KEY_F9); + + public var F10(get, never):Bool; + + inline function get_F10() + return check(KEY_F10); + + public var F11(get, never):Bool; + + inline function get_F11() + return check(KEY_F11); + + public var F12(get, never):Bool; + + inline function get_F12() + return check(KEY_F12); + + public var NUMPADONE(get, never):Bool; + + inline function get_NUMPADONE() + return check(KEY_KP_1); + + public var NUMPADTWO(get, never):Bool; + + inline function get_NUMPADTWO() + return check(KEY_KP_2); + + public var NUMPADTHREE(get, never):Bool; + + inline function get_NUMPADTHREE() + return check(KEY_KP_3); + + public var NUMPADFOUR(get, never):Bool; + + inline function get_NUMPADFOUR() + return check(KEY_KP_4); + + public var NUMPADFIVE(get, never):Bool; + + inline function get_NUMPADFIVE() + return check(KEY_KP_5); + + public var NUMPADSIX(get, never):Bool; + + inline function get_NUMPADSIX() + return check(KEY_KP_6); + + public var NUMPADSEVEN(get, never):Bool; + + inline function get_NUMPADSEVEN() + return check(KEY_KP_7); + + public var NUMPADEIGHT(get, never):Bool; + + inline function get_NUMPADEIGHT() + return check(KEY_KP_8); + + public var NUMPADNINE(get, never):Bool; + + inline function get_NUMPADNINE() + return check(KEY_KP_9); + + public var NUMPADZERO(get, never):Bool; + + inline function get_NUMPADZERO() + return check(KEY_KP_0); + + public var NUMPADMINUS(get, never):Bool; + + inline function get_NUMPADMINUS() + return check(KEY_KP_SUBTRACT); + + public var NUMPADPLUS(get, never):Bool; + + inline function get_NUMPADPLUS() + return check(KEY_KP_ADD); + + public var NUMPADPERIOD(get, never):Bool; + + inline function get_NUMPADPERIOD() + return check(KEY_KP_DECIMAL); + + public var NUMPADMULTIPLY(get, never):Bool; + + inline function get_NUMPADMULTIPLY() + return check(KEY_KP_MULTIPLY); + + public var NUMPADSLASH(get, never):Bool; + + inline function get_NUMPADSLASH() + return check(KEY_KP_DIVIDE); + + public var NONE(get, never):Bool; + + inline function get_NONE() + return check(KEY_NULL); + + public var ANY(get, never):Bool; + + inline function get_ANY() + return !check(KEY_NULL); +} \ No newline at end of file diff --git a/test/src/OtherState.hx b/test/src/OtherState.hx index 8d7f3e1..521b93e 100644 --- a/test/src/OtherState.hx +++ b/test/src/OtherState.hx @@ -21,7 +21,7 @@ class OtherState extends FlxState { super.update(elapsed); bgColor = Raylib.colorFromHSV(Raylib.getTime() * 100, 1, 1); maurice.angle = Math.sin(Raylib.getTime()) * 10; - if (Raylib.isKeyPressed(KEY_SPACE)) { + if (FlxG.keys.justPressed.SPACE) { FlxG.switchState(new SoundState()); } } diff --git a/test/src/PlayState.hx b/test/src/PlayState.hx index 9d69841..1d3a3cc 100644 --- a/test/src/PlayState.hx +++ b/test/src/PlayState.hx @@ -34,7 +34,7 @@ class PlayState extends FlxState { scythe.x = FlxMath.lerp(scythe.x, Raylib.getMousePosition().x, elapsed * 10); scythe.y = FlxMath.lerp(scythe.y, Raylib.getMousePosition().y, elapsed * 10); - if (Raylib.isKeyPressed(KEY_SPACE)) { + if (FlxG.keys.justPressed.SPACE) { FlxG.switchState(new OtherState()); } } diff --git a/test/src/SoundState.hx b/test/src/SoundState.hx index fbe83a2..aa1adf5 100644 --- a/test/src/SoundState.hx +++ b/test/src/SoundState.hx @@ -17,15 +17,15 @@ class SoundState extends FlxState { override public function update(elapsed:Float) { super.update(elapsed); - if (Raylib.isKeyPressed(KEY_SPACE)) { + if (FlxG.keys.justPressed.SPACE) { FlxG.switchState(new TextState()); } - if (Raylib.isKeyDown(KEY_LEFT)) { + if (FlxG.keys.pressed.LEFT) { sound.pitch -= elapsed; } - if (Raylib.isKeyDown(KEY_RIGHT)) { + if (FlxG.keys.pressed.RIGHT) { sound.pitch += elapsed; }