Advertisement
GravityScore

Window API

Dec 25th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. --  
  2. --  Windows API
  3. --  Created By GravityScore
  4. --  Concept Copywrited by StrikePoint Studios
  5. --  
  6.  
  7. -- To Do:
  8. -- - Comments
  9. -- - Assert statements
  10.  
  11.  
  12. --  -------- Global Variables
  13.  
  14. -- Version
  15. local version = "1.0"
  16.  
  17. -- Theme
  18. local theme = {}
  19. local defaultTheme = {["menu-bar-background"] = colors.gray, ["background"] = colors.black,
  20.     ["text-color"] = colors.white, ["window-bar-color"] = colors.gray,
  21.     ["window-background-color"] = colors.lightGray}
  22.  
  23. -- Events
  24. local event_desktopUpdate = "windowsapi_desktopUpdateEvent"
  25.  
  26.  
  27. --  -------- Content Class
  28.  
  29. Content = {}
  30. Content.__index = Content
  31.  
  32. function Content.new()
  33.     local cont = {}
  34.     setmetatable(cont, Content)
  35.  
  36.     cont._width = 0
  37.     cont._height = 0
  38.     cont._env = {}
  39.     cont._board = {}
  40.  
  41.     return cont
  42. end
  43.  
  44. function Content:_loadBoard()
  45.     for x = 1, self._width do
  46.         for y = 1, self._height do
  47.             cont._board[x][y] = {text = " ", bg = colors.black, tc = colors.white}
  48.         end
  49.     end
  50. end
  51.  
  52. function Content:_addString(v, x, y, bg, tc)
  53.     for i = 1, v:len() do
  54.         self._board[x][y] = {text = v:sub(i, i), ["bg"] = bg, ["tc"] = tc}
  55.     end
  56. end
  57.  
  58. local curx, cury = 0, 0
  59. local bg, tc = colors.black, colors.white
  60.  
  61. function Content:beginDrawing()
  62.     self._env = {}
  63.     self._env.term = {}
  64.     for k, v in pairs(term) do self._env.term[k] = v end
  65.  
  66.     curx, cury = 0, 0
  67.     bg, tc = theme["window-background-color"], theme["text-color"]
  68.  
  69.     term.write = function(text)
  70.         self:_addString(text, curx, cury, bg, tc)
  71.     end
  72.  
  73.     term.clear = function()
  74.         for x = 1, self._width do
  75.             for y = 1, self._height do
  76.                 self:_addString(" ", x, y, bg, colors.white)
  77.             end
  78.         end
  79.     end
  80.  
  81.     term.clearLine = function(y)
  82.         for x = 1, self._width do
  83.             self_:_addString(" ", x, y, bg, colors.white)
  84.         end
  85.     end
  86.  
  87.     term.setCursorPos = function(x, y)
  88.         curx, cury = x, y
  89.     end
  90.  
  91.     term.getCursorPos = function()
  92.         return curx, cury
  93.     end
  94.  
  95.     term.getSize = function()
  96.         return self._width, self._height
  97.     end
  98.  
  99.     term.scroll = function(n)
  100.         return nil
  101.     end
  102.  
  103.     term.setBackgroundColor = function(col)
  104.         bg = col
  105.     end
  106.  
  107.     term.getBackgroundColor = function()
  108.         return bg
  109.     end
  110.  
  111.     term.setTextColor = function(col)
  112.         tc = col
  113.     end
  114.  
  115.     term.getTextColor = function()
  116.         return tc
  117.     end
  118.  
  119.     term.redirect = function(target)
  120.         return nil
  121.     end
  122.  
  123.     term.restore = function()
  124.         return nil
  125.     end
  126. end
  127.  
  128. function Content:endDrawing()
  129.     if self._env.term ~= {} then
  130.         term = {}
  131.         for k, v in pairs(self._env.term) do term[k] = v end
  132.  
  133.         self._env = {}
  134.         self._env.term = {}
  135.     end
  136. end
  137.  
  138. function Content:_draw(x, y)
  139.     self:endDrawing()
  140.     for nx = 1, self._width do
  141.         for ny = 1, self._height do
  142.             a = self._board[nx][ny]
  143.             term.setTextColor(a.tc)
  144.             term.setBackgroundColor(a.bg)
  145.             term.setCursorPos(nx + x - 1, ny + y - 1)
  146.             term.write(a.text)
  147.         end
  148.     end
  149. end
  150.  
  151.  
  152. --  -------- Window Class
  153.  
  154. Window = {}
  155. Window.__index = Window
  156.  
  157. function Window.new(width, height)
  158.     local win = {}
  159.     setmetatable(win, Window)
  160.  
  161.     win.x = 0
  162.     win.y = 0
  163.     win.width = width
  164.     win.height = height
  165.     win.level = 1
  166.     win.title = "Window"
  167.     win.id = ""
  168.     win._content = {}
  169.  
  170.     return win
  171. end
  172.  
  173. function Window:setContent(cont)
  174.     cont._width = self.width
  175.     cont._height = self.height - 1
  176.     cont:_loadBoard()
  177.     self._content = cont
  178. end
  179.  
  180. function Window:_draw()
  181.     term.setCursorPos(self.x, self.y)
  182.     term.setBackgroundColor(theme["window-bar-color"])
  183.     term.setTextColor(theme["text-color"])
  184.     term.write(string.rep(" ", self.width))
  185.     term.write(" " .. self.title)
  186.    
  187.     self._content:_draw(self.x, self.y + 1)
  188. end
  189.  
  190. function Window:hitTest(x, y)
  191.     if x >= self.x and x <= self.x + self.width - 1 and
  192.             y >= self.y and y <= self.y + self.height - 1 then
  193.         return true
  194.     else
  195.         return false
  196.     end
  197. end
  198.  
  199.  
  200. --  -------- Desktop Class
  201.  
  202. Desktop = {}
  203. Desktop.__index = Desktop
  204.  
  205. function Desktop.new()
  206.     local desk = {}
  207.     setmetatable(desk, Desktop)
  208.  
  209.     desk.windows = {}
  210.     desk.background = colors.black
  211.     desk.title = "Desktop 1"
  212.  
  213.     return desk
  214. end
  215.  
  216. function Desktop:_render()
  217.     local function menuBar()
  218.         term.setBackgroundColor(theme["menu-bar-background"])
  219.         term.setTextColor(theme["text-color"])
  220.         term.setCursorPos(2, 1)
  221.         term.clearLine()
  222.         term.write(self.title)
  223.     end
  224.  
  225.     local function windows()
  226.         for i = 1, #self.windows do
  227.             local a = {}
  228.             for k, v in pairs(self.windows) do
  229.                 if v.level == i then a = v end
  230.             end
  231.             a:_render()
  232.         end
  233.     end
  234.  
  235.     term.setBackgroundColor(theme["background"])
  236.     term.setTextColor(theme["text-color"])
  237.     term.clear()
  238.     menuBar()
  239.     windows()
  240. end
  241.  
  242. function Desktop:addWindow(win, iden)
  243.     local l = 0
  244.     for k, v in pairs(self.windows) do
  245.         if v.level > l then
  246.             l = v.level
  247.         end
  248.     end
  249.  
  250.     win.id = iden
  251.     win.level = l + 1
  252.     self.windows[iden] = win
  253.     os.queueEvent(event_desktopUpdate)
  254. end
  255.  
  256. function Desktop:removeWindow(iden)
  257.     self.windows[iden] = nil
  258.     os.queueEvent(event_desktopUpdate)
  259. end
  260.  
  261. function Desktop:load()
  262.     self:_render()
  263. end
  264.  
  265.  
  266. -- Dummy test
  267. c = Content.new()
  268. c:beginDrawing()
  269. term.setCursorPos(1, 1)
  270. term.write("HAI DERE!")
  271. c:endDrawing()
  272.  
  273. w = Window.new(12, 12)
  274. w.x = 2
  275. w.y = 2
  276. w.title = "Hello there!"
  277. w:setContent(c)
  278.  
  279. d = Desktop.new()
  280. d:addWindow(w)
  281. d:load()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement