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

ada/app/stopwatch.adb · 3969 字节 · 下载原文

▶ 在浏览器里试玩

-- 秒表 —— 用 **Ada** 写的手机小应用(计时)
-- Stopwatch -- a small phone app written in **Ada** (timing)
--
-- 功能:点左半屏开始 / 暂停,点右半屏清零;读数 `分:秒.百分秒` 用 Integer'Image 显示。
--
-- ## 本语言的实测约束(探针量的)
--   · 库函数 `ui_*` 用 `K := ui_...(…)` 调用;`with VML_UI;` 取声明;
--   · 数组 1 基;`Integer'Image(v)` 转字符串(**带一个前导空格**,排版时留意);
--   · 逻辑运算符是 `and` / `or` / `not`,不等是 `/=`;
--   · `while 条件 loop … end loop;`、`if … then … elsif … else … end if;`
--
-- 跑法 —— 手机:vml run examples/ada/stopwatch.adb
-- 跑法 —— 桌面:vmlcli Examples/ada/stopwatch.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Stopwatch is
   K : Integer := 0;
   W : Integer := 0;
   H : Integer := 0;
   Running : Integer := 0;
   Acc : Integer := 0;
   Last_Ms : Integer := 0;
   Frames : Integer := 0;
   T : Integer := 0;
   Tid : Integer := 0;
   Total : Integer := 0;
   Cs : Integer := 0;
   Sec : Integer := 0;
   Min : Integer := 0;
   Lang : Integer := 0;
   Fs : Integer := 40;
   Bs : Integer := 16;
   Hfs : Integer := 11;
   Tty : Integer := 120;
   Dgy : Integer := 200;
   Bty : Integer := 320;
begin
   Lang := ui_scr_get_language();
   if Lang = 0 then
      K := ui_win_open(0, 0, 0);
   else
      K := ui_win_open(0, 0, 0);
   end if;
   W := ui_scr_get_w();
   H := ui_scr_get_h();
   if W <= 0 then
      W := 360;
   end if;
   if H <= 0 then
      H := 620;
   end if;
   Fs := W / 6;
   if Fs > 96 then
      Fs := 96;
   end if;
   if Fs < 24 then
      Fs := 24;
   end if;
   Bs := Fs * 2 / 5;
   if Bs < 12 then
      Bs := 12;
   end if;
   if Bs > 28 then
      Bs := 28;
   end if;
   Hfs := H / 48;
   if Hfs < 9 then
      Hfs := 9;
   end if;
   if Hfs > 13 then
      Hfs := 13;
   end if;
   Tty := H / 6;
   Dgy := H / 2 - Fs / 2;
   Bty := H * 3 / 4;

   Last_Ms := ui_time_get_ms();
   K := ui_scr_set_keep_on(1);
   Tid := ui_timer_set(50, 0);

   while ui_win_is_closed() = 0 loop
      if Running = 1 then
         Total := Acc + (ui_time_get_ms() - Last_Ms);
      else
         Total := Acc;
      end if;
      Cs := (Total / 10) mod 100;
      Sec := (Total / 1000) mod 60;
      Min := Total / 60000;

      K := ui_2d_clear(-15724520);
      K := ui_2d_text(W / 2, Dgy,
         Integer'Image(Min) & ":" & Integer'Image(Sec) & "." & Integer'Image(Cs),
         -1513232, Fs, 1);

      if Lang = 0 then
         K := ui_2d_text(W / 2, Tty, "秒表", -6643536, Bs, 1);
      else
         K := ui_2d_text(W / 2, Tty, "Stopwatch", -6643536, Bs, 1);
      end if;
      if Running = 1 then
         K := ui_2d_text(W / 4, Bty, "暂停", -2069424, Bs, 1);
      else
         K := ui_2d_text(W / 4, Bty, "开始", -11409298, Bs, 1);
      end if;
      K := ui_2d_text(W / 4 * 3, Bty, "清零", -11890471, Bs, 1);
      K := ui_2d_text(W / 2, H - Hfs - 10, "左半屏 开始/暂停 · 右半屏 清零", -6643536, Hfs, 1);
      K := ui_2d_present();

      T := ui_msg_fetch_wait(0);
      if T = 10 then
         exit;
      end if;

      if T = 6 or T = 4 then
         if ui_msg_get_a() < W / 2 then
            if Running = 1 then
               Acc := Acc + (ui_time_get_ms() - Last_Ms);
               Running := 0;
            else
               Last_Ms := ui_time_get_ms();
               Running := 1;
            end if;
         else
            Acc := 0;
            Last_Ms := ui_time_get_ms();
            Running := 0;
         end if;
      end if;

      if T = 1 then
         if ui_msg_get_a() = 27 then
            exit;
         end if;
      end if;

      if T = 9 then
         Frames := Frames + 1;
         if Frames >= 20000 then
            exit;
         end if;
      end if;
   end loop;

   K := ui_timer_kill(Tid);
   K := ui_scr_set_keep_on(0);
   K := ui_win_close();
end Stopwatch;