Thread Tools Display Modes
03-17-16, 09:58 PM   #1
tonyis3l33t
A Cyclonian
 
tonyis3l33t's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 47
[FixMyCode] beginner working with OnEnter and UIFrameFadeIn

Hi all! Wondering if I could get another pair of eyes on this....

trying to get the minimap to fade in when moused over, otherwise be faded out.
What it actually does: nothing


Code:
------------------
--Config
local fadeTime = .5		-- fade time in seconds
------------------

local DaftFade = CreateFrame('Frame')


--Events
local event = CreateFrame('Frame')
event:SetScript('OnEvent', function(self, event, ...) self[event](self, ...) end)


--Fading
function DaftFade:FadeIn(frame)
	local alpha = frame:GetAlpha()
	UIFrameFadeIn(frame, fadeTime*(1-alpha), alpha, 1)
end

function DaftFade:FadeOut(frame)
	local alpha = frame:GetAlpha()
	UIFrameFadeOut(frame, fadeTime*alpha, alpha, 0)
end


--Main
Minimap:HookScript("OnEnter", function() DaftFade:FadeIn(self.GetName()) end)
Minimap:HookScript("OnLeave", function() DaftFade:FadeOut(self.GetName()) end)
Thanks for your time!
  Reply With Quote
03-17-16, 10:49 PM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
If you're indoors you can't alter the opacity of the minimap, and even if you're outdoors it doesn't change the opacity of the tracking dots, it only fades the background.

Even if you could fade the minimap, your code has a few issues; I would just rewrite it like this..
Lua Code:
  1. local fadeTime = 0.5     -- fade time in seconds
  2. Minimap:SetAlpha(0) -- default opacity of 0
  3. Minimap:HookScript("OnEnter", function()
  4.     local alpha = Minimap:GetAlpha()
  5.     UIFrameFadeIn(Minimap, fadeTime * (1 - alpha), alpha, 1)
  6. end)
  7.  
  8. Minimap:HookScript("OnLeave", function()
  9.     local alpha = Minimap:GetAlpha()
  10.     UIFrameFadeOut(Minimap, fadeTime * alpha, alpha, 0)
  11. end)

Last edited by semlar : 03-17-16 at 10:59 PM.
  Reply With Quote
03-18-16, 05:27 AM   #3
tonyis3l33t
A Cyclonian
 
tonyis3l33t's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 47
Thanks semlar. I So in my code the issue might be calling a nil value when trying to get the alpha?

While I appreciate the rewrite, I have it this way because I'm trying to expand the fading to other frames later. Any tips on my crappy code, especially glaring issues?
  Reply With Quote
03-18-16, 09:04 AM   #4
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Just wanted to drop by and mention that UIFrameFadeIn can potentially spread execution taint, since it's used by some of the protected code in the standard UI. I investigated this a long time ago, since I ran into it without a clue what was causing it. I believe there might even be a post about the issue on these forums.

I suggest using a custom animation manager instead, which removes any chance of spreading taint. This code is 95% stolen from someone else (can't remember who), but it does the trick. You can include this "file" as is in your toc, then grab the functions from your addon table, which also solves the issue of reuseability since you can reach it from any file in your addon.

Lua Code:
  1. ---------------------------------------------------------------
  2. -- Animation.lua: Taint-free animation replacement
  3. ---------------------------------------------------------------
  4. -- Provides a replacement for managing alpha animations without
  5. -- risk of spreading taint, which might happen when the "real"
  6. -- functions are called from secure code.
  7.  
  8. local _, db = ...
  9.  
  10. ---------------------------------------------------------------
  11. -- Fade: Taint-free fade functions
  12. ---------------------------------------------------------------
  13.  
  14. local FADEFRAMES = {}
  15. local FadeManager = CreateFrame("Frame")
  16.  
  17. local function FadeRemoveFrame(frame)
  18.     tDeleteItem(FADEFRAMES, frame)
  19. end
  20.  
  21. local function FadeOnUpdate(self, elapsed)
  22.     local index = 1
  23.     local frame, fadeInfo
  24.     while FADEFRAMES[index] do
  25.         frame = FADEFRAMES[index]
  26.         fadeInfo = FADEFRAMES[index].fadeInfo
  27.         -- Reset the timer if there isn't one, this is just an internal counter
  28.         if ( not fadeInfo.fadeTimer ) then
  29.             fadeInfo.fadeTimer = 0
  30.         end
  31.         fadeInfo.fadeTimer = fadeInfo.fadeTimer + elapsed
  32.  
  33.         -- If the fadeTimer is less then the desired fade time then set the alpha otherwise hold the fade state, call the finished function, or just finish the fade
  34.         if fadeInfo.fadeTimer < fadeInfo.timeToFade then
  35.             if fadeInfo.mode == "IN" then
  36.                 frame:SetAlpha((fadeInfo.fadeTimer / fadeInfo.timeToFade) * (fadeInfo.endAlpha - fadeInfo.startAlpha) + fadeInfo.startAlpha)
  37.             elseif fadeInfo.mode == "OUT" then
  38.                 frame:SetAlpha(((fadeInfo.timeToFade - fadeInfo.fadeTimer) / fadeInfo.timeToFade) * (fadeInfo.startAlpha - fadeInfo.endAlpha)  + fadeInfo.endAlpha)
  39.             end
  40.         else
  41.             frame:SetAlpha(fadeInfo.endAlpha)
  42.             -- If there is a fadeHoldTime then wait until its passed to continue on
  43.             if fadeInfo.fadeHoldTime and fadeInfo.fadeHoldTime > 0  then
  44.                 fadeInfo.fadeHoldTime = fadeInfo.fadeHoldTime - elapsed
  45.             else
  46.                 -- Complete the fade and call the finished function if there is one
  47.                 FadeRemoveFrame(frame)
  48.                 if fadeInfo.finishedFunc then
  49.                     fadeInfo.finishedFunc(fadeInfo.finishedArg1, fadeInfo.finishedArg2, fadeInfo.finishedArg3, fadeInfo.finishedArg4)
  50.                     fadeInfo.finishedFunc = nil
  51.                 end
  52.             end
  53.         end
  54.        
  55.         index = index + 1
  56.     end
  57.    
  58.     if #FADEFRAMES == 0 then
  59.         self:SetScript("OnUpdate", nil)
  60.     end
  61. end
  62.  
  63. -- Generic fade function
  64. local function FadeFrame(frame, fadeInfo)
  65.     if not frame then
  66.         return
  67.     end
  68.     if not fadeInfo.mode then
  69.         fadeInfo.mode = "IN"
  70.     end
  71.     local alpha
  72.     if fadeInfo.mode == "IN" then
  73.         if not fadeInfo.startAlpha then
  74.             fadeInfo.startAlpha = 0
  75.         end
  76.         if not fadeInfo.endAlpha then
  77.             fadeInfo.endAlpha = 1.0
  78.         end
  79.         alpha = 0
  80.     elseif fadeInfo.mode == "OUT" then
  81.         if not fadeInfo.startAlpha then
  82.             fadeInfo.startAlpha = 1.0
  83.         end
  84.         if not fadeInfo.endAlpha then
  85.             fadeInfo.endAlpha = 0
  86.         end
  87.         alpha = 1.0
  88.     end
  89.     frame:SetAlpha(fadeInfo.startAlpha)
  90.  
  91.     frame.fadeInfo = fadeInfo
  92.  
  93.     local index = 1
  94.     while FADEFRAMES[index] do
  95.         -- If frame is already set to fade then return
  96.         if FADEFRAMES[index] == frame then
  97.             return
  98.         end
  99.         index = index + 1
  100.     end
  101.     tinsert(FADEFRAMES, frame)
  102.     FadeManager:SetScript("OnUpdate", FadeOnUpdate)
  103. end
  104.  
  105. -- Convenience function for simple fade in
  106. db.UIFrameFadeIn = function (frame, timeToFade, startAlpha, endAlpha)
  107.     local fadeInfo = {}
  108.     fadeInfo.mode = "IN"
  109.     fadeInfo.timeToFade = timeToFade
  110.     fadeInfo.startAlpha = startAlpha
  111.     fadeInfo.endAlpha = endAlpha
  112.     FadeFrame(frame, fadeInfo)
  113. end
  114.  
  115. -- Convenience function for simple fade out
  116. db.UIFrameFadeOut = function (frame, timeToFade, startAlpha, endAlpha)
  117.     local fadeInfo = {}
  118.     fadeInfo.mode = "OUT"
  119.     fadeInfo.timeToFade = timeToFade
  120.     fadeInfo.startAlpha = startAlpha
  121.     fadeInfo.endAlpha = endAlpha
  122.     FadeFrame(frame, fadeInfo)
  123. end
  124.  
  125.  
  126. ---------------------------------------------------------------
  127. -- Flash: Taint-free flash functions
  128. ---------------------------------------------------------------
  129. local FlashManager = CreateFrame("FRAME")
  130. local FLASHFRAMES = {}
  131.  
  132. local FlashTimers = {}
  133. local FlashTimerRefCount = {}
  134.  
  135. -- Function to stop flashing
  136. local function FlashStop(frame)
  137.     tDeleteItem(FLASHFRAMES, frame)
  138.     frame:SetAlpha(1.0)
  139.     frame.flashTimer = nil
  140.     if frame.syncId then
  141.         FlashTimerRefCount[frame.syncId] = FlashTimerRefCount[frame.syncId]-1
  142.         if FlashTimerRefCount[frame.syncId] == 0 then
  143.             FlashTimers[frame.syncId] = nil
  144.             FlashTimerRefCount[frame.syncId] = nil
  145.         end
  146.         frame.syncId = nil
  147.     end
  148.     if frame.showWhenDone then
  149.         frame:Show()
  150.     else
  151.         frame:Hide()
  152.     end
  153. end
  154.  
  155. -- Called every frame to update flashing frames
  156. local function FlashOnUpdate(self, elapsed)
  157.     local frame
  158.     local index = #FLASHFRAMES
  159.    
  160.     -- Update timers for all synced frames
  161.     for syncId, timer in pairs(FlashTimers) do
  162.         FlashTimers[syncId] = timer + elapsed
  163.     end
  164.    
  165.     while FLASHFRAMES[index] do
  166.         frame = FLASHFRAMES[index]
  167.         frame.flashTimer = frame.flashTimer + elapsed
  168.  
  169.         if (frame.flashTimer > frame.flashDuration) and frame.flashDuration ~= -1 then
  170.             FlashStop(frame)
  171.         else
  172.             local flashTime = frame.flashTimer
  173.             local alpha
  174.            
  175.             if frame.syncId then
  176.                 flashTime = FlashTimers[frame.syncId]
  177.             end
  178.            
  179.             flashTime = flashTime%(frame.fadeInTime+frame.fadeOutTime+(frame.flashInHoldTime or 0)+(frame.flashOutHoldTime or 0))
  180.             if flashTime < frame.fadeInTime then
  181.                 alpha = flashTime/frame.fadeInTime
  182.             elseif flashTime < frame.fadeInTime+(frame.flashInHoldTime or 0) then
  183.                 alpha = 1
  184.             elseif flashTime < frame.fadeInTime+(frame.flashInHoldTime or 0)+frame.fadeOutTime then
  185.                 alpha = 1 - ((flashTime - frame.fadeInTime - (frame.flashInHoldTime or 0))/frame.fadeOutTime)
  186.             else
  187.                 alpha = 0
  188.             end
  189.            
  190.             frame:SetAlpha(alpha)
  191.             frame:Show()
  192.         end
  193.        
  194.         -- Loop in reverse so that removing frames is safe
  195.         index = index - 1
  196.     end
  197.    
  198.     if #FLASHFRAMES == 0 then
  199.         self:SetScript("OnUpdate", nil)
  200.     end
  201. end
  202.  
  203. -- Function to start a frame flashing
  204. db.UIFrameFlash =  function(frame, fadeInTime, fadeOutTime, flashDuration, showWhenDone, flashInHoldTime, flashOutHoldTime, syncId)
  205.     if frame then
  206.         local index = 1
  207.         -- If frame is already set to flash then return
  208.         while FLASHFRAMES[index] do
  209.             if FLASHFRAMES[index] == frame then
  210.                 return
  211.             end
  212.             index = index + 1
  213.         end
  214.  
  215.         if syncId then
  216.             frame.syncId = syncId
  217.             if FlashTimers[syncId] == nil then
  218.                 FlashTimers[syncId] = 0
  219.                 FlashTimerRefCount[syncId] = 0
  220.             end
  221.             FlashTimerRefCount[syncId] = FlashTimerRefCount[syncId]+1
  222.         else
  223.             frame.syncId = nil
  224.         end
  225.        
  226.         -- Time it takes to fade in a flashing frame
  227.         frame.fadeInTime = fadeInTime
  228.         -- Time it takes to fade out a flashing frame
  229.         frame.fadeOutTime = fadeOutTime
  230.         -- How long to keep the frame flashing
  231.         frame.flashDuration = flashDuration
  232.         -- Show the flashing frame when the fadeOutTime has passed
  233.         frame.showWhenDone = showWhenDone
  234.         -- Internal timer
  235.         frame.flashTimer = 0
  236.         -- How long to hold the faded in state
  237.         frame.flashInHoldTime = flashInHoldTime
  238.         -- How long to hold the faded out state
  239.         frame.flashOutHoldTime = flashOutHoldTime
  240.        
  241.         tinsert(FLASHFRAMES, frame)
  242.        
  243.         FlashManager:SetScript("OnUpdate", FlashOnUpdate)
  244.     end
  245. end
__________________

Last edited by MunkDev : 03-18-16 at 09:11 AM.
  Reply With Quote
03-18-16, 08:44 PM   #5
tonyis3l33t
A Cyclonian
 
tonyis3l33t's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 47
Hey, thanks MunkDev for the sample code

So I troubleshot this a bit tonight just to learn from what I did wrong. Found my mistake:

Minimap:HookScript("OnEnter", function() DaftFade:FadeIn(self.GetName()) end)

the self.GetName() isn't working here. If I replace it with "Minimap" it works fine.
  Reply With Quote
03-18-16, 09:09 PM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,878
If you changed:
Code:
Minimap:HookScript("OnEnter", function() DaftFade:FadeIn(self.GetName()) end)
to:
Code:
Minimap:HookScript("OnEnter", function(self) DaftFade:FadeIn(self.GetName()) end)
But either way is essentially the same thing.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
03-19-16, 05:46 AM   #7
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
First of all, you should pass the frame itself to your function wrapper, not the name of the frame. Secondly, calling a function like this:
Lua Code:
  1. self.GetName()
...doesn't pass any frame into the GetName function. For future reference, you'll want to do this:
Lua Code:
  1. self:GetName()

https://coronalabs.com/blog/2015/12/...-dot-operator/

Try this:
Lua Code:
  1. Minimap:HookScript("OnEnter", function(self) DaftFade:FadeIn(self) end)
__________________

Last edited by MunkDev : 03-19-16 at 05:50 AM.
  Reply With Quote
03-22-16, 08:48 PM   #8
tonyis3l33t
A Cyclonian
 
tonyis3l33t's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2006
Posts: 47
So i spent many hours over the past week and ended up with this! My code is getting a little better as I gain experience, as expected. Thanks everyone for your insights.

http://www.wowinterface.com/download...-DaftFade.html
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » [FixMyCode] beginner working with OnEnter and UIFrameFadeIn


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