Quantcast
WoWInterface - View Single Post - Optimizing a fade out
View Single Post
09-25-18, 02:33 PM   #5
Terenna
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
Ended up having some bugs (naturally) and I wet coded it to:
Lua Code:
  1. LoadAddOn("Blizzard_TimeManager")
  2. local fadeouttable = {}
  3. table.insert(fadeouttable, GameTimeFrame)
  4. table.insert(fadeouttable, TimeManagerClockButton)
  5. table.insert(fadeouttable, MiniMapTracking)
  6. table.insert(fadeouttable, MiniMapChallengeMode)
  7. table.insert(fadeouttable, MiniMapInstanceDifficulty)
  8. table.insert(fadeouttable, GuildInstanceDifficulty)
  9.  
  10. local function FaderOnFinished(self)
  11.     self.__owner:SetAlpha(0)
  12. end
  13.  
  14. local function FaderOnUpdate(self)
  15.     self.__owner:SetAlpha(self.__animFrame:GetAlpha())
  16. end
  17.  
  18. local function CreateFrameFadeAnimation(frame)
  19.     if frame.fader then return end
  20.     local animFrame = CreateFrame('Frame', nil, frame)
  21.     animFrame.__owner = frame
  22.     frame.fader = animFrame:CreateAnimationGroup()
  23.     frame.fader.__owner = frame
  24.     frame.fader.__animFrame = animFrame
  25.     frame.fader.direction = nil
  26.     frame.fader.setToFinalAlpha = false
  27.     frame.fader.anim = frame.fader:CreateAnimation('Alpha')
  28.     frame.fader:HookScript("OnFinished", FaderOnFinished)
  29.     frame.fader:HookScript("OnUpdate", FaderOnUpdate)
  30. end
  31.  
  32. for k, v in pairs(fadeouttable) do
  33.     CreateFrameFadeAnimation(v)
  34. end
  35.  
  36. local function tMinimapFadeOut(frame)
  37.     if frame.fader.direction == 'out' then return end
  38.     frame.fader:Pause()
  39.     frame.fader.anim:SetFromAlpha(1)
  40.     frame.fader.anim:SetToAlpha(0)
  41.     frame.fader.anim:SetDuration(1)
  42.     frame.fader.anim:SetSmoothing('OUT')
  43.     frame.fader.anim:SetStartDelay(1)
  44.     frame.fader.direction = 'out'
  45.     frame.fader:Play()
  46. end
  47.  
  48. local lasttime
  49. Minimap:SetScript('OnLeave', function()
  50.     if Minimap:IsMouseOver() then return end --failsafe to ensure we don't hide with mouse still on minimap
  51.     if time() == lasttime then return end --another failsafe
  52.     lasttime = time()
  53.  
  54.     for k, v in pairs(fadeouttable) do
  55.         tMinimapFadeOut(v)
  56.     end
  57. end)
  58.  
  59. Minimap:SetScript('OnEnter', function()
  60.     for k, v in pairs(fadeouttable) do
  61.         v.fader:Pause()
  62.         v.fader.direction = nil
  63.         v:SetAlpha(1)
  64.     end
  65. end)

Works like a charm, it's a lot longer than my original code, but probably a tiny bit more efficient than doing a meta table look up 20 times.
  Reply With Quote