ludic/examples/library/ui_gamepad.ludic
2026-09-24 21:42:32 +03:00

52 lines
1.7 KiB
Text

# ui_gamepad.ludic — the pad and held keys, on the program's clock: a direction held presses once,
# again after 0.42 s and every 0.11 s after that, at any frame rate; A presses what has the focus,
# left and right step a range and a select, and B closes a popover.
import "ludic.ui"
import "ui_gamepad_parts/Pad.ludic"
program UiGamepad {
numbers float
var now: float = 0.0
function clock() -> float { return now }
function frame(i: UiInput) -> string {
ui_input(i)
ui_show("Pad", null, 0.0, 0.0, 400.0, 600.0)
let page = ui_nodes("Pad", null).children[0]
return page.children[len(page.children) - 1].text
}
# `what` held for `secs` at `hz`, then let go
function hold(what: string, secs: float, hz: int) -> string {
let frames = int(secs * float(hz) + 0.5)
for k in 0 .. frames + 1 {
now = now + 1.0 / float(hz)
let i = new UiInput
i.held_up = what == "up"
i.held_down = what == "down"
i.held_left = what == "left"
i.held_right = what == "right"
i.pad_a = what == "a"
i.pad_b = what == "b"
frame(i)
}
now = now + 1.0 / float(hz)
return frame(new UiInput)
}
function tap(what: string) -> string { return hold(what, 0.0, 60) }
entry {
ui_backend(new UiBackend)
ui_clock(fn clock)
frame(new UiInput)
hold("down", 1.0, 240)
let fast = tap("a")
hold("down", 1.0, 60)
hold("up", 0.5, 60)
let slow = tap("a")
for k in 0 .. 8 { tap("down") }
let vol = hold("right", 0.5, 60)
tap("down")
let q = tap("left")
tap("down")
let open = tap("a")
let shut = tap("b")
print(`{fast} | {slow} | {vol} | {q} | {open} | {shut}`)
}
}