/
ScrollingFrame.lua
319 lines (253 loc) · 8.76 KB
/
ScrollingFrame.lua
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
--[=[
Creates an inertia based scrolling frame that is animated and has inertia frames
Alternative to a Roblox ScrollingFrame with inertia scrolling and complete control over behavior and style.
@deprecated 1.0.0
@class ScrollingFrame
]=]
local require = require(script.Parent.loader).load(script)
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Maid = require("Maid")
local ScrollModel = require("ScrollModel")
local SCROLL_TYPE = require("SCROLL_TYPE")
local Table = require("Table")
local ScrollingFrame = {}
ScrollingFrame.ClassName = "ScrollingFrame"
ScrollingFrame.__index = ScrollingFrame
--[=[
Creates a new ScrollingFrame which can be used. Prefer Container.Active = true so scroll wheel works.
Container should be in a Frame with ClipsDescendants = true
@param gui BaseGui -- Gui to use
@return ScrollingFrame
]=]
function ScrollingFrame.new(gui)
local self = setmetatable({}, ScrollingFrame)
self._maid = Maid.new()
self.Gui = gui or error("No Gui")
self._container = self.Gui.Parent or error("No container")
self._scrollType = SCROLL_TYPE.Vertical;
self._scrollbars = {}
self._model = ScrollModel.new()
self._maid:GiveTask(self:BindInput(gui))
self._maid:GiveTask(self:BindInput(self._container))
self._maid:GiveTask(self._container:GetPropertyChangedSignal("AbsoluteSize"):Connect(function()
self:_updateScrollerSize()
self:_freeScroll(true)
end))
self._maid:GiveTask(self.Gui:GetPropertyChangedSignal("AbsoluteSize"):Connect(function()
self:_updateScrollerSize()
self:_freeScroll(true)
end))
self:_updateScrollerSize()
self:_updateRender()
return self
end
-- Sets the scroll type for the frame
function ScrollingFrame:SetScrollType(scrollType)
assert(Table.contains(SCROLL_TYPE, scrollType))
self._scrollType = scrollType
end
function ScrollingFrame:AddScrollbar(scrollbar)
assert(scrollbar, "Bad scrollbar")
scrollbar:SetScrollingFrame(self)
table.insert(self._scrollbars, scrollbar)
self._maid[scrollbar] = scrollbar
end
function ScrollingFrame:RemoveScrollbar(scrollbar)
local index = Table.getIndex(self._scrollbars, scrollbar)
if index then
table.remove(self._scrollbars, index)
self._maid[scrollbar] = nil
end
end
-- Scrolls to the position in pixels offset
function ScrollingFrame:ScrollTo(position, doNotAnimate)
self._model.Target = position
if doNotAnimate then
self._model.position = self._model.Target
self._model.Velocity = 0
end
end
-- Scrolls to the top
function ScrollingFrame:ScrollToTop(doNotAnimate)
self:ScrollTo(self._model.Min, doNotAnimate)
end
-- Scrolls to the bottom
function ScrollingFrame:ScrollToBottom(doNotAnimate)
self:ScrollTo(self._model.Max, doNotAnimate)
end
function ScrollingFrame:GetModel()
return self._model
end
function ScrollingFrame:_updateScrollerSize()
self._model.TotalContentLength = self.Gui.AbsoluteSize[self._scrollType.Direction]
self._model.ViewSize = self._container.AbsoluteSize[self._scrollType.Direction]
end
function ScrollingFrame:_updateRender()
if self._scrollType == SCROLL_TYPE.Vertical then
self.Gui.Position = UDim2.new(self.Gui.Position.X, UDim.new(0, self._model.BoundedRenderPosition))
elseif self._scrollType == SCROLL_TYPE.Horizontal then
self.Gui.Position = UDim2.new(UDim.new(0, self._model.BoundedRenderPosition), self.Gui.Position.Y)
else
error("[ScrollingFrame] - Bad ScrollType")
end
for _, scrollbar in pairs(self._scrollbars) do
if scrollbar.Destroy then
scrollbar:UpdateRender()
else
warn("[ScrollingFrame] - Scrollbar is destroyed")
end
end
end
function ScrollingFrame:StopDrag()
local position = self._model.Position
if self._model:GetDisplacementPastBounds(position) == 0 then
if self._model.Velocity > 0 then
self._model.Target = math.max(self._model.Target, position + self._model.Velocity * 0.5)
else
self._model.Target = math.min(self._model.Target, position + self._model.Velocity * 0.5)
end
end
self:_freeScroll()
end
-- Scrolls until model is at rest
function ScrollingFrame:_freeScroll(lowPriority)
if lowPriority and self._maid._updateMaid then
return
end
local maid = Maid.new()
self:_updateRender()
maid:GiveTask(RunService.Heartbeat:Connect(function()
self:_updateRender()
if self._model.AtRest then
self._maid._updateMaid = nil
end
end))
self._maid._updateMaid = maid
end
--
-- @param[opt=1] strength
function ScrollingFrame:_getVelocityTracker(strength)
strength = strength or 1
self._model.Velocity = 0
local lastUpdate = tick()
local lastPos = self._model.Position
return function()
local pos = self._model.Position
local elapsed = tick() - lastUpdate
local delta = lastPos - pos
if elapsed == 0 then
elapsed = 0.03
end
self._model.Velocity = self._model.Velocity - (delta / elapsed) * strength
lastPos = pos
lastUpdate = tick()
end
end
function ScrollingFrame:_getInputProcessor(inputBeganObject)
local startPos = self._model.Position
local updateVelocity = self:_getVelocityTracker()
local originalPos = inputBeganObject.Position
return function(inputObject)
local distance = (inputObject.Position - originalPos)[self._scrollType.Direction]
local pos = startPos - distance
self._model.Position = pos
self._model.Target = pos
self:_updateRender()
updateVelocity()
return distance
end
end
-- Binds input to a specific GUI
-- @return maid Maid -- To cleanup inputs
function ScrollingFrame:BindInput(gui, options)
local maid = Maid.new()
maid:GiveTask(gui.InputBegan:Connect(function(inputObject)
self:StartScrolling(inputObject, options)
end))
maid:GiveTask(gui.InputChanged:Connect(function(inputObject)
if gui.Active and inputObject.UserInputType == Enum.UserInputType.MouseWheel then
self._model.Target = self._model.Target - inputObject.Position.z * 80
self:_freeScroll()
end
end))
self._maid:GiveTask(maid)
return maid
end
function ScrollingFrame:StartScrolling(inputBeganObject, options)
if inputBeganObject.UserInputState ~= Enum.UserInputState.Begin then
-- Touch events moving into GUIs occur sometimes
return
end
if inputBeganObject.UserInputType == Enum.UserInputType.MouseButton1
or inputBeganObject.UserInputType == Enum.UserInputType.Touch then
local maid = Maid.new()
local startTime = tick()
local totalScrollDistance = 0
local processInput = self:_getInputProcessor(inputBeganObject)
if inputBeganObject.UserInputType == Enum.UserInputType.MouseButton1 then
maid:GiveTask(UserInputService.InputChanged:Connect(function(inputObject, _)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
totalScrollDistance = totalScrollDistance + math.abs(processInput(inputObject))
end
end))
elseif inputBeganObject.UserInputType == Enum.UserInputType.Touch then
maid:GiveTask(UserInputService.InputChanged:Connect(function(inputObject, _)
if inputObject.UserInputType == Enum.UserInputType.Touch then
totalScrollDistance = totalScrollDistance + math.abs(processInput(inputObject))
end
end))
end
maid:GiveTask(function()
self:_updateRender()
if options and options.OnClick then
local elapsedTime = tick() - startTime
local consideredClick = (elapsedTime <= 0.05) or (totalScrollDistance < 1)
if consideredClick then
options.OnClick(inputBeganObject)
end
end
end)
maid:GiveTask(UserInputService.InputEnded:Connect(function(inputObject, _)
if inputObject == inputBeganObject then
self:StopDrag()
end
end))
maid:GiveTask(UserInputService.WindowFocusReleased:Connect(function()
self:StopDrag()
end))
self._maid._updateMaid = maid
end
end
function ScrollingFrame:StartScrollbarScrolling(scrollbarContainer, inputBeganObject)
assert(scrollbarContainer, "Bad scrollbarContainer")
assert(inputBeganObject, "Bad inputBeganObject")
local maid = Maid.new()
local startPosition = inputBeganObject.Position
local startPercent = self._model.ContentScrollPercent
local updateVelocity = self:_getVelocityTracker(0.25)
maid:GiveTask(UserInputService.InputChanged:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
local direction = self._scrollType.Direction
local offset = (inputObject.Position - startPosition)[direction]
local percent = offset / (scrollbarContainer.AbsoluteSize[direction] * (1 - self._model.ContentScrollPercentSize))
self._model.ContentScrollPercent = startPercent + percent
self._model.TargetContentScrollPercent = self._model.ContentScrollPercent
self:_updateRender()
updateVelocity()
end
end))
maid:GiveTask(UserInputService.InputEnded:Connect(function(inputObject)
if inputObject == inputBeganObject then
self:StopDrag()
end
end))
self._maid._updateMaid = maid
return maid
end
function ScrollingFrame:Destroy()
self._maid:DoCleaning()
self._maid = nil
setmetatable(self, nil)
end
return ScrollingFrame