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

dart/app/calendar.dart · 7245 字节 · 下载原文

▶ 在浏览器里试玩

// 日历 —— Dart 版小应用(日期 / 月历)
// Calendar — a small app in the Dart edition (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
// 也顺带把「数字怎么上屏」再走一遍(Dart 用字符串插值)。
// and grid drawing), and walks "how do numbers reach the screen" once more (Dart uses interpolation).
//
// ## Dart 这一路的写法约束(见 `Examples/dart/app/snake.dart` 文件头)
// ## Style constraints on this Dart path (see the header of `Examples/dart/app/snake.dart`)
//
//   · **全部状态放 `main` 的局部变量**;颜色写十六进制;整除写 `~/`;数字用字符串插值;
//   · **all state lives in locals of `main`**; hex colours; `~/` for integer division; string interpolation;
//   · ⚠ 双循环里不要把循环内赋值的变量作实参 ⇒ 绘制内联表达式。
//   · ⚠ never pass a variable assigned inside a nested loop as an argument => inline when drawing.
//
// 跑法 —— 手机:`vml run examples/dart/calendar.dart`
// How to run — on the phone: `vml run examples/dart/calendar.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/calendar.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/calendar.dart --screen 480x640 --frames /tmp/fr`

import 'vml_ui.dart';

void main() {
  int sw = 360;
  int sh = 620;
  int cell = 50;
  int ox = 10;
  int oy = 120;
  int year = 2026;
  int month = 11;
  int todayY = 2026;
  int todayM = 11;
  int todayD = 14;
  int frames = 0;
  int i = 0;
  int t = 0;
  int k = 0;
  int count = 0;
  int start = 0;
  int days = 0;
  int col = 0;
  int row = 0;
  int done = 0;
  int titleY = 34;
  int weekY = 68;
  int gridY = 84;
  int cellH = 0;
  String title = "";
  String hud = "";

  int cBg = 0xFF101018;
  int cCell = 0xFF1A1A24;
  int cText = 0xFFE8E8F0;
  int cDim = 0xFF9AA0B0;
  int cToday = 0xFF51E86E;

  List<int> mdays = [];
  mdays.add(31);
  mdays.add(28);
  mdays.add(31);
  mdays.add(30);
  mdays.add(31);
  mdays.add(30);
  mdays.add(31);
  mdays.add(31);
  mdays.add(30);
  mdays.add(31);
  mdays.add(30);
  mdays.add(31);

  int lang = ui_scr_get_language();
  ui_win_open("日历", 0, 0);

  sw = ui_scr_get_w();
  sh = ui_scr_get_h();
  if (sw <= 0) { sw = 360; }
  if (sh <= 0) { sh = 620; }
  // 版面按「宽 × 高」两头夹:小屏时格子不能把底栏顶出画布
  // Constrain the layout on both axes so the grid never pushes the footer off-screen.
  cellH = (sh - gridY - 26) ~/ 6;
  cell = (sw - 20) ~/ 7;
  if (cellH < cell) { cell = cellH; }
  if (cell < 14) { cell = 14; }
  ox = (sw - 7 * cell) ~/ 2;
  oy = gridY;

  int tid = ui_timer_set(200, 1);
  ui_2d_present();

  while (done == 0 && ui_win_is_closed() == 0) {
    t = ui_msg_fetch_wait(200);
    if (t == 10) { break; }

    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) {
      if (ui_msg_get_a() < 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 = mdays[month - 1];
    if (month == 2) {
      if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { days = 29; }
    }

    // 1 号是星期几:从 1900-01-01(星期一)累加天数
    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;

    ui_2d_clear(cBg);

    // 标题
    if (lang == 0) { title = "$year 年 $month 月"; }
    else { title = "$year-$month"; }
    ui_2d_text(ox, titleY, title, cText, 20, 0);

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

    // 日期格
    i = 0;
    while (i < days) {
      col = (start + i) % 7;
      row = (start + i) ~/ 7;
      if (year == todayY && month == todayM && i + 1 == todayD) {
        ui_2d_rect(ox + col * cell + 1, oy + row * cell + 1, cell - 2, cell - 2, cToday, 1, 0, 2);
        hud = "${i + 1}";
        ui_2d_text(ox + col * cell + cell ~/ 2, oy + row * cell + cell ~/ 2 + 6, hud, cBg, cell ~/ 2, 1);
      } else {
        ui_2d_rect(ox + col * cell + 1, oy + row * cell + 1, cell - 2, cell - 2, cCell, 1, 0, 2);
        hud = "${i + 1}";
        ui_2d_text(ox + col * cell + cell ~/ 2, oy + row * cell + cell ~/ 2 + 6, hud, cText, cell ~/ 2, 1);
      }
      i = i + 1;
    }

    ui_2d_text(sw ~/ 2, sh - 16, "← → 切月 · ↑ ↓ 跳年 · ESC 退出", cDim, 11, 1);
    ui_2d_present();

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

  ui_timer_kill(tid);
  ui_win_close();
}