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 = 256,
- 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
- if type == "label" then
- local text = extra[1]
- local data = {
- type = "nebula.objectType.label",
- text = text,
- uuid = uuid(),
- x = x,
- y = y,
- visible = true
- }
- table.insert(parent.children, data)
- return 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
- },
- 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]
- elseif type == "frame" then
- local data = {
- type = "nebula.objectType.frame",
- uuid = uuid(),
- children = {},
- visible = true
- }
- data.addChild = function( ... )
- return createElement(data, ...)
- end
- table.insert(parent.children, data)
- return parent.children[#parent.children]
- end
- end
- local function createApplication(type, theme)
- if theme == nil then theme = "light" end
- application = {}
- if type == "default" then
- application.type = "nebula.applicationType.default"
- end
- if theme == "light" then
- application.theme = "nebula.theme.light"
- end
- application.children = {}
- application.uuid = uuid()
- application.addChild = function( ... )
- return createElement(application, ...)
- end
- return application
- end
- local function redraw()
- if application ~= {} then
- local theme = themes[application.theme]
- term.setBackgroundColor(theme.background)
- term.clear()
- local function loadItems(list)
- 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(theme.background)
- term.setTextColor(theme.text)
- term.write(v.text)
- elseif v.type == "nebula.objectType.button" and v.visible == true then
- term.setCursorPos(v.x, v.y)
- term.setBackgroundColor(theme.button.background)
- term.setTextColor(theme.button.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(theme.background)
- term.clear()
- loadItems(v.children)
- end
- end
- end
- loadItems(application)
- 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(theme.button.selectionBackground)
- term.setTextColor(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 currentSelectedFrameUUID == 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 == currentSelectedFrameUUID then
- checkButtons(v)
- break
- end
- end
- end
- end
- end
- end
- end
- end
- return {
- draw = redraw,
- createApplication = createApplication,
- elements = application,
- init = init
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement