scheme/app/stopwatch.scm · 5230 字节 · 下载原文
#include <vml_ui.scm>
; 秒表 —— 用 **Scheme** 写的手机小应用(计时)
; Stopwatch -- a small phone app written in **Scheme** (timing)
;
; 功能:点左半屏开始 / 暂停,点右半屏清零;读数 `分:秒.百分秒`。
; 计时驱动用定时器拍数(每 50ms 一拍,20 拍 = 1 秒)。
;
; 本前端的写法要点(与 ui/demo_ui.scm 同一套):
; · **用户函数里不能调库函数**(尾调用优化跳过序言)⇒ 所有 `ui_*` 调用写顶层;
; · `digit` 这种**纯返回字面量**的小函数可以定义(不调库);
; · 没有 `remainder` / `quotient` / `%` ⇒ 取模手算 `(- i (* (/ i n) n))`;
; · 没有 `break` ⇒ 退出靠 `do` 的终止条件 `(= quit 1)`。
;
; 跑法 —— 手机:vml run examples/scheme/stopwatch.scm
; 跑法 —— 桌面:vmlcli Examples/scheme/stopwatch.scm --screen 480x640 --frames /tmp/fr
(define c_bg -15724520)
(define c_txt -1513232)
(define c_dim -6643536)
(define c_ok -11409298)
(define c_no -2069424)
(define c_blue -11890471)
(define (digit n)
(if (= n 0) "0"
(if (= n 1) "1"
(if (= n 2) "2"
(if (= n 3) "3"
(if (= n 4) "4"
(if (= n 5) "5"
(if (= n 6) "6"
(if (= n 7) "7"
(if (= n 8) "8" "9"))))))))))
(define w 360)
(define h 620)
(define s 240)
(define margin 6)
(define titlefs 12)
(define timefs 40)
(define btnfs 12)
(define hintfs 10)
(define btnw 100)
(define btnh 30)
(define btnY 190)
(define titleY 20)
(define timeY 110)
(define tx 0)
(define running 0)
(define acc 0)
(define frames 0)
(define tid 0)
(define quit 0)
(define t 0)
(define sec 0)
(define min 0)
(define cs 0)
(define ax 0)
(define key 0)
(ui_win_open "秒表" 0 0)
(ui_scr_set_keep_on 1)
(set! w (ui_scr_get_w))
(set! h (ui_scr_get_h))
(if (< w 1) (set! w 360) 0)
(if (< h 1) (set! h 620) 0)
; —— 布局按实际画布现算:标题 / 读数 / 两个按钮 / 底部提示,字号随 min(w,h) 缩放 ——
(set! s (if (< w h) w h))
(set! margin (/ s 40))
(if (< margin 4) (set! margin 4) 0)
(if (> margin 24) (set! margin 24) 0)
(set! titlefs (/ s 20))
(if (< titlefs 12) (set! titlefs 12) 0)
(if (> titlefs 32) (set! titlefs 32) 0)
(set! timefs (/ s 6))
(if (< timefs 28) (set! timefs 28) 0)
(if (> timefs 96) (set! timefs 96) 0)
(set! btnfs (/ s 18))
(if (< btnfs 12) (set! btnfs 12) 0)
(if (> btnfs 30) (set! btnfs 30) 0)
(set! hintfs (/ s 24))
(if (< hintfs 9) (set! hintfs 9) 0)
(if (> hintfs 22) (set! hintfs 22) 0)
(set! titleY (+ margin titlefs))
(set! timeY (+ (/ (* h 2) 5) (/ (* timefs 7) 20)))
(set! btnh (/ s 8))
(if (< btnh 28) (set! btnh 28) 0)
(if (> btnh 72) (set! btnh 72) 0)
(set! btnY (- h hintfs (* margin 2) btnh))
(set! btnw (/ (- w (* 3 margin)) 2))
(if (< btnw 60) (set! btnw 60) 0)
(set! tid (ui_timer_set 50 0))
(do ()
((= quit 1))
(set! sec (/ acc 20))
(set! min (/ sec 60))
(set! sec (- sec (* min 60)))
(set! cs (* (- acc (* (/ acc 20) 20)) 5))
(ui_2d_clear c_bg)
(ui_2d_text (- (/ w 2) titlefs) titleY "秒表" c_dim titlefs 0)
(set! tx (- (/ w 2) (/ (* timefs 9) 4)))
(ui_2d_text tx timeY (digit (/ min 10)) c_txt timefs 0)
(set! tx (+ tx (/ (* timefs 3) 5)))
(ui_2d_text tx timeY (digit (- min (* (/ min 10) 10))) c_txt timefs 0)
(set! tx (+ tx (/ (* timefs 3) 5)))
(ui_2d_text tx timeY ":" c_dim timefs 0)
(set! tx (+ tx (/ (* timefs 9) 20)))
(ui_2d_text tx timeY (digit (/ sec 10)) c_txt timefs 0)
(set! tx (+ tx (/ (* timefs 3) 5)))
(ui_2d_text tx timeY (digit (- sec (* (/ sec 10) 10))) c_txt timefs 0)
(set! tx (+ tx (/ (* timefs 3) 5)))
(ui_2d_text tx timeY "." c_dim timefs 0)
(set! tx (+ tx (/ (* timefs 9) 20)))
(ui_2d_text tx timeY (digit (/ cs 10)) c_txt timefs 0)
(set! tx (+ tx (/ (* timefs 3) 5)))
(ui_2d_text tx timeY (digit (- cs (* (/ cs 10) 10))) c_txt timefs 0)
(if (= running 1)
(begin
(ui_2d_rect margin btnY btnw btnh -5427684 1 0 10)
(ui_2d_text (+ margin (/ (- btnw (* 2 btnfs)) 2)) (+ btnY (/ btnh 2) (/ (* btnfs 7) 20)) "暂停" c_txt btnfs 0))
(begin
(ui_2d_rect margin btnY btnw btnh -13730510 1 0 10)
(ui_2d_text (+ margin (/ (- btnw (* 2 btnfs)) 2)) (+ btnY (/ btnh 2) (/ (* btnfs 7) 20)) "开始" c_txt btnfs 0)))
(ui_2d_rect (- w margin btnw) btnY btnw btnh -13739380 1 0 10)
(ui_2d_text (+ (- w margin btnw) (/ (- btnw (* 2 btnfs)) 2)) (+ btnY (/ btnh 2) (/ (* btnfs 7) 20)) "清零" c_txt btnfs 0)
(set! tx (- (/ w 2) (* hintfs 8)))
(if (< tx margin) (set! tx margin) 0)
(ui_2d_text tx (- h hintfs) "左半屏 开始/暂停 · 右半屏 清零" c_dim hintfs 0)
(ui_2d_present)
(set! t (ui_msg_fetch_wait 0))
(if (= t 10) (set! quit 1) 0)
(if (= t 6)
(begin
(set! ax (ui_msg_get_a))
(if (< ax (/ w 2))
(if (= running 1) (set! running 0) (set! running 1))
(begin
(set! acc 0)
(set! running 0))))
0)
(if (= t 1)
(begin
(set! key (ui_msg_get_a))
(if (= key 27) (set! quit 1) 0))
0)
(if (= t 9)
(begin
(set! frames (+ frames 1))
(if (= running 1) (set! acc (+ acc 1)) 0)
(if (> frames 20000) (set! quit 1) 0))
0))
(ui_timer_kill tid)
(ui_scr_set_keep_on 0)
(ui_win_close)