Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Windows API
- -- Created By GravityScore
- -- Concept Copywrited by StrikePoint Studios
- --
- -- To Do:
- -- - Comments
- -- - Assert statements
- -- -------- Global Variables
- -- Version
- local version = "1.0"
- -- Theme
- local theme = {}
- local defaultTheme = {["menu-bar-background"] = colors.gray, ["background"] = colors.black,
- ["text-color"] = colors.white, ["window-bar-color"] = colors.gray,
- ["window-background-color"] = colors.lightGray}
- -- Events
- local event_desktopUpdate = "windowsapi_desktopUpdateEvent"
- -- -------- Content Class
- Content = {}
- Content.__index = Content
- function Content.new()
- local cont = {}
- setmetatable(cont, Content)
- cont._width = 0
- cont._height = 0
- cont._env = {}
- cont._board = {}
- return cont
- end
- function Content:_loadBoard()
- for x = 1, self._width do
- for y = 1, self._height do
- cont._board[x][y] = {text = " ", bg = colors.black, tc = colors.white}
- end
- end
- end
- function Content:_addString(v, x, y, bg, tc)
- for i = 1, v:len() do
- self._board[x][y] = {text = v:sub(i, i), ["bg"] = bg, ["tc"] = tc}
- end
- end
- local curx, cury = 0, 0
- local bg, tc = colors.black, colors.white
- function Content:beginDrawing()
- self._env = {}
- self._env.term = {}
- for k, v in pairs(term) do self._env.term[k] = v end
- curx, cury = 0, 0
- bg, tc = theme["window-background-color"], theme["text-color"]
- term.write = function(text)
- self:_addString(text, curx, cury, bg, tc)
- end
- term.clear = function()
- for x = 1, self._width do
- for y = 1, self._height do
- self:_addString(" ", x, y, bg, colors.white)
- end
- end
- end
- term.clearLine = function(y)
- for x = 1, self._width do
- self_:_addString(" ", x, y, bg, colors.white)
- end
- end
- term.setCursorPos = function(x, y)
- curx, cury = x, y
- end
- term.getCursorPos = function()
- return curx, cury
- end
- term.getSize = function()
- return self._width, self._height
- end
- term.scroll = function(n)
- return nil
- end
- term.setBackgroundColor = function(col)
- bg = col
- end
- term.getBackgroundColor = function()
- return bg
- end
- term.setTextColor = function(col)
- tc = col
- end
- term.getTextColor = function()
- return tc
- end
- term.redirect = function(target)
- return nil
- end
- term.restore = function()
- return nil
- end
- end
- function Content:endDrawing()
- if self._env.term ~= {} then
- term = {}
- for k, v in pairs(self._env.term) do term[k] = v end
- self._env = {}
- self._env.term = {}
- end
- end
- function Content:_draw(x, y)
- self:endDrawing()
- for nx = 1, self._width do
- for ny = 1, self._height do
- a = self._board[nx][ny]
- term.setTextColor(a.tc)
- term.setBackgroundColor(a.bg)
- term.setCursorPos(nx + x - 1, ny + y - 1)
- term.write(a.text)
- end
- end
- end
- -- -------- Window Class
- Window = {}
- Window.__index = Window
- function Window.new(width, height)
- local win = {}
- setmetatable(win, Window)
- win.x = 0
- win.y = 0
- win.width = width
- win.height = height
- win.level = 1
- win.title = "Window"
- win.id = ""
- win._content = {}
- return win
- end
- function Window:setContent(cont)
- cont._width = self.width
- cont._height = self.height - 1
- cont:_loadBoard()
- self._content = cont
- end
- function Window:_draw()
- term.setCursorPos(self.x, self.y)
- term.setBackgroundColor(theme["window-bar-color"])
- term.setTextColor(theme["text-color"])
- term.write(string.rep(" ", self.width))
- term.write(" " .. self.title)
- self._content:_draw(self.x, self.y + 1)
- end
- function Window:hitTest(x, y)
- if x >= self.x and x <= self.x + self.width - 1 and
- y >= self.y and y <= self.y + self.height - 1 then
- return true
- else
- return false
- end
- end
- -- -------- Desktop Class
- Desktop = {}
- Desktop.__index = Desktop
- function Desktop.new()
- local desk = {}
- setmetatable(desk, Desktop)
- desk.windows = {}
- desk.background = colors.black
- desk.title = "Desktop 1"
- return desk
- end
- function Desktop:_render()
- local function menuBar()
- term.setBackgroundColor(theme["menu-bar-background"])
- term.setTextColor(theme["text-color"])
- term.setCursorPos(2, 1)
- term.clearLine()
- term.write(self.title)
- end
- local function windows()
- for i = 1, #self.windows do
- local a = {}
- for k, v in pairs(self.windows) do
- if v.level == i then a = v end
- end
- a:_render()
- end
- end
- term.setBackgroundColor(theme["background"])
- term.setTextColor(theme["text-color"])
- term.clear()
- menuBar()
- windows()
- end
- function Desktop:addWindow(win, iden)
- local l = 0
- for k, v in pairs(self.windows) do
- if v.level > l then
- l = v.level
- end
- end
- win.id = iden
- win.level = l + 1
- self.windows[iden] = win
- os.queueEvent(event_desktopUpdate)
- end
- function Desktop:removeWindow(iden)
- self.windows[iden] = nil
- os.queueEvent(event_desktopUpdate)
- end
- function Desktop:load()
- self:_render()
- end
- -- Dummy test
- c = Content.new()
- c:beginDrawing()
- term.setCursorPos(1, 1)
- term.write("HAI DERE!")
- c:endDrawing()
- w = Window.new(12, 12)
- w.x = 2
- w.y = 2
- w.title = "Hello there!"
- w:setContent(c)
- d = Desktop.new()
- d:addWindow(w)
- d:load()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement