Advertisement
dlord

/lib/wm.lua

Jun 3rd, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. -- Window Manager API
  2.  
  3. depends.on("stack", "HZQqMVbH")
  4.  
  5. new = function(name, parentWindow, x, y, width, height)
  6.     if name == nil then
  7.         error("Window Manager name is required.")
  8.     end
  9.  
  10.     if parentWindow == nil then
  11.         error("Parent window is required when making a new window manager.")
  12.     end
  13.  
  14.     local windowManager = {}
  15.     local activeWindowStack = stack.new()
  16.     local hiddenWindowStack = stack.new()
  17.     local windowTracker = {}
  18.     local eventTracker = {}
  19.  
  20.     local root = window.create(parentWindow, x, y, width, height, true)
  21.  
  22.     windowManager.root = function()
  23.         return root
  24.     end
  25.  
  26.     windowManager.current = function()
  27.         return activeWindowStack.peek()
  28.     end
  29.  
  30.     windowManager.name = function()
  31.         return name
  32.     end
  33.  
  34.     windowManager.create = function(name, x, y, width, height)
  35.         local newWindow = window.create(windowManager.root(), x, y, width, height, true)
  36.         newWindow.name = name
  37.  
  38.         windowTracker[name] = newWindow
  39.  
  40.         return newWindow
  41.     end
  42.  
  43.     windowManager.addEvent = function(name, event, x, y, width, height, handler)
  44.         if eventTracker[name] == nil then
  45.             eventTracker[name] = {}
  46.         end
  47.  
  48.         eventTracker[name][event] = {
  49.             x = x,
  50.             y = y,
  51.             width = width,
  52.             height = height,
  53.             handler = handler
  54.         }
  55.     end
  56.  
  57.     windowManager.removeEvent = function(name, event)
  58.         if eventTracker[name] == nil then
  59.             return
  60.         end
  61.  
  62.         eventTracker[name][event] = nil
  63.     end
  64.  
  65.     windowManager.triggerEvents = function(name, x, y)
  66.         if eventTracker[name] == nil then
  67.             return
  68.         end
  69.  
  70.         for k, v in pairs(eventTracker[name]) do
  71.             local withinX = x >= v.x and x < (v.x + v.width)
  72.             local withinY = y >= v.y and y < (v.y + v.height)
  73.  
  74.             if withinX and withinY and v.handler then
  75.                 v.handler()
  76.             end
  77.         end
  78.     end
  79.  
  80.     windowManager.removeAllEvents = function(name)
  81.         eventTracker[name] = {}
  82.     end
  83.  
  84.     windowManager.get = function(name)
  85.         return windowTracker[name]
  86.     end
  87.  
  88.     windowManager.toggleCurrent = function(state)
  89.         local currentWindow = windowManager.current()
  90.  
  91.         if currentWindow then
  92.             currentWindow.setVisible(state)
  93.         end
  94.     end
  95.  
  96.     windowManager.show = function(name)
  97.         local windowToShow = windowManager.get(name)
  98.  
  99.         if windowToShow then
  100.             windowManager.toggleCurrent(false)
  101.             windowToShow.setVisible(true)
  102.             activeWindowStack.push(windowToShow)
  103.         end
  104.  
  105.         return windowToShow
  106.     end
  107.  
  108.     windowManager.hide = function()
  109.         local windowToHide = activeWindowStack.pop()
  110.  
  111.         if windowToHide ~= nil then
  112.             windowToHide.setVisible(false)
  113.  
  114.             windowManager.toggleCurrent(true)
  115.         end
  116.  
  117.         return windowToHide
  118.     end
  119.  
  120.     windowManager.showAll = function()
  121.         windowManager.root().setVisible(true)
  122.  
  123.         while #hiddenWindowStack > 0 do
  124.             local hiddenWindow = hiddenWindowStack.pop()
  125.             hiddenWindow.setVisible(true)
  126.             hiddenWindow.setVisible(false)
  127.             activeWindowStack.push(hiddenWindow)
  128.         end
  129.  
  130.         windowManager.toggleCurrent(true)
  131.     end
  132.  
  133.     windowManager.hideAll = function()
  134.         while #activeWindowStack > 0 do
  135.             local activeWindow = activeWindowStack.pop()
  136.             activeWindow.setVisible(false)
  137.             hiddenWindowStack.push(activeWindow)
  138.         end
  139.  
  140.         windowManager.root().clear()
  141.         windowManager.root().setVisible(false)
  142.     end
  143.  
  144.     windowManager.isVisible = function()
  145.         if #activeWindowStack > 0 and #hiddenWindowStack == 0 then
  146.             return true
  147.         else
  148.             return false
  149.         end
  150.     end
  151.  
  152.     windowManager.click = function(x, y)
  153.         if windowManager.isVisible() == false then
  154.             return false, nil
  155.         end
  156.  
  157.         local isClicked = false
  158.         local currentWindow = windowManager.current()
  159.  
  160.         if currentWindow == nil then
  161.             return
  162.         end
  163.        
  164.         local currentX, currentY = currentWindow.getPosition()
  165.         local width, height = currentWindow.getSize()
  166.  
  167.         local withinX = x >= currentX and x < (currentX + width)
  168.         local withinY = y >= currentY and y < (currentY + height)
  169.  
  170.         if withinX and withinY then
  171.             windowManager.triggerEvents(currentWindow.name, (x - currentX + 1), (y - currentY + 1))
  172.  
  173.             return true, currentWindow
  174.         else
  175.             return false, nil
  176.         end
  177.     end
  178.  
  179.     return windowManager
  180. end
  181.  
  182. print("Loading Window Manager API")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement