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

c/app/paint.c · 6398 字节 · 下载原文

▶ 在浏览器里试玩

/* paint.c —— 画板(手指/鼠标涂画),跑在任意 VML 宿主上
 * paint.c -- Paint (finger / mouse drawing), runs on any VML host
 *
 *     vml run examples/c/app/paint.c
 *
 * ## 跨平台口径
 * ## Cross-platform notes
 *
 * 只用稳定 `ui_*`;画布与调色板每帧按窗口尺寸布局;触摸与鼠标都收
 * (MAUI 一次手势会投递两条,用「同坐标跳过」去重);线条存成线段表,
 * 每帧重画 —— 不依赖任何宿主专有绘图缓存。
 *
 * ## 用法
 * ## How to use
 *
 * 在画布上按住拖动即可涂画;底部选颜色、选笔粗;「清空」擦掉全部。
 */

#include <vml_ui.h>

#define MAXSEG 6000

int W, H;
int g_lang;

/* 线段表(环形缓冲):x1,y1,x2,y2,color,width */
/* Segment table (ring buffer): x1,y1,x2,y2,color,width */
int sgx1[MAXSEG], sgy1[MAXSEG], sgx2[MAXSEG], sgy2[MAXSEG];
int sgcol[MAXSEG], sgw[MAXSEG];
int segHead, segCount;

int curColor;
int curWidth;
int drawing;
int lastX, lastY;

int paletteCol[8] = {
    0xFFE6EBF5, 0xFFE05A5A, 0xFFF3A64B, 0xFF4ADE80,
    0xFF5EC8F0, 0xFF8B7BF0, 0xFFF06BC8, 0xFF9AA6B8
};

int widths[3] = {3, 7, 14};
int curWidthIdx = 1;

/* ── 布局 ── */
/* -- Layout -- */

int palette_h(void)
{
    int h = H * 20 / 100;
    if (h > 150) h = 150;
    if (h < 96) h = 96;
    return h;
}

int canvas_h(void) { return H - palette_h() - 40; }

/* ── 绘制 ── */
/* -- Drawing -- */

void add_segment(int x1, int y1, int x2, int y2)
{
    int i;
    if (segCount < MAXSEG) {
        i = (segHead + segCount) % MAXSEG;
        segCount = segCount + 1;
    } else {
        i = segHead;
        segHead = (segHead + 1) % MAXSEG;
    }
    sgx1[i] = x1; sgy1[i] = y1;
    sgx2[i] = x2; sgy2[i] = y2;
    sgcol[i] = curColor;
    sgw[i] = curWidth;
}

void draw(void)
{
    int i, idx, px, py, pw, ph, sw, sx, sy, k;
    char label[8];

    ui_2d_clear(0xFF11141C);

    /* 画布底 */
    ui_2d_rect(8, 8, W - 16, canvas_h() - 16, 0xFF171C27, 1, 0, 10);

    /* 线段 */
    for (i = 0; i < segCount; i++) {
        idx = (segHead + i) % MAXSEG;
        ui_2d_line(sgx1[idx], sgy1[idx], sgx2[idx], sgy2[idx], sgcol[idx], sgw[idx]);
    }

    /* 调色板 / palette */
    ph = palette_h();
    py = H - ph;
    pw = (W - 24) / 8;
    if (pw > 56) pw = 56;
    sx = (W - pw * 8) / 2;
    for (i = 0; i < 8; i++) {
        int x = sx + i * pw;
        ui_2d_rect(x + 4, py + 8, pw - 8, 34, paletteCol[i], 1, 0, 8);
        if (curColor == paletteCol[i])
            ui_2d_rect(x + 1, py + 5, pw - 2, 40, 0xFF5EC8F0, 0, 2, 10);
    }

    /* 笔粗 / widths */
    for (i = 0; i < 3; i++) {
        int x = sx + i * 52;
        int y = py + 52;
        ui_2d_rect(x + 2, y, 44, 32, (i == curWidthIdx) ? 0xFF31405C : 0xFF202737, 1, 0, 8);
        ui_2d_circle(x + 24, y + 16, widths[i], 0xFFE6EBF5, 1, 0);
    }

    /* 清空 / clear */
    ui_2d_rect(W - 116, py + 52, 100, 32, 0xFF232938, 1, 0, 8);
    ui_2d_text_v(W - 66, py + 68, g_lang == 0 ? "清空" : "Clear", 0xFFE6EBF5, 13,
                 VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, 0);

    if (segCount >= MAXSEG) {
        ui_2d_text_v(W / 2, py + 96, g_lang == 0 ? "(画布已满,请清空)" : "(canvas full, please clear)",
                     0xFFF3A64B, 11, VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, 0);
    }

    ui_2d_present();
}

/* ── 交互 ── */
/* -- Input -- */

int in_palette(int x, int y) { return y >= H - palette_h(); }

void tap_palette(int x, int y)
{
    int i, pw = (W - 24) / 8, sx;
    if (pw > 56) pw = 56;
    sx = (W - pw * 8) / 2;
    /* 颜色行 */
    if (y >= H - palette_h() + 8 && y < H - palette_h() + 42) {
        i = (x - sx) / pw;
        if (i >= 0 && i < 8) { curColor = paletteCol[i]; ui_audio_beep(760, 35); }
        return;
    }
    /* 笔粗行 */
    if (y >= H - palette_h() + 52 && y < H - palette_h() + 84) {
        i = (x - sx) / 52;
        if (i >= 0 && i < 3) { curWidthIdx = i; curWidth = widths[i]; ui_audio_beep(620, 35); }
        return;
    }
    /* 清空 */
    if (x >= W - 116 && x < W - 16 && y >= H - palette_h() + 52 && y < H - palette_h() + 84) {
        segHead = 0; segCount = 0;
        ui_audio_beep(440, 80);
    }
}

int main(void)
{
    int m[4];
    int t;
    int lastTouchX = -1, lastTouchY = -1, sawTouch = 0;

    g_lang = ui_scr_get_language();
    ui_win_open_ex(g_lang == 0 ? "画板" : "Paint", 0, 0,
                   VML_WIN_ROTATABLE, VML_WIN_NO_GAMEPAD);
    W = ui_scr_get_w();
    H = ui_scr_get_h();
    ui_scr_set_keep_on(1);

    curColor = paletteCol[0];
    curWidth = widths[curWidthIdx];

    draw();

    while (ui_win_is_closed() == 0) {
        t = ui_msg_wait(m, 100);
        if (t == VML_MSG_WINDOWCLOSE) break;
        if (t == VML_MSG_WINDOWRESIZE) {
            W = ui_scr_get_w();
            H = ui_scr_get_h();
            draw();
            continue;
        }

        if (t == VML_MSG_TOUCHDOWN || t == VML_MSG_MOUSEDOWN) {
            /* 同一手势的第二条消息(MAUI 会成对投递)按坐标去重 */
            if (t == VML_MSG_MOUSEDOWN && sawTouch && m[1] == lastTouchX && m[2] == lastTouchY) {
                sawTouch = 0;
                continue;
            }
            if (t == VML_MSG_TOUCHDOWN) { lastTouchX = m[1]; lastTouchY = m[2]; sawTouch = 1; }
            if (in_palette(m[1], m[2])) {
                tap_palette(m[1], m[2]);
            } else {
                drawing = 1;
                lastX = m[1]; lastY = m[2];
            }
            draw();
            continue;
        }

        if (t == VML_MSG_TOUCHMOVE || t == VML_MSG_MOUSEMOVE) {
            if (!drawing) continue;
            if (t == VML_MSG_MOUSEMOVE && sawTouch && m[1] == lastTouchX && m[2] == lastTouchY)
                continue;
            if (t == VML_MSG_TOUCHMOVE) { lastTouchX = m[1]; lastTouchY = m[2]; }
            if (m[1] == lastX && m[2] == lastY) continue;
            add_segment(lastX, lastY, m[1], m[2]);
            lastX = m[1]; lastY = m[2];
            draw();
            continue;
        }

        if (t == VML_MSG_TOUCHUP || t == VML_MSG_MOUSEUP) {
            drawing = 0;
            continue;
        }
        if (t == VML_MSG_KEYDOWN) {
            if (m[1] == 67) { segHead = 0; segCount = 0; draw(); }   /* C 清空 */
            continue;
        }
    }

    ui_scr_set_keep_on(0);
    return 0;
}