local M = {} local COLOR_BACKDROP = 0x000000C0 local COLOR_BG = 0x303030FF local COLOR_BTN = 0x404040FF local COLOR_BTN_HOT = 0x606060FF local COLOR_TEXT = 0xE0E0E0FF local FONT_TITLE = 22 local FONT_BTN = 18 local PADDING = 18 -- def = { -- title = "Confirm", -- body = "text or array of lines", -- buttons = { { id="cancel", label="Cancel" }, { id="ok", label="Launch anyway", disabled=false } }, -- } function M.render(def, screen_w, screen_h) -- Backdrop. engine.render.draw_rect(0, 0, screen_w, screen_h, COLOR_BACKDROP) local w, h = math.min(screen_w - 80, 480), 0 -- Compute height: title + body-lines + buttons-row. local body_lines = type(def.body) == "table" and def.body or { def.body or "" } h = PADDING * 4 + FONT_TITLE + #body_lines * (FONT_BTN + 4) + 40 local x = (screen_w - w) / 2 local y = (screen_h - h) / 2 engine.render.draw_rect(x, y, w, h, COLOR_BG) engine.render.draw_text(def.title, x + PADDING, y + PADDING, FONT_TITLE, COLOR_TEXT) local cur_y = y + PADDING + FONT_TITLE + PADDING for _, line in ipairs(body_lines) do engine.render.draw_text(line, x + PADDING, cur_y, FONT_BTN, COLOR_TEXT) cur_y = cur_y + FONT_BTN + 4 end -- Buttons row at the bottom. local btn_w = (w - PADDING * (#def.buttons + 1)) / #def.buttons local btn_y = y + h - PADDING - 32 for i, b in ipairs(def.buttons) do local bx = x + PADDING + (i - 1) * (btn_w + PADDING) engine.render.draw_rect(bx, btn_y, btn_w, 32, b.disabled and 0x303030FF or COLOR_BTN) local lw, lh = engine.render.measure_text(b.label, FONT_BTN) engine.render.draw_text(b.label, bx + (btn_w - lw) / 2, btn_y + (32 - lh) / 2, FONT_BTN, COLOR_TEXT) b.rect = { x = bx, y = btn_y, w = btn_w, h = 32 } end end function M.handle_click(def, mx, my) for _, b in ipairs(def.buttons) do if not b.disabled and b.rect and engine.spatial.aabb_contains_point(b.rect, mx, my) then return b.id end end return nil end return M