Advertisement
Guest User

Untitled

a guest
Jun 12th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. --GuiManager
  2.  
  3. GuiManager =
  4. {
  5.     guis = {}
  6. }
  7.  
  8. function GuiManager:add(gui)
  9.     table.insert(self.guis, gui)
  10. end
  11.  
  12. function GuiManager:guiAtXY(x,y)
  13.     for i=1,#self.guis do
  14.         local g = self.guis[i]
  15.         if g.x < x and x < g.x + g.width and g.y < y and y < g.y + g.height then
  16.             return g
  17.         end
  18.     end
  19. end
  20.  
  21. function GuiManager:onKeypress(key)
  22.     if self.focussedControl then
  23.         return self.focussedControl:keyDown(key)
  24.     end
  25.     return false
  26. end
  27.  
  28. function GuiManager:onMouseUp(x,y,mb)
  29.     if self.dragging then
  30.         self.dragging.dragging = false
  31.         self.dragging = nil
  32.     end
  33.     if self.resizing then
  34.         self.resizing.resizing = false
  35.         self.resizing = nil
  36.     end
  37.     local g = self:guiAtXY(x,y)
  38.     if g then
  39.         local control = g:getControl(x - g.x, y - g.y)
  40.         if control then
  41.             control:mouseUp(x - g.x, y - g.y, mb)
  42.         else
  43.             g:mouseUp(x - g.x, y - g.y,mb)
  44.         end
  45.                 return true
  46.     end
  47.         return false
  48. end
  49.  
  50. function GuiManager:onMouseDown(x,y,mb)
  51.     local g = self:guiAtXY(x,y)
  52.     if g then
  53.         if #self.guis > 1 then
  54.             self:bringToTop(g)
  55.         end
  56.            
  57.         local control = g:getControl(x - g.x, y - g.y)
  58.         if control then
  59.             control:mouseDown(x - g.x, y - g.y, mb)
  60.             self:setFocus(control)
  61.         else
  62.             g:mouseDown(x - g.x, y - g.y,mb)
  63.             self:setFocus(g)
  64.         end
  65.         return true
  66.     end
  67.     return false
  68. end
  69.  
  70. function GuiManager:setFocus(control)
  71.     self.focussedControl = control
  72. end
  73.  
  74. function GuiManager:bringToTop(g)
  75.     local m = -math.huge
  76.     for i,v in ipairs(self.guis) do
  77.         if v ~= g then
  78.             m = math.max(m, v.zOrder)          
  79.         end
  80.     end
  81.     g.zOrder = m + 1
  82. end
  83.  
  84. function GuiManager:update(dt)
  85.     table.sort(self.guis, function (a,b)
  86.         return a.zOrder > b.zOrder
  87.     end)
  88.     for i,v in ipairs(self.guis) do
  89.         v:update(dt)
  90.     end
  91.     local x,y = ags.Mouse.x, ags.Mouse.y
  92.     local g = self:guiAtXY(x,y)
  93.     if g then
  94.         local control = g:getControl(x - g.x, y - g.y)
  95.         if control then
  96.             if self.lastOver ~= control then
  97.                 control:mouseEnter()
  98.                 if self.lastOver then self.lastOver:mouseLeave() end
  99.                 self.lastOver = control
  100.             end
  101.         elseif self.lastOver ~= g then
  102.             g:mouseEnter()
  103.             if self.lastOver then self.lastOver:mouseLeave() end
  104.             self.lastOver = g
  105.         end
  106.     end
  107. end
  108.  
  109. function GuiManager:draw(surface)
  110.     for i=#self.guis, 1,-1 do
  111.         self.guis[i]:draw(surface)
  112.     end
  113. end
  114.  
  115. return GuiManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement