ludic/examples/library/ui_gamepad.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

54 lines
2.3 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
state UiGamepadState {
now: float = 0.0
}
function clock(ui_gamepad_st: UiGamepadState) -> float { return ui_gamepad_st.now }
function frame(ui_st: mut UiState, i: UiInput) -> string {
ui_input(ui_st, i)
ui_show(ui_st, "Pad", null, 0.0, 0.0, 400.0, 600.0)
let page = ui_nodes(ui_st, "Pad", null).children[0]
return page.children[len(page.children) - 1].text
}
# `what` held for `secs` at `hz`, then let go
function hold(ui_gamepad_st: mut UiGamepadState, ui_st: mut UiState, what: string, secs: float, hz: int) -> string {
let frames = int(secs * float(hz) + 0.5)
for k in 0 .. frames + 1 {
ui_gamepad_st.now = ui_gamepad_st.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(ui_st, i)
}
ui_gamepad_st.now = ui_gamepad_st.now + 1.0 / float(hz)
return frame(ui_st, new UiInput)
}
function tap(ui_gamepad_st: mut UiGamepadState, ui_st: mut UiState, what: string) -> string { return hold(ui_gamepad_st, ui_st, what, 0.0, 60) }
entry (ui_gamepad_st: mut UiGamepadState, ui_st: mut UiState) {
ui_backend(ui_st, new UiBackend)
ui_clock(ui_st, fn clock)
frame(ui_st, new UiInput)
hold(ui_gamepad_st, ui_st, "down", 1.0, 240)
let fast = tap(ui_gamepad_st, ui_st, "a")
hold(ui_gamepad_st, ui_st, "down", 1.0, 60)
hold(ui_gamepad_st, ui_st, "up", 0.5, 60)
let slow = tap(ui_gamepad_st, ui_st, "a")
for k in 0 .. 8 { tap(ui_gamepad_st, ui_st, "down") }
let vol = hold(ui_gamepad_st, ui_st, "right", 0.5, 60)
tap(ui_gamepad_st, ui_st, "down")
let q = tap(ui_gamepad_st, ui_st, "left")
tap(ui_gamepad_st, ui_st, "down")
let open = tap(ui_gamepad_st, ui_st, "a")
let shut = tap(ui_gamepad_st, ui_st, "b")
print(`{fast} | {slow} | {vol} | {q} | {open} | {shut}`)
}
}