Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computer = require("computer")
- local component = require("component")
- local table = require("table")
- local unicode = require("unicode")
- local math = require("math")
- local gui = {}
- local screenAddress = component.list("screen")()
- local gpuAddress = component.list("gpu")()
- ---------------------------------------
- local function isPushed(px, py, t, button)
- local x2, y2
- if t.W then
- x2 = t.PosX + t.W
- else
- x2 = t.PosX + 1
- end
- if t.H then
- y2 = t.PosY + t.H
- else
- y2 = t.PosY + 1
- end
- if t.PosX <= px and x2 > px and t.PosY <= py and y2 > py and button == 0 and t.Enabled then
- return true
- else
- return false
- end
- end
- ---------------------------------------
- function gui.createButton(text, x, y, colors, fun)
- local obj = {}
- obj.Text = text
- obj.Type = "button"
- obj.PosX = x
- obj.PosY = y
- obj.W = unicode.wlen(text)
- obj.H = 1
- obj.ColorOn = colors[1]
- obj.ColorTxtOn = colors[2]
- obj.ColorOff = colors[3]
- obj.ColorTxtOff = colors[4]
- obj.Enabled = true
- obj.Call = fun
- obj.Draw = function(self)
- local colorBack = component.proxy(gpuAddress).getBackground(0)
- local colorFore = component.proxy(gpuAddress).getForeground(0xFFFFFF)
- --
- if self.Enabled then
- component.proxy(gpuAddress).setBackground(self.ColorOn)
- component.proxy(gpuAddress).setForeground(self.ColorTxtOn)
- else
- component.proxy(gpuAddress).setBackground(self.ColorOff)
- component.proxy(gpuAddress).setForeground(self.ColorTxtOff)
- end
- component.proxy(gpuAddress).set(self.PosX, self.PosY, self.Text)
- --
- component.proxy(gpuAddress).setBackground(colorBack)
- component.proxy(gpuAddress).setForeground(colorFore)
- end
- return obj
- end
- function gui.createFlag(text, x, y, colors)
- local obj = {}
- obj.Text = text
- obj.Type = "flag"
- obj.PosX = x
- obj.PosY = y
- obj.Active = false
- obj.ColorOn = colors[1]
- obj.ColorTxtOn = colors[2] or 0xFFFFFF - colors[1]
- obj.Enabled = true
- obj.Draw = function(self)
- local colorBack = component.proxy(gpuAddress).getBackground(0)
- local colorFore = component.proxy(gpuAddress).getForeground(0xFFFFFF)
- --
- component.proxy(gpuAddress).setBackground(0xFFFFFF)
- component.proxy(gpuAddress).setForeground(0)
- if self.Active then
- component.proxy(gpuAddress).set(self.PosX, self.PosY, "V")
- else
- component.proxy(gpuAddress).set(self.PosX, self.PosY, " ")
- end
- component.proxy(gpuAddress).setBackground(self.ColorOn)
- component.proxy(gpuAddress).setForeground(self.ColorTxtOn)
- component.proxy(gpuAddress).set(self.PosX + 1, self.PosY, self.Text)
- --
- component.proxy(gpuAddress).setBackground(colorBack)
- component.proxy(gpuAddress).setForeground(colorFore)
- end
- return obj
- end
- function gui.createProgressBar(x, y, w, h, colors, vertical)
- local obj = {}
- obj.Text = text
- obj.Type = "progBar"
- obj.PosX = x
- obj.PosY = y
- obj.W = w
- obj.H = h
- obj.Per = 0
- obj.Vertical = vertical or false
- obj.ColorOn = colors[1]
- obj.ColorTxtOn = colors[2] or 0xFFFFFF - colors[1]
- obj.Draw = function(self)
- component.proxy(gpuAddress).setBackground(0x555555)
- component.proxy(gpuAddress).fill(self.PosX, self.PosY, self.W, self.H, " ")
- if self.Per > 0 then
- local onePer = self.W / 100
- component.proxy(gpuAddress).setBackground(self.ColorOn)
- component.proxy(gpuAddress).fill(self.PosX, self.PosY, math.floor(onePer * self.Per), self.H, " ")
- elseif self.Per >= 100 then
- self.Per = 100
- component.proxy(gpuAddress).setBackground(self.ColorOn)
- component.proxy(gpuAddress).fill(self.PosX, self.PosY, self.W, self.H, " ")
- end
- end
- return obj
- end
- function gui.draw(buttons)
- for i, k in pairs(buttons) do
- k:Draw()
- end
- component.proxy(gpuAddress).setBackground(0)
- component.proxy(gpuAddress).setForeground(0xFFFFFF)
- end
- function gui.pull(buttons, timeOut)
- local t, _, pullX, pullY, click, nick = computer.pullSignal(timeOut)
- if t == "touch" then
- for i, k in pairs(buttons) do
- if isPushed(pullX, pullY, k, click) then
- if k.Type == "button" then
- if type(k.Call) == "function" then
- return true, k.Call(nick)
- else
- return true, k.Call
- end
- elseif k.Type == "flag" then
- k.Active = not k.Active
- return true, "toggle"
- end
- end
- end
- end
- return false
- end
- return gui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement