ludic/packages/ludic.ui/transition.ludic

28 lines
1 KiB
Text

# transition.ludic - `transition: opacity 0.2s`: when an element's opacity changes from one frame to
# the next it eases to the new value over that time instead of jumping (opacity is the property a
# game fades most; the others arrive as they are)
function tr_apply(ui_st: mut UiState, n: UiNode) -> void {
if n.tr_dur <= 0.0 { return }
let now = an_now(ui_st)
let want = n.alpha
for i in 0 .. len(ui_st.tr_keys) {
if ui_st.tr_keys[i] == n.key {
if ui_st.tr_to[i] != want {
ui_st.tr_from[i] = tr_value(ui_st, i, now, n.tr_dur)
ui_st.tr_to[i] = want
ui_st.tr_at[i] = now
}
n.alpha = tr_value(ui_st, i, now, n.tr_dur)
return
}
}
ui_kept(ui_st.tr_keys, n.key)
ui_kept(ui_st.tr_from, want)
ui_kept(ui_st.tr_to, want)
ui_kept(ui_st.tr_at, now)
}
function tr_value(ui_st: UiState, i: int, now: float, dur: float) -> float {
let t = Math.clamp((now - ui_st.tr_at[i]) / dur, 0.0, 1.0)
if t >= 1.0 { return ui_st.tr_to[i] }
return ui_st.tr_from[i] + (ui_st.tr_to[i] - ui_st.tr_from[i]) * t
}