aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/stopwatch.aup43
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/stopwatch.aup b/examples/stopwatch.aup
new file mode 100644
index 0000000..cd075b0
--- /dev/null
+++ b/examples/stopwatch.aup
@@ -0,0 +1,43 @@
+# A stopwatch. Time is an input: a timer ticks while a surface runs the app,
+# and each tick sees the time it happened (now); the views only show states.
+use vega
+
+record Lap {
+ at = 0
+}
+
+state running = 0
+state started = 0
+state elapsed = 0
+state laps: list of Lap
+state at: index of laps
+
+input toggle = key Space
+input lap = key KeyL
+input reset = key KeyR
+input tick = every 100 ms
+
+fn two(n) = if n < 10 { "0{n}" } else { "{n}" }
+fn tenths(ms) = "{floor(ms / 60000)}:{two(floor(ms / 1000) % 60)}.{floor(ms / 100) % 10}"
+
+on toggle.down {
+ if running { elapsed = now - started }
+ running = !running
+ if running { started = now - elapsed }
+}
+on tick { if running { elapsed = now - started } }
+on lap.down {
+ laps.append(Lap { at: elapsed })
+ at = laps.last
+}
+on reset.down { if !running { elapsed = 0 } }
+
+view title = "Stopwatch"
+view time = tenths(elapsed)
+view status = if running { "running" } else { "stopped" }
+
+view laps_list = each l in laps {
+ label = tenths(l.at)
+ tone = vega.when(l.position == laps.count - 1, vega.accent)
+}
+view laps_list_focus = at.position