Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Required Librarys
- text = require("text")
- term = require("term")
- component = require("component")
- event = require("event")
- gpu = component.gpu
- --Variables
- uiButton = {}
- uiBar = {}
- red = 0xFF0000
- green = 0x008000
- black = 0x000000
- white = 0xFFFFFF
- lime = 0x00FF00
- blue = 0x0000FF
- yellow = 0xFFFF00
- cyan = 0x00FFFF
- magenta = 0xFF00FF
- silver = 0xC0C0C0
- gray = 0x808080
- maroon = 0x800000
- olive = 0x808000
- purple = 0x800080
- teal = 0x008080
- navy = 0x000080
- --Functions
- function addButton(nm, text, func, buttonType, colorOn, colorOff, x, y, width, height, displayed, onState)
- uiButton[nm] = {}
- uiButton[nm]["x"] = x
- uiButton[nm]["y"] = y
- uiButton[nm]["width"] = width
- uiButton[nm]["height"] = height
- uiButton[nm]["buttonType"] = buttonType
- uiButton[nm]["text"] = text
- uiButton[nm]["func"] = func
- uiButton[nm]["colorOn"] = colorOn
- uiButton[nm]["colorOff"] = colorOff
- uiButton[nm]["currentColor"] = colorOff
- uiButton[nm]["displayed"] = displayed
- uiButton[nm]["onState"] = onState
- end
- function displayUI()
- gpu.setBackground(black)
- term.clear()
- for k, v in pairs(uiButton) do
- if uiButton[k]["displayed"] then
- gpu.setBackground(uiButton[k]["currentColor"])
- gpu.fill(uiButton[k]["x"], uiButton[k]["y"], uiButton[k]["width"], uiButton[k]["height"], "X")
- TPosX = uiButton[k]["width"] / 2 + uiButton[k]["x"] - math.floor(string.len(uiButton[k]["text"]) / 2)
- TPosY = uiButton[k]["height"] / 2 + uiButton[k]["y"]
- print(TPosX .. " " .. TPosY)
- gpu.set(TPosX, TPosY, uiButton[k]["text"])
- end
- end
- for k, v in pairs(uiBar) do
- if uiBar[k]["displayed"] then
- gpu.setBackground(uibar[k]["borderColor"])
- gpu.fill(uiBar[k]["x"], uiBar[k]["y"], uiBar[k]["width"], uiBar[k]["height"], " ")
- gpu.setBackground(uiBar[k]["barColor"])
- gpu.fill(uiBar[k]["x"] + 1, uiBar[k]["y"] + 1, uiBar[k]["width"] - 1, uiBar[k]["height"] - 1, " ")
- end
- end
- end
- function checkButton(x, y)
- for k, v in pairs(uiButton) do
- if uiButton[k]["displayed"] then
- if x >= uiButton[k]["x"] and x <= uiButton[k]["x"] + uiButton[k]["width"] then
- if y >= uiButton[k]["y"] and y <= uiButton[k]["y"] + uiButton[k]["height"] then
- if uiButton[k]["buttonType"] == "button" then
- uiButton[k]["currentColor"] = uiButton[k]["colorOn"]
- os.sleep(0.1)
- displayUI()
- uiButton[k]["func"]()
- uiButton[k]["currentColor"] = uiButton[k]["colorOff"]
- displayUI()
- elseif uiButton[k]["buttonType"] == "toggle" then
- if uiButton[k]["currentColor"] == uiButton[k]["colorOff"] then
- uiButton[k]["currentColor"] = uiButton[k]["colorOn"]
- uiButton[k]["onState"] = true
- elseif uiButton[k]["currentColor"] == uiButton[k]["colorOn"] then
- uiButton[k]["currentColor"] = uiButton[k]["colorOff"]
- uiButton[k]["onState"] = false
- end
- uiButton[k]["func"]()
- end
- end
- end
- end
- end
- end
- function addBar(nm, borderColor, barColor, x, y, width, height, displayed, percent)
- uiBar[nm] = {}
- uiBar[nm]["borderColor"] = borderColor
- uiBar[nm]["barColor"] = barColor
- uiBar[nm]["x"] = x
- uiBar[nm]["y"] = y
- uiBar[nm]["width"] = width
- uiBar[nm]["height"] = height
- uiBar[nm]["displayed"] = displayed
- uiBar[nm]["percent"] = percent
- end
- function exit()
- print("Exiting")
- os.sleep(1)
- gpu.setBackground(black)
- os.exit()
- end
- function testToggle()
- print("Being Toggled")
- os.sleep(1)
- end
- function doNothing()
- print("Doing Nothing")
- os.sleep(0.2)
- end
- function menuSwitch()
- if uiButton["menuSwitch"][onState] then
- uiButton["exit"]["displayed"] = true
- elseif not uiButton["menuSwitch"][onState] then
- uiButton["exit"]["displayed"] = false
- end
- end
- --Add buttons
- addButton("exit", "Exit Button", exit, "button", green, red, 5, 5, 15, 5, true, false)
- addButton("testToggle", "Test Toggle", testToggle, "toggle", green, red, 5, 25, 15, 5, true, false)
- addButton("example", "Example Button", doNothing, "button", green, red, 5, 35, 15, 5, true, false)
- addButton("menuSwitch", "Menu Switcher", menuSwitch, "toggle", green, red, 5, 45, 15, 5, true, false)
- addBar("testBar", gray, yellow, 25, 5, 15, 40, true, 50)
- --Main Code Block
- while true do
- e = {event.pull(1)}
- if e[4] == 207 then
- os.exit()
- elseif e[1] == "touch" then
- checkButton(e[3], e[4])
- end
- displayUI()
- end
Advertisement
Add Comment
Please, Sign In to add comment