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:
parent
a4c7a756f0
commit
d2b0413c30
9 changed files with 141 additions and 1 deletions
52
examples/library/ui_gamepad.ludic
Normal file
52
examples/library/ui_gamepad.ludic
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
# 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}`)
|
||||
}
|
||||
}
|
||||
7
examples/library/ui_gamepad_parts/Pad.ludic
Normal file
7
examples/library/ui_gamepad_parts/Pad.ludic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# Pad.ludic - eight buttons, a range, a select and a button that opens a popover, for a gamepad
|
||||
component Pad {
|
||||
state last: int = -1
|
||||
state vol: int = 0
|
||||
state q: int = 0
|
||||
state open: bool = false
|
||||
}
|
||||
8
examples/library/ui_gamepad_parts/Pad.xml
Normal file
8
examples/library/ui_gamepad_parts/Pad.xml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<div class="page">
|
||||
<each in="{range(8)}" as="r"><button on-click="set last = r">{r}</button></each>
|
||||
<input type="range" min="{0}" max="{10}" value="{vol}" on-change="set vol = event.value"/>
|
||||
<select value="{q}" on-change="set q = event.value"><option>a</option><option>b</option><option>c</option></select>
|
||||
<button on-click="set open = true">more</button>
|
||||
<if test="{open}"><div popover on-close="set open = false"><button>x</button></div></if>
|
||||
<p>last {last} vol {vol} q {q} open {open}</p>
|
||||
</div>
|
||||
Loading…
Add table
Add a link
Reference in a new issue