PageRenderTime 51ms CodeModel.GetById 30ms app.highlight 3ms RepoModel.GetById 17ms app.codeStats 0ms

/Tukui/modules/unitframes/plugins/oUF_Smooth/oUF_Smooth.lua

http://github.com/Asphyxia/Tukui
Lua | 53 lines | 45 code | 7 blank | 1 comment | 11 complexity | 41fc974032f8bafbdaa270d6abcacb64 MD5 | raw file
 1local T, C, L = unpack(select(2, ...)) -- Import: T - functions, constants, variables; C - config; L - locales
 2if C.unitframes.enable ~= true or C.unitframes.showsmooth ~= true then return end
 3
 4local _, ns = ...
 5local oUF = ns.oUF or oUF
 6
 7if not oUF then return end
 8
 9local smoothing = {}
10local function Smooth(self, value)
11	if value ~= self:GetValue() or value == 0 then
12		smoothing[self] = value
13	else
14		smoothing[self] = nil
15	end
16end
17
18local function SmoothBar(self, bar)
19	bar.SetValue_ = bar.SetValue
20	bar.SetValue = Smooth
21end
22
23local function hook(frame)
24	frame.SmoothBar = SmoothBar
25	if frame.Health and frame.Health.Smooth then
26		frame:SmoothBar(frame.Health)
27	end
28	if frame.Power and frame.Power.Smooth then
29		frame:SmoothBar(frame.Power)
30	end
31end
32
33for i, frame in ipairs(oUF.objects) do hook(frame) end
34oUF:RegisterInitCallback(hook)
35
36local f, min, max = CreateFrame('Frame'), math.min, math.max
37f:SetScript('OnUpdate', function()
38	local rate = GetFramerate()
39	local limit = 30/rate
40	for bar, value in pairs(smoothing) do
41		local cur = bar:GetValue()
42		local new = cur + min((value-cur)/3, max(value-cur, limit))
43		if new ~= new then
44			-- Mad hax to prevent QNAN.
45			new = value
46		end
47		bar:SetValue_(new)
48		if cur == value or abs(new - value) < 2 then
49			bar:SetValue_(value)
50			smoothing[bar] = nil
51		end
52	end
53end)