-- demo_ui.adb —— Ada 第四层：**最新 UI 接口**（宿主 `ui_*` 图元 + 绘图窗口）
-- demo_ui.adb -- Ada layer 4: **the newest UI interface** (host ui_* primitives + a drawing window)
--
-- 跑法（桌面 vmlcli）：
-- How to run (desktop vmlcli):
--   dotnet scripts/vmlcli/bin/Release/net10.0/vmlcli.dll Examples/ada/ui/demo_ui.adb \
--       --screen 480x640 --frames /tmp/ada_ui_frames/
--
-- ⚠ 要看画面请用 `--frames 目录`，不要用 `--frame` 单个文件：`ui_win_close()` 会在宿主侧
-- ⚠ To see images use --frames DIR, not --frame SINGLE-FILE: ui_win_close() clears the scene
--   把整个场景置空，跑完之后 `--frame` 已经没有场景可取；`--frames` 是每帧 present 当场拍快照。
--   on the host side, so afterwards --frame has no scene left; --frames snapshots at each present.
--
-- 四条本层事实：
-- Four facts about this layer:
--   ① 开窗显式：`ui_win_open_ex(标题, 宽, 高, 旋转, 手柄)`；宽高传 0 ⇒ 宿主给真实可用区。
--   ① Opening is explicit: ui_win_open_ex(title, w, h, rotatable, gamepad); passing 0 lets the host use the real area.
--   ② 尺寸要**开窗之后**问（开窗前的估算与真实画布总有出入）。
--   ② Ask for the size **after** opening (the pre-open estimate always differs from the real canvas).
--   ③ 图元只是追加进场景，`ui_2d_present()` 才是帧边界。
--   ③ primitives only append to the scene; ui_2d_present() is the frame boundary.
--   ④ 输入是消息队列：`ui_msg_fetch_wait(毫秒)` / `ui_win_is_closed()`。
--   ④ input is a message queue: ui_msg_fetch_wait(ms) / ui_win_is_closed().
--
-- 颜色是 0xAARRGGBB 作为 32 位有符号整数的十进制值（每行后面标了十六进制，别手算）。
-- Colours are 0xAARRGGBB as signed-32-bit decimals (each line carries its hex value; never hand-compute).
--
-- ◆ 有界：画满 20 帧、或收到任意按键（消息类型 1 = KeyDown）、或点了返回箭头就退出。
-- ◆ Bounded: exits after 20 frames, on any key (message type 1 = KeyDown), or on the back arrow.

with VML_UI;

procedure Demo_Ui is
   W : Integer := 0;
   H : Integer := 0;
   Frame : Integer := 0;
   Msg : Integer := 0;
   K : Integer := 0;
begin
   K := ui_win_open_ex("Ada UI demo", 0, 0, 0, 0);

   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;

   Frame := 0;
   while Frame < 20 loop
      -- 每帧重画（保留模式：场景每帧从头追加）
      -- Redraw every frame (retained mode: the scene is appended from scratch each frame)
      K := ui_2d_clear(-15722464);                                   -- 0xFF101820 深底
      K := ui_2d_rect(16, 16, W - 32, 96, -11890471, 1, 0, 10);      -- 0xFF4A90D9 面板
      K := ui_2d_circle(W / 2, H / 2, 48, -1618884, 1, 0);           -- 0xFFE74C3C 红圆
      K := ui_2d_line(16, H - 80, W - 16, H - 80, -12961206, 2);     -- 0xFF3A3A4A 分隔线
      K := ui_2d_text(24, 30, "Ada UI demo", -1513232, 18, 0);       -- 0xFFE8E8F0 标题
      K := ui_2d_text(24, 62, "rect / circle / line / text", -6643536, 12, 0);  -- 0xFF9AA0B0
      K := ui_2d_text(16, H - 56, "press any key to exit", -6643536, 12, 0);

      K := ui_2d_present();
      Frame := Frame + 1;

      Msg := ui_msg_fetch_wait(40);
      if Msg = 1 then
         Frame := 20;
      end if;
      if ui_win_is_closed() /= 0 then
         Frame := 20;
      end if;
   end loop;

   K := ui_win_close();
end Demo_Ui;
