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
- uiB = {}
- 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 = 0x 008080
- navy = 0x000080
- --Functions
- function addButton(nm, text, func, buttonType, colorOn, colorOff, x, y, width, height, displayed, onState)
- uiB[nm] = {}
- uiB[nm]["x"] = x
- uiB[nm]["y"] = y
- uiB[nm]["width"] = width
- uiB[nm]["height"] = height
- uiB[nm]["buttonType"] = buttonType
- uiB[nm]["text"] = text
- uiB[nm]["func"] = func
- uiB[nm]["colorOn"] = colorOn
- uiB[nm]["colorOff"] = colorOff
- uiB[nm]["currentColor"] = colorOff
- uiB[nm]["displayed"] = displayed
- uiB[nm]["onState"] = onState
- end
- function displayUI()
- gpu.setBackground(black)
- term.clear()
- for k, v in pairs(uiB) do
- if uiB[k]["displayed"] then
- gpu.setBackground(uiB[k]["currentColor"])
- gpu.fill(uiB[k]["x"], uiB[k]["y"], uiB[k]["width"], uiB[k]["height"], "X")
- TPosX = uiB[k]["width"] / 2 + uiB[k]["x"] - math.floor(string.len(uiB[k]["text"]) / 2)
- TPosY = uiB[k]["height"] / 2 + uiB[k]["y"]
- print(TPosX .. " " .. TPosY)
- gpu.set(TPosX, TPosY, uiB[k]["text"])
- end
- end
- end
- function checkButton(x, y)
- for k, v in pairs(uiB) do
- if uiB[k]["displayed"] then
- if x >= uiB[k]["x"] and x <= uiB[k]["x"] + uiB[k]["width"] then
- if y >= uiB[k]["y"] and y <= uiB[k]["y"] + uiB[k]["height"] then
- if uiB[k]["buttonType"] == "button" then
- uiB[k]["currentColor"] = uiB[k]["colorOn"]
- os.sleep(0.1)
- displayUI()
- uiB[k]["func"]()
- uiB[k]["currentColor"] = uiB[k]["colorOff"]
- displayUI()
- elseif uiB[k]["buttonType"] == "toggle" then
- if uiB[k]["currentColor"] == uiB[k]["colorOff"] then
- uiB[k]["currentColor"] = uiB[k]["colorOn"]
- uiB[k]["onState"] = true
- elseif uiB[k]["currentColor"] == uiB[k]["colorOn"] then
- uiB[k]["currentColor"] = uiB[k]["colorOff"]
- uiB[k]["onState"] = false
- end
- uiB[k]["func"]()
- end
- end
- end
- end
- end
- 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 uiB["menuSwitch"][onState] then
- uiB["exit"]["displayed"] = true
- elseif ~uiB["menuSwitch"][onState] then
- uiB["exit"]["displayed"] = false
- end
- end
- --Add buttons
- addButton("exit", "Exit Button", exit, "button", green, red, 5, 15, 15, 5, true, false)
- addButton("testToggle", "Test Toggle", testToggle, "toggle", green, red, 25, 15, 15, 5, true, false)
- addButton("example", "Example Button", doNothing, "button", green, red, 5, 15, 15, 5, true, false)
- addButton("menuSwitch", "Menu Switcher", menuSwitch, "toggle", green, red, 5, 15, 15, 5, true, false)
- --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