Back to Home

ESO Lua File v100022

libraries/zo_templates/statusbartemplates.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
function ZO_StatusBar_SetGradientColor(statusBar, gradientColorTable)
    if statusBar and gradientColorTable then
        local r, g, b, a = gradientColorTable[1]:UnpackRGBA()
        local r1, g1, b1, a1 = gradientColorTable[2]:UnpackRGBA()
        statusBar:SetGradientColors(r, g, b, a, r1, g1, b1, a1)
    end
end
    local startR, startG, startB = GetInterfaceColor(INTERFACE_COLOR_TYPE_GENERAL, INTERFACE_GENERAL_COLOR_STATUS_BAR_START)
    local endR, endG, endB = GetInterfaceColor(INTERFACE_COLOR_TYPE_GENERAL, INTERFACE_GENERAL_COLOR_STATUS_BAR_END)
    statusBar:SetGradientColors(startR, startG, startB, 1, endR, endG, endB, 1)
end
FORCE_INIT_SMOOTH_STATUS_BAR = true
do
    local g_updatingBars = {}
    local g_animationPool
    local DEFAULT_ANIMATION_TIME_MS = 500
    local function OnAnimationTransitionUpdate(animation, progress)
        local bar = animation.bar
        local initialValue = animation.initialValue
        local endValue = animation.endValue
        if progress < 1 then
            local newBarValue = zo_lerp(initialValue, endValue, progress)
            bar:SetValue(newBarValue)
        end
    end
    local function OnStopAnimation(animation)
        local animationKey = animation.key
        local bar = animation:GetFirstAnimation().bar
        bar.animation = nil
        g_animationPool:ReleaseObject(animationKey)
        if bar.onStopCallback then
            bar.onStopCallback(bar)
        end
    end
    local function AcquireAnimation()
        if not g_animationPool then
            local function Factory(objectPool)
                local animation = ANIMATION_MANAGER:CreateTimelineFromVirtual("ZO_StatusBarGrowTemplate")
                animation:GetFirstAnimation():SetUpdateFunction(OnAnimationTransitionUpdate)
                animation:SetHandler("OnStop", function(animation) OnStopAnimation(animation)  end)
                return animation
            end
            local function Reset(object)
                local customAnimation = object:GetFirstAnimation()
                customAnimation.bar = nil
                customAnimation.initialValue = nil
                customAnimation.endValue = nil
            end
            g_animationPool = ZO_ObjectPool:New(Factory, Reset)
        end
        local animation, key = g_animationPool:AcquireObject()
        animation.key = key
        return animation
    end
    function ZO_StatusBar_SmoothTransition(self, value, max, forceInit, onStopCallback, customApproachAmountMs)
        local oldValue = self:GetValue()
        self:SetMinMax(0, max)
        local oldMax = self.max or max
        self.max = max
        self.onStopCallback = onStopCallback
        if forceInit or max <= 0 then
            self:SetValue(value)
            if self.animation then
                self.animation:Stop()
            end
            if onStopCallback then
                onStopCallback(self)
            end
        else
            if oldMax > 0 and oldMax ~= max then
                local maxChange = max / oldMax
                oldValue = oldValue * maxChange
                self:SetValue(oldValue)
            end
            if not self.animation then
                local updateAnimation = AcquireAnimation()
                self.animation = updateAnimation
            else
                local x = 2
            end
            local customAnimation = self.animation:GetFirstAnimation()
            customAnimation:SetDuration(customApproachAmountMs or DEFAULT_ANIMATION_TIME_MS)
            customAnimation.bar = self
            customAnimation.initialValue = oldValue
            customAnimation.endValue = value
            
            self.animation:PlayFromStart()
        end
    end
    function ZO_StatusBar_GetTargetValue(statusBar)
        return statusBar.animation:GetFirstAnimation().endValue or statusBar:GetValue()
    end
end
ZO_WrappingStatusBar = ZO_Object:Subclass()
function ZO_WrappingStatusBar:New(...)
    local wrappingStatusBar = ZO_Object.New(self)
    wrappingStatusBar:Initialize(...)
    return wrappingStatusBar
end
function ZO_WrappingStatusBar:Initialize(statusBar, onLevelChangedCallback)
    self.statusBar = statusBar
    self.onAnimationFinishedCallback = function()
        self:OnAnimationFinished()
    end
end
function ZO_WrappingStatusBar:SetOnLevelChangeCallback(onLevelChangedCallback)
end
function ZO_WrappingStatusBar:SetOnCompleteCallback(onCompleteCallback)
end
function ZO_WrappingStatusBar:GetControl()
    return self.statusBar
end
function ZO_WrappingStatusBar:SetHidden(hidden)
    self.statusBar:SetHidden(hidden)
end
function ZO_WrappingStatusBar:GetValue()
    return ZO_StatusBar_GetTargetValue(self.statusBar)
end
function ZO_WrappingStatusBar:Reset()
    self.level = nil
end
function ZO_WrappingStatusBar:SetAnimationTime(time)
    self.customAnimationTime = time
end
function ZO_WrappingStatusBar:SetValue(level, value, max, noWrap)
    local forceInit = false
    if self.level ~= level then
        if self.level and level then
            if level > self.level and not noWrap then
                self.pendingLevels = (self.pendingLevels or 0) + level - self.level
            else
                self.pendingLevels = nil
            end
        else
            self.pendingLevels = nil
            forceInit = true
            noWrap = true
        end
        self.level = level
        
        if self.pendingLevels == nil and self.onLevelChangedCallback then
            self.onLevelChangedCallback(self, self.level)
        end
    end
    if self.pendingLevels and not forceInit and not noWrap then
        self.pendingValue = value
        self.pendingMax = max
        
        ZO_StatusBar_SmoothTransition(self.statusBar, max, max, nil, self.onAnimationFinishedCallback, self.customAnimationTime)
    else
        if forceInit then
            ZO_StatusBar_SmoothTransition(self.statusBar, value, max, true)
        else
            ZO_StatusBar_SmoothTransition(self.statusBar, value, max, false, self.onCompleteCallback, self.customAnimationTime)
        end
        self.pendingValue = nil
        self.pendingMax = nil
    end
end
function ZO_WrappingStatusBar:OnAnimationFinished()
    self.pendingLevels = self.pendingLevels - 1
    if self.pendingLevels == 0 then
        ZO_StatusBar_SmoothTransition(self.statusBar, 0, self.pendingMax, FORCE_INIT_SMOOTH_STATUS_BAR)
        ZO_StatusBar_SmoothTransition(self.statusBar, self.pendingValue, self.pendingMax, nil, self.onCompleteCallback, self.customAnimationTime)
        self.pendingLevels = nil
        self.pendingValue = nil
        self.pendingMax = nil
        if self.onLevelChangedCallback then
            self.onLevelChangedCallback(self, self.level)
        end
    else
        ZO_StatusBar_SmoothTransition(self.statusBar, 0, self.pendingMax, FORCE_INIT_SMOOTH_STATUS_BAR)
        ZO_StatusBar_SmoothTransition(self.statusBar, self.pendingMax, self.pendingMax, nil, self.onAnimationFinishedCallback, self.customAnimationTime)
        if self.onLevelChangedCallback then
            self.onLevelChangedCallback(self, self.level - self.pendingLevels)
        end
    end
end
--[[
Inventory Item Improvement Status Bar Mix In
--]]
ZO_InventoryItemImprovementStatusBar = {}
function ZO_InventoryItemImprovementStatusBar:Initialize(control)
    zo_mixin(control, ZO_InventoryItemImprovementStatusBar)
    control.currentBar = control:GetNamedChild("Bar")
    control.improvementBar = control:GetNamedChild("Underlay")
end
function ZO_InventoryItemImprovementStatusBar:SetMinMax(min, max)
    self.currentBar:SetMinMax(min, max)
    self.improvementBar:SetMinMax(min, max)
    self.min, self.max = min, max
end
function ZO_InventoryItemImprovementStatusBar:SetValueAndPreviewValue(value, previewValue)
    ZO_StatusBar_SmoothTransition(self.improvementBar, previewValue, self.max)
    self.currentBar:SetValue(value)
end
function ZO_InventoryItemImprovementStatusBar:SetGradientColors(...)
    self.currentBar:SetGradientColors(...)
end
--[[
Stable Training Skill Status Bar Mix In
--]]
ZO_StableTrainingBar_Gamepad = {}
function ZO_StableTrainingBar_Gamepad:Initialize(control)
    zo_mixin(control, ZO_StableTrainingBar_Gamepad)
    control.bar = control:GetNamedChild("StatusBar"):GetNamedChild("Bar")
    control.value = control:GetNamedChild("Value")
end
function ZO_StableTrainingBar_Gamepad:SetMinMax(min, max)
    self.currentBar:SetMinMax(min, max)
    self.improvementBar:SetMinMax(min, max)
    self.min, self.max = min, max
end
local FORCE_VALUE = true
function ZO_StableTrainingBar_Gamepad:SetValue(value, maxValue, format)
    ZO_StatusBar_SmoothTransition(self.bar, value, maxValue, FORCE_VALUE)
end
function ZO_StableTrainingBar_Gamepad:SetGradientColors(...)
    self.bar:SetGradientColors(...)
end