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

d/app/stopwatch.d · 6037 字节 · 下载原文

▶ 在浏览器里试玩

// 秒表 —— 用 **D** 写的手机小应用(计时)
// Stopwatch -- a small phone app written in **D** (timing)
//
// 功能:点左半屏开始 / 暂停,点右半屏清零;读数 `分:秒.百分秒`。
// 计时驱动用定时器拍数(每 50ms 一拍,20 拍 = 1 秒)。
//
// 本前端的写法要点(与 ui/demo_ui.d 同一套):
//   · `#include <vml_ui.d>`;颜色写**负数十进制**(D 词法器不认 0x);
//   · `ui_2d_text` 只收 `char[]` —— **整数不会自动转串**,所以每个例程都带
//     一套 `digit(n)` + `drawNum(...)` 助手,逐位画数字。
//
// 跑法 —— 手机:vml run examples/d/stopwatch.d
// 跑法 —— 桌面:vmlcli Examples/d/stopwatch.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);
}

void drawPad2(int x, int y, int v, int col, int sz) {
    ui_2d_text(x, y, digit(v / 10), col, sz, 0);
    ui_2d_text(x + sz / 2, y, digit(v % 10), col, sz, 0);
}

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

void main() {
    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 small = w;
    if (h < small) { small = h; }

    // 字号与按钮尺寸随画布短边缩放:240 小屏压缩、1024 大屏放大。
    int title_fs = small / 16;
    if (title_fs < 14) { title_fs = 14; }
    if (title_fs > 40) { title_fs = 40; }
    int big_fs = small / 5;
    if (big_fs < 34) { big_fs = 34; }
    if (big_fs > 140) { big_fs = 140; }
    int btn_fs = small / 16;
    if (btn_fs < 14) { btn_fs = 14; }
    if (btn_fs > 32) { btn_fs = 32; }
    int hint_fs = small / 24;
    if (hint_fs < 10) { hint_fs = 10; }
    if (hint_fs > 18) { hint_fs = 18; }

    int title_y = h * 12 / 100;
    if (title_y < title_fs + 2) { title_y = title_fs + 2; }
    int time_y = h * 45 / 100;
    if (time_y < title_y + big_fs) { time_y = title_y + big_fs + 4; }
    int bh = small / 6;
    if (bh < 40) { bh = 40; }
    if (bh > 140) { bh = 140; }
    int btn_y = h * 70 / 100;
    if (btn_y + bh > h - hint_fs - 16) { btn_y = h - hint_fs - 16 - bh; }
    if (btn_y < time_y + 8) { btn_y = time_y + 8; }
    int bw = w / 4;
    if (bw < 70) { bw = 70; }
    if (bw > 320) { bw = 320; }
    if (bw > (w - 24) / 2) { bw = (w - 24) / 2; }
    int lbtn_x = w / 4 - bw / 2;
    int rbtn_x = w * 3 / 4 - bw / 2;
    if (lbtn_x < 6) { lbtn_x = 6; }
    if (rbtn_x < lbtn_x + bw + 8) { rbtn_x = lbtn_x + bw + 8; }
    if (rbtn_x + bw > w - 6) { rbtn_x = w - 6 - bw; }

    int running = 0;
    int acc = 0;
    int frames = 0;
    int tid = ui_timer_set(50, 0);

    while (frames < 20000) {
        int sec = acc / 20;
        int min = sec / 60;
        sec = sec - min * 60;
        int cs = (acc - (acc / 20) * 20) * 5;

        k = ui_2d_clear(-15724520);
        k = ui_2d_text(w / 2, title_y, "秒表", -6643536, title_fs, 1);

        // 读数整体居中:min + ':' + sec + '.' + 两位百分秒
        int minw = numDigits(min) * big_fs / 2;
        int secw = numDigits(sec) * big_fs / 2;
        int totalw = minw + big_fs / 2 + secw + big_fs / 2 + big_fs;
        int cx = w / 2 - totalw / 2;
        drawNum(cx, time_y, min, -1513232, big_fs);
        cx = cx + minw;
        k = ui_2d_text(cx, time_y, ":", -6643536, big_fs, 0);
        cx = cx + big_fs / 2;
        drawNum(cx, time_y, sec, -1513232, big_fs);
        cx = cx + secw;
        k = ui_2d_text(cx, time_y, ".", -6643536, big_fs, 0);
        cx = cx + big_fs / 2;
        drawPad2(cx, time_y, cs, -1513232, big_fs);

        int txt_y = btn_y + bh / 2 + btn_fs * 35 / 100;
        if (running == 1) {
            k = ui_2d_rect(lbtn_x, btn_y, bw, bh, -5427684, 1, 0, 10);
            k = ui_2d_text(lbtn_x + bw / 2, txt_y, "暂停", -1513232, btn_fs, 1);
        } else {
            k = ui_2d_rect(lbtn_x, btn_y, bw, bh, -13730510, 1, 0, 10);
            k = ui_2d_text(lbtn_x + bw / 2, txt_y, "开始", -1513232, btn_fs, 1);
        }
        k = ui_2d_rect(rbtn_x, btn_y, bw, bh, -13739380, 1, 0, 10);
        k = ui_2d_text(rbtn_x + bw / 2, txt_y, "清零", -1513232, btn_fs, 1);
        k = ui_2d_text(w / 2, h - 8, "左半屏 开始/暂停 · 右半屏 清零", -6643536, hint_fs, 1);
        k = ui_2d_present();

        int msg = ui_msg_fetch_wait(0);
        if (msg == 10) { frames = 20000; }
        if (msg == 6) {
            int ax = ui_msg_get_a();
            if (ax < w / 2) {
                if (running == 1) { running = 0; } else { running = 1; }
            } else {
                acc = 0;
                running = 0;
            }
        }
        if (msg == 1) {
            int key = ui_msg_get_a();
            if (key == 27) { frames = 20000; }
        }
        if (msg == 9) {
            frames = frames + 1;
            if (running == 1) { acc = acc + 1; }
        }
    }

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