Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---- This is an API used for drawing buttons,windows or labels on a ComputerCraft monitor ----
- ---- @Author ghosttje ----
- ---- NOTES ----
- -- Also support for multiple monitors
- -- Animation still in development
- -- Still testing code
- ---- EXAMPLE: HOW TO USE ----
- -- In progress (Still need to test first)
- ---- FUNCTIONS ----
- -- [] = optional parameter
- -- function centerTextOnWindow(window, text)
- -- function initMonitor([backgroundColor], [textColor])
- -- function addMonitor(name, mon, [backgroundColor], [textColor])
- -- function addWindow(parentName, name, window, [backgroundColor], [textColor])
- -- function addNewWindow(parentName, name, x, y, width, height, [textColor], [backgroundColor])
- -- function addButtonMain(name, x, y, width, height, text, [func], [funcParam], [backgroundColor], [textColor])
- -- function addButton(parentName, name, x, y, width, height, text, [func], [funcParam], [backgroundColor], [textColor])
- -- function addLabelMain(name, x, y, text, [textColor])
- -- function addLabel(parentName, name, x, y, text, [textColor])
- -- function addAnimation(parentName, name, [func], [funcParam], [parentType])
- -- function draw()
- -- function drawItem(item)
- -- function toggleButton(button)
- -- function animateBlink(animation)
- -- function buttonClickEvent()
- -- function checkButton(x, y)
- -- function printButtonInfo()
- -- function initEventHandlerNone(eventHandler)
- -- function initEventHandler(eventHandler, mainFunc)
- -- function startParallelNone(function1, function2)
- -- function startParallel(function1, function2, function3)
- ---- Getters And Setters Windows ----
- -- function getWindowName(index)
- -- function setWindowName(index, name)
- -- function changeWindowName(oldName, newName)
- -- function getWindowItem(name)
- -- function getWindow(name)
- -- function setWindow(name, window)
- -- function getWindowParent(name)
- -- function setWindowParent(name, parent)
- -- function getWindowBackgroundColor(name)
- -- function setWindowBackgroundColor(name, backgroundColor)
- -- function getWindowTextColor(name)
- -- function setWindowTextColor(name, textColor)
- -- function getWindowAnimationName(name)
- -- function setWindowAnimationName(name, animationName)
- ---- Getters And Setters Buttons ----
- -- function getButtonName(index)
- -- function setButtonName(index, name)
- -- function changeButtonName(oldName, newName)
- -- function getButton(name)
- -- function getButtonFunction(name)
- -- function setButtonFunction(name, func)
- -- function getButtonFuncParam(name)
- -- function setButtonFuncParam(name, funcParam)
- -- function getButtonBackgroundColor(name)
- -- function setButtonBackgroundColor(name, backgroundColor)
- -- function getButtonTextColor(name)
- -- function setButtonTextColor(name, textColor)
- -- function getButtonText(name)
- -- function setButtonText(name, text)
- -- function getButtonWindow(name)
- -- function setButtonWindow(name, window)
- -- function getButtonParent(name)
- -- function setButtonParent(name, parent)
- -- function getButtonAnimationName(name)
- -- function setButtonAnimationName(name, animationName)
- ---- Getters And Setters Labels ----
- -- function getLabelName(index)
- -- function setLabelName(index, name)
- -- function changeLabelName(oldName, newName)
- -- function getLabel(name)
- -- function getLabelX(name)
- -- function setLabelX(name, x)
- -- function getLabelY(name)
- -- function setLabelY(name, y)
- -- function getLabelText(name)
- -- function setLabelText(name, text)
- -- function getLabelParent(name)
- -- function setLabelParent(name, parentName)
- -- function getLabelTextColor(name)
- -- function setLabelTextColor(name, textColor)
- -- function getLabelAnimationName(name)
- -- function setLabelAnimationName(name, animationName)
- ---- WINDOW DATA ----
- -- windows[name]["name"]
- -- windows[name]["window"]
- -- windows[name]["parent"]
- -- windows[name]["animationName"]
- -- windows[name]["textColor"]
- -- windows[name]["backgroundColor"]
- -- windows[name]["type"]
- ---- BUTTON DATA ----
- -- buttons[name]["name"]
- -- buttons[name]["text"]
- -- buttons[name]["func"]
- -- buttons[name]["funcParam"]
- -- buttons[name]["animationName"]
- -- buttons[name]["window"]
- -- buttons[name]["textColor"]
- -- buttons[name]["backgroundColor"]
- -- buttons[name]["parent"]
- -- buttons[name]["type"]
- ---- LABEL DATA ----
- -- labels[name]["name"]
- -- labels[name]["x"]
- -- labels[name]["y"]
- -- labels[name]["text"]
- -- labels[name]["parent"]
- -- labels[name]["animationName"]
- -- labels[name]["textColor"]
- -- labels[name]["type"]
- local initMonitorName = "mainMonitor"
- local funcParamConst = {activeColor=colors.red, displayTime=2}
- local windows = {}
- local buttons = {}
- local labels = {}
- local animations = {}
- ---- Local Functions ----
- local function createWindow(name, win, typeName, backgroundColor, textColor, parent, animationName)
- parent = parent or ""
- animationName = animationName or ""
- textColor = textColor or colors.white
- backgroundColor = backgroundColor or colors.black
- typeName = typeName or "window"
- local window = {}
- window["name"] = name
- window["window"] = win
- window["parent"] = parent
- window["animationName"] = animationName
- window["textColor"] = textColor
- window["backgroundColor"] = backgroundColor
- window["type"] = typeName
- return window
- end
- local function createButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
- print("Adding button...")
- print(name .. ": " .. text .. " (" .. x .. ", " .. y .. "),(" .. width .. ", " .. height .. ")")
- func = func or toggleButton
- funcParam = funcParam or funcParamConst
- backgroundColor = backgroundColor or colors.green
- textColor = textColor or colors.white
- local button = {}
- button["name"] = name
- button["text"] = text
- button["func"] = func
- button["funcParam"] = funcParam
- button["animationName"] = ""
- button["window"] = window.create(windows[parentName]["window"], x, y, width, height, false)
- button["textColor"] = textColor
- button["backgroundColor"] = backgroundColor
- button["parent"] = parentName
- button["type"] = "button"
- return button
- end
- local function createLabel(parentName, name, x, y, width, height, text, textColor, animationName)
- textColor = textColor or ""
- animationName = animationName or ""
- local label = {}
- label["name"] = name
- label["x"] = x
- label["y"] = y
- label["text"] = text
- label["parent"] = parentName
- label["animationName"] = animationName
- label["textColor"] = textColor
- label["type"] = "label"
- return label
- end
- local function initColors(item)
- if item["type"] ~= "label" then
- item["window"].setBackgroundColor(item["backgroundColor"])
- end
- item["window"].setTextColor(item["textColor"])
- end
- local function drawScreen(window)
- window["window"].setBackgroundColor(window["backgroundColor"])
- window["window"].setTextColor(window["textColor"])
- window["window"].clear()
- end
- local function drawMonitor(monitor)
- drawScreen(monitor)
- end
- local function drawWindow(window)
- drawScreen(window)
- window["window"].setVisible(true)
- end
- local function drawButton(button)
- button["window"].clear()
- button["window"].setVisible(true)
- centerTextOnWindow(button["window"], button["text"])
- end
- local function drawLabel(label)
- label["window"].setTextColor(label["textColor"])
- label["parent"].setCursorPos(label["x"], label["y"])
- label["parent"].write(label["text"])
- end
- ---- Public Functions ----
- function centerTextOnWindow(window, text)
- local x, y = window.getPosition()
- local width, height = window.getSize()
- window.setCursorPos(math.ceil((width / 2) - (text:len() / 2) + 1), math.ceil(height / 2))
- window.write(text)
- end
- function initMonitor(backgroundColor, textColor)
- print("Init monitor")
- windows[initMonitorName] = createWindow(initMonitorName, peripheral.find("monitor"), "monitor", backgroundColor, textColor)
- return "mainMonitor"
- end
- function addMonitor(name, mon, backgroundColor, textColor)
- windows[name] = createWindow(name, mon, "monitor", backgroundColor, textColor)
- end
- function addWindow(parentName, name, window, backgroundColor, textColor)
- textColor = textColor or window.getTextColor()
- backgroundColor = backgroundColor or window.getBackgroundColor()
- windows[name] = createWindow(name, window, "window", backgroundColor, textColor, parentName)
- end
- function addNewWindow(parentName, name, x, y, width, height, backgroundColor, textColor)
- local window = window.create(mon, x, y, width, height, false)
- backgroundColor = backgroundColor or colors.lightGray
- textColor = textColor or colors.white
- windows[name] = createWindow(name, window, "window", backgroundColor, textColor, parentName)
- end
- function addButtonMain(name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
- print("Add to main monitor")
- parentName = initMonitorName
- buttons[name] = createButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
- end
- function addButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
- print("Add to " .. parentName)
- buttons[name] = createButton(parentName, name, x, y, width, height, text, func, funcParam, backgroundColor, textColor)
- end
- function addLabelMain(name, x, y, text, textColor)
- parentName = initMonitorName
- labels[name] = createLabel(parentName, name, x, y, text, textColor)
- end
- function addLabel(parentName, name, x, y, text, textColor)
- labels[name] = createLabel(parentName, name, x, y, text, textColor)
- end
- function addAnimation(parentName, name, func, param, parentType)
- parentType = parentType or "button"
- func = func or toggleButton
- params = {displayTime = 1, activeColor = colors.blue}
- funcParam = funcParam or params
- animations[name]["name"] = name
- animations[name]["parent"] = parentName
- animations[name]["func"] = func
- animations[name]["funcParam"] = funcParam
- animations[name]["parentType"] = parentType
- animations[name]["type"] = "animation"
- end
- function draw()
- print("Starting drawing...")
- for k, v in pairs(windows) do
- initColors(v)
- if v["type"] == "monitor" then
- drawMonitor(v)
- else
- drawWindow(v)
- end
- end
- for k, v in pairs(buttons) do
- initColors(v)
- drawButton(v)
- end
- for k, v in pairs(labels) do
- initColors(v)
- drawLabel(v)
- end
- print("Drawing done")
- end
- function drawItem(item)
- print("Redraw: " .. item["name"])
- if item["type"] == "monitor" then
- drawMonitor(item)
- elseif item["type"] == "window" then
- drawWindow(item)
- elseif item["type"] == "button" then
- drawButton(item)
- elseif item["type"] == "label" then
- drawLabel(item)
- end
- end
- function toggleButton(button)
- button["window"].setBackgroundColor(button["funcParam"]["activeColor"])
- drawItem(button)
- sleep(button["funcParam"]["displayTime"])
- button["window"].setBackgroundColor(button["backgroundColor"])
- drawItem(button)
- end
- function animateBlink(animation)
- local item
- if animation["parentType"] == "button" then
- item = buttons[animation["parent"]]
- elseif animation["parentType"] == "monitor" or animation["parentType"] == "window" then
- item = windows[animation["parent"]]
- else
- return
- end
- item["window"].setBackgroundColor(item["animateParam"]["blinkColor"])
- sleep(animation["funcParam"]["displayTime"])
- item["window"].setBackgroundColor(item["backgroundColor"])
- end
- function buttonClickEvent()
- while true do
- print("ButtonClickTest")
- event, side, x, y = os.pullEvent("monitor_touch")
- checkButton(x, y)
- end
- end
- function checkButton(x, y)
- for k, v in pairs(buttons) do
- local xOffset = 0
- local yOffset = 0
- local parentName = v["parent"]
- while parentName ~= initMonitorName and parentName ~= "" do
- local tX, tY = windows[parentName]["window"].getPosition()
- xOffset = xOffset + tX
- yffset = yOffset + tY
- parentName = windows[parentName]["parent"]
- end
- local xButton, yButton = v["window"].getPosition()
- local width, height = v["window"].getSize()
- if x >= xButton + xOffset and x <= xButton + xOffset + width and y >= yButton + yOffset and y <= yButton + yOffset + height then
- os.queueEvent("button", v["name"], x, y)
- if v["func"] ~= "" then
- v["func"](v)
- end
- end
- end
- end
- function printInfo()
- for k, v in pairs(windows) do
- print("--Window: " .. v["name"])
- if v["type"] == "window" then
- local x, y = v["window"].getPosition()
- print("Location: (" .. x .. ", " .. y .. ")")
- end
- local width, height = v["window"].getSize()
- print("Size: (" .. width .. ", " .. height .. ")")
- print("Parent: " .. v["parent"])
- end
- for k, v in pairs(buttons) do
- print("--Button: " .. v["name"])
- local x, y = v["window"].getPosition()
- print("Location: (" .. x .. ", " .. y .. ")")
- local width, height = v["window"].getSize()
- print("Size: (" .. width .. ", " .. height .. ")")
- print("Text: " .. v["text"])
- end
- for k, v in pairs(labels) do
- print("--Label: " .. v["name"])
- print("Location: (" .. x .. ", " .. y .. ")")
- print("Text: " .. v["text"])
- print("Parent: " .. v["parent"])
- end
- end
- function initEventHandlerNone(eventHandler)
- startParallelNone(buttonClickEvent, eventHandler)
- end
- function initEventHandler(eventHandler, mainFunc)
- startParallel(buttonClickEvent, eventHandler, mainFunc)
- end
- function startParallelNone(function1, function2)
- parallel.waitForAll(function1, function2)
- end
- function startParallel(function1, function2, function3)
- parallel.waitForAll(function1, function2, function3)
- end
- ---- Getters And Setters Windows ----
- function getWindowName(index)
- return windows[index]["name"]
- end
- function setWindowName(index, name)
- local window = windows[index]
- table.remove(index)
- windows[name] = window
- end
- function changeWindowName(oldName, newName)
- local window = windows[oldName]
- table.remove(oldName)
- windows[newName] = window
- end
- function getWindowItem(name)
- return windows[name]
- end
- function getWindow(name)
- return windows[name]["window"]
- end
- function setWindow(name, window)
- windows[name]["window"] = window
- end
- function getWindowParent(name)
- return windows[name]["parent"]
- end
- function setWindowParent(name, parent)
- windows[name]["parent"] = parent
- end
- function getWindowBackgroundColor(name)
- return windows[name]["backgroundColor"]
- end
- function setWindowBackgroundColor(name, backgroundColor)
- windows[name]["backgroundColor"] = backgroundColor
- end
- function getWindowTextColor(name)
- return windows[name]["textColor"]
- end
- function setWindowTextColor(name, textColor)
- windows[name]["textColor"] = textColor
- end
- function getWindowAnimationName(name)
- return windows[name]["animationName"]
- end
- function setWindowAnimationName(name, animationName)
- windows[name]["animationName"] = animationName
- end
- ---- Getters And Setters Buttons ----
- function getButtonName(index)
- return buttons[index]["name"]
- end
- function setButtonName(index, name)
- local button = buttons[index]
- table.remove(index)
- buttons[name] = button
- end
- function changeButtonName(oldName, newName)
- local button = buttons[oldName]
- table.remove(oldName)
- buttons[newName] = button
- end
- function getButton(name)
- return buttons[name]
- end
- function getButtonFunction(name)
- return buttons[name]["func"]
- end
- function setButtonFunction(name, func)
- buttons[name]["func"] = func
- end
- function getButtonFuncParam(name)
- return buttons[name]["funcParam"]
- end
- function setButtonFuncParam(name, funcParam)
- buttons[name]["funcParam"] = funcParam
- end
- function getButtonBackgroundColor(name)
- return buttons[name]["backgroundColor"]
- end
- function setButtonBackgroundColor(name, backgroundColor)
- buttons[name]["backgroundColor"] = backgroundColor
- end
- function getButtonTextColor(name)
- return buttons[name]["textColor"]
- end
- function setButtonTextColor(name, textColor)
- buttons[name]["textColor"] = textColor
- end
- function getButtonText(name)
- return buttons[name]["text"]
- end
- function setButtonText(name, text)
- buttons[name]["text"] = text
- end
- function getButtonWindow(name)
- return buttons[name]["window"]
- end
- function setButtonWindow(name, window)
- buttons[name]["window"] = text
- end
- function getButtonParent(name)
- return buttons[name]["parent"]
- end
- function setButtonParent(name, parent)
- buttons[name]["parent"] = parent
- local x, y = buttons[name]["window"].getPosition()
- local width, height = buttons[name]["window"].getSize()
- buttons[name]["window"] = window.create(parent, x, y, width, height, buttons[name]["textColor"], buttons[name]["backgroundColor"])
- end
- function getButtonAnimationName(name)
- return buttons[name]["animationName"]
- end
- function setButtonAnimationName(name, animationName)
- buttons[name]["animationName"] = animationName
- end
- ---- Getters And Setters Labels ----
- function getLabelName(index)
- return buttons[index]["name"]
- end
- function setLabelName(index, name)
- local label = labels[index]
- table.remove(index)
- labels[name] = label
- end
- function changeLabelName(oldName, newName)
- local label = labels[oldName]
- table.remove(oldName)
- labels[newName] = label
- end
- function getLabel(name)
- return labels[name]
- end
- function getLabelX(name)
- return labels[name]["x"]
- end
- function setLabelX(name, x)
- labels[name]["x"] = x
- end
- function getLabelY(name)
- return labels[name]["y"]
- end
- function setLabelY(name, y)
- labels[name]["y"] = y
- end
- function getLabelText(name)
- return labels[name]["text"]
- end
- function setLabelText(name, text)
- labels[name]["text"] = text
- end
- function getLabelParent(name)
- return labels[name]["parent"]
- end
- function setLabelParent(name, parentName)
- labels[name]["parent"] = parentName
- end
- function getLabelTextColor(name)
- return labels[name]["textColor"]
- end
- function setLabelTextColor(name, textColor)
- labels[name]["textColor"] = textColor
- end
- function getLabelAnimationName(name)
- return labels[name]["animationName"]
- end
- function setLabelAnimationName(name, animationName)
- labels[name]["animationName"] = animationName
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement