Advertisement
Guest User

nebula.lua

a guest
Jan 21st, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1. local application = {}
  2. local currentFrameUUID = ""
  3.  
  4. local themes = {
  5.   ["nebula.theme.light"] = {
  6.     background = 1,
  7.     text = 128,
  8.     button = {
  9.       background = 256,
  10.       text = 128,
  11.       selectionBackground = 128,
  12.       selectionText = 1
  13.     }
  14.   }
  15. }
  16.  
  17. local random = math.random
  18. local function uuid()
  19.   local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
  20.   return string.gsub(template, '[xy]', function (c)
  21.     local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
  22.     return string.format('%x', v)
  23.   end)
  24. end
  25.  
  26. local function createElement(parent, type, x, y, ...)
  27.   extra = { ... }
  28.   if not parent.children then
  29.     error("Could not add type " .. type .. " to element")
  30.     return nil
  31.   end
  32.   local theme = themes[application.theme]
  33.  
  34.   if type == "label" then
  35.     local text = extra[1]
  36.     local data = {
  37.       type = "nebula.objectType.label",
  38.       text = text,
  39.       uuid = uuid(),
  40.       colors = {
  41.         background = extra[2] or theme.background,
  42.         text = extra[3] or theme.text,
  43.       },
  44.       x = x,
  45.       y = y,
  46.       visible = true
  47.     }
  48.     table.insert(parent.children, data)
  49.     return parent.children[#parent.children], #parent.children
  50.   elseif type == "image" then
  51.     local image = extra[1]
  52.     local data = {
  53.       type = "nebula.objectType.image",
  54.       text = text,
  55.       uuid = uuid(),
  56.       image = image,
  57.       x = x,
  58.       y = y,
  59.       visible = true
  60.     }
  61.     table.insert(parent.children, data)
  62.     return parent.children[#parent.children], #parent.children
  63.   elseif type == "button" then
  64.     local text = extra[1]
  65.     local clickEvent = extra[2]
  66.     local paddingX = extra[3] or 1
  67.     local data = {
  68.       type = "nebula.objectType.button",
  69.       text = text,
  70.       uuid = uuid(),
  71.       padding = {
  72.         x = paddingX
  73.       },
  74.       colors = {
  75.         background = extra[4] or theme.button.background,
  76.         text = extra[5] or theme.button.text,
  77.         selectionBackground = extra[6] or theme.button.selectionBackground,
  78.         selectionText = extra[7] or theme.button.selectionText
  79.       },
  80.       onClick = clickEvent,
  81.       x = x,
  82.       y = y,
  83.       endX = x + string.len(string.rep(" ", paddingX) .. text .. string.rep(" ", paddingX)),
  84.       endY = y,
  85.       visible = true
  86.     }
  87.     table.insert(parent.children, data)
  88.     return parent.children[#parent.children], #parent.children
  89.   elseif type == "frame" then
  90.     local data = {
  91.       type = "nebula.objectType.frame",
  92.       uuid = uuid(),
  93.       children = {},
  94.       visible = true,
  95.       backgroundColor = x or theme.background
  96.     }
  97.     data.addChild = function( ... )
  98.       return createElement(data, ...)
  99.     end
  100.     table.insert(parent.children, data)
  101.     return parent.children[#parent.children], #parent.children
  102.   end
  103. end
  104.  
  105. local function createApplication(type, theme, backgroundColor)
  106.   if theme == nil then theme = "light" end
  107.   local lapplication = {}
  108.   if type == "default" then
  109.     lapplication.type = "nebula.applicationType.default"
  110.   end
  111.   if theme == "light" then
  112.     lapplication.theme = "nebula.theme.light"
  113.   end
  114.   lapplication.children = {}
  115.   lapplication.uuid = uuid()
  116.   lapplication.backgroundColor = backgroundColor
  117.   if backgroundColor == nil then
  118.     lapplication.backgroundColor = theme.background
  119.   end
  120.   lapplication.addChild = function( ... )
  121.     return createElement(lapplication, ...)
  122.   end
  123.   application = lapplication
  124.   return application
  125. end
  126.  
  127. local function redraw()
  128.   if application ~= {} then
  129.     local theme = themes[application.theme]
  130.  
  131.     local function loadItems(list)
  132.       term.setBackgroundColor(list.backgroundColor or theme.background)
  133.       term.clear()
  134.       currentFrameUUID = list.uuid
  135.       for i, v in pairs(list.children) do
  136.         if v.type == "nebula.objectType.label" and v.visible == true then
  137.           term.setCursorPos(v.x, v.y)
  138.           term.setBackgroundColor(v.colors.background)
  139.           term.setTextColor(v.colors.text)
  140.           term.write(v.text)
  141.         elseif v.type == "nebula.objectType.button" and v.visible == true then
  142.           term.setCursorPos(v.x, v.y)
  143.           term.setBackgroundColor(v.colors.background)
  144.           term.setTextColor(v.colors.text)
  145.           term.write(string.rep(" ", v.padding.x) .. v.text .. string.rep(" ", v.padding.x))
  146.         elseif v.type == "nebula.objectType.frame" and v.visible == true then
  147.           term.setBackgroundColor(v.backgroundColor)
  148.           term.clear()
  149.           loadItems(v)
  150.           return
  151.         elseif v.type == "nebula.objectType.image" and v.visible == true then
  152.           paintutils.drawImage(paintutils.loadImage(v.image), v.x, v.y)
  153.         end
  154.       end
  155.     end
  156.     loadItems(application)
  157.     term.setBackgroundColor(colors.white)
  158.   else
  159.     error("No application")
  160.   end
  161. end
  162.  
  163. local function init()
  164.   while true do
  165.     local e = {os.pullEvent()}
  166.     if e[1] == "mouse_click" then
  167.       local m, x, y = e[2], e[3], e[4]
  168.       local function checkButtons(frame)
  169.         for i, v in pairs(frame.children) do
  170.           if v.type == "nebula.objectType.button" then
  171.             if x >= v.x and x <= v.endX and y >= v.y and y <= v.endY and v.visible == true then
  172.               local theme = themes[application.theme]
  173.               term.setCursorPos(v.x, v.y)
  174.               term.setBackgroundColor(v.colors.selectionBackground or theme.button.selectionBackground)
  175.               term.setTextColor(v.colors.selectionText or theme.button.selectionText)
  176.               term.write(string.rep(" ", v.padding.x) .. v.text .. string.rep(" ", v.padding.x))
  177.               sleep(0.1)
  178.               redraw()
  179.  
  180.               v.onClick()
  181.               break
  182.             end
  183.           end
  184.         end
  185.       end
  186.  
  187.       if currentFrameUUID == application.uuid then
  188.         checkButtons(application)
  189.       else
  190.         local function checkChildren(object)
  191.           for i, v in pairs(object.children) do
  192.             if v.type == "nebula.objectType.frame" then
  193.               if v.uuid == currentFrameUUID then
  194.                 checkButtons(v)
  195.                 break
  196.               end
  197.             end
  198.           end
  199.         end
  200.         checkChildren(application)
  201.       end
  202.     elseif e[1] == "nebula_close" then
  203.       term.setBackgroundColor(colors.black)
  204.       term.setTextColor(colors.white)
  205.       term.clear()
  206.       break
  207.     end
  208.   end
  209. end
  210.  
  211. local function close()
  212.   os.queueEvent("nebula_close")
  213. end
  214.  
  215. return {
  216.   draw = redraw,
  217.   createApplication = createApplication,
  218.   elements = application,
  219.   init = init,
  220.   close = close
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement