Advertisement
Ewgeniy

hwui

Jul 3rd, 2025 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | None | 0 0
  1. local term = require("term")
  2. local gpu = require("component").gpu
  3. local event = require("event")
  4.  
  5. local tui = {
  6.   w = 80,
  7.   h = 25,
  8.   dirty = true
  9. }
  10.  
  11. function tui.updateResolution()
  12.   tui.w, tui.h = gpu.getResolution()
  13. end
  14.  
  15. function tui.clear()
  16.   gpu.setBackground(0x000000)
  17.   gpu.setForeground(0xFFFFFF)
  18.   term.clear()
  19. end
  20.  
  21. function tui.markDirty()
  22.   tui.dirty = true
  23. end
  24.  
  25. function tui.label(x, y, text)
  26.   gpu.set(x, y, text)
  27. end
  28.  
  29. function tui.centerText(y, text)
  30.   local x = math.floor((tui.w - #text) / 2)
  31.   gpu.set(x, y, text)
  32. end
  33.  
  34. function tui.tabBar(tabs, current)
  35.   gpu.setBackground(0x444444)
  36.   gpu.setForeground(0xFFFFFF)
  37.   term.setCursor(1, 1)
  38.   local pos = 1
  39.   for i, tab in ipairs(tabs) do
  40.     local name = " " .. tab.name .. " "
  41.     gpu.set(pos, 1, name)
  42.     if i == current then
  43.       gpu.setBackground(0xAAAAAA)
  44.       gpu.setForeground(0x000000)
  45.       gpu.set(pos, 1, name)
  46.       gpu.setBackground(0x444444)
  47.       gpu.setForeground(0xFFFFFF)
  48.     end
  49.     pos = pos + #name
  50.   end
  51.   for i = pos, tui.w do
  52.     gpu.set(i, 1, " ")
  53.   end
  54. end
  55.  
  56. function tui.box(x, y, x2, y2, title)
  57.   gpu.setBackground(0x222222)
  58.   gpu.setForeground(0xFFFFFF)
  59.   for i = y, y2 do
  60.     gpu.fill(x, i, x2 - x + 1, 1, " ")
  61.   end
  62.   if title then
  63.     gpu.set(x + 2, y, " " .. title .. " ")
  64.   end
  65. end
  66.  
  67. function tui.button(x, y, label, callback)
  68.   local width = #label + 2
  69.   gpu.setBackground(0x0066CC)
  70.   gpu.setForeground(0xFFFFFF)
  71.   gpu.set(x, y, " " .. label .. " ")
  72.   table.insert(tui._buttons, {
  73.     x = x, y = y,
  74.     w = width, h = 1,
  75.     cb = callback
  76.   })
  77. end
  78.  
  79. function tui.progress(x, y, width, value, max, label)
  80.   local ratio = math.min(1, value / max)
  81.   local filled = math.floor(width * ratio)
  82.   local text = string.format("[%s%s] %s%% %s", string.rep("=", filled), string.rep(" ", width - filled), math.floor(ratio * 100), label or "")
  83.   gpu.set(x, y, text)
  84. end
  85.  
  86. function tui.checkbox(x, y, label, state, callback)
  87.   local box = state and "[x]" or "[ ]"
  88.   gpu.set(x, y, box .. " " .. label)
  89.   table.insert(tui._buttons, {
  90.     x = x, y = y,
  91.     w = #box + 1 + #label,
  92.     h = 1,
  93.     cb = function()
  94.       callback(not state)
  95.     end
  96.   })
  97. end
  98.  
  99. function tui.slider(x, y, width, value, max, callback)
  100.   local percent = math.floor((value / max) * 100)
  101.   local bar = "[" .. string.rep("=", math.floor((width - 2) * value / max)) .. string.rep(" ", width - 2 - math.floor((width - 2) * value / max)) .. "] " .. percent .. "%"
  102.   gpu.set(x, y, bar)
  103.   table.insert(tui._buttons, {
  104.     x = x, y = y,
  105.     w = width,
  106.     h = 1,
  107.     cb = function()
  108.       value = value + 10
  109.       if value > max then value = 0 end
  110.       callback(value)
  111.       tui.markDirty()
  112.     end
  113.   })
  114. end
  115.  
  116. function tui.toast(msg, duration)
  117.   duration = duration or 1
  118.   tui._toast = {msg = msg, until = computer.uptime() + duration}
  119. end
  120.  
  121. function tui.confirm(msg, callback)
  122.   local x = math.floor(tui.w / 2 - (#msg + 6) / 2)
  123.   local y = math.floor(tui.h / 2)
  124.   tui.box(x, y - 1, x + #msg + 6, y + 2, "Подтверждение")
  125.   gpu.set(x + 2, y, msg)
  126.   tui.button(x + 2, y + 1, "Да", function()
  127.     tui._confirm = nil
  128.     callback(true)
  129.   end)
  130.   tui.button(x + 8, y + 1, "Нет", function()
  131.     tui._confirm = nil
  132.     callback(false)
  133.   end)
  134.   tui._confirm = true
  135. end
  136.  
  137. function tui.start(update, render)
  138.   tui.updateResolution()
  139.   tui._buttons = {}
  140.   tui._toast = nil
  141.   tui._confirm = nil
  142.   tui.clear()
  143.   render()
  144.   tui.dirty = false
  145.  
  146.   while true do
  147.     if tui.dirty then
  148.       tui._buttons = {}
  149.       tui.clear()
  150.       render()
  151.       if tui._toast and computer.uptime() > tui._toast.until then
  152.         tui._toast = nil
  153.       end
  154.       if tui._toast then
  155.         tui.centerText(tui.h, tui._toast.msg)
  156.       end
  157.       tui.dirty = false
  158.     end
  159.  
  160.     update()
  161.  
  162.     local ev = {event.pull(0.05)}
  163.     if ev[1] == "touch" then
  164.       local _, _, tx, ty = table.unpack(ev)
  165.       for _, btn in ipairs(tui._buttons) do
  166.         if tx >= btn.x and tx <= btn.x + btn.w - 1 and ty == btn.y then
  167.           btn.cb()
  168.           break
  169.         end
  170.       end
  171.     elseif ev[1] == "key_down" and ev[4] == 211 then
  172.       os.exit()
  173.     end
  174.   end
  175. end
  176.  
  177. return tui
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement