← 都来码 / Dolaima← 例程索引lua/app/calendar.lua▶ 在浏览器里试玩 原文

lua/app/calendar.lua · 8813 字节 · 下载原文

▶ 在浏览器里试玩

-- 日历 —— 用 **Lua** 写的手机小应用(日期 / 月历)
-- Calendar — a small phone app written in **Lua** (dates / month view)
--
-- 功能:一屏一个月历;← → 切月,↑ ↓ 跳年;今天那一格高亮;ESC 退出。
--
-- 这一份演示的是**纯计算的界面**(没有碰撞/计时逻辑,只有日期算术 + 网格绘制)。
--
-- ## 本语言的实测约束(见 Examples/lua/app/snake.lua / memory.lua 文件头)
--   · 全局状态**必须写在函数定义之前**;入口是 `main()`;
--   · **没有整数除法**:`/` 返回浮点、UI 参数按位型解释 ⇒ 一律 `idiv`;
--   · 逻辑运算符是 `or` / `and`;`!=` 写 `~=`;
--   · 数字上屏走**点阵**(`FONT` 15 位掩码)。
--
-- 跑法 —— 手机:vml run examples/lua/calendar.lua
-- 跑法 —— 桌面:vmlcli Examples/lua/calendar.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
MZ_CELL = 50
MZ_OX = 10
MZ_OY = 120
TITLE_PX = 4
WDAY_FS = 12
YEAR = 2026
MONTH = 11
TY = 2026
TM = 11
TD = 14
FRAMES = 0
LANG = 0
ALIVE = 1
T_YEAR = "年"
T_MONTH = "月"
T_HINT = "← → 切月 · ↑ ↓ 跳年 · ESC 退出"
MDAYS = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

function idiv(a, b)
    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

function num_width(v)
    local digits = 1
    local n = v
    while n >= 10 do
        n = idiv(n, 10)
        digits = digits + 1
    end
    return digits * 4 - 1
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
    -- 月历要同时受宽(7 列)和高(最多 6 行 + 标题/提示)约束
    -- The month grid is bounded both by width (7 columns) and height (up to 6 rows + header/hint)
    local mn = MZ_SW
    if MZ_SH < mn then mn = MZ_SH end
    TITLE_PX = idiv(mn, 60)
    if TITLE_PX < 3 then TITLE_PX = 3 end
    if TITLE_PX > 6 then TITLE_PX = 6 end
    MZ_CELL = idiv(MZ_SW - 16, 7)
    WDAY_FS = idiv(MZ_CELL, 6)
    if WDAY_FS < 10 then WDAY_FS = 10 end
    if WDAY_FS > 16 then WDAY_FS = 16 end
    -- 标题(数字点阵 5*TITLE_PX 高)+ 星期表头 + 两道间距 ⇒ 网格顶边
    -- Title (5*TITLE_PX tall) + weekday header + gaps ⇒ top of the month grid.
    local top = 10 + 5 * TITLE_PX + WDAY_FS + 14
    local hcell = idiv(MZ_SH - top - 40, 6)
    if hcell < MZ_CELL then
        MZ_CELL = hcell
    end
    if MZ_CELL < 18 then
        MZ_CELL = 18
    end
    if MZ_CELL > 100 then
        MZ_CELL = 100
    end
    MZ_OX = idiv(MZ_SW - 7 * MZ_CELL, 2)
    MZ_OY = top
end

function leap(y)
    if y % 4 == 0 and y % 100 ~= 0 then
        return 1
    end
    if y % 400 == 0 then
        return 1
    end
    return 0
end

function month_days()
    local d = MDAYS[MONTH]
    if MONTH == 2 and leap(YEAR) == 1 then
        d = 29
    end
    return d
end

function month_start()
    local count = 0
    local y = 1900
    while y < YEAR do
        count = count + 365
        if leap(y) == 1 then
            count = count + 1
        end
        y = y + 1
    end
    local m = 1
    while m < MONTH do
        if m == 2 then
            if leap(YEAR) == 1 then
                count = count + 29
            else
                count = count + 28
            end
        else
            count = count + MDAYS[m]
        end
        m = m + 1
    end
    return count % 7
end

