Back to Home

ESO Lua File v101038

libraries/zo_spinner/zo_spinner.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
ZO_Spinner = ZO_InitializingCallbackObject:Subclass()
SPINNER_MODE_CLAMP = 1
SPINNER_MODE_WRAP = 2
ZO_SPINNER_SMALL_INCREMENT = 1
ZO_SPINNER_LARGE_INCREMENT = 10
-- Wrap for arbitrary ranges including negative numbers
-- Does *not* support floats
local function WrapInt(value, min, max)
    return (zo_floor(value) - min) % (max - min + 1) + min
end
local function ClampInt(value, min, max, step)
    value = zo_roundToNearest(value, step)
    return zo_clamp(value, min, max)
end
local function DefaultSpinnerPlaySound(spinner, currentValue, oldValue)
    if currentValue > oldValue then
        PlaySound(SOUNDS.SPINNER_UP)
    else
        PlaySound(SOUNDS.SPINNER_DOWN)
    end
end
local DEFAULT_ACCELERATION_TIME_MS = 350
function ZO_Spinner:Initialize(control, min, max, isGamepad, spinnerMode, accelerationTime)
    self.control = control
    self.decreaseButton = control:GetNamedChild("Decrease")
    self.increaseButton = control:GetNamedChild("Increase")
    self.display = control:GetNamedChild("Display")
    self.enabled = true
    self.mouseEnabled = true
    self.normalColor = ZO_SELECTED_TEXT
    self.errorColor = ZO_ERROR_COLOR
    
    if(isGamepad ~= true) then
        self:InitializeHandlers()
    end
    if spinnerMode == SPINNER_MODE_WRAP then
        self.constrainRangeFunc = WrapInt
    else 
        self.constrainRangeFunc = ClampInt
    end
    self.spinnerMode = spinnerMode or SPINNER_MODE_CLAMP
    self.step = 1
    self.value = math.huge
    self:SetMinMax(min, max)
    self.accelerationTime = accelerationTime or DEFAULT_ACCELERATION_TIME_MS
end
local DIRECTION_INCREMENT = 1
local DIRECTION_DECREMENT = -1
function ZO_Spinner:InitializeHandlers()
    local function Increment() 
        self:OnButtonDown(DIRECTION_INCREMENT) 
    end
    local function Decrement() 
        self:OnButtonDown(DIRECTION_DECREMENT)
    end
    
    local function Release()
        self:OnButtonUp()
    end
    self.increaseButton:SetHandler("OnMouseDown", Increment)
    self.increaseButton:SetHandler("OnMouseDoubleClick", Increment)
    self.increaseButton:SetHandler("OnMouseUp", Release)
    self.decreaseButton:SetHandler("OnMouseDown", Decrement)
    self.decreaseButton:SetHandler("OnMouseDoubleClick", Decrement)
    self.decreaseButton:SetHandler("OnMouseUp", Release)
    local function OnMouseWheel(_, delta)
        self:OnMouseWheel(delta) 
    end
    self.control:SetHandler("OnMouseWheel", OnMouseWheel)
    if self.display and self.display:GetType() == CT_EDITBOX then
        self.display:SetHandler("OnFocusLost", function() self:OnFocusLost() end)
        self.display:SetHandler("OnMouseWheel", OnMouseWheel)
    end
end
function ZO_Spinner:SetNormalColor(normalColor)
    self.normalColor = normalColor
    self:UpdateDisplay()
end
function ZO_Spinner:SetErrorColor(errorColor)
    self.errorColor = errorColor
    self:UpdateDisplay()
end
function ZO_Spinner:SetFont(fontString)
    self.display:SetFont(fontString)
end
function ZO_Spinner:OnMouseWheel(delta)
    if self.enabled then
        self:ModifyValue((delta > 0 and DIRECTION_INCREMENT or DIRECTION_DECREMENT) * (IsShiftKeyDown() and ZO_SPINNER_LARGE_INCREMENT or ZO_SPINNER_SMALL_INCREMENT))
    end
end
function ZO_Spinner:OnFocusLost(delta)
    local text = self.display:GetText()
    local textAsNumber = tonumber(text)
    if not textAsNumber or not self:SetValue(textAsNumber) then
        self:UpdateDisplay()
    end
end
do
    local TIME_BETWEEN_MODIFIES = 1000
    local MAX_ACCEL_FACTOR = 25
    function ZO_Spinner:GetOnUpdateFunction()
        self.onUpdate = self.onUpdate or function()
            local now = GetGameTimeMilliseconds()
            local startDelta = now - self.startTime
            local accelerationFactor = zo_min(zo_floor(startDelta / self.accelerationTime) + 1, MAX_ACCEL_FACTOR)
            self.atMaxAccelerationFactor = accelerationFactor == MAX_ACCEL_FACTOR
            local delta = now - self.lastUpdate
            self.timeUntilNextModify = self.timeUntilNextModify - delta * accelerationFactor
            if self.timeUntilNextModify < 0 then
                self.timeUntilNextModify = self.timeUntilNextModify + TIME_BETWEEN_MODIFIES
                self:ModifyValue(self.direction * (IsShiftKeyDown() and ZO_SPINNER_LARGE_INCREMENT or ZO_SPINNER_SMALL_INCREMENT))
            end
            self.lastUpdate = now
        end
        return self.onUpdate
    end
    function ZO_Spinner:OnButtonDown(direction)
        self.direction = direction
        local now = GetGameTimeMilliseconds()
        self.startTime = now
        self.lastUpdate = now
        self.timeUntilNextModify = TIME_BETWEEN_MODIFIES
        self.control:SetHandler("OnUpdate", self:GetOnUpdateFunction())
        if IsShiftKeyDown() then
            self:ModifyValue(self.direction * ZO_SPINNER_LARGE_INCREMENT)
        else
            self:ModifyValue(self.direction * ZO_SPINNER_SMALL_INCREMENT)
        end
    end
    function ZO_Spinner:IsAtMaxAccelerationFactor()
        return self.atMaxAccelerationFactor
    end
end
function ZO_Spinner:OnButtonUp(direction)
    self.control:SetHandler("OnUpdate", nil)
end
--[[A function responsible to constraining a spinner to a set of valid values.
The function takes a value and a requested delta. It should return the value
plus the delta (the target value) if that is in the valid set. Otherwise it should
return the closest valid value to the the target value]]
--
end
function ZO_Spinner:SetStep(step)
    if self.step ~= step then
        self.step = step
        if not self:SetValue(self.value) then
            self:UpdateButtons()
        end
    end
end
function ZO_Spinner:GetStep()
    return self.step
end
function ZO_Spinner:SetValueMinAndMax(value, min, max)
    min = min or 0
    max = max or math.huge
    if min ~= self.min or max ~= self.max or value ~= self.value then
        self.min = min
        self.max = max
        if not self:SetValue(value) then
            self:UpdateButtons()
        end
    end
end
function ZO_Spinner:SetMinMax(min, max)
end
function ZO_Spinner:GetMin()
    if type(self.min) == "function" then
        return self.min(self)
    end
    return self.min
end
function ZO_Spinner:GetMax()
    if type(self.max) == "function" then
        return self.max(self)
    end
    return self.max
end
function ZO_Spinner:GetControl()
    return self.control
end
function ZO_Spinner:GetValue()
    return self.value
end
function ZO_Spinner:SetSoftMax(softMax)
    self.softMax = softMax
    self:UpdateDisplay()
end
function ZO_Spinner:GetSoftMax()
    return self.softMax
end
function ZO_Spinner:UpdateButtons()
    if self.hideButtons then
        self.increaseButton:SetHidden(true)
        self.decreaseButton:SetHidden(true)
    else
        self.increaseButton:SetHidden(false)
        self.decreaseButton:SetHidden(false)
        self.increaseButton:SetEnabled(self:IsIncreaseEnabled())
        self.decreaseButton:SetEnabled(self:IsDecreaseEnabled())
    end    
    if self.display then
        if self.display:GetType() == CT_EDITBOX then
            if self:GetMax() == self:GetMin() or not self.enabled then
                self.display:SetMouseEnabled(false)
            else
                self.display:SetMouseEnabled(self.mouseEnabled)
            end
        end
    end
    self.control:SetMouseEnabled(self.mouseEnabled)
    self.increaseButton:SetMouseEnabled(self.mouseEnabled)
    self.decreaseButton:SetMouseEnabled(self.mouseEnabled)
end
function ZO_Spinner:IsIncreaseEnabled()
    if not self.enabled then
        return false
    else
        return self.value + self.step <= self:GetMax()
    end
end
function ZO_Spinner:IsDecreaseEnabled()
    if not self.enabled then
        return false
    else
        return self.value - self.step >= self:GetMin()
    end
end
function ZO_Spinner:SetMouseEnabled(mouseEnabled)
    self.mouseEnabled = mouseEnabled
    self:UpdateButtons()
end
function ZO_Spinner:SetValue(value, forceSet)
    if value ~= nil then
        value = self.constrainRangeFunc(value, self:GetMin(), self:GetMax(), self.step)
        if self.validValuesFunction then
            value = self.validValuesFunction(value, 0)
        end
        if (value ~= self.value) or forceSet then
            if value == 0 then
                -- protect against -0
                self.value = 0
            else
                self.value = value
            end
            self:UpdateDisplay()
            self:UpdateButtons()
            self:FireCallbacks("OnValueChanged", value)
            return true
        end
    end
    return false
end
function ZO_Spinner:AddToMouseInputGroup(inputGroup, inputType)
    inputGroup:Add(self.control, inputType)
    inputGroup:Add(self.increaseButton, inputType)
    inputGroup:Add(self.decreaseButton, inputType)
end
function ZO_Spinner:UpdateDisplay()
    if self.display then
        local valueText
        if self.displayTextOverride then
            valueText = self.displayTextOverride
        elseif self.valueFormatFunction then
            valueText = self.valueFormatFunction(self.value)
        else
            valueText = self.value
        end
        self.display:SetText(valueText)
        if self.softMax and self.value > self.softMax then
            self.display:SetColor(self.errorColor:UnpackRGBA())
        else
            self.display:SetColor(self.normalColor:UnpackRGBA())
        end
    end
end
function ZO_Spinner:GetFormattedValueText()
    local valueText
    if self.displayTextOverride then
        valueText = self.displayTextOverride
    elseif self.valueFormatFunction then
        valueText = self.valueFormatFunction(self.value)
    else
        valueText = self.value
    end
    return valueText
end
    self:UpdateDisplay()
end
function ZO_Spinner:SetDisplayTextOverride(displayTextOverride)
    self.displayTextOverride = displayTextOverride
    self:UpdateDisplay()
end
function ZO_Spinner:ModifyValue(change)
    if self.value then
        local oldValue = self.value
        local targetValue = self.value + change * self.step
        if self.validValuesFunction then
            targetValue = self.validValuesFunction(self.value, change * self.step)
        end
        if self:SetValue(targetValue)  then
            self.onPlaySoundFunction(self, self.value, oldValue)
        end
    end
end
function ZO_Spinner:SetEnabled(enabled)
    self.enabled = enabled
    self:UpdateButtons()
end
function ZO_Spinner:IsEnabled()
    return self.enabled
end
function ZO_Spinner:SetButtonsHidden(hideButtons)
    self.hideButtons = hideButtons
    self:UpdateButtons()
end
end
--
-- ZO_SpinnerWithLabels
--
ZO_SpinnerWithLabels = ZO_Spinner:Subclass()
function ZO_SpinnerWithLabels:Initialize(control, min, max, isGamepad, spinnerMode, accelerationTime)
    self.decreaseKeyLabel = control:GetNamedChild("DecreaseKeyLabel")
    self.increaseKeyLabel = control:GetNamedChild("IncreaseKeyLabel")
    ZO_Spinner.Initialize(self, control, min, max, isGamepad, spinnerMode, accelerationTime)
end
function ZO_SpinnerWithLabels:UpdateButtons()
    ZO_Spinner.UpdateButtons(self)
    if not self.hideButtons then
        self.increaseKeyLabel:SetEnabled(self:IsIncreaseEnabled())
        self.decreaseKeyLabel:SetEnabled(self:IsDecreaseEnabled())
    end
end