php/app/stopwatch.php · 4126 字节 · 下载原文
<?php
// 秒表 —— 用 **PHP** 写的手机小应用(计时)
// Stopwatch -- a small phone app written in **PHP** (timing)
//
// 功能:点左半屏开始 / 暂停,点右半屏清零;读数 `分:秒.百分秒`。
// 计时驱动用定时器拍数(每 50ms 一拍,20 拍 = 1 秒)。
//
// 本前端的写法要点(与 ui/demo_ui.php 同一套):
// · 库函数直接按名字写;`ui_win_open_ex(标题, 0, 0, 0, 0)`;
// · `/` 是**浮点除法** ⇒ 一律 `intdiv()`;
// · 数字上屏靠字符串拼接(`"得分 " . $n`),`strval()` 不存在。
//
// 跑法 —— 手机:vml run examples/php/stopwatch.php
// 跑法 —— 桌面:vmlcli Examples/php/stopwatch.php --screen 480x640 --frames /tmp/fr
$k = ui_win_open_ex("秒表", 0, 0, 0, 0);
ui_scr_set_keep_on(1);
$w = ui_scr_get_w();
$h = ui_scr_get_h();
if ($w <= 0) { $w = 360; }
if ($h <= 0) { $h = 620; }
// 布局随画布缩放:字号跟着短边,读数各段按字号比例排布,按钮贴底部。
// Layout scales with the canvas: fonts follow the short side, clock segments keep their
// proportions, buttons hug the bottom so the tap zones stay reachable.
$s = $w;
if ($h < $s) { $s = $h; }
$fz = intdiv($s * 16, 360);
if ($fz < 8) { $fz = 8; }
$ft = intdiv($s * 46, 360);
if ($ft < 14) { $ft = 14; }
$fb = intdiv($s * 18, 360);
if ($fb < 9) { $fb = 9; }
$fnote = intdiv($s * 11, 360);
if ($fnote < 8) { $fnote = 8; }
$titleY = intdiv($h * 120, 620);
if ($titleY < $fz + 4) { $titleY = $fz + 4; }
$timeY = $titleY + intdiv($ft, 2) + $ft;
$margin = intdiv($w, 20);
if ($margin < 6) { $margin = 6; }
$bgap = intdiv($w, 30);
if ($bgap < 4) { $bgap = 4; }
$bw = intdiv($w - $margin * 2 - $bgap, 2);
$bh = intdiv($h * 60, 620);
if ($bh < 18) { $bh = 18; }
$btnY = $h - intdiv($s, 12) - $bh;
$cx = intdiv($w, 2);
$p1 = intdiv($ft * 90, 46);
$p2 = intdiv($ft * 30, 46);
$p3 = intdiv($ft * 10, 46);
$p4 = intdiv($ft * 80, 46);
$p5 = intdiv($ft * 100, 46);
$running = 0;
$acc = 0;
$frames = 0;
$tid = ui_timer_set(50, 0);
while ($frames < 20000) {
$sec = intdiv($acc, 20);
$min = intdiv($sec, 60);
$sec = $sec - $min * 60;
$cs = ($acc - intdiv($acc, 20) * 20) * 5;
$ts = "" . $min;
$tc = "" . $sec;
$tcs = "" . $cs;
if ($cs < 10) { $tcs = "0" . $tcs; }
$k = ui_2d_clear(-15724520);
$k = ui_2d_text($cx, $titleY, "秒表", -6643536, $fz, 1);
$k = ui_2d_text($cx - $p1, $timeY, $ts, -1513232, $ft, 0);
$k = ui_2d_text($cx - $p2, $timeY, ":", -6643536, $ft, 0);
$k = ui_2d_text($cx + $p3, $timeY, $tc, -1513232, $ft, 0);
$k = ui_2d_text($cx + $p4, $timeY, ".", -6643536, $ft, 0);
$k = ui_2d_text($cx + $p5, $timeY, $tcs, -1513232, $ft, 0);
$btnX2 = $w - $margin - $bw;
if ($running == 1) {
$k = ui_2d_rect($margin, $btnY, $bw, $bh, -5427684, 1, 0, 10);
$k = ui_2d_text($margin + intdiv($bw, 2), $btnY + intdiv($bh, 2) + intdiv($fb, 10), "暂停", -1513232, $fb, 1);
} else {
$k = ui_2d_rect($margin, $btnY, $bw, $bh, -13730510, 1, 0, 10);
$k = ui_2d_text($margin + intdiv($bw, 2), $btnY + intdiv($bh, 2) + intdiv($fb, 10), "开始", -1513232, $fb, 1);
}
$k = ui_2d_rect($btnX2, $btnY, $bw, $bh, -13739380, 1, 0, 10);
$k = ui_2d_text($btnX2 + intdiv($bw, 2), $btnY + intdiv($bh, 2) + intdiv($fb, 10), "清零", -1513232, $fb, 1);
$k = ui_2d_text($cx, $h - intdiv($s, 40), "左半屏 开始/暂停 · 右半屏 清零", -6643536, $fnote, 1);
$k = ui_2d_present();
$msg = ui_msg_fetch_wait(0);
if ($msg == 10) { $frames = 20000; }
if ($msg == 6) {
$ax = ui_msg_get_a();
if ($ax < intdiv($w, 2)) {
if ($running == 1) { $running = 0; } else { $running = 1; }
} else {
$acc = 0;
$running = 0;
}
}
if ($msg == 1) {
$key = ui_msg_get_a();
if ($key == 27) { $frames = 20000; }
}
if ($msg == 9) {
$frames = $frames + 1;
if ($running == 1) { $acc = $acc + 1; }
}
}
ui_timer_kill($tid);
ui_scr_set_keep_on(0);
$k = ui_win_close();
echo "stopwatch done", chr(10);
?>