Skip to content

Commit

Permalink
add keyboard input
Browse files Browse the repository at this point in the history
  • Loading branch information
Vortex2Oblivion committed Jan 9, 2025
1 parent b1b6d07 commit 00a19a7
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/flixel/FlxG.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package flixel;

import flixel.input.FlxKeyManager;
import raylib.Raylib;
import flixel.system.FlxMemory;
import flixel.FlxCamera;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -57,6 +60,8 @@ class FlxG {
initialWidth = width;
initialHeight = height;
cameras.reset();

keys = new FlxKeyManager();
}

@:noCompletion
Expand Down
26 changes: 26 additions & 0 deletions src/flixel/input/FlxBaseKeyList.hx
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 8 additions & 0 deletions src/flixel/input/FlxInputState.hx
Original file line number Diff line number Diff line change
@@ -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;
}
20 changes: 20 additions & 0 deletions src/flixel/input/FlxKeyManager.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 00a19a7

Please sign in to comment.