Advertisement
Guest User

LibAdjust.lua

a guest
Dec 8th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. --[[
  2. This is a modified version of LibJostle-3.0 below you can find the Author and a link to his website for more information.
  3. Author(s): ckknight (ckknight@gmail.com)
  4. Website: http://ckknight.wowinterface.com/
  5. Edited by Cokedrivers
  6. --]]
  7.  
  8. local MAJOR_VERSION = "LibBasicAdjust-1.0"
  9. local MINOR_VERSION = tonumber(("$Revision: 1 $"):match("(%d+)")) + 90000
  10.  
  11. if not LibStub then error(MAJOR_VERSION .. " requires LibStub") end
  12.  
  13. local oldLib = LibStub:GetLibrary(MAJOR_VERSION, true)
  14. local Adjust = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
  15. if not Adjust then
  16. return
  17. end
  18.  
  19. local blizzardFrames = {'MainMenuBar',}
  20.  
  21. local blizzardFramesData = {}
  22.  
  23. local _G = _G
  24.  
  25. Adjust.hooks = oldLib and oldLib.hooks or {}
  26. Adjust.bottomFrames = oldLib and oldLib.bottomFrames or {}
  27. Adjust.topAdjust = oldLib and oldLib.topAdjust
  28. Adjust.bottomAdjust = oldLib and oldLib.bottomAdjust
  29.  
  30. if Adjust.bottomAdjust == nil then
  31. Adjust.bottomAdjust = true
  32. end
  33.  
  34. Adjust.frame = oldLib and oldLib.frame or CreateFrame("Frame")
  35. local AdjustFrame = Adjust.frame
  36. local start = GetTime()
  37. local nextTime = 0
  38. local fullyInitted = false
  39. AdjustFrame:SetScript("OnUpdate", function(this, elapsed)
  40. local now = GetTime()
  41. if now - start >= 3 then
  42. fullyInitted = true
  43. for k,v in pairs(blizzardFramesData) do
  44. blizzardFramesData[k] = nil
  45. end
  46. this:SetScript("OnUpdate", function(this, elapsed)
  47. if GetTime() >= nextTime then
  48. Adjust:Refresh()
  49. this:Hide()
  50. end
  51. end)
  52. end
  53. end)
  54.  
  55. function AdjustFrame:Schedule(time)
  56. time = time or 0
  57. nextTime = GetTime() + time
  58. self:Show()
  59. end
  60.  
  61. AdjustFrame:UnregisterAllEvents()
  62.  
  63. AdjustFrame:SetScript("OnEvent", function(this, event, ...)
  64. return Adjust[event](Adjust, ...)
  65. end)
  66.  
  67. AdjustFrame:RegisterEvent("PLAYER_AURAS_CHANGED")
  68. AdjustFrame:RegisterEvent("PLAYER_REGEN_DISABLED")
  69. AdjustFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
  70. AdjustFrame:RegisterEvent("PLAYER_CONTROL_GAINED")
  71.  
  72. function Adjust:PLAYER_AURAS_CHANGED()
  73. AdjustFrame:Schedule()
  74. end
  75.  
  76. function Adjust:GetScreenBottom()
  77. local top = 0
  78. for _,frame in ipairs(self.bottomFrames) do
  79. if frame.IsShown and frame:IsShown() and frame.GetTop and frame:GetTop() and frame:GetTop() > top then
  80. top = frame:GetTop()
  81. end
  82. end
  83. return top
  84. end
  85.  
  86. function Adjust:RegisterBottom(frame)
  87. for _,f in ipairs(self.bottomFrames) do
  88. if f == frame then
  89. return
  90. end
  91. end
  92. table.insert(self.bottomFrames, frame)
  93. AdjustFrame:Schedule()
  94. return true
  95. end
  96.  
  97. function Adjust:Unregister(frame)
  98. for k,f in ipairs(self.bottomFrames) do
  99. if f == frame then
  100. table.remove(self.bottomFrames, k)
  101. AdjustFrame:Schedule()
  102. return true
  103. end
  104. end
  105. end
  106.  
  107. function Adjust:IsBottomAdjusting()
  108. return self.bottomAdjust
  109. end
  110.  
  111. function Adjust:EnableBottomAdjusting()
  112. if not self.bottomAdjust then
  113. self.bottomAdjust = not self.bottomAdjust
  114. AdjustFrame:Schedule()
  115. end
  116. end
  117.  
  118. function Adjust:DisableBottomAdjusting()
  119. if self.bottomAdjust then
  120. self.bottomAdjust = not self.bottomAdjust
  121. AdjustFrame:Schedule()
  122. end
  123. end
  124.  
  125. local tmp = {}
  126. local queue = {}
  127. local inCombat = false
  128. function Adjust:ProcessQueue()
  129. if not inCombat and HasFullControl() then
  130. for k in pairs(queue) do
  131. self:Refresh(k)
  132. queue[k] = nil
  133. end
  134. end
  135. end
  136. function Adjust:PLAYER_CONTROL_GAINED()
  137. self:ProcessQueue()
  138. end
  139.  
  140. function Adjust:PLAYER_REGEN_ENABLED()
  141. inCombat = false
  142. self:ProcessQueue()
  143. end
  144.  
  145. function Adjust:PLAYER_REGEN_DISABLED()
  146. inCombat = true
  147. end
  148.  
  149. local function isClose(alpha, bravo)
  150. return math.abs(alpha - bravo) < 0.1
  151. end
  152.  
  153. function Adjust:Refresh(...)
  154. if not fullyInitted then
  155. return
  156. end
  157.  
  158. local screenHeight = GetScreenHeight()
  159. local bottomOffset = self:IsBottomAdjusting() and self:GetScreenBottom() or 0
  160. if topOffset ~= bottomOffset ~= 0 then
  161. AdjustFrame:Schedule(10)
  162. end
  163.  
  164. local frames
  165. if select('#', ...) >= 1 then
  166. for k in pairs(tmp) do
  167. tmp[k] = nil
  168. end
  169. for i = 1, select('#', ...) do
  170. tmp[i] = select(i, ...)
  171. end
  172. frames = tmp
  173. else
  174. frames = blizzardFrames
  175. end
  176.  
  177. if inCombat or not HasFullControl() and not UnitHasVehicleUI("player") then
  178. for _,frame in ipairs(frames) do
  179. if type(frame) == "string" then
  180. frame = _G[frame]
  181. end
  182. if frame then
  183. queue[frame] = true
  184. end
  185. end
  186. return
  187. end
  188.  
  189. local screenHeight = GetScreenHeight()
  190. for _,frame in ipairs(frames) do
  191. if type(frame) == "string" then
  192. frame = _G[frame]
  193. end
  194.  
  195. local framescale = frame and frame.GetScale and frame:GetScale() or 1
  196.  
  197. if frame and not blizzardFramesData[frame] and frame.GetTop and frame:GetCenter() and select(2, frame:GetCenter()) then
  198. if select(2, frame:GetCenter()) <= screenHeight / 2 then
  199. blizzardFramesData[frame] = {y = frame:GetBottom(), top = false}
  200. else
  201. blizzardFramesData[frame] = {y = frame:GetTop() - screenHeight / framescale, top = true}
  202. end
  203. end
  204. end
  205.  
  206. for _,frame in ipairs(frames) do
  207. if type(frame) == "string" then
  208. frame = _G[frame]
  209. end
  210.  
  211. local framescale = frame and frame.GetScale and frame:GetScale() or 1
  212.  
  213. if (frame and frame.IsUserPlaced and not frame:IsUserPlaced()) then
  214. local frameData = blizzardFramesData[frame]
  215. if (select(2, frame:GetPoint(1)) ~= UIParent and select(2, frame:GetPoint(1)) ~= WorldFrame) then
  216. -- do nothing
  217. elseif frame == MainMenuBar and Gypsy_HotBarCapsule then
  218. -- do nothing
  219. elseif frame == MainMenuBar or not (frameData.lastScale and frame.GetScale and frameData.lastScale == frame:GetScale()) or not (frameData.lastX and frameData.lastY and (not isClose(frameData.lastX, frame:GetLeft()) or not isClose(frameData.lastY, frame:GetTop()))) then
  220. local anchor
  221. local anchorAlt
  222. local width, height = GetScreenWidth(), GetScreenHeight()
  223. local x
  224.  
  225. if frame:GetRight() and frame:GetLeft() then
  226. local anchorFrame = UIParent
  227. if frame == MainMenuBar or frame == GroupLootFrame1 then
  228. x = 0
  229. anchor = ""
  230. end
  231. local y = blizzardFramesData[frame].y
  232. local offset = 0
  233. if not blizzardFramesData[frame].top then
  234. anchor = "BOTTOM" .. anchor
  235. offset = bottomOffset / framescale
  236. end
  237. frame:ClearAllPoints()
  238. frame:SetPoint(anchor, anchorFrame, anchorAlt or anchor, x, y + offset - 4)
  239. blizzardFramesData[frame].lastX = frame:GetLeft()
  240. blizzardFramesData[frame].lastY = frame:GetTop()
  241. blizzardFramesData[frame].lastScale = framescale
  242. end
  243. end
  244. end
  245. end
  246. end
  247.  
  248. if AceLibrary then
  249. compat()
  250. elseif Rock then
  251. function Adjust:OnLibraryLoad(major, version)
  252. if major == "AceLibrary" then
  253. compat()
  254. end
  255. end
  256. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement