You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
2.9 KiB

4 years ago
local E, L, V, P, G = unpack(ElvUI)
5 years ago
-- Credit: ls- (lightspark)
5 months ago
5 years ago
local abs, next, Lerp = abs, next, Lerp
local tonumber, assert = tonumber, assert
local activeObjects = {}
local handledObjects = {}
local TARGET_FPS = 60
local AMOUNT = 0.33
5 months ago
local frame = CreateFrame('Frame')
function E:Smoothing_Clamp(v, min, max)
if not min then min = 0 end
if not max then max = 1 end
5 years ago
if v > max then
return max
elseif v < min then
return min
end
return v
end
5 months ago
function E:Smoothing_IsCloseEnough(new, target, range)
5 years ago
if range > 0 then
return abs((new - target) / range) <= 0.001
end
return true
end
5 months ago
function E:Smoothing_OnUpdate(elapsed)
5 years ago
for object, target in next, activeObjects do
5 months ago
local new = Lerp(object._value, target, E:Smoothing_Clamp(AMOUNT * elapsed * TARGET_FPS))
if E:Smoothing_IsCloseEnough(new, target, object._max - object._min) then
5 years ago
new = target
5 months ago
5 years ago
activeObjects[object] = nil
end
object:SetValue_(new)
object._value = new
end
end
5 months ago
function E:Smoothing_SetSmoothedValue(value)
5 years ago
value = tonumber(value)
assert(value, 'bar_SetSmoothedValue requires (value) to be a number.')
self._value = self:GetValue()
5 months ago
activeObjects[self] = E:Smoothing_Clamp(value, self._min, self._max)
5 years ago
end
5 months ago
function E:Smoothing_SetSmoothedMinMaxValues(min, max)
5 years ago
min, max = tonumber(min), tonumber(max)
assert(min and max, 'bar_SetSmoothedMinMaxValues requires (min and max) to be a number.')
self:SetMinMaxValues_(min, max)
if self._max and self._max ~= max then
local ratio = 1
if max ~= 0 and self._max and self._max ~= 0 then
ratio = max / (self._max or max)
end
local target = activeObjects[self]
if target then
activeObjects[self] = target * ratio
end
local cur = self._value
if cur then
self:SetValue_(cur * ratio)
self._value = cur * ratio
end
end
self._min = min
self._max = max
end
5 months ago
function E:Smoothing_Enable(bar)
5 years ago
bar._min, bar._max = bar:GetMinMaxValues()
bar._value = bar:GetValue()
if not bar.SetValue_ then
bar.SetValue_ = bar.SetValue
5 months ago
bar.SetValue = E.Smoothing_SetSmoothedValue
5 years ago
end
if not bar.SetMinMaxValues_ then
bar.SetMinMaxValues_ = bar.SetMinMaxValues
5 months ago
bar.SetMinMaxValues = E.Smoothing_SetSmoothedMinMaxValues
5 years ago
end
if not frame:GetScript('OnUpdate') then
5 months ago
frame:SetScript('OnUpdate', E.Smoothing_OnUpdate)
5 years ago
end
handledObjects[bar] = true
end
5 months ago
function E:Smoothing_Disable(bar)
5 years ago
if activeObjects[bar] then
bar:SetValue_(activeObjects[bar])
activeObjects[bar] = nil
end
if handledObjects[bar] then
handledObjects[bar] = nil
end
if bar.SetValue_ then
bar.SetValue = bar.SetValue_
bar.SetValue_ = nil
end
if bar.SetMinMaxValues_ then
bar.SetMinMaxValues = bar.SetMinMaxValues_
bar.SetMinMaxValues_ = nil
end
if not next(handledObjects) then
frame:SetScript('OnUpdate', nil)
end
end
function E:SetSmoothingAmount(amount)
5 months ago
AMOUNT = E:Smoothing_Clamp(amount, 0.2, 0.8)
5 years ago
end
function E:SetSmoothing(bar, enable)
if enable then
5 months ago
E:Smoothing_Enable(bar)
5 years ago
else
5 months ago
E:Smoothing_Disable(bar)
5 years ago
end
end