Advertisement
ben_mkiv

hazeUI.lua

Apr 24th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.78 KB | None | 0 0
  1. borders = require("borders")
  2.  
  3. hazeUI = {
  4.     els = {},
  5.     lastTouchUser = nil,
  6.     currentScreen = nil,
  7.     config = { screen = false, gpu = false },
  8.     gpu = nil,
  9.     super = self }
  10.  
  11. function copy (t) -- shallow-copy a table
  12.     if type(t) ~= "table" then return t end
  13.     local meta = getmetatable(t)
  14.     local target = {}
  15.     for k, v in pairs(t) do target[k] = v end
  16.     setmetatable(target, meta)
  17.     return target end
  18.  
  19. function clone (t) -- deep-copy a table
  20.     if type(t) ~= "table" then return t end
  21.     local meta = getmetatable(t)
  22.     local target = {}
  23.     for k, v in pairs(t) do
  24.         if type(v) == "table" then target[k] = clone(v)
  25.         else target[k] = v end end
  26.     setmetatable(target, meta)
  27.     return target end
  28.  
  29. function hazeUI:drawStatusbar(x, y, w, h, s, j, prefix)
  30.     perc = math.floor((100/s) * j)
  31.     bar = 1+math.floor( 0.5 + (((w-2) / s) * j))
  32.     if prefix ~= nil then self.gpu.fill(x, y, w, h+1, " ");
  33.     else self.gpu.fill(x, y, w, h, " "); end
  34.     self.gpu.set(x, y, "[")
  35.     self.gpu.set(x+w, y, "]")
  36.     self.gpu.fill(x+1, y, bar, 1, "#");
  37.     self.gpu.fill(x+1+bar, y, (w-bar-1), 1, "-");
  38.     if prefix ~= nil then self.gpu.set(x, y+1, perc.."% ".. prefix.." "..j.." of "..s) end
  39. end
  40.  
  41. function hazeUI:drawElement(el)
  42.     if not el or el == nil then return end
  43.  
  44.     local text = el.text
  45.  
  46.     if type(text) == "string" then text = { text } end
  47.  
  48.     local xOffset = {}
  49.  
  50.     if not text then text = "" end
  51.  
  52.     for i=1,#text do
  53.         if string.len(text[i]) > (el.w - (2*el.textPadding)) then
  54.             text[i] = string.sub(text[i], 0, (el.w-(2*el.textPadding)))
  55.         end
  56.  
  57.         if el.textalign == "center" then
  58.             xOffset[i] = math.floor((el.w-(2*el.textPadding)-string.len(text[i])) / 2)
  59.         elseif el.textalign == "right" then
  60.             xOffset[i] = (el.w-(2*el.textPadding)) - string.len(text[i])
  61.         else
  62.             xOffset[i] = 0
  63.         end
  64.     end
  65.  
  66.     if el.fg then self.gpu.setForeground(el.fg) end
  67.     if el.bg then self.gpu.setBackground(el.bg) end
  68.  
  69.     self.gpu.fill(el.x, el.y, el.w, el.h, " ")
  70.  
  71.     yT = el.y + math.ceil((el.h-#text)/2)
  72.  
  73.     for i=1,#text do
  74.         xT = el.x + el.textPadding + xOffset[i]
  75.         self.gpu.set(xT, yT+i-1, text[i])
  76.     end
  77.  
  78.     if el.border ~= nil then
  79.         borders.draw(el.x, el.y, el.w, el.h, el.borderColor, el.bg, el.border, self.gpu)
  80.     end
  81.  
  82.     self.gpu.setBackground(0x000000)
  83.     self.gpu.setForeground(0xFFFFFF)
  84. end
  85.  
  86. function hazeUI:flushElements(force)
  87.     if force == nil then force = false end
  88.     for j=1,#self.els do
  89.         local fl = false
  90.         if self.els[j].window ~= self.currentScreen then
  91.             if self.els[j].window ~= "all" then fl = true end
  92.         end
  93.  
  94.         if force ~= false and self.els[j].window ~= "all" then fl = true end
  95.  
  96.         if fl then
  97.             table.remove(self.els, j)
  98.             self:flushElements(force)
  99.             return
  100.         end
  101.     end
  102. end
  103.  
  104. function hazeUI:drawScreen(name)
  105.     width, height = self.gpu.getResolution()
  106.     self.currentScreen = name
  107.     self.gpu.setBackground(0x0)
  108.     self.gpu.fill(1,1,width,height, " ")
  109.     if name ~= "all" then self:drawGroup("all") end
  110.     self:drawGroup(name)
  111. end
  112.  
  113. function hazeUI:drawGroup(name, subgroup)
  114.     for j=1,#self.els do
  115.         if self.els[j].window == name then
  116.             if not subgroup or self.els[j].group == subgroup then
  117.                 self:drawElement(self.els[j])
  118.             end end end end
  119.  
  120. -- add a button to the local cache
  121. function hazeUI:addButton(x, y, w, h, text, group, foreground, background, textalign, cb, cb_parm)
  122.     local el = {}
  123.     el.x = x
  124.     el.y = y
  125.     el.w = w
  126.     el.h = h
  127.     el.border = nil
  128.     el.borderColor = 0xABFEFE
  129.     el.window = group
  130.     el.bg = background
  131.     el.fg = foreground
  132.     el.text = text
  133.     el.textalign = textalign
  134.     el.textPadding = 1
  135.     el.cb = cb
  136.     el.cb_parm = cb_parm
  137.     table.insert(self.els, el)
  138.     return #self.els
  139. end
  140.  
  141. function hazeUI:setElement(options)
  142.     index = options.index
  143.     if options.border then self.els[index].border = options.border end
  144.     if options.borderColor then self.els[index].borderColor = options.borderColor end
  145.     if options.textPadding then self.els[index].textPadding = options.textPadding end
  146.     if options.cb then self.els[index].cb = options.cb end
  147.     if options.cb_parm then self.els[index].cb_parm = options.cb_parm end
  148.     if options.text then self.els[index].text = options.text end
  149.     if options.textalign then self.els[index].textalign = options.textalign end
  150.     if options.bg then self.els[index].bg = options.bg end
  151.     if options.fg then self.els[index].fg = options.fg end
  152.     if options.window then self.els[index].window = options.window end
  153.     if options.group then self.els[index].group = options.group end
  154.     if options.x then self.els[index].x = options.x end
  155.     if options.y then self.els[index].y = options.y end
  156.     if options.w then self.els[index].w = options.w end
  157.     if options.h then self.els[index].h = options.h end
  158. end
  159.  
  160. -- interpret touch event
  161. function hazeUI:touchEvent(x, y, user)
  162.     self.lastTouchUser = user
  163.     for j=1,#self.els do
  164.         local t = self.els[j]
  165.         if t and t.cb then
  166.             if t.window == self.currentScreen or t.window == "all" then
  167.                 if t.x <= x and (t.x+t.w-1) >= x and t.y <= y and (t.y+t.h-1) >= y then
  168.                     if type(t.cb) == "function" then return t.cb(t.cb_parm)
  169.                     elseif type(t.cb) == "string" then
  170.                         return self.super[t.cb](self.super, t.cb_parm)
  171.                     end
  172.                 end end end end end
  173.  
  174. -- remove all buttons which belongs to name
  175. function hazeUI:removeGroup(name)
  176.     for j=1,#self.els do
  177.         if self.els[j].window == name then
  178.             table.remove(self.els, j)
  179.             -- do that recursion to avoid skipping entrys
  180.             return self:removeGroup(name)
  181.         end end end
  182.  
  183. -- remove all buttons which belongs to name and group
  184. function hazeUI:removeSubGroup(group, name)
  185.     for j=1,#self.els do
  186.         if self.els[j].window == group and self.els[j].group == name then
  187.             table.remove(self.els, j)
  188.             -- do that recursion to avoid skipping entrys
  189.             return self:removeSubGroup(group, name)
  190.         end end end
  191.  
  192. function hazeUI:list(title, text, list, callback, colorHeader, colorsList, preSelect)
  193.     if colorHeader == nil or not colorHeader then
  194.         colorHeader = {}
  195.         colorHeader.title = { bg = 0x0097FF, fg = 0x282828 }
  196.         colorHeader.text  = { bg = 0xFFDB00, fg = 0x282828 }
  197.     end
  198.  
  199.     if colorsList == nil or not colorsList then
  200.         colorsList = {}
  201.         table.insert(colorsList, { bg = 0xFFDF00, fg = 0x282828 })
  202.         table.insert(colorsList, { bg = 0xFFA200, fg = 0x282828 })
  203.     end
  204.  
  205.     self:addButton(2, 3, 70, 1, title, "hazeUI_list", colorHeader.title.fg, colorHeader.title.bg, "left")
  206.     self:addButton(2, 4, 70, 1, text, "hazeUI_list", colorHeader.text.fg, colorHeader.text.bg , "left")
  207.     for i=1,#list do
  208.         local b = self:addButton(2, 6+(2*(i-1)), 70, 2, list[i], "hazeUI_list", colorsList[((i%2)+1)].fg, colorsList[((i%2)+1)].bg, "left",
  209.             function()
  210.                 self:flushElements(true)
  211.                 callback.f({ value = i, label = list[i], e = callback.p })
  212.             end)
  213.  
  214.         if preSelect ~= "" and preSelect == list[i] then
  215.             self:setElement({index = b, bg = 0xDADADA})
  216.         end
  217.  
  218.         os.sleep(0)
  219.     end
  220.  
  221.     self:drawScreen("hazeUI_list")
  222. end
  223.  
  224. function hazeUI:textInput(label, callback)
  225.     local term = require("term")
  226.  
  227.     inputButton = self:addButton(2, 3, #label + 2, 1, label, "hazeUI_textInput", 0x0, 0xFFA200, "left")
  228.     self:drawScreen("hazeUI_textInput")
  229.     term.setCursor(3, 4)
  230.     input = io.read()  
  231.     self:removeGroup("hazeUI_textInput")
  232.     callback.f({ value = input, e = callback.p })
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement