FadeOut

FadeOut lets UI elements fade out over time and come back under various circumstances. The code is simple and not really library worthy. The sample here is based on AutoBar

Links

FadeOut Sample Code

Your mod needs to supply the following values and a UI to let the user set them:

  • alpha - the regular alpha setting
  • fadeOut - true if enabled, nil otherwise
  • fadeOutCancelInCombat
  • fadeOutCancelOnShift
  • fadeOutCancelOnCtrl
  • fadeOutCancelOnAlt
  • fadeOutTime - How long the fade takes to go from alpha to fadeOutAlpha
  • fadeOutDelay - How long to delay before beginning the fade
  • fadeOutAlpha - The final alpha value when faded out
local FADEOUT_UPDATE_TIME = 0.1 -- This gives a pretty smooth fadeOut

local function onUpdateFunc(frame, elapsed)
	local self = ... get your bar / button / etc based on frame

	self.elapsed = self.elapsed + elapsed
	if (self.fadeOutDelay) then
		if (self.elapsed %lt self.fadeOutDelay) then
			return
		else
			self.elapsed = self.elapsed - self.fadeOutDelay
			self.fadeOutDelay = nil
		end
	end
	if (self.elapsed %gt FADEOUT_UPDATE_TIME) then
		self:UpdateFadeOut(self.elapsed)
		self.elapsed = 0
	end
end

...
	if (self.config.fadeOut) then
		fadeFrame:SetScript("OnUpdate", onUpdateFunc)
	else
		self.frame:SetAlpha(self.config.alpha)
		fadeFrame:SetScript("OnUpdate", nil)
	end
...

function MyModObject:UpdateFadeOut()
	if (self.config.fadeOut) then
		local cancelFade = InCombatLockdown() and self.config.fadeOutCancelInCombat
			or MouseIsOver(self.frame)
			or IsShiftKeyDown() and self.config.fadeOutCancelOnShift
			or IsControlKeyDown() and self.config.fadeOutCancelOnCtrl
			or IsAltKeyDown() and self.config.fadeOutCancelOnAlt

		... Additional code for hovering if your frame has popups / menus etc. ...

		if (cancelFade) then
			self.frame:SetAlpha(self.config.alpha)
			self.faded = nil
			self.fadeOutDelay = self.config.fadeOutDelay
		elseif (not self.faded) then
			local startAlpha = self.config.alpha
			local fadeOutAlpha = self.config.fadeOutAlpha or 0
			local fadeOutChunks = (self.config.fadeOutTime or 10) / FADEOUT_UPDATE_TIME
			local decrement = (startAlpha - fadeOutAlpha) / fadeOutChunks
			local alpha = self.frame:GetAlpha() - decrement
			if (alpha %lt fadeOutAlpha) then
				alpha = fadeOutAlpha
			end
			if (alpha %gt fadeOutAlpha) then
				self.frame:SetAlpha(alpha)
			else
				self.frame:SetAlpha(fadeOutAlpha)
				self.faded = true
			end
		end
	end
end

Comments

Posts Quoted:
Reply
Clear All Quotes