Back to Home

ESO Lua File v101044

libraries/zo_templates/scrollcontainer_shared.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
ZO_ScrollContainer_Shared = {}
local SLIDER_MIN_VALUE = 0
local MIN_SCROLL_VALUE = 0
local MAX_SCROLL_VALUE = 100
function ZO_ScrollContainer_Shared:Initialize()
    self.scroll = self:GetNamedChild("Scroll")
    self.scroll:SetHandler("OnRectHeightChanged", function(...) self:OnScrollHeightChanged(...) end)
    self.scroll:SetHandler("OnMouseWheel", function(...) self:OnScrollBarMouseWheel(...) end)
    self.scroll:SetHandler("OnScrollOffsetChanged", function(...) self:OnScrollOffsetChanged(...) end)
    self.scrollbar = self:GetNamedChild("ScrollBar")
    self.scrollUpButton = self.scrollbar:GetNamedChild("Up")
    self.scrollUpButton:SetHandler("OnMouseDown", ZO_ScrollUp_OnMouseDown)
    self.scrollDownButton = self.scrollbar:GetNamedChild("Down")
    self.scrollDownButton:SetHandler("OnMouseDown", ZO_ScrollDown_OnMouseDown)
    self.scrollbar:SetHandler("OnRectHeightChanged", function(...) self:OnScrollHeightChanged(...) end)
    self.scrollbar:SetHandler("OnMouseWheel", function(...) self:OnScrollBarMouseWheel(...) end)
    self.scrollbar:SetHandler("OnValueChanged", function(...) self:OnScrollBarValueChanged(...) end)
    self.isScrollBarEthereal = false
    self.useScrollbar = true
    self.hideScrollBarOnDisabled = true
    self.useFadeGradient = true
    self:SetHandler("OnUpdate", function() self:OnUpdate() end)
    self:SetHandler("OnEffectivelyShown", function() self:OnEffectivelyShown() end)
    self:SetHandler("OnEffectivelyHidden", function() self:OnEffectivelyHidden() end)
    self.scroll:SetHandler("OnScrollExtentsChanged", function(...) self:OnScrollExtentsChanged(...) end)
    self.scrollIndicator = self:GetNamedChild("ScrollIndicator")
    self.scrollKeyUp = self:GetNamedChild("ScrollKeyUp")
    self.scrollKeyDown = self:GetNamedChild("ScrollKeyDown")
    self.scrollInput = 0
    self.scrollValue = SLIDER_MIN_VALUE
    self.directionalInputActivated = false
    self.scrollIndicatorEnabled = true
    self.animation, self.timeline = ZO_CreateScrollAnimation(self)
    local function OnInputChanged()
        if IsInGamepadPreferredMode() then
            self:UpdateScrollIndicator()
        end
    end
    local SHOW_UNBOUND = true
    local DEFAULT_GAMEPAD_ACTION_NAME = nil
    ZO_Keybindings_RegisterLabelForBindingUpdate(self.scrollKeyUp, "UI_SHORTCUT_RIGHT_STICK_UP", SHOW_UNBOUND, DEFAULT_GAMEPAD_ACTION_NAME, OnInputChanged)
    ZO_Keybindings_RegisterLabelForBindingUpdate(self.scrollKeyDown, "UI_SHORTCUT_RIGHT_STICK_DOWN")
    -- We only need to register one of the above with OnInputChanged because one call of that function does everything we need
    ZO_UpdateScrollFade(self.useFadeGradient, self.scroll, ZO_SCROLL_DIRECTION_VERTICAL)
    self:RegisterForEvent(EVENT_GAMEPAD_PREFERRED_MODE_CHANGED, function(_, ...) self:OnGamepadPreferredModeChanged(...) end)
end
function ZO_ScrollContainer_Shared:ResetToTop()
    self.scrollValue = SLIDER_MIN_VALUE
    ZO_ScrollAnimation_MoveWindow(self, self.scrollValue)
    ZO_UpdateScrollFade(self.useFadeGradient, self.scroll, ZO_SCROLL_DIRECTION_VERTICAL, ZO_GetScrollMaxFadeGradientSize(self))
end
function ZO_ScrollContainer_Shared:SetScrollIndicatorEnabled(enabled)
    self.scrollIndicatorEnabled = enabled
end
function ZO_ScrollContainer_Shared:UpdateScrollIndicator()
    local _, verticalExtents = self.scroll:GetScrollExtents()
    local shouldShowGamepadKeybinds = ZO_Keybindings_ShouldShowGamepadKeybind()
    local hideGamepad = not (self.scrollIndicatorEnabled and verticalExtents ~= 0 and shouldShowGamepadKeybinds)
    local hideKeyboard = not (self.scrollIndicatorEnabled and verticalExtents ~= 0 and not shouldShowGamepadKeybinds)
    self.scrollIndicator:SetHidden(hideGamepad)
    self.scrollKeyUp:SetHidden(hideKeyboard)
    self.scrollKeyDown:SetHidden(hideKeyboard)
end
function ZO_ScrollContainer_Shared:UpdateScrollBar()
    if IsInGamepadPreferredMode() then
        return
    end
    local scroll = self.scroll
    local _, verticalOffset = scroll:GetScrollOffsets()
    local _, verticalExtents = scroll:GetScrollExtents()
    local scrollbar  = self.scrollbar
    --thumb resizing
    local scale = scroll:GetScale()
    local scrollBarHeight = scrollbar:GetHeight() / scale
    local scrollAreaHeight = scroll:GetHeight() / scale
    if verticalExtents > 0 and scrollBarHeight >= 0 and scrollAreaHeight >= 0 then
        local thumbHeight = scrollBarHeight * scrollAreaHeight / (verticalExtents + scrollAreaHeight)
        scrollbar:SetThumbTextureHeight(thumbHeight)
    else
        scrollbar:SetThumbTextureHeight(scrollBarHeight)
    end
    --set mouse input enabled based on scrollability
    local scrollEnabled = verticalExtents > 0 or verticalOffset > 0
    scroll:SetMouseEnabled(scrollEnabled)
    --auto scroll bar hiding
    local wasHidden = scrollbar:IsHidden()
    local scrollbarHidden = not self.useScrollbar or (self.hideScrollBarOnDisabled and not scrollEnabled) or self.isScrollBarEthereal
    scrollbar:SetHidden(scrollbarHidden)
    local maxScrollValue = (not scrollbarHidden or self.isScrollBarEthereal) and MAX_SCROLL_VALUE or MIN_SCROLL_VALUE
    scrollbar:SetMinMax(MIN_SCROLL_VALUE, maxScrollValue)
    if wasHidden and not scrollbarHidden and scrollbar.resetScrollbarOnShow then
        ZO_Scroll_ResetToTop(self)
        self.scrollValue = MIN_SCROLL_VALUE
    end
    if verticalExtents > 0 then
        local previousScrollBarValue = scrollbar:GetValue()
        verticalOffset = self.scrollRawOffset and self.scrollRawOffset or verticalOffset
        local finalValue = MAX_SCROLL_VALUE * (verticalOffset / verticalExtents)
        if not zo_floatsAreEqual(previousScrollBarValue, finalValue) then
            scrollbar:SetValue(finalValue)
        end
        -- If the previous and current scrollBar values are the same,
        -- the onValueChanged function will not be invoked to update the scroll child position.
        -- We need to explicitly call to recalculate our scroll child position since our extents changed.
        if previousScrollBarValue == scrollbar:GetValue() then
            scroll:SetVerticalScroll(verticalOffset)
        end
    else
        ZO_Scroll_ResetToTop(self)
    end
    self.scrollRawOffset = nil
end
function ZO_ScrollContainer_Shared:SetDisabled(disabled)
    self.disabled = disabled
end
function ZO_ScrollContainer_Shared:RefreshDirectionalInputActivation()
    local _, verticalExtents = self.scroll:GetScrollExtents()
    local canScroll = verticalExtents > 0
    if not self.disabled and not self:IsHidden() and canScroll then
        if not self.directionalInputActivated then
            self.directionalInputActivated = true
            ZO_SCROLL_SHARED_INPUT:Activate(self, self)
        end
    else
        if self.directionalInputActivated then
            self.directionalInputActivated = false
            ZO_SCROLL_SHARED_INPUT:Deactivate(self)
        end
    end
end
function ZO_ScrollContainer_Shared:OnEffectivelyShown()
    self:ResetToTop()
    self.inScrollArea = nil
end
function ZO_ScrollContainer_Shared:OnEffectivelyHidden()
end
function ZO_ScrollContainer_Shared:OnScrollHeightChanged()
    if IsInGamepadPreferredMode() then
        self:UpdateScrollIndicator()
    else
        self:UpdateScrollBar()
    end
    ZO_UpdateScrollFade(self.useFadeGradient, self.scroll, ZO_SCROLL_DIRECTION_VERTICAL)
end
function ZO_ScrollContainer_Shared:OnScrollExtentsChanged()
    if IsInGamepadPreferredMode() then
        self:UpdateScrollIndicator()
    else
        self:UpdateScrollBar()
    end
    ZO_UpdateScrollFade(self.useFadeGradient, self.scroll, ZO_SCROLL_DIRECTION_VERTICAL)
end
function ZO_ScrollContainer_Shared:OnScrollBarMouseWheel(control, delta)
    ZO_ScrollRelative(self, -delta * 40)
    end
end
function ZO_ScrollContainer_Shared:OnScrollOffsetChanged(control, delta)
end
function ZO_ScrollContainer_Shared:OnScrollBarValueChanged(control, value)
    local scroll = self.scroll
    local _, verticalExtents = scroll:GetScrollExtents()
    scroll:SetVerticalScroll((value/MAX_SCROLL_VALUE) * verticalExtents)
end
function ZO_ScrollContainer_Shared:OnUpdate()
    if IsInGamepadPreferredMode() then
        local scrollInput = ZO_SCROLL_SHARED_INPUT:GetY()
        if scrollInput ~= 0 then
            ZO_ScrollRelative(self, -scrollInput * 10)
            if self.onInteractWithScrollbarCallback then
                self.onInteractWithScrollbarCallback()
            end
        end
    else
        local inScrollArea = MouseIsOver(self)
        if inScrollArea ~= self.inScrollArea then
            self.inScrollArea = inScrollArea
            if inScrollArea then
                ZO_VerticalScrollbarBase_OnScrollAreaEnter(self.scrollbar)
            else
                ZO_VerticalScrollbarBase_OnScrollAreaExit(self.scrollbar)
            end
        end
    end
end
function ZO_ScrollContainer_Shared:OnGamepadPreferredModeChanged(isGamepadPreferred)
    if isGamepadPreferred then
        self.scrollbar:SetHidden(true)
        self:UpdateScrollIndicator()
    else
        self.scrollIndicator:SetHidden(true)
        self.scrollKeyUp:SetHidden(true)
        self.scrollKeyDown:SetHidden(true)
        self:UpdateScrollBar()
    end
end
function ZO_ScrollContainer_Shared:ScrollControlIntoView(control)
    local scroll = self.scroll
    local scrollTop = scroll:GetTop()
    local scrollBottom = scroll:GetBottom()
    local controlTop = control:GetTop()
    local controlBottom = control:GetBottom()
    if controlTop < scrollTop then
        ZO_ScrollRelative(self, controlTop - scrollTop)
    elseif controlBottom > scrollBottom then
        ZO_ScrollRelative(self, controlBottom - scrollBottom)
    end
end
function ZO_ScrollContainer_Shared:ScrollControlIntoCentralView(control)
    local scroll = self.scroll
    local scrollTop = scroll:GetTop()
    local scrollBottom = scroll:GetBottom()
    local controlTop = control:GetTop()
    local controlBottom = control:GetBottom()
    local halfControlHeight = (controlBottom - controlTop) * 0.5
    local halfScrollHeight = (scrollBottom - scrollTop) * 0.5
    local scrollDistance = controlTop + halfControlHeight - scrollTop - halfScrollHeight
    ZO_ScrollRelative(self, scrollDistance)
end
function ZO_ScrollContainer_Shared.SetScrollIndicatorAnchor(scrollContainer, anchorRelativeToControl, anchorRelativePoint, offsetX, offsetY)
    offsetX = offsetX or 0
    offsetY = offsetY or 0
    scrollContainer.scrollIndicator:SetAnchor(CENTER, anchorRelativeToControl, anchorRelativePoint, offsetX, offsetY)
end
-- Functions for XML
function ZO_ScrollContainer_Shared.InitializeFromControl(control)
    zo_mixin(control, ZO_ScrollContainer_Shared)
    control:Initialize()
end