Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ui = {}
- ui.elements = {
- button = {
- clickedColor = colors.white, label = "", _onPressed = function(self) end, _onReleased = function(self) end,
- update = function(self)
- end,
- draw = function(self, x, y)
- local color = self.color
- if self.isPressed then color = self.clickedColor end
- paintutils.drawFilledBox(x, y, x+self.w-1, y+self.h-1, color)
- end,
- onClick = function(self)
- self.isPressed = true
- end,
- onMouse_up = function(self)
- self.isPressed = false
- end
- },
- label = {
- x = 1, y = 1, w = 1, h = 1, color = colors.gray, textColor = colors.white, text = "",
- draw = function(self, x, y)
- term.write(self.text)
- end
- },
- checkbox = {
- ticked = false,
- draw = function(self, x, y)
- if ticked then
- term.write("x")
- else
- term.write(" ")
- end
- end
- },
- panel = {}
- }
- ui.new = setmetatable({}, {__index = function(self, elementType)
- return function(args)
- return setmetatable(setmetatable(args, {__index = ui.elements[elementType]}), {x = 1, y = 1, w = 1, h = 1, color = colors.gray, textColor = colors.white})
- end
- end})
- ui.draw = function(elements)
- term.setBackgroundColor(colors.gray)
- term.clear()
- parent = parent or {x = 1, y = 1}
- if not parent.w then
- parent.w, parent.h = term.getSize()
- end
- for elementName, element in pairs(elements) do
- term.setCursorPos(element.x, element.y)
- term.setBackgroundColor(element.color)
- term.setTextColor(element.textColor)
- element:draw(parent.x+element.x-1, parent.y+element.y-1)
- if element.children then
- ui.draw(element.children, element)
- end
- end
- end
- ui.update = function(elements, parent)
- local event, var1, var2, var3 = os.pullEvent()
- parent = parent or {x = 1, y = 1}
- if not parent.w then
- parent.w, parent.h = term.getSize()
- end
- for elementName, element in pairs(elements) do
- term.setCursorPos(element.x, element.y)
- local eventMethod = element["on"..string.upper(string.sub(event, 1, 1))..string.sub(event, 2)]
- if eventMethod then
- eventMethod(element, var1, var2, var3)
- end
- if element.onClick and event == "mouse_click" and var2 >= element.x and var3 >= element.y and var2 < element.x+element.w and var3 < element.y+element.h then
- element:onClick(var1, element.x-var2, element.y-var3)
- end
- element:update(parent.x+element.x-1, parent.y+element.y-1, event, var1, var2, var3)
- if element.children then
- ui.update(element.children, element)
- end
- end
- end
- return ui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement