Advertisement
QuicksilverBoy

gui

Feb 5th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. local computer = require("computer")
  2. local component = require("component")
  3. local table = require("table")
  4. local unicode = require("unicode")
  5. local math = require("math")
  6.  
  7. local gui = {}
  8. local screenAddress = component.list("screen")()
  9. local gpuAddress = component.list("gpu")()
  10. ---------------------------------------
  11. local function isPushed(px, py, t, button)
  12.     local x2, y2
  13.  
  14.     if t.W then
  15.         x2 = t.PosX + t.W
  16.     else
  17.         x2 = t.PosX + 1
  18.     end
  19.  
  20.     if t.H then
  21.         y2 = t.PosY + t.H
  22.     else
  23.         y2 = t.PosY + 1
  24.     end
  25.  
  26.     if t.PosX <= px and x2 > px and t.PosY <= py and y2 > py and button == 0 and t.Enabled then
  27.         return true
  28.     else
  29.         return false
  30.     end
  31. end
  32. ---------------------------------------
  33. function gui.createButton(text, x, y, colors, fun)
  34.     local obj = {}
  35.         obj.Text = text
  36.         obj.Type = "button"
  37.         obj.PosX = x
  38.         obj.PosY = y
  39.         obj.W = unicode.wlen(text)
  40.         obj.H = 1
  41.         obj.ColorOn = colors[1]
  42.         obj.ColorTxtOn = colors[2]
  43.         obj.ColorOff = colors[3]
  44.         obj.ColorTxtOff = colors[4]
  45.         obj.Enabled = true
  46.         obj.Call = fun
  47.         obj.Draw = function(self)
  48.             local colorBack = component.proxy(gpuAddress).getBackground(0)
  49.             local colorFore = component.proxy(gpuAddress).getForeground(0xFFFFFF)
  50.             --
  51.             if self.Enabled  then
  52.                 component.proxy(gpuAddress).setBackground(self.ColorOn)
  53.                 component.proxy(gpuAddress).setForeground(self.ColorTxtOn)
  54.             else
  55.                 component.proxy(gpuAddress).setBackground(self.ColorOff)
  56.                 component.proxy(gpuAddress).setForeground(self.ColorTxtOff)
  57.             end
  58.             component.proxy(gpuAddress).set(self.PosX, self.PosY, self.Text)
  59.             --
  60.             component.proxy(gpuAddress).setBackground(colorBack)
  61.             component.proxy(gpuAddress).setForeground(colorFore)
  62.         end
  63.     return obj
  64. end
  65.  
  66. function gui.createFlag(text, x, y, colors)
  67.     local obj = {}
  68.         obj.Text = text
  69.         obj.Type = "flag"
  70.         obj.PosX = x
  71.         obj.PosY = y
  72.         obj.Active = false
  73.         obj.ColorOn = colors[1]
  74.         obj.ColorTxtOn = colors[2] or 0xFFFFFF - colors[1]
  75.         obj.Enabled = true
  76.         obj.Draw = function(self)
  77.             local colorBack = component.proxy(gpuAddress).getBackground(0)
  78.             local colorFore = component.proxy(gpuAddress).getForeground(0xFFFFFF)
  79.             --
  80.             component.proxy(gpuAddress).setBackground(0xFFFFFF)
  81.             component.proxy(gpuAddress).setForeground(0)
  82.             if self.Active then
  83.                 component.proxy(gpuAddress).set(self.PosX, self.PosY, "V")
  84.             else
  85.                 component.proxy(gpuAddress).set(self.PosX, self.PosY, " ")
  86.             end
  87.             component.proxy(gpuAddress).setBackground(self.ColorOn)
  88.             component.proxy(gpuAddress).setForeground(self.ColorTxtOn)
  89.             component.proxy(gpuAddress).set(self.PosX + 1, self.PosY, self.Text)
  90.             --
  91.             component.proxy(gpuAddress).setBackground(colorBack)
  92.             component.proxy(gpuAddress).setForeground(colorFore)
  93.         end
  94.     return obj
  95. end
  96.  
  97. function gui.createProgressBar(x, y, w, h, colors, vertical)
  98.     local obj = {}
  99.         obj.Text = text
  100.         obj.Type = "progBar"
  101.         obj.PosX = x
  102.         obj.PosY = y
  103.         obj.W = w
  104.         obj.H = h
  105.         obj.Per = 0
  106.         obj.Vertical = vertical or false
  107.         obj.ColorOn = colors[1]
  108.         obj.ColorTxtOn = colors[2] or 0xFFFFFF - colors[1]
  109.         obj.Draw = function(self)
  110.             component.proxy(gpuAddress).setBackground(0x555555)
  111.             component.proxy(gpuAddress).fill(self.PosX, self.PosY, self.W, self.H, " ")
  112.             if self.Per > 0 then
  113.                 local onePer = self.W / 100
  114.                 component.proxy(gpuAddress).setBackground(self.ColorOn)
  115.                 component.proxy(gpuAddress).fill(self.PosX, self.PosY, math.floor(onePer * self.Per), self.H, " ")
  116.             elseif self.Per >= 100 then
  117.                 self.Per = 100
  118.                 component.proxy(gpuAddress).setBackground(self.ColorOn)
  119.                 component.proxy(gpuAddress).fill(self.PosX, self.PosY, self.W, self.H, " ")
  120.             end
  121.         end
  122.     return obj
  123. end
  124.  
  125. function gui.draw(buttons)
  126.     for i, k in pairs(buttons) do
  127.         k:Draw()
  128.     end
  129.     component.proxy(gpuAddress).setBackground(0)
  130.     component.proxy(gpuAddress).setForeground(0xFFFFFF)
  131. end
  132.  
  133. function gui.pull(buttons, timeOut)
  134.     local t, _, pullX, pullY, click, nick = computer.pullSignal(timeOut)
  135.     if t == "touch" then
  136.         for i, k in pairs(buttons) do
  137.             if isPushed(pullX, pullY, k, click) then
  138.                 if k.Type == "button" then
  139.                     if type(k.Call) == "function" then
  140.                         return true, k.Call(nick)
  141.                     else
  142.                         return true, k.Call
  143.                     end
  144.                 elseif k.Type == "flag" then
  145.                     k.Active = not k.Active
  146.                     return true, "toggle"
  147.                 end
  148.                
  149.             end
  150.         end
  151.     end
  152.     return false
  153. end
  154.  
  155. return gui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement