c/app/expense.c · 9312 字节 · 下载原文
/* expense.c —— 记账本(分类记一笔、看总额与流水),跑在任意 VML 宿主上
* expense.c -- Expense tracker (categorised entries, total and recent list), runs on any VML host
*
* vml run Examples/c/app/expense.c
*
* ## 跨平台口径
* ## Cross-platform notes
*
* 全整数(单位:元);只用稳定 `ui_*`;布局每帧按画布尺寸现算,覆盖
* 240x240 ~ 1024x1024;触摸/鼠标同坐标去重;不依赖输入法、不依赖外部文件。
*
* ## 用法
* ## How to use
*
* 选分类 → 用 -10 / -1 / +1 / +10 调金额 → 「记一笔」入账;流水按时间倒序显示,
* 顶部是累计。金额为 0 时不会入账;「清零」清空全部流水。
*/
#include <vml_ui.h>
#define MAX_ENTRIES 64
#define N_CAT 4
int W, H;
int g_lang;
int cat;
int amount; /* 元 / yuan */
int entCat[MAX_ENTRIES];
int entAmt[MAX_ENTRIES];
int entHead; /* 环形缓冲:下一条写入位置 */
int entCount;
int total;
int catCol[N_CAT] = {0xFFF3A64B, 0xFF5EC8F0, 0xFFF06BC8, 0xFF9AA6B8};
char* catNameCN[N_CAT] = {"餐饮", "交通", "购物", "其他"};
char* catNameEN[N_CAT] = {"Food", "Transit", "Shop", "Other"};
char g_buf[24];
/* ── 工具 ── */
/* -- Helpers -- */
int clampi(int v, int lo, int hi)
{
if (v < lo) return lo;
if (v > hi) return hi;
return v;
}
char* num_str(int v)
{
int n = 0, i = 0;
char t[12];
if (v < 0) { g_buf[n] = '-'; n = n + 1; v = -v; }
if (v == 0) { g_buf[n] = '0'; n = n + 1; }
else {
while (v > 0 && i < 11) { t[i] = (char)('0' + v % 10); i = i + 1; v = v / 10; }
while (i > 0) { i = i - 1; g_buf[n] = t[i]; n = n + 1; }
}
g_buf[n] = 0;
return g_buf;
}
int ent_at(int k) /* k=0 是最新一条 / k=0 is the newest */
{
return (entHead - 1 - k + MAX_ENTRIES * 2) % MAX_ENTRIES;
}
/* ── 状态 ── */
/* -- State -- */
void add_entry(void)
{
if (amount <= 0) return;
entCat[entHead] = cat;
entAmt[entHead] = amount;
entHead = (entHead + 1) % MAX_ENTRIES;
if (entCount < MAX_ENTRIES) entCount = entCount + 1;
total = total + amount;
amount = 0;
ui_audio_beep(880, 50);
}
void clear_all(void)
{
entHead = 0;
entCount = 0;
total = 0;
amount = 0;
ui_audio_beep(440, 70);
}
/* ── 布局 ── */
/* -- Layout -- */
int g_m, g_titleFS, g_chipH, g_catY, g_amtY, g_amtFS;
int g_stepY, g_stepH, g_actY, g_actH, g_listY, g_rowH, g_maxRows;
int g_narrow;
void layout(void)
{
int mn = W < H ? W : H;
int avail, rows;
g_narrow = (H < 420 || W < 360);
g_m = clampi(W / 40, 6, 24);
g_titleFS = clampi(mn / 20, 11, 26);
g_chipH = clampi(H * 7 / 100, 20, 40);
g_catY = clampi(H * 10 / 100, 24, 40);
g_amtY = g_catY + g_chipH + clampi(H * 5 / 100, 6, 24);
g_amtFS = clampi(mn / 9, 16, 52);
g_stepY = g_amtY + g_amtFS + clampi(H * 3 / 100, 4, 14);
g_stepH = clampi(H * 7 / 100, 20, 44);
g_actY = g_stepY + g_stepH + clampi(H * 2 / 100, 3, 10);
g_actH = clampi(H * 8 / 100, 20, 46);
g_listY = g_actY + g_actH + clampi(H * 2 / 100, 4, 12);
avail = H - g_listY - g_m;
rows = g_narrow ? 4 : 8;
g_rowH = avail / rows;
if (g_rowH > 54) g_rowH = 54;
if (g_rowH < 14) { g_rowH = 14; rows = avail / 14; if (rows < 1) rows = 1; }
g_maxRows = rows;
if (g_maxRows > entCount) g_maxRows = entCount;
}
/* ── 绘制 ── */
/* -- Drawing -- */
void chip(int x, int y, int w, int h, char* label, int sel, int fs, int col)
{
ui_2d_rect(x, y, w, h, sel ? 0xFF31405C : 0xFF202737, 1, 0, h / 3);
ui_2d_text_v(x + w / 2, y + h / 2, label, sel ? col : 0xFFB9C4D6, fs,
VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
}
void draw(void)
{
int i, cw, y, fs, small = g_narrow;
char tmp[32];
layout();
ui_2d_clear(0xFF11141C);
/* 标题 + 总额 */
ui_2d_text_v(g_m, clampi(H * 4 / 100, 10, 26),
g_lang == 0 ? "记账本" : "Expenses", 0xFFE6EBF5, g_titleFS,
VML_ANCHOR_LEFT, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
tmp[0] = 0;
strcat(tmp, g_lang == 0 ? "合计 " : "Total ");
strcat(tmp, num_str(total));
ui_2d_text_v(W - g_m, clampi(H * 4 / 100, 10, 26), tmp, 0xFF4ADE80,
clampi(g_titleFS - 3, 10, 22),
VML_ANCHOR_RIGHT, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
/* 分类 */
cw = (W - g_m * 2) / N_CAT;
for (i = 0; i < N_CAT; i++) {
chip(g_m + i * cw + 2, g_catY, cw - 4, g_chipH,
g_lang == 0 ? catNameCN[i] : catNameEN[i], i == cat,
clampi(g_chipH * 4 / 10, 9, 16), catCol[i]);
}
/* 金额 */
ui_2d_text_v(W / 2, g_amtY, num_str(amount), 0xFFE6EBF5, g_amtFS,
VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
ui_2d_text_v(W / 2 + (g_amtFS * 3 / 4), g_amtY, g_lang == 0 ? "元" : "yuan",
0xFF8B94A7, clampi(g_amtFS / 3, 9, 16),
VML_ANCHOR_LEFT, VML_VANCHOR_MIDDLE, 0);
/* 步进 */
{
char* labels[4] = {"-10", "-1", "+1", "+10"};
cw = (W - g_m * 2) / 4;
for (i = 0; i < 4; i++) {
chip(g_m + i * cw + 2, g_stepY, cw - 4, g_stepH, labels[i], 0,
clampi(g_stepH * 4 / 10, 9, 16), 0xFFE6EBF5);
}
}
/* 记一笔 / 清零 */
{
int half = (W - g_m * 2 - 6) / 2;
chip(g_m, g_actY, half, g_actH, g_lang == 0 ? "记一笔" : "Add",
0, clampi(g_actH * 4 / 10, 9, 16), 0xFF4ADE80);
chip(g_m + half + 6, g_actY, half, g_actH, g_lang == 0 ? "清零" : "Clear",
0, clampi(g_actH * 4 / 10, 9, 16), 0xFFE05A5A);
}
/* 流水(最新在上) */
if (entCount == 0) {
ui_2d_text_v(W / 2, g_listY + (H - g_listY) / 2, g_lang == 0 ? "还没有记录" : "no entries yet",
0xFF606878, small ? 10 : 14,
VML_ANCHOR_CENTER, VML_VANCHOR_MIDDLE, 0);
} else {
y = g_listY;
for (i = 0; i < g_maxRows; i++) {
int k = ent_at(i);
int rh = g_rowH - (small ? 2 : 6);
fs = clampi(rh * 5 / 10, 9, 18);
ui_2d_rect(g_m, y, W - g_m * 2, rh, 0xFF1B2130, 1, 0, rh / 4);
ui_2d_circle(g_m + (small ? 10 : 16), y + rh / 2, small ? 3 : 5, catCol[entCat[k]], 1, 0);
ui_2d_text_v(g_m + (small ? 20 : 32), y + rh / 2,
g_lang == 0 ? catNameCN[entCat[k]] : catNameEN[entCat[k]],
0xFFB9C4D6, fs,
VML_ANCHOR_LEFT, VML_VANCHOR_MIDDLE, 0);
tmp[0] = 0;
strcat(tmp, num_str(entAmt[k]));
ui_2d_text_v(W - g_m - (small ? 6 : 12), y + rh / 2, tmp, 0xFFE6EBF5, fs + 2,
VML_ANCHOR_RIGHT, VML_VANCHOR_MIDDLE, VML_FONT_BOLD);
y = y + g_rowH;
}
}
ui_2d_present();
}
/* ── 交互 ── */
/* -- Input -- */
void tap(int x, int y)
{
int i, cw, half;
layout();
if (y >= g_catY && y < g_catY + g_chipH) {
cw = (W - g_m * 2) / N_CAT;
i = (x - g_m) / cw;
if (i >= 0 && i < N_CAT) { cat = i; ui_audio_beep(760, 30); }
return;
}
if (y >= g_stepY && y < g_stepY + g_stepH) {
cw = (W - g_m * 2) / 4;
i = (x - g_m) / cw;
if (i == 0) amount = clampi(amount - 10, 0, 999999);
else if (i == 1) amount = clampi(amount - 1, 0, 999999);
else if (i == 2) amount = clampi(amount + 1, 0, 999999);
else if (i == 3) amount = clampi(amount + 10, 0, 999999);
if (i >= 0 && i < 4) ui_audio_beep(520, 30);
return;
}
if (y >= g_actY && y < g_actY + g_actH) {
half = (W - g_m * 2 - 6) / 2;
if (x >= g_m && x < g_m + half) { add_entry(); return; }
if (x >= g_m + half + 6 && x < g_m + half + 6 + half) { clear_all(); return; }
}
}
int main(void)
{
int m[4];
int t;
int lastX = -1, lastY = -1, sawTouch = 0;
g_lang = ui_scr_get_language();
ui_win_open_ex(g_lang == 0 ? "记账本" : "Expenses", 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);
cat = 0;
amount = 0;
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) {
lastX = m[1]; lastY = m[2]; sawTouch = 1;
tap(m[1], m[2]);
draw();
continue;
}
if (t == VML_MSG_MOUSEDOWN) {
if (sawTouch && m[1] == lastX && m[2] == lastY) { sawTouch = 0; continue; }
tap(m[1], m[2]);
draw();
continue;
}
if (t == VML_MSG_KEYDOWN) {
if (m[1] == 37) { amount = clampi(amount - 1, 0, 999999); draw(); }
else if (m[1] == 39) { amount = clampi(amount + 1, 0, 999999); draw(); }
else if (m[1] == 13) { add_entry(); draw(); }
continue;
}
}
ui_scr_set_keep_on(0);
return 0;
}