lua/app/stopwatch.lua · 6843 字节 · 下载原文
-- 秒表 —— 用 **Lua** 写的手机小应用(计时 / 点阵读数)
-- Stopwatch — a small phone app written in **Lua** (timing / dot-matrix readout)
--
-- 功能:点左半屏开始 / 暂停,点右半屏清零;读数 `分:秒.百分秒` 用点阵数字画。
-- Features: tap the left half to start/pause, the right half to reset; the readout
-- `mm:ss.cc` is drawn with dot-matrix digits.
--
-- 为什么用秒表补位:本语言的大表(几百格)实测不可靠(见 Examples/lua/app/maze.lua 文件头),
-- 秒表只需几个标量 + 点阵 —— 正好演示「无表状态 + 点阵数字」这条路。
-- Why a stopwatch: big tables (hundreds of slots) measured unreliable in this language
-- (see the header of Examples/lua/app/maze.lua); the stopwatch needs only scalars plus the
-- dot-matrix font, which is exactly the "no big tables" path.
--
-- ## 本语言的实测约束(见 Examples/lua/app/snake.lua / memory.lua 文件头)
-- · 全局状态**必须写在函数定义之前**;入口是 `main()`;
-- · **没有整数除法**:`/` 返回浮点、UI 参数按位型解释 ⇒ 一律 `idiv`;
-- · 逻辑运算符是 `or` / `and`;`!=` 写 `~=`;
-- · 点阵位测试用**取模**(`mask % (bitv * 2) >= bitv`),别用 idiv(b=1 会慢到卡死)。
--
-- 跑法 —— 手机:vml run examples/lua/stopwatch.lua
-- 跑法 —— 桌面:vmlcli Examples/lua/stopwatch.lua --screen 480x640 --frames /tmp/fr
require("vml_ui")
FONT = {31599, 29850, 29671, 31207, 18925, 31183, 31695, 18727, 31727, 31215}
BIT = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384}
MZ_SW = 360
MZ_SH = 620
-- 布局(relayout 每帧刷新;由画布尺寸现算,240x240 到 1024x1024 都放得下)
-- Layout (refreshed by relayout; derived from the canvas so it fits 240x240 through 1024x1024)
R_PX = 10
R_X = 80
R_Y = 200
TITLE_Y = 120
BTN_Y = 320
FS_TITLE = 16
FS_HINT = 11
RUNNING = 0
BASE_MS = 0
ACC_MS = 0
LAST_MS = 0
FRAMES = 0
LANG = 0
ALIVE = 1
T_TITLE = "秒表"
T_START = "开始"
T_STOP = "暂停"
T_RESET = "清零"
T_HINT = "左半屏 开始/暂停 · 右半屏 清零"
function idiv(a, b)
if b <= 0 then return 0 end
local q = 0
local r = a
if r < 0 then r = 0 - r end
while r >= b do
r = r - b
q = q + 1
end
if a < 0 then return 0 - q end
return q
end
function draw_digit(x, y, d, px, color)
if d < 0 then d = 0 end
if d > 9 then d = 9 end
local mask = FONT[d + 1]
local r = 0
while r < 5 do
local c = 0
while c < 3 do
local bitv = BIT[r * 3 + c + 1]
if mask % (bitv * 2) >= bitv then
ui_2d_rect(x + c * px, y + r * px, px, px, color, 1, 0, 0)
end
c = c + 1
end
r = r + 1
end
end
function draw_number(x, y, v, px, color)
if v < 0 then v = 0 end
if v > 999999 then v = 999999 end
local digits = 1
local n = v
while n >= 10 do
n = idiv(n, 10)
digits = digits + 1
end
local i = digits
local rest = v
while i >= 1 do
local d = rest % 10
rest = idiv(rest, 10)
draw_digit(x + (i - 1) * px * 4, y, d, px, color)
i = i - 1
end
end
-- 两位定宽数字(前面补 0)
function draw2(x, y, v, px, color)
draw_digit(x, y, idiv(v, 10) % 10, px, color)
draw_digit(x + px * 4, y, v % 10, px, color)
end
function relayout()
MZ_SW = ui_scr_get_w()
MZ_SH = ui_scr_get_h()
if MZ_SW <= 0 then
MZ_SW = 360
end
if MZ_SH <= 0 then
MZ_SH = 620
end
local mn = MZ_SW
if MZ_SH < mn then mn = MZ_SH end
-- 点阵格边长按短边缩放,整条读数(36 格宽)水平居中
-- Dot-matrix cell scales with the short side; the whole readout (36 cells wide) is centred.
R_PX = idiv(mn, 40)
if R_PX < 3 then R_PX = 3 end
if R_PX > 12 then R_PX = 12 end
R_X = idiv(MZ_SW - R_PX * 36, 2)
R_Y = idiv(MZ_SH, 2) - idiv(R_PX * 5, 2)
FS_TITLE = idiv(mn, 15)
if FS_TITLE < 12 then FS_TITLE = 12 end
if FS_TITLE > 22 then FS_TITLE = 22 end
FS_HINT = idiv(mn, 24)
if FS_HINT < 8 then FS_HINT = 8 end
if FS_HINT > 13 then FS_HINT = 13 end
TITLE_Y = idiv(MZ_SH, 7)
BTN_Y = idiv(MZ_SH * 2, 3)
end
function elapsed_ms()
if RUNNING == 1 then
return ACC_MS + (ui_time_get_ms() - LAST_MS)
end
return ACC_MS
end
function draw()
ui_2d_clear(-15724520)
local total = elapsed_ms()
local cs = idiv(total, 10) % 100
local sec = idiv(total, 1000) % 60
local minute = idiv(total, 60000)
local px = R_PX
local rx = R_X
local ry = R_Y
local col = -1513232
if RUNNING == 0 then
col = -11409298
end
-- 分:秒.百分秒
draw2(rx, ry, minute % 100, px, col)
ui_2d_text(rx + px * 9, ry + px * 2, ":", -6643536, px * 2, VML_ANCHOR_LEFT)
draw2(rx + px * 14, ry, sec, px, col)
ui_2d_text(rx + px * 23, ry + px * 4, ".", -6643536, px * 2, VML_ANCHOR_LEFT)
draw2(rx + px * 28, ry, cs, px, col)
ui_2d_text(idiv(MZ_SW, 2), TITLE_Y, T_TITLE, -6643536, FS_TITLE, VML_ANCHOR_CENTER)
if RUNNING == 1 then
ui_2d_text(idiv(MZ_SW, 4), BTN_Y, T_STOP, -2069424, FS_TITLE, VML_ANCHOR_CENTER)
else
ui_2d_text(idiv(MZ_SW, 4), BTN_Y, T_START, -11409298, FS_TITLE, VML_ANCHOR_CENTER)
end
ui_2d_text(idiv(MZ_SW * 3, 4), BTN_Y, T_RESET, -11890471, FS_TITLE, VML_ANCHOR_CENTER)
ui_2d_text(idiv(MZ_SW, 2), MZ_SH - FS_HINT - 8, T_HINT, -6643536, FS_HINT, VML_ANCHOR_CENTER)
ui_2d_present()
end
function main()
LANG = ui_scr_get_language()
ui_win_open("秒表", 0, 0)
relayout()
ACC_MS = 0
RUNNING = 0
LAST_MS = ui_time_get_ms()
draw()
TID = ui_timer_set(50, 1)
while ALIVE == 1 do
local t = ui_msg_fetch_wait(50)
if t == 10 then
ALIVE = 0
end
if t == 6 or t == 4 then
if ui_msg_get_a() < idiv(MZ_SW, 2) then
if RUNNING == 1 then
ACC_MS = ACC_MS + (ui_time_get_ms() - LAST_MS)
RUNNING = 0
else
LAST_MS = ui_time_get_ms()
RUNNING = 1
end
else
ACC_MS = 0
LAST_MS = ui_time_get_ms()
RUNNING = 0
end
end
if t == 1 then
local k = ui_msg_get_a()
if k == 27 then
ALIVE = 0
end
end
if t == 11 then
relayout()
end
if t == 9 then
FRAMES = FRAMES + 1
if FRAMES >= 20000 then ALIVE = 0 end
end
draw()
end
ui_timer_kill(TID)
ui_win_close()
end
main()