Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local application = {}
- local currentFrameUUID = ""
- local themes = {
- ["nebula.theme.light"] = {
- background = 1,
- text = 128,
- button = {
- background = 256,
- text = 128,
- selectionBackground = 128,
- selectionText = 1
- }
- }
- }
- local random = math.random
- local function uuid()
- local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
- return string.gsub(template, '[xy]', function (c)
- local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
- return string.format('%x', v)
- end)
- end
- local function createElement(parent, type, x, y, ...)
- extra = { ... }
- if not parent.children then
- error("Could not add type " .. type .. " to element")
- return nil
- end
- local theme = themes[application.theme]
- if type == "label" then
- local text = extra[1]
- local data = {
- type = "nebula.objectType.label",
- text = text,
- uuid = uuid(),
- colors = {
- background = extra[2] or theme.background,
- text = extra[3] or theme.text,
- },
- x = x,
- y = y,
- visible = true
- }
- table.insert(parent.children, data)
- return parent.children[#parent.children], #parent.children
- elseif type == "image" then
- local image = extra[1]
- local data = {
- type = "nebula.objectType.image",
- text = text,
- uuid = uuid(),
- image = image,
- x = x,
- y = y,
- visible = true
- }
- table.insert(parent.children, data)
- return parent.children[#parent.children], #parent.children
- elseif type == "button" then
- local text = extra[1]
- local clickEvent = extra[2]
- local paddingX = extra[3] or 1
- local data = {
- type = "nebula.objectType.button",
- text = text,
- uuid = uuid(),
- padding = {
- x = paddingX
- },
- colors = {
- background = extra[4] or theme.button.background,
- text = extra[5] or theme.button.text,
- selectionBackground = extra[6] or theme.button.selectionBackground,
- selectionText = extra[7] or theme.button.selectionText
- },
- onClick = clickEvent,
- x = x,
- y = y,
- endX = x + string.len(string.rep(" ", paddingX) .. text .. string.rep(" ", paddingX)),
- endY = y,
- visible = true
- }
- table.insert(parent.children, data)
- return parent.children[#parent.children], #parent.children
- elseif type == "frame" then
- local data = {
- type = "nebula.objectType.frame",
- uuid = uuid(),
- children = {},
- visible = true,
- backgroundColor = x or theme.background
- }
- data.addChild = function( ... )
- return createElement(data, ...)
- end
- table.insert(parent.children, data)
- return parent.children[#parent.children], #parent.children
- end
- end
- local function createApplication(type, theme, backgroundColor)
- if theme == nil then theme = "light" end
- local lapplication = {}
- if type == "default" then
- lapplication.type = "nebula.applicationType.default"
- end
- if theme == "light" then
- lapplication.theme = "nebula.theme.light"
- end
- lapplication.children = {}
- lapplication.uuid = uuid()
- lapplication.backgroundColor = backgroundColor
- if backgroundColor == nil then
- lapplication.backgroundColor = theme.background
- end
- lapplication.addChild = function( ... )
- return createElement(lapplication, ...)
- end
- application = lapplication
- return application
- end
- local function redraw()
- if application ~= {} then
- local theme = themes[application.theme]
- local function loadItems(list)
- term.setBackgroundColor(list.backgroundColor or theme.background)
- term.clear()
- currentFrameUUID = list.uuid
- for i, v in pairs(list.children) do
- if v.type == "nebula.objectType.label" and v.visible == true then
- term.setCursorPos(v.x, v.y)
- term.setBackgroundColor(v.colors.background)
- term.setTextColor(v.colors.text)
- term.write(v.text)
- elseif v.type == "nebula.objectType.button" and v.visible == true then
- term.setCursorPos(v.x, v.y)
- term.setBackgroundColor(v.colors.background)
- term.setTextColor(v.colors.text)
- term.write(string.rep(" ", v.padding.x) .. v.text .. string.rep(" ", v.padding.x))
- elseif v.type == "nebula.objectType.frame" and v.visible == true then
- term.setBackgroundColor(v.backgroundColor)
- term.clear()
- loadItems(v)
- return
- elseif v.type == "nebula.objectType.image" and v.visible == true then
- paintutils.drawImage(paintutils.loadImage(v.image), v.x, v.y)
- end
- end
- end
- loadItems(application)
- term.setBackgroundColor(colors.white)
- else
- error("No application")
- end
- end
- local function init()
- while true do
- local e = {os.pullEvent()}
- if e[1] == "mouse_click" then
- local m, x, y = e[2], e[3], e[4]
- local function checkButtons(frame)
- for i, v in pairs(frame.children) do
- if v.type == "nebula.objectType.button" then
- if x >= v.x and x <= v.endX and y >= v.y and y <= v.endY and v.visible == true then
- local theme = themes[application.theme]
- term.setCursorPos(v.x, v.y)
- term.setBackgroundColor(v.colors.selectionBackground or theme.button.selectionBackground)
- term.setTextColor(v.colors.selectionText or theme.button.selectionText)
- term.write(string.rep(" ", v.padding.x) .. v.text .. string.rep(" ", v.padding.x))
- sleep(0.1)
- redraw()
- v.onClick()
- break
- end
- end
- end
- end
- if currentFrameUUID == application.uuid then
- checkButtons(application)
- else
- local function checkChildren(object)
- for i, v in pairs(object.children) do
- if v.type == "nebula.objectType.frame" then
- if v.uuid == currentFrameUUID then
- checkButtons(v)
- break
- end
- end
- end
- end
- checkChildren(application)
- end
- elseif e[1] == "nebula_close" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- break
- end
- end
- end
- local function close()
- os.queueEvent("nebula_close")
- end
- return {
- draw = redraw,
- createApplication = createApplication,
- elements = application,
- init = init,
- close = close
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement