ruby/app/calendar.rb · 11423 字节 · 下载原文
# 日历 —— 用 **Ruby** 写的手机小应用(日期 / 月历)
# Calendar — a small phone app written in **Ruby** (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
# 也顺带把「数字怎么上屏」再走一遍(Ruby 用 .to_s)。
# and grid drawing), and walks "how do numbers reach the screen" once more (Ruby uses .to_s).
#
# ## Ruby 这一路的写法约束(见 Examples/ruby/app/minesweeper.rb 文件头)
# ## Style constraints on this Ruby path (see the header of Examples/ruby/app/minesweeper.rb)
#
# · **所有 def 放在文件最前面,全局变量赋值在它们之后**;
# · **every def sits at the very top, and global assignments come after them**;
# · 状态一律 g_ 前缀;数组用 [] + push 填充;数字转字符串用 .to_s;颜色写负数十进制;
# · state is g_-prefixed; arrays are built with [] + push; numbers to strings use .to_s;
# colours are negative decimals;
# · **"只写不读"的名字在函数里写的是该函数自己的局部** ⇒ 用 cal_touch_state 先把状态名各读一遍。
# · **a name that is only written and never read gets written to the function's own local**
# ⇒ cal_touch_state reads every state name once first.
#
# 跑法 —— 手机:vml run examples/ruby/calendar.rb
# How to run — on the phone: vml run examples/ruby/calendar.rb
# 跑法 —— 桌面:vmlcli Examples/ruby/calendar.rb --screen 480x640 --frames /tmp/fr
# How to run — on the desktop: vmlcli Examples/ruby/calendar.rb --screen 480x640 --frames /tmp/fr
# UI 库的显式引用(用户 2026-10-01 定的口径),声明文件 Lib/ruby/vml_ui.rb 由 GenLib 生成。
require 'vml_ui'
# ══════════════════════════════════════════════════════════════════════════
# 第一段:全部函数(⚠ 必须排在"给全局赋值"之前,理由见文件头那条)
# ══════════════════════════════════════════════════════════════════════════
def cal_touch_state()
return g_mdays.length + g_year + g_month + g_today_y + g_today_m + g_today_d + g_frames + g_cell + g_ox + g_oy + g_sw + g_sh + g_lang + g_pad + g_fs_title + g_fs_head + g_fs_day + g_fs_foot + g_ty + g_hy
end
def cal_relayout()
g_sw = ui_scr_get_w()
g_sh = ui_scr_get_h()
if g_sw <= 0
g_sw = 360
end
if g_sh <= 0
g_sh = 620
end
# 左右留白随画布走;格子同时受**宽和高**约束(240x240 上按宽度算出的 30px
# 会让 6 行格子越过画布底 240)—— 行数固定按 6 行留位,翻月不会上下跳。
# Padding follows the canvas; the cell is bounded by **both width and height**
# (on 240x240 the width-derived 30 px made six rows overflow the 240 bottom) --
# the row count is always six so changing month never makes the block jump.
g_pad = g_sw / 18
if g_pad < 6
g_pad = 6
end
g_cell = (g_sw - g_pad * 2) / 7
hc = (g_sh - 80 - 34) / 6
if hc < g_cell
g_cell = hc
end
if g_cell < 14
g_cell = 14
end
g_ox = (g_sw - 7 * g_cell) / 2
gh = 6 * g_cell
g_oy = (g_sh - (64 + gh)) / 2 + 64
max_t = g_sh - gh - 40
if g_oy > max_t
g_oy = max_t
end
if g_oy < 70
g_oy = 70
end
# 字号随 min(W,H) 缩放;各档都夹住上下限,极端画布上也不会大到压叠。
# Font sizes scale with min(W,H); each is clamped so even extreme canvases cannot overlap.
g_fs_title = g_cell / 2
if g_fs_title < 12
g_fs_title = 12
end
if g_fs_title > 26
g_fs_title = 26
end
g_fs_head = g_cell / 3
if g_fs_head < 8
g_fs_head = 8
end
if g_fs_head > 14
g_fs_head = 14
end
g_fs_day = g_cell / 3
if g_fs_day < 7
g_fs_day = 7
end
if g_fs_day > 20
g_fs_day = 20
end
g_fs_foot = g_sh / 26
if g_fs_foot < 8
g_fs_foot = 8
end
if g_fs_foot > 14
g_fs_foot = 14
end
g_ty = g_oy - 64
if g_ty < 4
g_ty = 4
end
g_hy = g_oy - 28
if g_hy < g_ty + g_fs_title + 4
g_hy = g_ty + g_fs_title + 4
end
end
def cal_days()
d = g_mdays[g_month - 1]
if g_month == 2
if (g_year % 4 == 0 && g_year % 100 != 0) || g_year % 400 == 0
d = 29
end
end
return d
end
def cal_start()
count = 0
i = 1900
while i < g_year
count = count + 365
if (i % 4 == 0 && i % 100 != 0) || i % 400 == 0
count = count + 1
end
i = i + 1
end
i = 1
while i < g_month
if i == 2
if (g_year % 4 == 0 && g_year % 100 != 0) || g_year % 400 == 0
count = count + 29
else
count = count + 28
end
else
count = count + g_mdays[i - 1]
end
i = i + 1
end
return count % 7
end
def cal_draw()
ui_2d_clear(g_c_bg)
# 标题:整串居中画 —— 小屏上按旧写法(年/月分开、固定 +62/+128)会互相压叠。
# Title: draw it as one centred string -- the old split placement (fixed +62/+128)
# overlapped itself on small canvases.
if g_lang == 0 then ui_2d_text(g_sw / 2, g_ty + g_fs_title, g_year.to_s + " 年 " + g_month.to_s + " 月", g_c_text, g_fs_title, VML_ANCHOR_CENTER) else ui_2d_text(g_sw / 2, g_ty + g_fs_title, g_year.to_s + "-" + g_month.to_s, g_c_text, g_fs_title, VML_ANCHOR_CENTER) end
# 星期表头
i = 0
while i < 7
if g_lang == 0
if i == 0
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "一", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 1
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "二", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 2
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "三", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 3
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "四", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 4
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "五", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 5
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "六", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 6
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "日", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
else
if i == 0
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "Mo", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 1
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "Tu", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 2
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "We", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 3
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "Th", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 4
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "Fr", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 5
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "Sa", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
if i == 6
ui_2d_text(g_ox + i * g_cell + g_cell / 2, g_hy + g_fs_head, "Su", g_c_dim, g_fs_head, VML_ANCHOR_CENTER)
end
end
i = i + 1
end
# 日期格
start = cal_start()
days = cal_days()
i = 0
while i < days
col = (start + i) % 7
row = (start + i) / 7
px = g_ox + col * g_cell
py = g_oy + row * g_cell
if g_year == g_today_y && g_month == g_today_m && i + 1 == g_today_d
ui_2d_rect(px + 1, py + 1, g_cell - 2, g_cell - 2, g_c_today, 1, 0, 2)
ui_2d_text(px + g_cell / 2, py + g_cell / 2 + g_cell / 6, (i + 1).to_s, g_c_bg, g_fs_day, VML_ANCHOR_CENTER)
else
ui_2d_rect(px + 1, py + 1, g_cell - 2, g_cell - 2, g_c_cell, 1, 0, 2)
ui_2d_text(px + g_cell / 2, py + g_cell / 2 + g_cell / 6, (i + 1).to_s, g_c_text, g_fs_day, VML_ANCHOR_CENTER)
end
i = i + 1
end
if g_lang == 0 then ui_2d_text(g_sw / 2, g_sh - g_fs_foot - 6, "← → 切月 · ↑ ↓ 跳年 · ESC 退出", g_c_dim, g_fs_foot, VML_ANCHOR_CENTER) else ui_2d_text(g_sw / 2, g_sh - g_fs_foot - 6, "← → month · ↑ ↓ year · ESC quits", g_c_dim, g_fs_foot, VML_ANCHOR_CENTER) end
end
# ══════════════════════════════════════════════════════════════════════════
# 第二段:全局布局与状态(⚠ 必须在所有 def 之后;文件头那条)
# ══════════════════════════════════════════════════════════════════════════
g_c_bg = -15724520 # 0xFF101018
g_c_cell = -15066588 # 0xFF1A1A24
g_c_text = -1513232 # 0xFFE8E8F0
g_c_dim = -6643536 # 0xFF9AA0B0
g_c_today = -11409298 # 0xFF51E86E
g_lang = ui_scr_get_language()
g_sw = 360
g_sh = 620
g_cell = 50
g_ox = 10
g_oy = 120
g_pad = 10
g_fs_title = 20
g_fs_head = 12
g_fs_day = 12
g_fs_foot = 11
g_ty = 36
g_hy = 78
g_year = 2026
g_month = 11
g_today_y = 2026
g_today_m = 11
g_today_d = 14
g_frames = 0
g_alive = 1
g_mdays = []
g_mdays.push(31)
g_mdays.push(28)
g_mdays.push(31)
g_mdays.push(30)
g_mdays.push(31)
g_mdays.push(30)
g_mdays.push(31)
g_mdays.push(31)
g_mdays.push(30)
g_mdays.push(31)
g_mdays.push(30)
g_mdays.push(31)
ui_win_open("日历", 0, 0)
cal_relayout()
g_tid = ui_timer_set(200, 1)
while g_alive == 1
t = ui_msg_fetch_wait(200)
if t == 10
g_alive = 0
end
if t == 1
k = ui_msg_get_a()
if k == 27
g_alive = 0
end
if k == 37
g_month = g_month - 1
if g_month < 1
g_month = 12
g_year = g_year - 1
end
end
if k == 39
g_month = g_month + 1
if g_month > 12
g_month = 1
g_year = g_year + 1
end
end
if k == 38
g_year = g_year + 1
end
if k == 40
g_year = g_year - 1
end
if g_year < 1900
g_year = 1900
end
if g_year > 2100
g_year = 2100
end
end
if t == 6 || t == 4
if ui_msg_get_a() < g_sw / 2
g_month = g_month - 1
if g_month < 1
g_month = 12
g_year = g_year - 1
end
else
g_month = g_month + 1
if g_month > 12
g_month = 1
g_year = g_year + 1
end
end
if g_year < 1900
g_year = 1900
end
if g_year > 2100
g_year = 2100
end
end
if t == 11
cal_relayout()
end
if t == 9
g_frames = g_frames + 1
if g_frames >= 20000
g_alive = 0
end
end
cal_draw()
ui_2d_present()
end
ui_timer_kill(g_tid)
ui_win_close()