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

http://github.com/Asphyxia/Tukui · Lua · 53 lines · 45 code · 7 blank · 1 comment · 21 complexity · 41fc974032f8bafbdaa270d6abcacb64 MD5 · raw file

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