function draw()
    ui_2d_clear(-15724520)

    -- 标题:年 / 月分开画(数字点阵);字号随格子缩放
    -- Title: year / month drawn separately with dot-matrix digits; sizes scale with the cell.
    local tpx = TITLE_PX
    local tfs = tpx * 4
    local yx = MZ_OX
    draw_number(yx, 10, YEAR, tpx, -1513232)
    local yx2 = yx + 15 * tpx + 6
    ui_2d_text(yx2, 10 + 5 * tpx, T_YEAR, -6643536, tfs, VML_ANCHOR_LEFT)
    local mx = yx2 + tfs + 6
    draw_number(mx, 10, MONTH, tpx, -1513232)
    ui_2d_text(mx + 7 * tpx + 6, 10 + 5 * tpx, T_MONTH, -6643536, tfs, VML_ANCHOR_LEFT)

    -- 星期表头(中文单字)
    local names = {"一", "二", "三", "四", "五", "六", "日"}
    local i = 0
    while i < 7 do
        ui_2d_text(MZ_OX + i * MZ_CELL + idiv(MZ_CELL, 2), MZ_OY - 6, names[i + 1], -6643536, WDAY_FS, VML_ANCHOR_CENTER)
        i = i + 1
    end

    local start = month_start()
    local days = month_days()
    local dpx = idiv(MZ_CELL, 7)
    -- 小屏格子只有 24px:用 2px 步长,两位数之间留出空隙,免得相邻两格粘在一起
    -- Tiny cells (24px on 240x240): step 2 keeps a gap between the two-digit numbers of adjacent cells.
    if MZ_CELL < 32 then dpx = 2 end
    if dpx < 2 then dpx = 2 end
    if dpx > 8 then dpx = 8 end
    i = 0
    while i < days do
        local col = (start + i) % 7
        local row = idiv(start + i, 7)
        local px = MZ_OX + col * MZ_CELL
        local py = MZ_OY + row * MZ_CELL
        if YEAR == TY and MONTH == TM and i + 1 == TD then
            ui_2d_rect(px + 1, py + 1, MZ_CELL - 2, MZ_CELL - 2, -11409298, 1, 0, 2)
            draw_number(px + idiv(MZ_CELL, 2) - idiv(num_width(i + 1) * dpx, 2) + 2,
                py + idiv(MZ_CELL, 2) - idiv(5 * dpx, 2), i + 1, dpx, -15724520)
        else
            ui_2d_rect(px + 1, py + 1, MZ_CELL - 2, MZ_CELL - 2, -15066588, 1, 0, 2)
            draw_number(px + idiv(MZ_CELL, 2) - idiv(num_width(i + 1) * dpx, 2) + 2,
                py + idiv(MZ_CELL, 2) - idiv(5 * dpx, 2), i + 1, dpx, -1513232)
        end
        i = i + 1
    end

    ui_2d_text(idiv(MZ_SW, 2), MZ_SH - 22, T_HINT, -6643536, 10, VML_ANCHOR_CENTER)
    ui_2d_present()
end

function main()
    LANG = ui_scr_get_language()
    ui_win_open("日历", 0, 0)

    relayout()

    TID = ui_timer_set(200, 1)

    while ALIVE == 1 do
        local t = ui_msg_fetch_wait(200)
        if t == 10 then
            ALIVE = 0
        end

        if t == 1 then
            local k = ui_msg_get_a()
            if k == 27 then
                ALIVE = 0
            end
            if k == 37 then
                MONTH = MONTH - 1
                if MONTH < 1 then
                    MONTH = 12
                    YEAR = YEAR - 1
                end
            end
            if k == 39 then
                MONTH = MONTH + 1
                if MONTH > 12 then
                    MONTH = 1
                    YEAR = YEAR + 1
                end
            end
            if k == 38 then
                YEAR = YEAR + 1
            end
            if k == 40 then
                YEAR = YEAR - 1
            end
            if YEAR < 1900 then
                YEAR = 1900
            end
            if YEAR > 2100 then
                YEAR = 2100
            end
        end

        if t == 6 or t == 4 then
            if ui_msg_get_a() < idiv(MZ_SW, 2) then
                MONTH = MONTH - 1
                if MONTH < 1 then
                    MONTH = 12
                    YEAR = YEAR - 1
                end
            else
                MONTH = MONTH + 1
                if MONTH > 12 then
                    MONTH = 1
                    YEAR = YEAR + 1
                end
            end
            if YEAR < 1900 then
                YEAR = 1900
            end
            if YEAR > 2100 then
                YEAR = 2100
            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()