1// Numbers moving to their targets (decisions/F11.md, F11e,2// `ui.animated`): a signal of a course, its moves added together so a new3// target keeps the speed, the bindings reading it marked as each frame4// begins while it moves.56import std::string;7import std::vector;8import std::option;9import std::math;10import std::time;1112import pkg::alloy::ui;13import pkg::alloy::ui::store;14import pkg::alloy::ui::tree;15import pkg::alloy::ui::layout;16import pkg::alloy::ui::painting;17import pkg::alloy::ui::events;18import pkg::alloy::ui::widgets;19import pkg::alloy::ui::fields;20import pkg::alloy::ui::scrolling;21import pkg::alloy::ui::running;22import pkg::alloy::ui::timers;23import pkg::alloy::ui::layers;24import pkg::alloy::ui::motion;25import pkg::alloy::ui::transitions;26import pkg::alloy::ui::languages;27import pkg::alloy::ui::extensions;28import pkg::alloy::ui::views;29import ui_app;3031// An animated number: its target, and the moves under way to it, each32// the distance it started behind the target and when, added together so33// a new target keeps the speed; a repeated motion's start and where it34// goes from.35pub type Course = struct {36 pub target: f32,37 pub motion: Motion,38 pub moves: Vector<Step> = Vector<Step>::empty(),39 pub from: f32 = 0.0,40 pub start: Option<u64> = ::None,41 pub listed: bool = false, // among the numbers moving42};4344pub type Step = struct {45 pub gap: f32,46 pub start: u64,47};4849// The moves added up past which they are gathered into one.50const STEPS: u64 = 4;5152pub fn value_at(self c: &Course, now: u64) -> f32 {53 if (c.start is ::Some |start|) {54 return c.from + (c.target - c.from) * c.motion.along(since(now, start)).at;55 }56 var value = c.target;57 for (c.moves.view()) |&step| {58 value -= step.gap * (1.0 - c.motion.along(since(now, step.start)).at);59 }60 return value;61}6263// Whether the number still moves at the time.64pub fn moves_at(self c: &Course, now: u64) -> bool {65 if (c.start is ::Some |start|) {66 return !c.motion.along(since(now, start)).done;67 }68 for (c.moves.view()) |&step| {69 if (!c.motion.along(since(now, step.start)).done) {70 return true;71 }72 }73 return false;74}7576pub fn make_animated(self ui: &var Ui, value: f32, motion: Motion) -> Animated {77 ui.animations();78 return Animated {79 .course = ui.signal(Course { .target = value, .motion = motion, .from = value })80 };81}8283pub fn animate_to(self ui: &var Ui, course: Signal<Course>, target: f32) {84 const motion = course.peek(&ui).motion;85 if (ui.reduced_motion() && !motion.essential) {86 ui.jump_to(course, target);87 return;88 }89 const now = ui.time.nanoseconds;90 course.update(&var ui, |now, target| (c: &var Course) {91 const value = c.value_at(now);92 if (c.motion.repeat is ::Once) {93 var k: u64 = 0;94 while (k < c.moves.length()) {95 if (c.motion.along(since(now, c.moves.get(k).start)).done) {96 c.moves.remove(k);97 } else {98 k += 1;99 }100 }101 if (c.moves.length() >= STEPS) {102 c.moves.clear();103 c.moves.push(Step { .gap = target - value, .start = now });104 } else {105 c.moves.push(Step { .gap = target - c.target, .start = now });106 }107 } else {108 c.from = value;109 c.start = ::Some(now);110 }111 c.target = target;112 });113 if (!course.peek(&ui).listed) {114 course.peek_var(&var ui).listed = true;115 ui.animations().values.push(course);116 }117 ui.moving = true;118}119120pub fn jump_to(self ui: &var Ui, course: Signal<Course>, value: f32) {121 course.update(&var ui, |value| (c: &var Course) {122 c.target = value;123 c.from = value;124 c.start = ::None;125 c.moves.clear();126 });127}128129// Marks the bindings reading the numbers moving, which run this frame,130// forgetting those that came to rest or went.131fn mark_values(self ui: &var Ui) {132 const now = ui.time.nanoseconds;133 var values = move ui.animations().values;134 ui.animations().values = Vector<Signal<Course>>::empty();135 for (values.view()) |&course| {136 if (!ui.signal_alive(course.index, course.generation)) {137 continue;138 }139 ui.changed_signal(course.index);140 if (course.peek(&ui).moves_at(now)) {141 ui.animations().values.push(course);142 } else {143 course.peek_var(&var ui).listed = false;144 }145 }146}147148// The numbers moving (`animated`), marked as each frame begins.149pub type Animations : Extension = struct {150 pub values: Vector<Signal<Course>> = Vector<Signal<Course>>::empty(),151};152153pub fn frame(self a: &var Animations, ui: &var Ui) {154 ui.mark_values();155}156157pub fn painted(self a: &var Animations, ui: &var Ui) -> bool => a.values.length() > 0;158159pub fn animations(self ui: &var Ui) -> &var Animations {160 if (ui.extension<Animations>() is ::None) {161 ui.install(new Animations {});162 }163 if (ui.extension_var<Animations>() is ::Some |&var a|) {164 return &var a;165 }166 panic "pkg::alloy::ui: no animations";167}