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

rust/app/calendar.rs · 11023 字节 · 下载原文

▶ 在浏览器里试玩

// 日历 —— 用 **Rust** 写的手机小应用(日期 / 月历)
// Calendar — a small phone app written in **Rust** (dates / month view)
//
// 功能:一屏一个月历;← → 切月,↑ ↓ 跳年;今天那一格高亮;ESC 退出。
// Features: one month per screen; left/right changes the month, up/down the year; today's cell is
// highlighted; ESC quits.
//
// 这一份演示的是**纯计算的界面**(没有游戏循环里的碰撞/计时逻辑,只有日期算术 + 网格绘制),
// This one exercises a **pure-computation UI** (no collision or timer logic, just date arithmetic
// 也顺带把「数字怎么上屏」再走一遍。
// and grid drawing), and walks "how do numbers reach the screen" once more.
//
// ## Rust 这一路的写法约束(见 `Examples/rust/app/snake.rs` 文件头)
// ## Style constraints on this Rust path (see the header of `Examples/rust/app/snake.rs`)
//
//   · **全部状态放 `main` 的局部变量**,循环一律 `while`;**循环变量在 `main` 顶部声明一次**;
//   · **all state lives in locals of `main`**, loops are always `while`; **loop variables are
//     declared once at the top of `main`**;
//   · **全程序只养一条 Vec** ⇒ 每月天数表就放在这一条里;
//   · **exactly one Vec per program** => the days-per-month table lives in it;
//   · 数字转字符串走库里的 `int_to_str`(共享静态缓冲,取一次立刻用掉);颜色写负数十进制。
//   · numbers go through the library's `int_to_str` (one shared static buffer; take and use it at once);
//     colours are negative decimals.
//
// 跑法 —— 手机:`vml run examples/rust/calendar.rs`
// How to run — on the phone: `vml run examples/rust/calendar.rs`
// 跑法 —— 桌面:`vmlcli Examples/rust/calendar.rs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/rust/calendar.rs --screen 480x640 --frames /tmp/fr`

#include <vml_ui.rs>
use vml_ui::*;

