Back to Home

ESO Lua File v101041

libraries/zo_horizontalscrolllist/zo_horizontalscrolllist.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
ZO_HorizontalScrollList = ZO_Object:Subclass()
ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES = 
{
    INITIAL_UPDATE = 1,
    MOVE_RIGHT = 2,
    MOVE_LEFT = 3,
    -- LAST allows derived classes to start their movement enumerations after the base movements
    LAST = 4,
}
ZO_HORIZONTAL_SCROLL_LIST_DISPLAY_FIXED_NUMBER_OF_ENTRIES = 1
ZO_HORIZONTAL_SCROLL_LIST_ANCHOR_ENTRIES_AT_FIXED_DISTANCE = 2
    if type == ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.INITIAL_UPDATE then
        PlaySound(SOUNDS.HOR_LIST_ITEM_SELECTED)
    elseif type == ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.MOVE_RIGHT then
        PlaySound(SOUNDS.HOR_LIST_ITEM_SELECTED)
    elseif type == ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.MOVE_LEFT then
        PlaySound(SOUNDS.HOR_LIST_ITEM_SELECTED)
    end
end
--[[ Public API ]]--
function ZO_HorizontalScrollList:New(...)
    local horizontalScrollList = ZO_Object.New(self)
    horizontalScrollList:Initialize(...)
    return horizontalScrollList
end
local function DefaultEqualityFunction(leftData, rightData)
    return leftData == rightData
end
local DEFAULT_NUM_VISIBLE_ENTRIES = 5
-- for best results numVisibleEntries should be odd
function ZO_HorizontalScrollList:Initialize(control, templateName, numVisibleEntries, setupFunction, equalityFunction, onCommitWithItemsFunction, onClearedFunction)
    self.control = control
    control.horizontalScrollList = self
    self.scrollControl = control:GetNamedChild("Scroll")
    self.numVisibleEntries = (numVisibleEntries or DEFAULT_NUM_VISIBLE_ENTRIES) + 2
    self.halfNumVisibleEntries = zo_floor(self.numVisibleEntries * .5)
    self.controlEntryWidth = control:GetWidth() / (self.numVisibleEntries - 2)
    self.offsetBetweenEntries = 0
    self.displayEntryType = ZO_HORIZONTAL_SCROLL_LIST_DISPLAY_FIXED_NUMBER_OF_ENTRIES
    self.leftArrow = self.control:GetNamedChild("LeftArrow")
    self.rightArrow = self.control:GetNamedChild("RightArrow")
    self.allowWrapping = false
    self.isMoving = false
    self.enabled = true
    local function OnDragStart(clickedControl, button)
        if self.enabled and self:CanScroll() then
            self.dragging = true
            self.draggingXStart = GetUIMousePosition()
        end
    end
    local function OnDragStop()
        if self.dragging then
            self.dragging = false
            local totalDeltaX = GetUIMousePosition() - self.draggingXStart
            local lastFrameDeltaX = GetUIMouseDeltas() * 15
            self:SetSelectedIndex(zo_round((self:CalculateSelectedIndexOffset() + totalDeltaX + lastFrameDeltaX) / self.controlEntryWidth))
        end
    end
    local function OnMouseUp(clickedControl, button, upInside)
        if self.dragging then
            OnDragStop()
        elseif button == MOUSE_BUTTON_INDEX_LEFT and upInside and self.enabled then
            self:SelectControl(clickedControl)
            if self.onControlClicked then
                self.onControlClicked(clickedControl, button, upInside)
            end
        end
    end
    self.control:SetHandler("OnDragStart", OnDragStart)
    self.control:SetHandler("OnMouseUp", OnDragStop)
    self.controls = {}
    for i = 1, self.numVisibleEntries do
        local entryControl = CreateControlFromVirtual("$(parent)", self.scrollControl, templateName, i)
        entryControl:SetHandler("OnDragStart", OnDragStart)
        entryControl:SetHandler("OnMouseUp", OnMouseUp)
        self.controls[i] = entryControl
    end
    self.control:SetHandler("OnUpdate", function(_, currentFrameTimeSeconds) self:OnUpdate(currentFrameTimeSeconds) end)
    self.noItemsLabel = self.control:GetNamedChild("NoItemsLabel")
    self:Clear()
end
function ZO_HorizontalScrollList:SetAllowWrapping(allowWrapping)
    self.allowWrapping = allowWrapping
end
function ZO_HorizontalScrollList:SetOnMovementChangedCallback(onMovementChangedCallback)
end
function ZO_HorizontalScrollList:SetEnabled(enabled)
    if self.enabled ~= enabled then
        self.enabled = enabled
        self.dragging = false
        self:RefreshVisible()
    end
end
function ZO_HorizontalScrollList:SetScaleExtents(minScale, maxScale)
    self.minScale = minScale
    self.maxScale = maxScale
end
function ZO_HorizontalScrollList:SetNoItemText(text)
    if self.noItemsLabel then
        self.noItemsText = text
        self.noItemsLabel:SetText(text)
    end
end
function ZO_HorizontalScrollList:GetNoItemText()
    return self.noItemsText
end
function ZO_HorizontalScrollList:SetDisplayEntryType(displayEntryType)
    self.displayEntryType = displayEntryType
end
function ZO_HorizontalScrollList:SetOffsetBetweenEntries(offsetBetweenEntries)
    self.offsetBetweenEntries = offsetBetweenEntries
end
function ZO_HorizontalScrollList:IsMoving()
    return self.isMoving
end
function ZO_HorizontalScrollList:SetSelectionHighlightInfo(selectionHighlightControl, selectionHighlightAnimation)
    self.selectionHighlightControl = selectionHighlightControl
    self.selectionHighlightAnimation = selectionHighlightAnimation
end
-- use to override the calculated entry width when we have a scroll list
-- whose width is not equivalent to the number of visible entries
function ZO_HorizontalScrollList:SetEntryWidth(entryWidth)
    self.controlEntryWidth = entryWidth
end
function ZO_HorizontalScrollList:RefreshVisible()
    if #self.list > 0 then
        local INITIAL_UPDATE = true
        if self.isMoving then
            self:UpdateAnchors(self.lastPrimaryControlOffsetX, INITIAL_UPDATE)
        else
            local RESELECTING_DURING_REBUILD = true
            self:UpdateAnchors(self:CalculateSelectedIndexOffsetWithDrag(), INITIAL_UPDATE, RESELECTING_DURING_REBUILD)
        end
    end
end
function ZO_HorizontalScrollList:AddEntry(data)
    self.list[#self.list + 1] = data
end
function ZO_HorizontalScrollList:MoveLeft(isAutoScrollEvent)
    if self:CanScroll() then
        self:SetSelectedIndex((self.selectedIndex or 0) - 1, nil, nil, nil, isAutoScrollEvent)
    end
end
function ZO_HorizontalScrollList:MoveRight(isAutoScrollEvent)
    if self:CanScroll() then
        self:SetSelectedIndex((self.selectedIndex or 0) + 1, nil, nil, nil, isAutoScrollEvent)
    end
end
function ZO_HorizontalScrollList:SetSelectedIndex(selectedIndex, allowEvenIfDisabled, withoutAnimation, reselectingDuringRebuild, isAutoScrollEvent)
    if self.enabled or allowEvenIfDisabled then
        self.lastScrollTime = GetFrameTimeSeconds()
        self.lastInteractionAutomatic = isAutoScrollEvent
        if selectedIndex and not self.allowWrapping then
            selectedIndex = zo_clamp(selectedIndex, 1 - #self.list, 0)
        end
        if self.selectedIndex ~= selectedIndex then
            if self.onTargetDataChangedCallback then
                local oldData = self.targetData
                local targetIndex = self:CalculateDataIndexFromOffset(-selectedIndex)
                self.targetData = self.list[targetIndex]
                self.onTargetDataChangedCallback(self.targetData, oldData, reselectingDuringRebuild)
            end
            if self.selectedIndex and self.active then
                if selectedIndex > self.selectedIndex then
                    self.onPlaySoundFunction(ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.MOVE_RIGHT)
                else
                    self.onPlaySoundFunction(ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.MOVE_LEFT)
                end
            end
            self.selectedIndex = selectedIndex
        end
    end
    if withoutAnimation then
    end
end
function ZO_HorizontalScrollList:SetSelectedDataIndex(dataIndex, allowEvenIfDisabled, withoutAnimation)
    self:SetSelectedIndex(1 - dataIndex, allowEvenIfDisabled, withoutAnimation)
end
function ZO_HorizontalScrollList:Clear()
    self.lastPrimaryControlOffsetX = nil
    self.oldSelectedData = self.selectedData
    self.selectedIndex = nil
    self.selectedData = nil
    self:SetMoving(false)
    self.list = {}
    for i, entry in ipairs(self.controls) do
        entry:SetHidden(true)
    end
    if self.onClearedFunction then
        self.onClearedFunction(self)
    end
end
local function FindMatchingIndex(oldSelectedData, halfNumVisibleEntries, newDataList, equalityFunction)
    if oldSelectedData then
        for newDataIndex, newData in ipairs(newDataList) do
            if equalityFunction(oldSelectedData, newData) then
                return 1 - newDataIndex
            end
        end
    end
    return nil
end
function ZO_HorizontalScrollList:FindIndexFromData(oldSelectedData, equalityFunction)
    if (not equalityFunction) then
        equalityFunction = function (oldData, newData) return (oldData == newData) end
    end
    return FindMatchingIndex(oldSelectedData, self.halfNumVisibleEntries, self.list, equalityFunction)
end
function ZO_HorizontalScrollList:Commit()
    local hasItems = #self.list > 0
    if hasItems then
        for i, entry in ipairs(self.controls) do
            entry:SetHidden(false)
        end
        local matchingIndex = FindMatchingIndex(self.oldSelectedData, self.halfNumVisibleEntries, self.list, self.equalityFunction)
        local reselectingDuringRebuild = matchingIndex ~= nil
        local ALLOW_EVEN_IF_DISABLED = true
        self:SetSelectedIndex(matchingIndex or 0, ALLOW_EVEN_IF_DISABLED, reselectingDuringRebuild)
        local INITIAL_UPDATE = true
        self:UpdateAnchors(self:CalculateSelectedIndexOffsetWithDrag(), INITIAL_UPDATE, reselectingDuringRebuild)
    end
    self.oldSelectedData = nil
    if self.noItemsLabel then
        self.noItemsLabel:SetHidden(hasItems)
    end
    local hideArrows = not self:CanScroll()
    self.leftArrow:SetHidden(hideArrows)
    self.rightArrow:SetHidden(hideArrows)
    if hasItems and self.onCommitWithItemsFunction then
    end
end
function ZO_HorizontalScrollList:GetSelectedData()
    return self.selectedData
end
function ZO_HorizontalScrollList:GetControl()
    return self.control
end
function ZO_HorizontalScrollList:GetSelectedIndex()
    return self.selectedIndex
end
function ZO_HorizontalScrollList:GetCenterControl()
    return self.controls[self.halfNumVisibleEntries + 1]
end
function ZO_HorizontalScrollList:GetNumItems()
    return #self.list
end
function ZO_HorizontalScrollList:IsEmpty()
    return #self.list == 0
end
function ZO_HorizontalScrollList:CanScroll()
    return #self.list > 1
end
function ZO_HorizontalScrollList:ApplyTemplateToControls(template)
    for i, entry in ipairs(self.controls) do
        ApplyTemplateToControl(entry, template)
    end
end
function ZO_HorizontalScrollList:SetMouseEnabled(mouseEnabled)
    self.control:SetMouseEnabled(mouseEnabled)
end
function ZO_HorizontalScrollList:SetAutoScroll(movementType, autoScrollDuration, postInteractionDuration)
    self.autoScrollMovementType = movementType
    self.autoScrollDuration = autoScrollDuration
    self.autoScrollPostInteractionDuration = postInteractionDuration or autoScrollDuration
end
function ZO_HorizontalScrollList:ResetAutoScrollTimer()
    self.lastScrollTime = GetFrameTimeSeconds()
    self.lastInteractionAutomatic = false
end
--[[ Private API ]]--
function ZO_HorizontalScrollList:OnUpdate(currentFrameTimeSeconds)
    if #self.list > 0 and self.lastPrimaryControlOffsetX then
        local targetOffsetX = self:CalculateSelectedIndexOffsetWithDrag()
        if self.dragging then
            self:SetMoving(true)
            self:UpdateAnchors(targetOffsetX)
        elseif zo_abs(targetOffsetX - self.lastPrimaryControlOffsetX) > 2 then
            self:SetMoving(true)
            local xOffset = zo_deltaNormalizedLerp(self.lastPrimaryControlOffsetX, targetOffsetX, .2)
            self:UpdateAnchors(xOffset)
        elseif self.isMoving then
            self:SetMoving(false)
            self:UpdateAnchors(targetOffsetX)
        elseif self.autoScrollMovementType and self:CanScroll() then
            local currentDuration = self.lastInteractionAutomatic and self.autoScrollDuration or self.autoScrollPostInteractionDuration
            if currentFrameTimeSeconds > self.lastScrollTime + currentDuration then
                local IS_AUTO_EVENT = true
                if self.autoScrollMovementType == ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.MOVE_LEFT then
                    self:MoveLeft(IS_AUTO_EVENT)
                elseif self.autoScrollMovementType == ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.MOVE_RIGHT then
                    self:MoveRight(IS_AUTO_EVENT)
                end
            end
        end
    end
end
function ZO_HorizontalScrollList:CalculateSelectedIndexOffset()
    if self.selectedIndex then
        return self.selectedIndex * self.controlEntryWidth
    end
    return 0
end
function ZO_HorizontalScrollList:CalculateSelectedIndexOffsetWithDrag()
    if self.dragging then
        if self.allowWrapping then
            return self:CalculateSelectedIndexOffset() + (GetUIMousePosition() - self.draggingXStart)
        end
        return zo_clamp(self:CalculateSelectedIndexOffset() + (GetUIMousePosition() - self.draggingXStart), -self.controlEntryWidth * (#self.list - 1), 0)
    end
end
function ZO_HorizontalScrollList:CalculateOffsetIndex(controlIndex, newVisibleIndex)
    return (controlIndex - newVisibleIndex) - self.halfNumVisibleEntries - 1
end
function ZO_HorizontalScrollList:CalculateControlIndexFromOffset(offsetIndex, newVisibleIndex)
    return offsetIndex + newVisibleIndex + self.halfNumVisibleEntries + 1
end
function ZO_HorizontalScrollList:CalculateDataIndexFromOffset(offsetIndex)
    return offsetIndex % #self.list + 1
end
end
end
function ZO_HorizontalScrollList:UpdateAnchors(primaryControlOffsetX, initialUpdate, reselectingDuringRebuild)
    if self.isUpdatingAnchors then return end
    self.isUpdatingAnchors = true
    local oldPrimaryControlOffsetX = self.lastPrimaryControlOffsetX or 0
    local oldVisibleIndex = zo_round(oldPrimaryControlOffsetX / self.controlEntryWidth)
    local newVisibleIndex = zo_round(primaryControlOffsetX / self.controlEntryWidth)
    local visibleIndicesChanged = oldVisibleIndex ~= newVisibleIndex
    local oldData = self.selectedData
    for i, control in ipairs(self.controls) do
        local index = self:CalculateOffsetIndex(i, newVisibleIndex)
        if not (self.allowWrapping and self:CanScroll()) and (index >= #self.list or index < 0) then
            control:SetHidden(true)
        else
            control:SetHidden(false)
            if initialUpdate or visibleIndicesChanged then
                local dataIndex = self:CalculateDataIndexFromOffset(index)
                local selected = i == self.halfNumVisibleEntries + 1
                local data = self.list[dataIndex]
                if selected then
                    self.selectedData = data
                    if not reselectingDuringRebuild and self.selectionHighlightAnimation and not self.selectionHighlightAnimation:IsPlaying() then
                        self.selectionHighlightAnimation:PlayFromStart()
                    end
                    if not initialUpdate and not reselectingDuringRebuild and self.dragging then
                        self.onPlaySoundFunction(ZO_HORIZONTALSCROLLLIST_MOVEMENT_TYPES.INITIAL_UPDATE)
                    end
                end
                self.setupFunction(control, data, selected, reselectingDuringRebuild, self.enabled, self.selectedFromParent)
            end
            local offsetX = primaryControlOffsetX + index * self.controlEntryWidth
            control:ClearAnchors()
            if self.displayEntryType == ZO_HORIZONTAL_SCROLL_LIST_DISPLAY_FIXED_NUMBER_OF_ENTRIES then
                self:SetDefaultEntryAnchor(control, offsetX)
            else
                self:AnchorEntryAtFixedOffset(control, offsetX, index, newVisibleIndex)
            end
            if self.minScale and self.maxScale then
                local amount = ZO_EaseInQuintic(zo_max(1.0 - zo_abs(offsetX) / (self.control:GetWidth() * .5), 0.0))
                control:SetScale(zo_lerp(self.minScale, self.maxScale, amount))
            end
        end
    end
    self.lastPrimaryControlOffsetX = primaryControlOffsetX
    self.leftArrow:SetEnabled(self.enabled and (self.allowWrapping or newVisibleIndex ~= 0))
    self.rightArrow:SetEnabled(self.enabled and (self.allowWrapping or newVisibleIndex ~= 1 - #self.list))
    self.isUpdatingAnchors = false
    if (self.selectedData ~= oldData or initialUpdate) and self.onSelectedDataChangedCallback then
        self.onSelectedDataChangedCallback(self.selectedData, oldData, reselectingDuringRebuild)
    end
end
function ZO_HorizontalScrollList:SetDefaultEntryAnchor(control, offsetX)
    control:SetAnchor(CENTER, self.control, CENTER, offsetX)
end
function ZO_HorizontalScrollList:AnchorEntryAtFixedOffset(control, offsetX, index, newVisibleIndex)
    local controlHalfWidth = self.controlEntryWidth / 2
    -- check if control is on the left side
    if offsetX < -controlHalfWidth then
        local nextControlIndex = self:CalculateControlIndexFromOffset(index + 1, newVisibleIndex)
        local nextControl = self.controls[nextControlIndex]
        control:SetAnchor(RIGHT, nextControl, LEFT, -self.offsetBetweenEntries)
    -- check if control is on the right side
    elseif offsetX > controlHalfWidth then
        local previousControlIndex = self:CalculateControlIndexFromOffset(index - 1, newVisibleIndex)
        local previousControl = self.controls[previousControlIndex]
        control:SetAnchor(LEFT, previousControl, RIGHT, self.offsetBetweenEntries)
    else
        self:SetDefaultEntryAnchor(control, offsetX)
    end
end
function ZO_HorizontalScrollList:SetOnControlClicked(onControlClicked)
end
function ZO_HorizontalScrollList:SelectControl(controlToSelect)
    for i, control in ipairs(self.controls) do
        if controlToSelect == control then
            self:SetSelectedIndex((self.selectedIndex or 0) - (i - self.halfNumVisibleEntries - 1))
            break
        end
    end
end
function ZO_HorizontalScrollList:SelectControlFromCondition(conditionFunction)
    if (conditionFunction) then
        for i, control in ipairs(self.controls) do
            if conditionFunction(control) then
                self:SetSelectedIndex((self.selectedIndex or 0) - (i - self.halfNumVisibleEntries - 1))
                break
            end
        end
    end
end
function ZO_HorizontalScrollList:SetMoving(isMoving)
    if self.isMoving ~= isMoving then
        self.isMoving = isMoving
        if self.onMovementChangedCallback then
            self.onMovementChangedCallback(self, isMoving)
        end
        if self.selectionHighlightControl then
            self.selectionHighlightControl:SetHidden(isMoving)
        end
    end
end
--This is for the case where a horizontal scroll list is an entry in another list
-- and you want to change the horizontals look based on it being selected
function ZO_HorizontalScrollList:SetSelectedFromParent(selected)
    self.selectedFromParent = selected
end
function ZO_HorizontalScrollList:SetPlaySoundFunction(fn)
end
    local horizontalScrollList = control.horizontalScrollList
    if horizontalScrollList:CanScroll() then
        if delta > 0 then
            horizontalScrollList:MoveLeft()
        else
            horizontalScrollList:MoveRight()
        end
    end
end