Optimizing a fade out - WoWInterface
Thread Tools Display Modes
Prev Previous Post   Next Post Next
09-25-18, 05:04 AM   #1
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
Optimizing a fade out

Good morning all,

I have written a small snippet of code to show certain aspects of the minimap children, by controlling their alpha, when I mouseover the minimap. Further, when the mouse leaves the minimap, it starts a fadeout process that occurs relatively smoothly, 20 incremental decreases of alpha by 0.05 over 1 second. I couldn't think of a better way to code this, and everything works as intended. I am writing this to determine how to do it better.

Thank you, I'm guessing this is way over engineered and am eager to learn more about how to do this more efficiently.

Lua Code:
  1. local fadeouttimer = 1
  2. local subtractor = fadeouttimer/20
  3. local fader
  4. local function tMinimapFadeOut()
  5.     if Minimap:IsMouseOver() then --mouse reentered during fadeout, cancel the ticker, reset the timer, and set alphas back to 1
  6.         fader:Cancel()
  7.         fadeouttimer = 1
  8.         GameTimeFrame:SetAlpha(1)
  9.         TimeManagerClockButton:SetAlpha(1)
  10.         MiniMapTracking:SetAlpha(1)
  11.         MiniMapChallengeMode:SetAlpha(1)
  12.         MiniMapInstanceDifficulty:SetAlpha(1)
  13.         GuildInstanceDifficulty:SetAlpha(1)
  14.     end
  16.     fadeouttimer = fadeouttimer - subtractor
  17.     GameTimeFrame:SetAlpha(fadeouttimer or 0) -- or 0 for when the game inevitably determines that 0 is actually a tiny negative number
  18.     TimeManagerClockButton:SetAlpha(fadeouttimer or 0)
  19.     MiniMapTracking:SetAlpha(fadeouttimer or 0)
  20.     MiniMapChallengeMode:SetAlpha(fadeouttimer or 0)
  21.     MiniMapInstanceDifficulty:SetAlpha(fadeouttimer or 0)
  22.     GuildInstanceDifficulty:SetAlpha(fadeouttimer or 0)
  24.     if fadeouttimer <= 0 then --reset the fadeouttimer for next time
  25.         fader:Cancel() --failsafe to stop any previous fades that are ongoing when the mouse leaves (in case the mouse enters and then exits and then reenters)
  26.         fadeouttimer = 1
  27.     end
  28. end
  30. local lasttime = 0
  31. Minimap:SetScript('OnLeave', function()
  32.     if Minimap:IsMouseOver() then return end --failsafe to ensure we don't hide with mouse still on minimap
  33.     if time() == lasttime then return end --another failsafe
  34.     lasttime = time()
  35.     fader = C_Timer.NewTicker(0.05, tMinimapFadeOut, 20) --run 20 times over 1 second (20*0.05 = 1)
  36. end)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Optimizing a fade out

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off