fn main() {
    let mut lang = ui_scr_get_language();
    let mut sw = 360;
    let mut sh = 620;
    let mut year = 2026;
    let mut month = 11;
    let mut today_y = 2026;
    let mut today_m = 11;
    let mut today_d = 14;
    let mut frames = 0;
    let mut i = 0;
    let mut j = 0;
    let mut t = 0;
    let mut k = 0;
    let mut count = 0;
    let mut start = 0;
    let mut days = 0;
    let mut col = 0;
    let mut row = 0;
    let mut done = 0;
    let mut tid = 0;
    let mut cell = 0;
    let mut ox = 0;
    let mut oy = 0;
    let mut unit = 0;
    let mut title_size = 0;
    let mut title_y = 0;
    let mut week_size = 0;
    let mut week_y = 0;
    let mut foot_size = 0;
    let mut foot_y = 0;
    let mut avail = 0;
    let mut maxcell = 0;
    let mut day_size = 0;

    let c_bg = -15724520;        // 0xFF101018
    let c_cell = -15066588;      // 0xFF1A1A24
    let c_text = -1513232;       // 0xFFE8E8F0
    let c_dim = -6643536;        // 0xFF9AA0B0
    let c_today = -11409298;     // 0xFF51E86E

    // 一条 Vec:每月天数(1..12 月,2 月先填 28,闰年时临时改)。
    // ⚠ 下标是 `month - 1` ⇒ 1 月必须落在 mdays[0],**不要**在前面垫一个 0
    // (垫了就会整体错一位:11 月读到 10 月的 31 天)。
    // One Vec: days per month (1..12; February holds 28 and is patched for leap years).
    // ⚠ The index is `month - 1`, so January must sit at mdays[0] -- **do not** pad a leading 0
    // (that shifts everything by one: November reads October's 31 days).
    let mut mdays: Vec<i32> = vec![31];
    mdays.push(28);
    mdays.push(31);
    mdays.push(30);
    mdays.push(31);
    mdays.push(30);
    mdays.push(31);
    mdays.push(31);
    mdays.push(30);
    mdays.push(31);
    mdays.push(30);
    mdays.push(31);

    if lang == 0 { ui_win_open("日历", 0, 0); }
    else { ui_win_open("Calendar", 0, 0); }
    ui_scr_set_keep_on(1);

    sw = ui_scr_get_w();
    sh = ui_scr_get_h();
    if sw <= 0 { sw = 360; }
    if sh <= 0 { sh = 620; }

    // 版面全部按真实画布现算:字号随 min(W,H) 缩放,格子同时受宽、高两侧约束。
    // Layout is derived from the real canvas: font sizes scale with min(W,H); cells are capped by both width and height.
    unit = sw;
    if sh < unit { unit = sh; }
    title_size = unit / 18;
    if title_size < 14 { title_size = 14; }
    if title_size > 44 { title_size = 44; }
    // `ui_2d_text` 的 y 是**基线**(文字底边),所以标题基线至少要让出一个字高,否则顶部被裁。
    // `ui_2d_text`'s y is the **baseline** (text bottom), so the title baseline must leave at least one text height or the top gets clipped.
    title_y = sh / 50;
    if title_y < 6 { title_y = 6; }
    if title_y > 20 { title_y = 20; }
    title_y = title_y + title_size;
    week_size = unit / 40;
    if week_size < 9 { week_size = 9; }
    if week_size > 22 { week_size = 22; }
    week_y = title_y + title_size + 8;
    oy = week_y + week_size + 10;
    foot_size = week_size;
    foot_y = sh - foot_size - 8;
    avail = foot_y - foot_size - oy - 6;
    maxcell = avail / 6;
    cell = (sw - 12) / 7;
    if cell > maxcell { cell = maxcell; }
    if cell < 14 { cell = 14; }
    ox = (sw - 7 * cell) / 2;
    day_size = cell / 3;
    if day_size < 8 { day_size = 8; }

    tid = ui_timer_set(200, 0);

    while done == 0 {
        if ui_win_is_closed() != 0 { done = 1; }
        if done != 0 { break; }

        t = ui_msg_fetch_wait(0);
        if t == 10 { done = 1; }

        if t == 1 {
            k = ui_msg_get_a();
            if k == 27 { done = 1; }
            if k == 37 {
                month = month - 1;
                if month < 1 { month = 12; year = year - 1; }
            }
            if k == 39 {
                month = month + 1;
                if month > 12 { month = 1; year = year + 1; }
            }
            if k == 38 { year = year + 1; }
            if k == 40 { year = year - 1; }
            if year < 1900 { year = 1900; }
            if year > 2100 { year = 2100; }
        }

        if t == 6 || t == 4 {
            let tx = ui_msg_get_a();
            let ty = ui_msg_get_b();
            if tx < sw / 2 {
                month = month - 1;
                if month < 1 { month = 12; year = year - 1; }
            } else {
                month = month + 1;
                if month > 12 { month = 1; year = year + 1; }
            }
            if year < 1900 { year = 1900; }
            if year > 2100 { year = 2100; }
        }

        // 这个月有几天(闰年 2 月 29)
        // Days in this month (leap February has 29)
        days = mdays[month - 1];
        if month == 2 {
            if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 { days = 29; }
        }

        // 1 号是星期几:从 1900-01-01(星期一)累加天数
        // Weekday of the 1st: count days from 1900-01-01 (a Monday)
        count = 0;
        i = 1900;
        while i < year {
            count = count + 365;
            if (i % 4 == 0 && i % 100 != 0) || i % 400 == 0 { count = count + 1; }
            i = i + 1;
        }
        i = 1;
        while i < month {
            if i == 2 {
                if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 { count = count + 29; }
                else { count = count + 28; }
            } else {
                count = count + mdays[i - 1];
            }
            i = i + 1;
        }
        start = count % 7;   // 0 = 星期一

        ui_2d_clear(c_bg);

        // 标题:年 / 月(数字与单位分开画,避开字符串拼接)
        // Title: year / month (numbers and units drawn separately; no string concatenation)
        ui_2d_text(ox, title_y, int_to_str(year), c_text, title_size, 0);
        if lang == 0 { ui_2d_text(ox + title_size * 31 / 10, title_y, "年", c_dim, title_size * 4 / 5, 0); }
        else { ui_2d_text(ox + title_size * 31 / 10, title_y, "-", c_dim, title_size * 4 / 5, 0); }
        ui_2d_text(ox + title_size * 9 / 2, title_y, int_to_str(month), c_text, title_size, 0);
        if lang == 0 { ui_2d_text(ox + title_size * 32 / 5, title_y, "月", c_dim, title_size * 4 / 5, 0); }
        else { ui_2d_text(ox + title_size * 32 / 5, title_y, "", c_dim, title_size * 4 / 5, 0); }

        // 星期表头
        // Weekday header
        i = 0;
        while i < 7 {
            if lang == 0 {
                if i == 0 { ui_2d_text(ox + i * cell + cell / 2, week_y, "一", c_dim, week_size, 1); }
                if i == 1 { ui_2d_text(ox + i * cell + cell / 2, week_y, "二", c_dim, week_size, 1); }
                if i == 2 { ui_2d_text(ox + i * cell + cell / 2, week_y, "三", c_dim, week_size, 1); }
                if i == 3 { ui_2d_text(ox + i * cell + cell / 2, week_y, "四", c_dim, week_size, 1); }
                if i == 4 { ui_2d_text(ox + i * cell + cell / 2, week_y, "五", c_dim, week_size, 1); }
                if i == 5 { ui_2d_text(ox + i * cell + cell / 2, week_y, "六", c_dim, week_size, 1); }
                if i == 6 { ui_2d_text(ox + i * cell + cell / 2, week_y, "日", c_dim, week_size, 1); }
            } else {
                if i == 0 { ui_2d_text(ox + i * cell + cell / 2, week_y, "Mo", c_dim, week_size, 1); }
                if i == 1 { ui_2d_text(ox + i * cell + cell / 2, week_y, "Tu", c_dim, week_size, 1); }
                if i == 2 { ui_2d_text(ox + i * cell + cell / 2, week_y, "We", c_dim, week_size, 1); }
                if i == 3 { ui_2d_text(ox + i * cell + cell / 2, week_y, "Th", c_dim, week_size, 1); }
                if i == 4 { ui_2d_text(ox + i * cell + cell / 2, week_y, "Fr", c_dim, week_size, 1); }
                if i == 5 { ui_2d_text(ox + i * cell + cell / 2, week_y, "Sa", c_dim, week_size, 1); }
                if i == 6 { ui_2d_text(ox + i * cell + cell / 2, week_y, "Su", c_dim, week_size, 1); }
            }
            i = i + 1;
        }

        // 日期格
        // Day cells
        i = 0;
        while i < days {
            let idx = start + i;
            col = idx % 7;
            row = idx / 7;
            let px = ox + col * cell;
            let py = oy + row * cell;
            ui_2d_rect(px + 1, py + 1, cell - 2, cell - 2, c_cell, 1, 0, 2);
            if year == today_y && month == today_m && i + 1 == today_d {
                ui_2d_rect(px + 1, py + 1, cell - 2, cell - 2, c_today, 1, 0, 2);
                ui_2d_text(px + cell / 2, py + cell / 2 + day_size / 2, int_to_str(i + 1), c_bg, day_size, 1);
            } else {
                ui_2d_text(px + cell / 2, py + cell / 2 + day_size / 2, int_to_str(i + 1), c_text, day_size, 1);
            }
            i = i + 1;
        }

        ui_2d_text(sw / 2, foot_y, "← → 切月 · ↑ ↓ 跳年 · ESC 退出", c_dim, foot_size, 1);
        ui_2d_present();

        frames = frames + 1;
        if frames >= 20000 { done = 1; }
    }

    ui_timer_kill(tid);
    ui_scr_set_keep_on(0);
    ui_win_close();
}