feat(ui): the gamepad moves the focus, presses and goes back; a held direction repeats on the clock

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-24 21:42:32 +03:00
parent a4c7a756f0
commit d2b0413c30
9 changed files with 141 additions and 1 deletions

View file

@ -21,6 +21,7 @@ import "controls_parts.ludic"
import "controls_key.ludic"
import "controls_number.ludic"
import "input.ludic"
import "input_pad.ludic"
import "focus.ludic"
import "scroll.ludic"
import "scroll_bar.ludic"

View file

@ -1,5 +1,6 @@
# input.ludic - what the player did this frame, read once from the runtime's Input: the pointer, its
# button, the wheel, the keys a UI answers and what was typed. A test or a host can set it instead.
# button, the wheel, the keys a UI answers, the first pad and what was typed. A test or a host can set
# it instead (input_pad.ludic makes a held direction repeat).
export property UiInput {
x: float = -1.0
y: float = -1.0
@ -17,6 +18,12 @@ export property UiInput {
backspace: bool = false
typed: string = ""
key: int = 0 # any key pressed this frame, for a key-capture field
held_up: bool = false # a direction held - an arrow key, the d-pad or the left stick -
held_down: bool = false # which presses its arrow now and repeats while it is held
held_left: bool = false
held_right: bool = false
pad_a: bool = false # the pad's south button held: Enter
pad_b: bool = false # its east button held: Escape
}
var in_now: UiInput = new UiInput
var in_was_down: bool = false
@ -45,7 +52,9 @@ function in_read() -> void {
i.backspace = Input.key_pressed(Key.Backspace)
i.typed = Input.text()
i.key = in_any_key()
in_held(i)
}
in_repeat(i)
in_now = i
}
function in_any_key() -> int {
@ -57,6 +66,7 @@ function in_any_key() -> int {
# this frame's input, given by a test or a host that reads its own
export function ui_input(i: UiInput) -> void {
in_was_down = in_now.down
in_repeat(i)
in_now = i
in_given = true
}

View file

@ -0,0 +1,53 @@
# input_pad.ludic - the first gamepad, and a held direction: the d-pad and the left stick move the
# focus as the arrows do, A is Enter and B is Escape; any direction held presses once, then again
# after 0.42 s and every 0.11 s after that, on the ui clock
const RP_FIRST: float = 0.42
const RP_EVERY: float = 0.11
const PAD_STICK: float = 0.5
var rp_next: []float = [-1.0, -1.0, -1.0, -1.0]
var rp_a: bool = false
var rp_b: bool = false
# what is held this frame: the arrow keys, the d-pad (SDL's order: 11 up, 12 down, 13 left, 14
# right) and the stick past half way along its larger axis
function in_held(i: UiInput) -> void {
i.held_up = Input.key_down(Key.Up)
i.held_down = Input.key_down(Key.Down)
i.held_left = Input.key_down(Key.Left)
i.held_right = Input.key_down(Key.Right)
if not Input.pad_connected(0) { return }
let ax = float(Input.pad_axis(0, 0))
let ay = float(Input.pad_axis(0, 1))
let flat = Math.abs(ax) >= Math.abs(ay)
i.held_up = i.held_up or Input.pad_button(0, 11) or (not flat and ay < 0.0 - PAD_STICK)
i.held_down = i.held_down or Input.pad_button(0, 12) or (not flat and ay > PAD_STICK)
i.held_left = i.held_left or Input.pad_button(0, 13) or (flat and ax < 0.0 - PAD_STICK)
i.held_right = i.held_right or Input.pad_button(0, 14) or (flat and ax > PAD_STICK)
i.pad_a = Input.pad_button(0, 0)
i.pad_b = Input.pad_button(0, 1)
}
# the held directions and buttons as this frame's presses
function in_repeat(i: UiInput) -> void {
let now = an_now()
if rp_fire(0, i.held_up, now) { i.up = true }
if rp_fire(1, i.held_down, now) { i.down_key = true }
if rp_fire(2, i.held_left, now) { i.left = true }
if rp_fire(3, i.held_right, now) { i.right = true }
if i.pad_a and not rp_a { i.enter = true }
if i.pad_b and not rp_b { i.escape = true }
rp_a = i.pad_a
rp_b = i.pad_b
}
function rp_fire(k: int, held: bool, now: float) -> bool {
if not held {
rp_next[k] = -1.0
return false
}
if rp_next[k] < 0.0 {
rp_next[k] = now + RP_FIRST
return true
}
if now + 0.000001 < rp_next[k] { return false }
rp_next[k] = rp_next[k] + RP_EVERY
if rp_next[k] <= now { rp_next[k] = now + RP_EVERY }
return true
}