ludic/examples/library/ui_ease.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

57 lines
2.4 KiB
Text

# ui_ease.ludic — easing never depends on the frame rate: a transition and an animation stepped at
# 60, 120, 180 and 240 Hz on the program's clock are at the same value half way (kept in float, never
# cut to whole pixels) and land exactly on their targets when their time is up.
import "ludic.ui"
program UiEase {
numbers float
state UiEaseState {
dim: bool = false
now: float = 0.0
}
view Ease (ui_ease_st: UiEaseState) {
dim = ui_ease_st.dim
}
const STYLE: string = "<style>@keyframes slide { from { left: 0 } to { left: 37 } }\n.go { animation: slide 0.25s }\n.soft { transition: opacity 0.25s }\n.dim { opacity: 0.2 }</style>"
const BODY: string = "<p class='go'>a</p><p class=\"soft {dim ? 'dim' : ''}\">b</p><p class='dim'>c</p></screen>"
function clock(ui_ease_st: UiEaseState) -> float { return ui_ease_st.now }
function nodes(ui_st: mut UiState, name: string) -> UiNode {
ui_show(ui_st, name, view_ease(), 0.0, 0.0, 200.0, 200.0)
return ui_nodes(ui_st, name, view_ease())
}
# one rate, on its own screen: the slide from its first frame, the fade from half a second in
function at_rate(ui_ease_st: mut UiEaseState, ui_st: mut UiState, hz: int) -> string {
let name = `r{hz}`
ui_ease_st.dim = false
var mid = ""
var slid = false
var landed = false
var over = false
for k in 0 .. hz + 1 {
ui_ease_st.now = float(k) / float(hz)
if k == hz / 2 { ui_ease_st.dim = true }
let r = nodes(ui_st, name)
let go = r.children[0]
let soft = r.children[1]
let want = r.children[2].alpha
if k == hz / 10 { mid = `{int(Math.round(go.left * 100.0))} ` }
if k == hz / 2 + hz / 10 { mid = mid + `{int(Math.round(soft.alpha * 1000.0))}` }
if k == hz / 4 { slid = go.left == 37.0 }
if k == hz / 2 + hz / 4 { landed = soft.alpha == want }
if k > hz / 2 + hz / 4 and soft.alpha != want { over = true }
}
return `{hz}: {mid} slid {slid} landed {landed} left {over}`
}
entry (ui_ease_st: mut UiEaseState, ui_st: mut UiState) {
ui_backend(ui_st, new UiBackend)
ui_clock(ui_st, fn clock)
var page = "<ui>" + STYLE
for i in 0 .. 4 {
let hz = 60 * (i + 1)
page = page + `<screen name='r{hz}'>` + BODY
}
ui_load_text(ui_st, page + "</ui>", "e.xml")
var out = ""
for i in 0 .. 4 { out = out + at_rate(ui_ease_st, ui_st, 60 * (i + 1)) + "; " }
print(out)
}
}