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

d/app/calendar.d · 6241 字节 · 下载原文

▶ 在浏览器里试玩

// 日历 —— 用 **D** 写的手机小应用(日期 / 月历)
// Calendar -- a small phone app written in **D** (dates / month view)
//
// 功能:一屏一个月历(2026 年,每月天数与 1 号星期几用常量表);← → 切月,
// 点左右半屏也能切;ESC 退出。数字逐位绘制(见 digit / drawNum)。
//
// 跑法 —— 手机:vml run examples/d/calendar.d
// 跑法 —— 桌面:vmlcli Examples/d/calendar.d --screen 480x640 --frames /tmp/fr

#include <vml_ui.d>

char[] digit(int d) {
    if (d == 0) { return "0"; }
    if (d == 1) { return "1"; }
    if (d == 2) { return "2"; }
    if (d == 3) { return "3"; }
    if (d == 4) { return "4"; }
    if (d == 5) { return "5"; }
    if (d == 6) { return "6"; }
    if (d == 7) { return "7"; }
    if (d == 8) { return "8"; }
    return "9";
}

void drawNum(int x, int y, int v, int col, int sz) {
    int p1000 = v / 1000;
    int p100 = (v / 100) % 10;
    int p10 = (v / 10) % 10;
    int p1 = v % 10;
    int cx = x;
    if (p1000 > 0) {
        ui_2d_text(cx, y, digit(p1000), col, sz, 0);
        cx = cx + sz / 2;
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    } else if (p100 > 0) {
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    }
    if (p1000 > 0 || p100 > 0 || p10 > 0) {
        ui_2d_text(cx, y, digit(p10), col, sz, 0);
        cx = cx + sz / 2;
    }
    ui_2d_text(cx, y, digit(p1), col, sz, 0);
}

int numDigits(int v) {
    int d = 1;
    while (v >= 10) { v = v / 10; d = d + 1; }
    return d;
}

void main() {
    int[24] cal;
    cal[0] = 31; cal[1] = 28; cal[2] = 31; cal[3] = 30;
    cal[4] = 31; cal[5] = 30; cal[6] = 31; cal[7] = 31;
    cal[8] = 30; cal[9] = 31; cal[10] = 30; cal[11] = 31;
    cal[12] = 3; cal[13] = 6; cal[14] = 6; cal[15] = 2;
    cal[16] = 4; cal[17] = 0; cal[18] = 2; cal[19] = 5;
    cal[20] = 1; cal[21] = 3; cal[22] = 6; cal[23] = 1;

    int k = ui_win_open("日历", 0, 0);
    k = ui_scr_set_keep_on(1);
    int w = ui_scr_get_w();
    int h = ui_scr_get_h();
    if (w <= 0) { w = 360; }
    if (h <= 0) { h = 620; }

    int month = 11;
    int year = 2026;

    // 版面全部按画布现算:240 小屏压缩字号与留白,1024 大屏放宽。
    int small = w;
    if (h < small) { small = h; }
    int title_fs = small / 12;
    if (title_fs < 14) { title_fs = 14; }
    if (title_fs > 34) { title_fs = 34; }
    int label_fs = title_fs * 4 / 5;
    int week_fs = small / 26;
    if (week_fs < 8) { week_fs = 8; }
    if (week_fs > 12) { week_fs = 12; }
    int hint_fs = small / 22;
    if (hint_fs < 9) { hint_fs = 9; }
    if (hint_fs > 13) { hint_fs = 13; }

    int title_y = h / 16;
    if (title_y < title_fs + 2) { title_y = title_fs + 2; }
    int gap1 = h / 80;
    if (gap1 < 4) { gap1 = 4; }
    int week_y = title_y + title_fs + gap1;
    int gap2 = h / 40;
    if (gap2 < 6) { gap2 = 6; }
    int oy = week_y + week_fs + gap2;
    int hint_y = h - 8;

    int pad = w / 50;
    if (pad < 8) { pad = 8; }
    int cellw = (w - 2 * pad) / 7;
    int sideH = (hint_y - hint_fs - 4 - oy - gap1) / 6;
    if (sideH < cellw) { cellw = sideH; }
    if (cellw < 10) { cellw = 10; }
    int ox = (w - cellw * 7) / 2;
    int day_fs = cellw / 3;
    if (day_fs < 9) { day_fs = 9; }

    int frames = 0;
    int tid = ui_timer_set(200, 0);

    while (frames < 20000) {
        int days = cal[month - 1];
        int startd = cal[12 + month - 1];
        k = ui_2d_clear(-15724520);

        int tx = ox;
        drawNum(tx, title_y, year, -1513232, title_fs);
        tx = tx + numDigits(year) * title_fs / 2 + title_fs / 3;
        k = ui_2d_text(tx, title_y, "年", -6643536, label_fs, 0);
        tx = tx + label_fs + title_fs / 3;
        drawNum(tx, title_y, month, -10044566, title_fs);
        tx = tx + numDigits(month) * title_fs / 2 + title_fs / 3;
        k = ui_2d_text(tx, title_y, "月", -6643536, label_fs, 0);

        k = ui_2d_text(ox + cellw / 2, week_y, "一", -6643536, week_fs, 1);
        k = ui_2d_text(ox + cellw / 2 + cellw, week_y, "二", -6643536, week_fs, 1);
        k = ui_2d_text(ox + cellw / 2 + cellw * 2, week_y, "三", -6643536, week_fs, 1);
        k = ui_2d_text(ox + cellw / 2 + cellw * 3, week_y, "四", -6643536, week_fs, 1);
        k = ui_2d_text(ox + cellw / 2 + cellw * 4, week_y, "五", -6643536, week_fs, 1);
        k = ui_2d_text(ox + cellw / 2 + cellw * 5, week_y, "六", -6643536, week_fs, 1);
        k = ui_2d_text(ox + cellw / 2 + cellw * 6, week_y, "日", -6643536, week_fs, 1);

        int i = 0;
        while (i < days) {
            int idx = i + startd;
            int row = idx / 7;
            int col = idx - row * 7;
            int px = ox + col * cellw + 1;
            int py = oy + row * cellw + 1;
            int num_y = py + cellw / 2 + day_fs * 35 / 100;
            int num_x = px + cellw / 4;
            if (month == 11 && i + 1 == 14) {
                k = ui_2d_rect(px, py, cellw - 2, cellw - 2, -10044566, 1, 0, 4);
                drawNum(num_x, num_y, i + 1, -15724520, day_fs);
            } else {
                k = ui_2d_rect(px, py, cellw - 2, cellw - 2, -14408660, 1, 0, 4);
                drawNum(num_x, num_y, i + 1, -1513232, day_fs);
            }
            i = i + 1;
        }
        k = ui_2d_text(w / 2, hint_y, "← → 切月 · ESC 退出", -6643536, hint_fs, 1);
        k = ui_2d_present();

        int msg = ui_msg_fetch_wait(0);
        if (msg == 10) { frames = 20000; }
        if (msg == 1) {
            int key = ui_msg_get_a();
            if (key == 27) { frames = 20000; }
            if (key == 37) { month = month - 1; if (month < 1) { month = 12; } }
            if (key == 39) { month = month + 1; if (month > 12) { month = 1; } }
        }
        if (msg == 6) {
            int ax = ui_msg_get_a();
            if (ax < w / 2) {
                month = month - 1;
                if (month < 1) { month = 12; }
            } else {
                month = month + 1;
                if (month > 12) { month = 1; }
            }
        }
        if (msg == 9) { frames = frames + 1; }
    }

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