Advertisement
Guest User

Mover bug

a guest
Jan 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.39 KB | None | 0 0
  1. local K, C, L = unpack(select(2, ...))
  2. local Module = K:NewModule("Movers", "AceEvent-3.0")
  3.  
  4. -- Lua API
  5. local _G = _G
  6. local table_insert = table.insert
  7. local unpack = unpack
  8.  
  9. -- Wow API
  10. local CreateFrame = _G.CreateFrame
  11. local ERR_NOT_IN_COMBAT = _G.ERR_NOT_IN_COMBAT
  12. local InCombatLockdown = _G.InCombatLockdown
  13. local pairs = _G.pairs
  14. local print = _G.print
  15. local UIParent = _G.UIParent
  16.  
  17. -- Global variables that we don't cache, list them here for mikk's FindGlobals script
  18. -- GLOBALS: KkthnxUIData
  19.  
  20. local playerName = UnitName("Player")
  21. local playerRealm = GetRealmName()
  22.  
  23. Module.Frames = {}
  24. Module.Defaults = {}
  25.  
  26. function Module:SaveDefaults(frame, a1, p, a2, x, y)
  27.     if not a1 then
  28.         return
  29.     end
  30.  
  31.     if not p then
  32.         p = UIParent
  33.     end
  34.  
  35.     local Data = Module.Defaults
  36.     local Frame = frame:GetName()
  37.  
  38.     Data[Frame] = {a1, p:GetName(), a2, x, y}
  39. end
  40.  
  41. function Module:RestoreDefaults(button)
  42.     local FrameName = self.Parent:GetName()
  43.     local Data = Module.Defaults[FrameName]
  44.     local SavedVariables = KkthnxUIData[playerRealm][playerName].Movers
  45.  
  46.     if IsShiftKeyDown() and (button == "RightButton") and (Data) and not (InCombatLockdown()) then
  47.         local Anchor1, ParentName, Anchor2, X, Y = unpack(Data)
  48.         local Frame = _G[FrameName]
  49.         local Parent = _G[ParentName]
  50.  
  51.         Frame:ClearAllPoints()
  52.         Frame:SetPoint(Anchor1, Parent, Anchor2, X, Y)
  53.  
  54.         Frame.DragInfo:ClearAllPoints()
  55.         Frame.DragInfo:SetAllPoints(Frame)
  56.  
  57.         -- Delete Saved Variable
  58.         SavedVariables[FrameName] = nil
  59.     end
  60. end
  61.  
  62. function Module:RegisterFrame(frame)
  63.     if not frame then return end
  64.  
  65.     local Anchor1, Parent, Anchor2, X, Y = frame:GetPoint()
  66.  
  67.     table_insert(self.Frames, frame)
  68.  
  69.     self:SaveDefaults(frame, Anchor1, Parent, Anchor2, X, Y)
  70. end
  71.  
  72. function Module:OnDragStart()
  73.     if InCombatLockdown() then
  74.         return K.Print(ERR_NOT_IN_COMBAT)
  75.     end
  76.     self.moving = true
  77.     self:StartMoving()
  78. end
  79.  
  80. function Module:OnDragStop()
  81.     if InCombatLockdown() then
  82.         return K.Print(ERR_NOT_IN_COMBAT)
  83.     end
  84.     self.moving = nil
  85.     self:StopMovingOrSizing()
  86.  
  87.     local Data = KkthnxUIData[playerRealm][playerName].Movers
  88.     local Anchor1, Parent, Anchor2, X, Y = self:GetPoint()
  89.     local FrameName = self.Parent:GetName()
  90.     local Frame = self.Parent
  91.  
  92.     Frame:ClearAllPoints()
  93.     Frame:SetPoint(Anchor1, Parent, Anchor2, X, Y)
  94.  
  95.     if not Parent then
  96.         Parent = UIParent
  97.     end
  98.  
  99.     Data[FrameName] = {Anchor1, Parent:GetName(), Anchor2, X, Y}
  100. end
  101.  
  102. function Module:CreateDragInfo()
  103.     self.DragInfo = CreateFrame("Button", nil, self)
  104.     self.DragInfo:SetAllPoints(self)
  105.     self.DragInfo:SetFrameLevel(self:GetFrameLevel() + 1)
  106.     self.DragInfo:SetWidth(self:GetWidth())
  107.     self.DragInfo:SetHeight(self:GetHeight())
  108.     self.DragInfo:SetMovable(true)
  109.     self.DragInfo:SetToplevel(true)
  110.     self.DragInfo:RegisterForDrag("LeftButton")
  111.     self.DragInfo:SetClampedToScreen(true)
  112.     self.DragInfo:SetTemplate("Transparent", true)
  113.     self.DragInfo:SetBackdropColor(72/255, 133/255, 237/255, 0.6)
  114.     self.DragInfo:Hide()
  115.     self.DragInfo:SetScript("OnMouseUp", Module.RestoreDefaults)
  116.     self.DragInfo:SetScript("OnEnter", function(self)
  117.         self:SetBackdropColor(K.Color.r, K.Color.g, K.Color.b, 0.8)
  118.     end)
  119.     self.DragInfo:SetScript("OnLeave", function(self)
  120.         self:SetBackdropColor(72/255, 133/255, 237/255, 0.6)
  121.     end)
  122.  
  123.     self.DragInfo.Name = self.DragInfo:CreateFontString(nil, "OVERLAY")
  124.     self.DragInfo.Name:SetFont(C["Media"].Font, C["Media"].FontSize, C["Media"].FontStyle)
  125.     self.DragInfo.Name:SetPoint("CENTER")
  126.     self.DragInfo.Name:SetTextColor(1, 1, 1)
  127.     self.DragInfo.Name:SetText(self:GetName())
  128.     self.DragInfo.Name:SetWidth(self:GetWidth() - 4)
  129.  
  130.     self.DragInfo.Parent = self.DragInfo:GetParent()
  131. end
  132.  
  133. function Module:StartOrStopMoving(width, height)
  134.     if InCombatLockdown() then
  135.         return K.Print(ERR_NOT_IN_COMBAT)
  136.     end
  137.  
  138.     if not self.IsEnabled then
  139.         self.IsEnabled = true
  140.     else
  141.         self.IsEnabled = false
  142.     end
  143.  
  144.     for i = 1, #self.Frames do
  145.         local Frame = Module.Frames[i]
  146.  
  147.         if self.IsEnabled then
  148.             if not Frame.DragInfo then
  149.                 self.CreateDragInfo(Frame)
  150.             end
  151.  
  152.             if Frame.unit then
  153.                 Frame.oldunit = Frame.unit
  154.                 Frame.unit = "player"
  155.                 Frame:SetAttribute("unit", "player")
  156.             end
  157.  
  158.             Frame.DragInfo:SetScript("OnDragStart", self.OnDragStart)
  159.             Frame.DragInfo:SetScript("OnDragStop", self.OnDragStop)
  160.             Frame.DragInfo:SetParent(UIParent)
  161.             Frame.DragInfo:Show()
  162.  
  163.             if Frame.DragInfo:GetFrameLevel() ~= 100 then
  164.                 Frame.DragInfo:SetFrameLevel(100)
  165.             end
  166.  
  167.             if Frame.DragInfo:GetFrameStrata() ~= "HIGH" then
  168.                 Frame.DragInfo:SetFrameStrata("HIGH")
  169.             end
  170.  
  171.             if Frame.DragInfo:GetHeight() < 14 then
  172.                 Frame.DragInfo:ClearAllPoints()
  173.                 Frame.DragInfo:SetWidth(width or Frame:GetWidth())
  174.                 Frame.DragInfo:SetHeight(height or Frame:GetHeight())
  175.                 Frame.DragInfo:SetPoint("TOP", Frame)
  176.             end
  177.         else
  178.             if Frame.unit then
  179.                 Frame.unit = Frame.oldunit
  180.                 Frame:SetAttribute("unit", Frame.unit)
  181.             end
  182.  
  183.             if Frame.DragInfo then
  184.                 Frame.DragInfo:SetParent(Frame.DragInfo.Parent)
  185.                 Frame.DragInfo:Hide()
  186.                 Frame.DragInfo:SetScript("OnDragStart", nil)
  187.                 Frame.DragInfo:SetScript("OnDragStop", nil)
  188.  
  189.                 if Frame.DragInfo.CurrentHeight then
  190.                     Frame.DragInfo:ClearAllPoints()
  191.                     Frame.DragInfo:SetAllPoints(Frame)
  192.                 end
  193.             end
  194.         end
  195.     end
  196. end
  197.  
  198. function Module:IsRegisteredFrame(frame)
  199.     local Match = false
  200.  
  201.     for i = 1, #self.Frames do
  202.         if self.Frames[i] == frame then
  203.             Match = true
  204.         end
  205.     end
  206.  
  207.     return Match
  208. end
  209.  
  210. function Module:OnEvent(event)
  211.     if (event == "PLAYER_ENTERING_WORLD") then
  212.         if not KkthnxUIData[playerRealm][playerName].Movers then
  213.             KkthnxUIData[playerRealm][playerName].Movers = {}
  214.         end
  215.  
  216.         local Data = KkthnxUIData[playerRealm][playerName].Movers
  217.  
  218.         for Frame, Position in pairs(Data) do
  219.             local Frame = _G[Frame]
  220.             local IsRegistered = self:IsRegisteredFrame(Frame)
  221.  
  222.             if Frame and IsRegistered then
  223.                 local Anchor1, Parent, Anchor2, X, Y = Frame:GetPoint()
  224.  
  225.                 self:SaveDefaults(Frame, Anchor1, Parent, Anchor2, X, Y)
  226.  
  227.                 Anchor1, Parent, Anchor2, X, Y = unpack(Position)
  228.  
  229.                 Frame:ClearAllPoints()
  230.                 Frame:SetPoint(Anchor1, _G[Parent], Anchor2, X, Y)
  231.             end
  232.         end
  233.         if (event == "PLAYER_REGEN_DISABLED") then
  234.             if self.IsEnabled then
  235.                 self:StartOrStopMoving()
  236.             end
  237.         end
  238.     end
  239. end
  240.  
  241. function Module:OnInitialize()
  242.     self:RegisterEvent("PLAYER_ENTERING_WORLD", "OnEvent")
  243.     self:RegisterEvent("PLAYER_REGEN_DISABLED", "OnEvent")
  244. end
  245.  
  246. K["Movers"] = Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement