Advertisement
Jummit

Computercraft UI api WIP

Jul 2nd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. local ui = {}
  2. ui.elements = {
  3.   button = {
  4.     clickedColor = colors.white, label = "", _onPressed = function(self) end, _onReleased = function(self) end,
  5.     update = function(self)
  6.     end,
  7.     draw = function(self, x, y)
  8.       local color = self.color
  9.       if self.isPressed then color = self.clickedColor end
  10.       paintutils.drawFilledBox(x, y, x+self.w-1, y+self.h-1, color)
  11.     end,
  12.     onClick = function(self)
  13.       self.isPressed = true
  14.     end,
  15.     onMouse_up = function(self)
  16.       self.isPressed = false
  17.     end
  18.   },
  19.   label = {
  20.     x = 1, y = 1, w = 1, h = 1, color = colors.gray, textColor = colors.white, text = "",
  21.     draw = function(self, x, y)
  22.       term.write(self.text)
  23.     end
  24.   },
  25.   checkbox = {
  26.     ticked = false,
  27.     draw = function(self, x, y)
  28.       if ticked then
  29.         term.write("x")
  30.       else
  31.         term.write(" ")
  32.       end
  33.     end
  34.   },
  35.   panel = {}
  36. }
  37. ui.new = setmetatable({}, {__index = function(self, elementType)
  38.   return function(args)
  39.     return setmetatable(setmetatable(args, {__index = ui.elements[elementType]}), {x = 1, y = 1, w = 1, h = 1, color = colors.gray, textColor = colors.white})
  40.   end
  41. end})
  42. ui.draw = function(elements)
  43.   term.setBackgroundColor(colors.gray)
  44.   term.clear()
  45.   parent = parent or {x = 1, y = 1}
  46.   if not parent.w then
  47.     parent.w, parent.h = term.getSize()
  48.   end
  49.   for elementName, element in pairs(elements) do
  50.     term.setCursorPos(element.x, element.y)
  51.     term.setBackgroundColor(element.color)
  52.     term.setTextColor(element.textColor)
  53.     element:draw(parent.x+element.x-1, parent.y+element.y-1)
  54.     if element.children then
  55.       ui.draw(element.children, element)
  56.     end
  57.   end
  58. end
  59. ui.update = function(elements, parent)
  60.   local event, var1, var2, var3 = os.pullEvent()
  61.   parent = parent or {x = 1, y = 1}
  62.   if not parent.w then
  63.     parent.w, parent.h = term.getSize()
  64.   end
  65.   for elementName, element in pairs(elements) do
  66.     term.setCursorPos(element.x, element.y)
  67.     local eventMethod = element["on"..string.upper(string.sub(event, 1, 1))..string.sub(event, 2)]
  68.     if eventMethod then
  69.       eventMethod(element, var1, var2, var3)
  70.     end
  71.     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
  72.       element:onClick(var1, element.x-var2, element.y-var3)
  73.     end
  74.     element:update(parent.x+element.x-1, parent.y+element.y-1, event, var1, var2, var3)
  75.     if element.children then
  76.       ui.update(element.children, element)
  77.     end
  78.   end
  79. end
  80.  
  81. return ui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement