1// Motion (decisions/F11.md, F11e): easing curves, numbers moving2// to their targets (`Animated`), the hover and press amounts elements3// paint with, and frames. While something moves the app draws one frame4// after another, each first marking what moves (the bindings reading a5// moving number, the elements easing or settling, rooms revealing), then6// running the bindings, laying out and painting, then moving the canvas7// nodes of what comes and goes (transitions.alloy); when nothing moves no8// frame is asked for.910import std::string;11import std::vector;12import std::option;13import std::math;14import std::time;15import pkg::alloy::window;16import pkg::alloy::draw;1718import ui;19import store;20import tree;21import layout;22import painting;23import layers;24import transitions;25import extensions;2627/* --- Easing --- */2829// How far along the easing is at `t` of its duration, from 0 to 1.30pub fn eased(self easing: &Easing, t: f32) -> f32 {31 const x = clamp(t, 0.0, 1.0);32 return match (easing) {33 ::Linear x;34 ::Standard cubic(0.2, 0.0, 0.0, 1.0, x);35 ::In cubic(0.42, 0.0, 1.0, 1.0, x);36 ::Out cubic(0.0, 0.0, 0.58, 1.0, x);37 ::InOut cubic(0.42, 0.0, 0.58, 1.0, x);38 ::Cubic |p| cubic(p[0], p[1], p[2], p[3], x);39 };40}4142// A cubic Bézier curve from (0, 0) to (1, 1) through the control points,43// as CSS's cubic-bezier: its y where its x is `x`, the curve's parameter44// found by Newton's method, else by halving.45fn cubic(x1: f32, y1: f32, x2: f32, y2: f32, x: f32) -> f32 {46 if (x <= 0.0 || x >= 1.0) {47 return x;48 }49 var t = x;50 for ([..8 to u64]) |n| {51 const error = bezier(x1, x2, t) - x;52 if (abs(error) < 0.00001) {53 return bezier(y1, y2, t);54 }55 const slope = bezier_slope(x1, x2, t);56 if (abs(slope) < 0.000001) {57 break;58 }59 t = clamp(t - error / slope, 0.0, 1.0);60 }61 var low: f32 = 0.0;62 var high: f32 = 1.0;63 t = x;64 for ([..30 to u64]) |n| {65 const at = bezier(x1, x2, t);66 if (abs(at - x) < 0.00001) {67 break;68 }69 if (at < x) {70 low = t;71 } else {72 high = t;73 }74 t = (low + high) / 2.0;75 }76 return bezier(y1, y2, t);77}7879// A coordinate of the curve at `t`, its two control points' being `a`80// and `b`.81fn bezier(a: f32, b: f32, t: f32) -> f32 {82 const u = 1.0 - t;83 return 3.0 * u * u * t * a + 3.0 * u * t * t * b + t * t * t;84}8586fn bezier_slope(a: f32, b: f32, t: f32) -> f32 {87 const u = 1.0 - t;88 return 3.0 * u * u * a + 6.0 * u * t * (b - a) + 3.0 * t * t * (1.0 - b);89}9091// How far along a motion is: eased, from 0 to 1, and whether it ended.92exp type Along = struct {93 exp at: f32,94 exp done: bool,95};9697// How far along the motion is `elapsed` nanoseconds after it started,98// its delay first; a repeated one goes round as its repeat says, back99// and forth when alternating.100exp fn along(self motion: &Motion, elapsed: u64) -> Along {101 const delay = motion.delay.nanoseconds;102 if (elapsed < delay) {103 return Along { .at = 0.0, .done = false };104 }105 const run = elapsed - delay;106 const span = motion.duration.nanoseconds;107 if (span == 0) {108 return Along { .at = 1.0, .done = true };109 }110 const rounds = run / span;111 const t = (run % span) to f32 / span to f32;112 return match (motion.repeat) {113 ::Once if (rounds >= 1) Along { .at = 1.0, .done = true } else Along {114 .at = motion.easing.eased(t),115 .done = false116 };117 ::Times |n| if (rounds >= n to u64) Along { .at = 1.0, .done = true } else Along {118 .at = motion.easing.eased(t),119 .done = false120 };121 ::Forever Along { .at = motion.easing.eased(t), .done = false };122 ::Alternate Along {123 .at = motion.easing.eased(if (rounds % 2 == 0) t else 1.0 - t),124 .done = false125 };126 };127}128129// The nanoseconds from `start` to `now`, none before it.130exp fn since(now: u64, start: u64) -> u64 => if (now > start) now - start else 0;131132/* --- Hovering and pressing --- */133134// An element whose hover or press eases toward its states since `at`,135// from the amounts it had then.136pub type Ease = struct {137 pub node: u32,138 pub generation: u32,139 pub hover: f32,140 pub press: f32,141 pub at: u64,142};143144// How hovered and pressed the node is now: its states, or on the way to145// them since they changed.146pub fn amounts(self ui: &Ui, index: u32) -> [f32 : 2] {147 const node = &ui.node(index);148 var hover: f32 = if (node.hovered) 1.0 else 0.0;149 var press: f32 = if (node.pressed) 1.0 else 0.0;150 for (ui.eases.view()) |&e| {151 if (e.node == index && e.generation == node.generation) {152 const span = ui.theme.motion.short.nanoseconds;153 const t = since(ui.time.nanoseconds, e.at) to f32 / max(span to f32, 1.0);154 const k = Easing::Standard.eased(t);155 hover = e.hover + (hover - e.hover) * k;156 press = e.press + (press - e.press) * k;157 }158 }159 return [hover, press];160}161162// Eases the node's hover and press from where they are now, its states163// about to change; at once before the first frame.164pub fn ease_states(self ui: &var Ui, index: u32) {165 if (!ui.drawn) {166 return;167 }168 const now = ui.amounts(index);169 const generation = ui.node(index).generation;170 const at = ui.time.nanoseconds;171 for (ui.eases.items()) |&var e| {172 if (e.node == index) {173 e = Ease {174 .node = index,175 .generation = generation,176 .hover = now[0],177 .press = now[1],178 .at = at179 };180 return;181 }182 }183 ui.eases.push(Ease {184 .node = index,185 .generation = generation,186 .hover = now[0],187 .press = now[1],188 .at = at189 });190}191192/* --- Frames --- */193194// What motion keeps once a program moves something (an extension, so a195// program moving nothing carries none of its code): the visuals of what196// comes and goes and the scrolls moving, marked as each frame begins and197// moved once it is painted.198pub type Visuals : Extension = struct {199 pub visuals: Vector<Visual> = Vector<Visual>::empty(),200 pub scrolls: Vector<ScrollMove> = Vector<ScrollMove>::empty(),201 // a room opens or closes this frame, moving what follows it by202 // layout, which `moves` then leaves alone203 pub revealing: bool = false,204};205206exp fn frame(self v: &var Visuals, ui: &var Ui) {207 ui.mark_visuals();208}209210pub fn painted(self v: &var Visuals, ui: &var Ui) -> bool {211 ui.move_visuals();212 return v.visuals.length() > 0 || v.scrolls.length() > 0;213}214215pub fn owner_going(self v: &var Visuals, ui: &var Ui, owner: u32) {216 ui.leave_owner(owner);217}218219pub fn placed(self v: &var Visuals, ui: &var Ui, index: u32) {220 ui.place_moving(index);221}222223// Motion's state, installed when first asked for.224pub fn motion_state(self ui: &var Ui) -> &var Visuals {225 if (ui.extension<Visuals>() is ::None) {226 ui.install(new Visuals {});227 }228 if (ui.extension_var<Visuals>() is ::Some |&var v|) {229 return &var v;230 }231 panic "pkg::alloy::ui: no motion";232}233234exp fn use_motion(self ui: &var Ui) {235 ui.motion_state();236}237238// Brings the Ui to a frame at its time: what moves marked, the bindings239// run and what changed laid out, then painted, then what comes and goes240// moved on the canvas.241exp fn frame(self ui: &var Ui) {242 ui.before_frame();243 ui.update_all();244 ui.paint_all();245 if (ui.extensions.length() > 0) {246 ui.moving = ui.extensions_painted();247 }248 ui.drawn = ui.scene is ::Some;249}250251// Marks what moves this frame: elements easing their hover or press, and252// those settling, painted again; then what motion's state holds.253fn before_frame(self ui: &var Ui) {254 const now = ui.time.nanoseconds;255 if (ui.eases.length() > 0) {256 const span = ui.theme.motion.short.nanoseconds;257 var k: u64 = 0;258 while (k < ui.eases.length()) {259 const e = ui.eases.get(k);260 const alive = ui.node(e.node).alive && ui.node(e.node).generation == e.generation;261 if (alive) {262 ui.mark_paint(e.node);263 }264 if (!alive || since(now, e.at) >= span) {265 ui.eases.swap_remove(k);266 } else {267 k += 1;268 }269 }270 }271 if (ui.settling.length() > 0) {272 var settling = move ui.settling;273 ui.settling = Vector<u32>::empty();274 for (settling.view()) |&index| {275 if (ui.node(index).alive) {276 ui.mark_paint(index);277 }278 }279 }280 ui.extensions_frame();281}282283// Whether something moves, which the app draws frames for.284exp fn animating(self ui: &Ui) -> bool {285 return ui.moving || ui.eases.length() > 0 || ui.settling.length() > 0;286}287288// The milliseconds on the window's clock, which an element's `changed`289// keeps.290pub fn clock_ms(self ui: &Ui) -> u32 => (ui.time.nanoseconds / 1000000) to u32;