Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Window Manager API
- depends.on("stack", "HZQqMVbH")
- new = function(name, parentWindow, x, y, width, height)
- if name == nil then
- error("Window Manager name is required.")
- end
- if parentWindow == nil then
- error("Parent window is required when making a new window manager.")
- end
- local windowManager = {}
- local activeWindowStack = stack.new()
- local hiddenWindowStack = stack.new()
- local windowTracker = {}
- local eventTracker = {}
- local root = window.create(parentWindow, x, y, width, height, true)
- windowManager.root = function()
- return root
- end
- windowManager.current = function()
- return activeWindowStack.peek()
- end
- windowManager.name = function()
- return name
- end
- windowManager.create = function(name, x, y, width, height)
- local newWindow = window.create(windowManager.root(), x, y, width, height, true)
- newWindow.name = name
- windowTracker[name] = newWindow
- return newWindow
- end
- windowManager.addEvent = function(name, event, x, y, width, height, handler)
- if eventTracker[name] == nil then
- eventTracker[name] = {}
- end
- eventTracker[name][event] = {
- x = x,
- y = y,
- width = width,
- height = height,
- handler = handler
- }
- end
- windowManager.removeEvent = function(name, event)
- if eventTracker[name] == nil then
- return
- end
- eventTracker[name][event] = nil
- end
- windowManager.triggerEvents = function(name, x, y)
- if eventTracker[name] == nil then
- return
- end
- for k, v in pairs(eventTracker[name]) do
- local withinX = x >= v.x and x < (v.x + v.width)
- local withinY = y >= v.y and y < (v.y + v.height)
- if withinX and withinY and v.handler then
- v.handler()
- end
- end
- end
- windowManager.removeAllEvents = function(name)
- eventTracker[name] = {}
- end
- windowManager.get = function(name)
- return windowTracker[name]
- end
- windowManager.toggleCurrent = function(state)
- local currentWindow = windowManager.current()
- if currentWindow then
- currentWindow.setVisible(state)
- end
- end
- windowManager.show = function(name)
- local windowToShow = windowManager.get(name)
- if windowToShow then
- windowManager.toggleCurrent(false)
- windowToShow.setVisible(true)
- activeWindowStack.push(windowToShow)
- end
- return windowToShow
- end
- windowManager.hide = function()
- local windowToHide = activeWindowStack.pop()
- if windowToHide ~= nil then
- windowToHide.setVisible(false)
- windowManager.toggleCurrent(true)
- end
- return windowToHide
- end
- windowManager.showAll = function()
- windowManager.root().setVisible(true)
- while #hiddenWindowStack > 0 do
- local hiddenWindow = hiddenWindowStack.pop()
- hiddenWindow.setVisible(true)
- hiddenWindow.setVisible(false)
- activeWindowStack.push(hiddenWindow)
- end
- windowManager.toggleCurrent(true)
- end
- windowManager.hideAll = function()
- while #activeWindowStack > 0 do
- local activeWindow = activeWindowStack.pop()
- activeWindow.setVisible(false)
- hiddenWindowStack.push(activeWindow)
- end
- windowManager.root().clear()
- windowManager.root().setVisible(false)
- end
- windowManager.isVisible = function()
- if #activeWindowStack > 0 and #hiddenWindowStack == 0 then
- return true
- else
- return false
- end
- end
- windowManager.click = function(x, y)
- if windowManager.isVisible() == false then
- return false, nil
- end
- local isClicked = false
- local currentWindow = windowManager.current()
- if currentWindow == nil then
- return
- end
- local currentX, currentY = currentWindow.getPosition()
- local width, height = currentWindow.getSize()
- local withinX = x >= currentX and x < (currentX + width)
- local withinY = y >= currentY and y < (currentY + height)
- if withinX and withinY then
- windowManager.triggerEvents(currentWindow.name, (x - currentX + 1), (y - currentY + 1))
- return true, currentWindow
- else
- return false, nil
- end
- end
- return windowManager
- end
- print("Loading Window Manager API")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement