Advertisement
Guest User

nebula.lua

a guest
Jan 20th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 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 = 256,
  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.   if type == "label" then
  33.     local text = extra[1]
  34.     local data = {
  35.       type = "nebula.objectType.label",
  36.       text = text,
  37.       uuid = uuid(),
  38.       x = x,
  39.       y = y,
  40.       visible = true
  41.     }
  42.     table.insert(parent.children, data)
  43.     return parent.children[#parent.children]
  44.   elseif type == "button" then
  45.     local text = extra[1]
  46.     local clickEvent = extra[2]
  47.     local paddingX = extra[3] or 1
  48.     local data = {
  49.       type = "nebula.objectType.button",
  50.       text = text,
  51.       uuid = uuid(),
  52.       padding = {
  53.         x = paddingX
  54.       },
  55.       onClick = clickEvent,
  56.       x = x,
  57.       y = y,
  58.       endX = x + string.len(string.rep(" ", paddingX) .. text .. string.rep(" ", paddingX)),
  59.       endY = y,
  60.       visible = true
  61.     }
  62.     table.insert(parent.children, data)
  63.     return parent.children[#parent.children]
  64.   elseif type == "frame" then
  65.     local data = {
  66.       type = "nebula.objectType.frame",
  67.       uuid = uuid(),
  68.       children = {},
  69.       visible = true
  70.     }
  71.     data.addChild = function( ... )
  72.       return createElement(data, ...)
  73.     end
  74.     table.insert(parent.children, data)
  75.     return parent.children[#parent.children]
  76.   end
  77. end
  78.  
  79. local function createApplication(type, theme)
  80.   if theme == nil then theme = "light" end
  81.   application = {}
  82.   if type == "default" then
  83.     application.type = "nebula.applicationType.default"
  84.   end
  85.   if theme == "light" then
  86.     application.theme = "nebula.theme.light"
  87.   end
  88.   application.children = {}
  89.   application.uuid = uuid()
  90.   application.addChild = function( ... )
  91.     return createElement(application, ...)
  92.   end
  93.   return application
  94. end
  95.  
  96. local function redraw()
  97.   if application ~= {} then
  98.     local theme = themes[application.theme]
  99.     term.setBackgroundColor(theme.background)
  100.     term.clear()
  101.     local function loadItems(list)
  102.       currentFrameUUID = list.uuid
  103.       for i, v in pairs(list.children) do
  104.         if v.type == "nebula.objectType.label" and v.visible == true then
  105.           term.setCursorPos(v.x, v.y)
  106.           term.setBackgroundColor(theme.background)
  107.           term.setTextColor(theme.text)
  108.           term.write(v.text)
  109.         elseif v.type == "nebula.objectType.button" and v.visible == true then
  110.           term.setCursorPos(v.x, v.y)
  111.           term.setBackgroundColor(theme.button.background)
  112.           term.setTextColor(theme.button.text)
  113.           term.write(string.rep(" ", v.padding.x) .. v.text .. string.rep(" ", v.padding.x))
  114.         elseif v.type == "nebula.objectType.frame" and v.visible == true then
  115.           term.setBackgroundColor(theme.background)
  116.           term.clear()
  117.           loadItems(v.children)
  118.         end
  119.       end
  120.     end
  121.     loadItems(application)
  122.   else
  123.     error("No application")
  124.   end
  125. end
  126.  
  127. local function init()
  128.   while true do
  129.     local e = {os.pullEvent()}
  130.     if e[1] == "mouse_click" then
  131.       local m, x, y = e[2], e[3], e[4]
  132.       local function checkButtons(frame)
  133.         for i, v in pairs(frame.children) do
  134.           if v.type == "nebula.objectType.button" then
  135.             if x >= v.x and x <= v.endX and y >= v.y and y <= v.endY and v.visible == true then
  136.               local theme = themes[application.theme]
  137.               term.setCursorPos(v.x, v.y)
  138.               term.setBackgroundColor(theme.button.selectionBackground)
  139.               term.setTextColor(theme.button.selectionText)
  140.               term.write(string.rep(" ", v.padding.x) .. v.text .. string.rep(" ", v.padding.x))
  141.               sleep(0.1)
  142.               redraw()
  143.  
  144.               v.onClick()
  145.               break
  146.             end
  147.           end
  148.         end
  149.       end
  150.       if currentSelectedFrameUUID == application.uuid then
  151.         checkButtons(application)
  152.       else
  153.         local function checkChildren(object)
  154.           for i, v in pairs(object.children) do
  155.             if v.type == "nebula.objectType.frame" then
  156.               if v.uuid == currentSelectedFrameUUID then
  157.                 checkButtons(v)
  158.                 break
  159.               end
  160.             end
  161.           end
  162.         end
  163.       end
  164.     end
  165.   end
  166. end
  167.  
  168. return {
  169.   draw = redraw,
  170.   createApplication = createApplication,
  171.   elements = application,
  172.   init = init
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement