Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mon = peripheral.find("monitor")
- if not mon then
- print("Could not find a Monitor")
- end
- mon.setTextScale(1)
- mon.setTextColor(colors.white)
- mon.setBackgroundColor(colors.black)
- buttons = {}
- charts = {}
- labels = {}
- function clearAll()
- buttons = {}
- charts = {}
- labels = {}
- mon.clear()
- end
- function addButton(name, text, func, xmin, xmax, ymin, ymax, color, activeColor, bType, activeText)
- local color = color or colors.red
- local activeColor = activeColor or colors.lime
- buttons[name] = {}
- buttons[name]["text"] = text
- buttons[name]["func"] = func
- buttons[name]["active"] = false
- buttons[name]["xmin"] = xmin
- buttons[name]["xmax"] = xmax
- buttons[name]["ymin"] = ymin
- buttons[name]["ymax"] = ymax
- buttons[name]["color"] = color
- buttons[name]["activeColor"] = activeColor
- buttons[name]["type"] = bType
- buttons[name]["activeText"] = activeText
- end
- function addChart(name, xmin, xmax, ymin, ymax, value, color, label)
- local color = color or colors.lime
- charts[name] = {}
- charts[name]["xmin"] = xmin
- charts[name]["xmax"] = xmax
- charts[name]["ymin"] = ymin
- charts[name]["ymax"] = ymax
- charts[name]["value"] = value
- charts[name]["color"] = color
- charts[name]["label"] = label
- end
- function addLabel(name, x, y, value, color)
- local color = color or colors.white
- labels[name] = {}
- labels[name]["x"] = x
- labels[name]["y"] = y
- labels[name]["value"] = value
- labels[name]["color"] = color
- end
- function drawButtons()
- local currColor
- for name, data in pairs(buttons) do
- local active = data["active"]
- if active == true then
- currColor = data["activeColor"]
- else
- currColor = data["color"]
- end
- drawButton(name, data, currColor)
- end
- end
- function drawCharts()
- for name, data in pairs(charts) do
- drawChart(name, data)
- end
- end
- function drawLabels()
- for name, data in pairs(labels) do
- drawLabel(data["x"], data["y"], data["value"], data["color"])
- end
- end
- function drawButton(name, data, currColor)
- mon.setBackgroundColor(currColor)
- local text = data["text"]
- if data["active"] == true then
- text = data["activeText"]
- end
- local xspot = math.floor((data["xmax"] - data["xmin"] - string.len(text)) / 2) + 1
- local yspot = math.floor((data["ymin"] + data["ymax"]) / 2)
- for i = data["ymin"], data["ymax"] do
- mon.setCursorPos(data["xmin"], i)
- if i == yspot then
- for j = 0, data["xmax"] - data["xmin"] - string.len(text) + 1 do
- if j == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for k = data["xmin"], data["xmax"] do
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- end
- function drawChart(name, data)
- local h = data["ymax"] - data["ymin"]
- local percentagePerStep = math.floor(100 / h)
- local barHeight = math.floor(data["value"] / percentagePerStep)
- local xspot = math.floor((data["xmax"] - data["xmin"]) / 2) - 1
- local yspot = math.floor((data["ymin"] + data["ymax"]) / 2)
- local text = data["value"] .. "%"
- if data["label"] ~= nil then
- labelX = math.floor((data["xmax"] - data["xmin"] - string.len(data["label"])) / 2)
- drawLabel(data["xmin"] + labelX, data["ymax"] + 1, data["label"], colors.white)
- end
- local emptyColor = 0
- if data["color"] == colors.gray then
- emptyColor = colors.lightGray
- else
- emptyColor = colors.gray
- end
- if data["color"] == colors.white then
- mon.setTextColor(colors.black)
- end
- for i = data["ymin"], data["ymax"] do
- mon.setCursorPos(data["xmin"], i)
- if i == yspot then
- for j = 0, data["xmax"] - data["xmin"] - string.len(text) + 1 do
- if barHeight + data["ymax"] >= data["ymax"] + (data["ymax"] - i) then
- mon.setBackgroundColor(data["color"])
- else
- mon.setBackgroundColor(emptyColor)
- end
- if j == xspot then
- mon.write(text)
- else
- mon.write(" ")
- end
- end
- else
- for k = data["xmin"], data["xmax"] do
- if barHeight + data["ymax"] >= data["ymax"] + (data["ymax"] - i) then
- mon.setBackgroundColor(data["color"])
- else
- mon.setBackgroundColor(emptyColor)
- end
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- end
- function paintGUI()
- mon.clear()
- drawButtons()
- drawCharts()
- drawLabels()
- end
- function drawLabel(x, y, value, color)
- mon.setCursorPos(x, y)
- mon.setTextColor(color)
- mon.write(value)
- end
- function drawHeading(text)
- w, h = mon.getSize()
- mon.setCursorPos((w - string.len(text)) / 2 + 1, 1)
- mon.write(text)
- end
- function toggleButton(name)
- buttons[name]["active"] = not buttons[name]["active"]
- print(buttons[name]["active"])
- paintGUI()
- end
- function flashButton(name)
- toggleButton(name)
- sleep(0.15)
- toggleButton(name)
- end
- function updateLabel(name, value, color)
- labels[name]["value"] = value;
- if color ~= nil then
- labels[name]["color"] = color
- end
- paintGUI()
- end
- function updateChart(name, value, color)
- charts[name]["value"] = value
- if color ~= nil then
- charts[name]["color"] = color
- end
- paintGUI()
- end
- function checkxy(x, y)
- for name, data in pairs(buttons) do
- if y >= data["ymin"] and y <= data["ymax"] then
- if x >= data["xmin"] and x <= data["xmax"] then
- if data["type"] == "button" then
- flash(name)
- data["func"]()
- elseif data["type"] == "switch" then
- data["func"]()
- toggleButton(name)
- else
- error("That's not a button type bro.")
- end
- return true
- end
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement