Cleaned/improved input manager.
Added single frame actions for menus etc Cleaned up continuous actions and set both to be captured.
This commit is contained in:
+24
-17
@@ -4,35 +4,42 @@ import rl "vendor:raylib"
|
|||||||
|
|
||||||
// ------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
// Input Manager, captures input events and stores them into a struct.
|
// Input Manager, captures input events and stores them into a struct.
|
||||||
// Passing the struct down allows for components to use inputs independantly
|
// Passing the struct down allows for components to use inputs independently
|
||||||
// ------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
|
|
||||||
Input_Event :: struct {
|
Input_Event :: struct {
|
||||||
// Normalized and merged from keyboard + controller
|
// Continuous movement normalized
|
||||||
move_dir: rl.Vector2,
|
move_dir: rl.Vector2,
|
||||||
pause_pressed: bool,
|
|
||||||
|
// single frame actions UI and triggers
|
||||||
|
up_pressed: bool,
|
||||||
|
down_pressed: bool,
|
||||||
|
confirm_pressed: bool,
|
||||||
|
pause_pressed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
poll_input :: proc() -> Input_Event {
|
poll_input :: proc() -> Input_Event {
|
||||||
input_event := Input_Event{}
|
input_event := Input_Event{}
|
||||||
input_dir := rl.Vector2{0,0}
|
input_dir := rl.Vector2{0, 0}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// Keyboard
|
// Continuous Input ie movement code
|
||||||
// ------------------------------------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) { input_dir.y -= 1 }
|
if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) { input_dir.y -= 1 }
|
||||||
if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) { input_dir.y += 1 }
|
if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) { input_dir.y += 1 }
|
||||||
|
|
||||||
// single-fire == IsKeyPressed, held == IsKeyDown
|
|
||||||
input_event.pause_pressed = rl.IsKeyPressed(.P)
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------------
|
|
||||||
// Movement/direction needs update
|
|
||||||
// ------------------------------------------------------------------------------------------------------
|
|
||||||
if input_dir.x != 0 || input_dir.y != 0 {
|
if input_dir.x != 0 || input_dir.y != 0 {
|
||||||
input_dir = rl.Vector2Normalize(input_dir)
|
input_dir = rl.Vector2Normalize(input_dir)
|
||||||
}
|
}
|
||||||
input_event.move_dir = input_dir
|
input_event.move_dir = input_dir
|
||||||
|
|
||||||
return input_event
|
// --------------------------------------------------------------------------
|
||||||
|
// actions single press ie menu navigation etc
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
input_event.up_pressed = rl.IsKeyPressed(.W) || rl.IsKeyPressed(.UP)
|
||||||
|
input_event.down_pressed = rl.IsKeyPressed(.S) || rl.IsKeyPressed(.DOWN)
|
||||||
|
input_event.confirm_pressed = rl.IsKeyPressed(.ENTER) || rl.IsKeyPressed(.SPACE)
|
||||||
|
input_event.pause_pressed = rl.IsKeyPressed(.P)
|
||||||
|
|
||||||
|
return input_event
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user