-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathWidget.lua
More file actions
436 lines (403 loc) · 16 KB
/
Copy pathWidget.lua
File metadata and controls
436 lines (403 loc) · 16 KB
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
-- I named this 'Widget' instead of 'Icon' to make a clear difference between the icon *object* and
-- the icon (aka Widget) instance.
-- This contains the core components of the icon such as the button, image, label and notice. It's
-- also responsible for handling the automatic resizing of the widget (based upon image visibility and text length)
return function(icon, Icon)
local widget = Instance.new("Frame")
widget:SetAttribute("WidgetUID", icon.UID)
widget.Name = "Widget"
widget.BackgroundTransparency = 1
widget.Visible = true
widget.ZIndex = 20
widget.Active = false
widget.ClipsDescendants = true
local button = Instance.new("Frame")
button.Name = "IconButton"
button.Visible = true
button.ZIndex = 2
button.BorderSizePixel = 0
button.Parent = widget
button.ClipsDescendants = true
button.Active = false -- This is essential for mobile scrollers to work when dragging
icon.deselected:Connect(function()
button.ClipsDescendants = true
task.delay(0.2, function()
if icon.isSelected then
button.ClipsDescendants = false
end
end)
end)
-- Account for PreferredTransparency which can be set by every player
local GuiService = game:GetService("GuiService")
icon:setBehaviour("IconButton", "BackgroundTransparency", function(value)
local preference = GuiService.PreferredTransparency
local newValue = value * preference
if value == 1 then
return value
end
return newValue
end)
icon.janitor:add(GuiService:GetPropertyChangedSignal("PreferredTransparency"):Connect(function()
icon:refreshAppearance(button, "BackgroundTransparency")
end))
local iconCorner = Instance.new("UICorner")
iconCorner:SetAttribute("Collective", "IconCorners")
iconCorner.Name = "UICorner"
iconCorner.Parent = button
local menu = require(script.Parent.Menu)(icon)
local menuUIListLayout = menu.MenuUIListLayout
local menuGap = menu.MenuGap
menu.Parent = button
local iconSpot = Instance.new("Frame")
iconSpot.Name = "IconSpot"
iconSpot.BackgroundColor3 = Color3.fromRGB(225, 225, 225)
iconSpot.BackgroundTransparency = 0.9
iconSpot.Visible = true
iconSpot.AnchorPoint = Vector2.new(0, 0.5)
iconSpot.ZIndex = 5
iconSpot.Parent = menu
local iconSpotCorner = iconCorner:Clone()
iconSpotCorner.Parent = iconSpot
local overlay = iconSpot:Clone()
overlay.UICorner.Name = "OverlayUICorner"
overlay.Name = "IconOverlay"
overlay.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
overlay.ZIndex = iconSpot.ZIndex + 1
overlay.Size = UDim2.new(1, 0, 1, 0)
overlay.Position = UDim2.new(0, 0, 0, 0)
overlay.AnchorPoint = Vector2.new(0, 0)
overlay.Visible = false
overlay.Parent = iconSpot
local clickRegion = Instance.new("TextButton")
clickRegion:SetAttribute("CorrespondingIconUID", icon.UID)
clickRegion.Name = "ClickRegion"
clickRegion.BackgroundTransparency = 1
clickRegion.Visible = true
clickRegion.Text = ""
clickRegion.ZIndex = 20
clickRegion.Selectable = true
clickRegion.SelectionGroup = true
clickRegion.Parent = iconSpot
local Gamepad = require(script.Parent.Parent.Features.Gamepad)
Gamepad.registerButton(clickRegion)
local clickRegionCorner = iconCorner:Clone()
clickRegionCorner.Parent = clickRegion
local contents = Instance.new("Frame")
contents.Name = "Contents"
contents.BackgroundTransparency = 1
contents.Size = UDim2.fromScale(1, 1)
contents.Parent = iconSpot
local contentsList = Instance.new("UIListLayout")
contentsList.Name = "ContentsList"
contentsList.FillDirection = Enum.FillDirection.Horizontal
contentsList.VerticalAlignment = Enum.VerticalAlignment.Center
contentsList.SortOrder = Enum.SortOrder.LayoutOrder
contentsList.VerticalFlex = Enum.UIFlexAlignment.SpaceEvenly
contentsList.Padding = UDim.new(0, 3)
contentsList.Parent = contents
local paddingLeft = Instance.new("Frame")
paddingLeft.Name = "PaddingLeft"
paddingLeft.LayoutOrder = 1
paddingLeft.ZIndex = 5
paddingLeft.BorderColor3 = Color3.fromRGB(0, 0, 0)
paddingLeft.BackgroundTransparency = 1
paddingLeft.BorderSizePixel = 0
paddingLeft.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
paddingLeft.Parent = contents
local paddingCenter = Instance.new("Frame")
paddingCenter.Name = "PaddingCenter"
paddingCenter.LayoutOrder = 3
paddingCenter.ZIndex = 5
paddingCenter.Size = UDim2.new(0, 0, 1, 0)
paddingCenter.BorderColor3 = Color3.fromRGB(0, 0, 0)
paddingCenter.BackgroundTransparency = 1
paddingCenter.BorderSizePixel = 0
paddingCenter.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
paddingCenter.Parent = contents
local paddingRight = Instance.new("Frame")
paddingRight.Name = "PaddingRight"
paddingRight.LayoutOrder = 5
paddingRight.ZIndex = 5
paddingRight.BorderColor3 = Color3.fromRGB(0, 0, 0)
paddingRight.BackgroundTransparency = 1
paddingRight.BorderSizePixel = 0
paddingRight.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
paddingRight.Parent = contents
local iconLabelContainer = Instance.new("Frame")
iconLabelContainer.Name = "IconLabelContainer"
iconLabelContainer.LayoutOrder = 4
iconLabelContainer.ZIndex = 3
iconLabelContainer.AnchorPoint = Vector2.new(0, 0.5)
iconLabelContainer.Size = UDim2.new(0, 0, 0.5, 0)
iconLabelContainer.BackgroundTransparency = 1
iconLabelContainer.Position = UDim2.new(0.5, 0, 0.5, 0)
iconLabelContainer.Parent = contents
local iconLabel = Instance.new("TextLabel")
local viewportX = workspace.CurrentCamera.ViewportSize.X+200
iconLabel.Name = "IconLabel"
iconLabel.LayoutOrder = 4
iconLabel.ZIndex = 15
iconLabel.AnchorPoint = Vector2.new(0, 0)
iconLabel.Size = UDim2.new(0, viewportX, 1, 0)
iconLabel.ClipsDescendants = false
iconLabel.BackgroundTransparency = 1
iconLabel.Position = UDim2.fromScale(0, 0)
iconLabel.RichText = true
iconLabel.TextColor3 = Color3.fromRGB(255, 255, 255)
iconLabel.TextXAlignment = Enum.TextXAlignment.Left
iconLabel.Text = ""
iconLabel.TextWrapped = true
iconLabel.TextScaled = false
iconLabel.Active = false
iconLabel.AutoLocalize = true
iconLabel.Parent = iconLabelContainer
local iconImage = Instance.new("ImageLabel")
iconImage.Name = "IconImage"
iconImage.LayoutOrder = 2
iconImage.ZIndex = 15
iconImage.AnchorPoint = Vector2.new(0, 0.5)
iconImage.Size = UDim2.new(0, 0, 0.5, 0)
iconImage.BackgroundTransparency = 1
iconImage.Position = UDim2.new(0, 11, 0.5, 0)
iconImage.ScaleType = Enum.ScaleType.Stretch
iconImage.Active = false
iconImage.Parent = contents
local iconImageCorner = iconCorner:Clone()
iconImageCorner:SetAttribute("Collective", nil)
iconImageCorner.CornerRadius = UDim.new(0, 0)
iconImageCorner.Name = "IconImageCorner"
iconImageCorner.Parent = iconImage
local TweenService = game:GetService("TweenService")
local resizingCount = 0
local function handleLabelAndImageChangesUnstaggered(forceUpdateString)
-- We defer changes by a frame to eliminate all but 1 requests which
-- could otherwise stack up to 20+ requests in a single frame
-- We then repeat again once to account for any final changes
-- Deferring is also essential because properties are set immediately
-- afterwards (therefore calculations will use the correct values)
task.defer(function()
local indicator = icon.indicator
local usingIndicator = indicator and indicator.Visible
local usingText = usingIndicator or iconLabel.Text ~= ""
local usingImage = iconImage.Image ~= "" and iconImage.Image ~= nil
local _alignment = Enum.HorizontalAlignment.Center
local NORMAL_BUTTON_SIZE = UDim2.fromScale(1, 1)
local buttonSize = NORMAL_BUTTON_SIZE
if usingImage and not usingText then
iconLabelContainer.Visible = false
iconImage.Visible = true
paddingLeft.Visible = false
paddingCenter.Visible = false
paddingRight.Visible = false
elseif not usingImage and usingText then
iconLabelContainer.Visible = true
iconImage.Visible = false
paddingLeft.Visible = true
paddingCenter.Visible = false
paddingRight.Visible = true
elseif usingImage and usingText then
iconLabelContainer.Visible = true
iconImage.Visible = true
paddingLeft.Visible = true
paddingCenter.Visible = not usingIndicator
paddingRight.Visible = not usingIndicator
_alignment = Enum.HorizontalAlignment.Left
end
button.Size = buttonSize
local function getItemWidth(item)
local targetWidth = item:GetAttribute("TargetWidth") or item.AbsoluteSize.X
return targetWidth
end
local contentsPadding = contentsList.Padding.Offset
local initialWidgetWidth = contentsPadding --0
local textWidth = iconLabel.TextBounds.X
iconLabelContainer.Size = UDim2.new(0, textWidth, iconLabel.Size.Y.Scale, 0)
for _, child in pairs(contents:GetChildren()) do
if child:IsA("GuiObject") and child.Visible == true then
local itemWidth = getItemWidth(child)
initialWidgetWidth += itemWidth + contentsPadding
end
end
local widgetMinimumWidth = widget:GetAttribute("MinimumWidth")
local widgetMinimumHeight = widget:GetAttribute("MinimumHeight")
local widgetBorderSize = widget:GetAttribute("BorderSize")
local widgetWidth = math.clamp(initialWidgetWidth, widgetMinimumWidth, viewportX)
local menuIcons = icon.menuIcons
local additionalWidth = 0
local hasMenu = #menuIcons > 0
local showMenu = hasMenu and icon.isSelected
if showMenu then
for _, frame in pairs(menu:GetChildren()) do
if frame ~= iconSpot and frame:IsA("GuiObject") and frame.Visible then
additionalWidth += getItemWidth(frame) + menuUIListLayout.Padding.Offset
end
end
if not iconSpot.Visible then
widgetWidth -= (getItemWidth(iconSpot) + menuUIListLayout.Padding.Offset*2 + widgetBorderSize)
end
additionalWidth -= (widgetBorderSize*0.5)
widgetWidth += additionalWidth - (widgetBorderSize*0.75)
end
menuGap.Visible = showMenu and iconSpot.Visible
local desiredWidth = widget:GetAttribute("DesiredWidth")
if desiredWidth and widgetWidth < desiredWidth then
widgetWidth = desiredWidth
end
icon.updateMenu:Fire()
local preWidth = math.max(widgetWidth-additionalWidth, widgetMinimumWidth)
local spotWidth = preWidth-(widgetBorderSize*2)
local menuWidth = menu:GetAttribute("MenuWidth")
local totalMenuWidth = menuWidth and menuWidth + spotWidth + menuUIListLayout.Padding.Offset + 10
if totalMenuWidth then
local maxWidth = menu:GetAttribute("MaxWidth")
if maxWidth then
totalMenuWidth = math.max(maxWidth, widgetMinimumWidth)
end
menu:SetAttribute("MenuCanvasWidth", widgetWidth)
if totalMenuWidth < widgetWidth then
widgetWidth = totalMenuWidth
end
end
local style = Enum.EasingStyle.Quint
local direction = Enum.EasingDirection.Out
local spotWidthMax = math.max(spotWidth, getItemWidth(iconSpot), iconSpot.AbsoluteSize.X)
local widgetWidthMax = math.max(widgetWidth, getItemWidth(widget), widget.AbsoluteSize.X)
local SPEED = 750
local spotTweenInfo = TweenInfo.new(spotWidthMax/SPEED, style, direction)
local widgetTweenInfo = TweenInfo.new(widgetWidthMax/SPEED, style, direction)
TweenService:Create(iconSpot, spotTweenInfo, {
Position = UDim2.new(0, widgetBorderSize, 0.5, 0),
Size = UDim2.new(0, spotWidth, 1, -widgetBorderSize*2),
}):Play()
TweenService:Create(clickRegion, spotTweenInfo, {
Size = UDim2.new(0, spotWidth, 1, 0),
}):Play()
local newWidgetSize = UDim2.fromOffset(widgetWidth, widgetMinimumHeight)
local updateInstantly = widget.Size.Y.Offset ~= widgetMinimumHeight
if updateInstantly then
widget.Size = newWidgetSize
end
widget:SetAttribute("TargetWidth", newWidgetSize.X.Offset)
local movingTween = TweenService:Create(widget, widgetTweenInfo, {
Size = newWidgetSize,
})
movingTween:Play()
resizingCount += 1
for i = 1, widgetTweenInfo.Time * 100 do
task.delay(i/100, function()
Icon.iconChanged:Fire(icon)
end)
end
task.delay(widgetTweenInfo.Time-0.2, function()
resizingCount -= 1
task.defer(function()
if resizingCount == 0 then
icon.resizingComplete:Fire()
end
end)
end)
icon:updateParent()
end)
end
local Utility = require(script.Parent.Parent.Utility)
local handleLabelAndImageChanges = Utility.createStagger(0.01, handleLabelAndImageChangesUnstaggered)
local firstTimeSettingFontFace = true
icon:setBehaviour("IconLabel", "Text", handleLabelAndImageChanges)
icon:setBehaviour("IconLabel", "FontFace", function(value)
local previousFontFace = iconLabel.FontFace
if previousFontFace == value then
return
end
task.spawn(function()
--[[
local fontLink = value.Family
if string.match(fontLink, "rbxassetid://") then
local ContentProvider = game:GetService("ContentProvider")
local assets = {fontLink}
ContentProvider:PreloadAsync(assets)
end--]]
-- Afaik there's no way to determine when a Font Family has
-- loaded (even with ContentProvider), so we just have to try
-- a few times and hope it loads within the refresh period
handleLabelAndImageChanges()
if firstTimeSettingFontFace then
firstTimeSettingFontFace = false
for i = 1, 10 do
task.wait(1)
handleLabelAndImageChanges()
end
end
end)
end)
local function updateBorderSize()
task.defer(function()
local borderOffset = widget:GetAttribute("BorderSize")
local alignment = icon.alignment
local alignmentOffset = (iconSpot.Visible == false and 0) or (alignment == "Right" and -borderOffset) or borderOffset
menu.Position = UDim2.new(0, alignmentOffset, 0, 0)
menuGap.Size = UDim2.fromOffset(borderOffset, 0)
menuUIListLayout.Padding = UDim.new(0, 0)
handleLabelAndImageChanges()
end)
end
icon:setBehaviour("Widget", "BorderSize", updateBorderSize)
icon:setBehaviour("IconSpot", "Visible", updateBorderSize)
icon.startMenuUpdate:Connect(handleLabelAndImageChanges)
icon.updateSize:Connect(handleLabelAndImageChanges)
icon:setBehaviour("ContentsList", "HorizontalAlignment", handleLabelAndImageChanges)
icon:setBehaviour("Widget", "Visible", handleLabelAndImageChanges)
icon:setBehaviour("Widget", "DesiredWidth", handleLabelAndImageChanges)
icon:setBehaviour("Widget", "MinimumWidth", handleLabelAndImageChanges)
icon:setBehaviour("Widget", "MinimumHeight", handleLabelAndImageChanges)
icon:setBehaviour("Indicator", "Visible", handleLabelAndImageChanges)
icon:setBehaviour("IconImageRatio", "AspectRatio", handleLabelAndImageChanges)
icon:setBehaviour("IconImage", "Image", function(value)
local textureId = (tonumber(value) and "http://www.roblox.com/asset/?id="..value) or value or ""
if iconImage.Image ~= textureId then
handleLabelAndImageChanges()
end
return textureId
end)
icon.alignmentChanged:Connect(function(newAlignment)
if newAlignment == "Center" then
newAlignment = "Left"
end
menuUIListLayout.HorizontalAlignment = Enum.HorizontalAlignment[newAlignment]
updateBorderSize()
end)
-- Localization support (refresh icon size whenever player changes language changes in-game)
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local lastLocaleId = localPlayer.LocaleId
icon.janitor:add(localPlayer:GetPropertyChangedSignal("LocaleId"):Connect(function()
task.delay(0.2, function()
local newLocaleId = localPlayer.LocaleId
if newLocaleId ~= lastLocaleId then
lastLocaleId = newLocaleId
icon:refresh()
task.wait(0.5)
icon:refresh()
end
end)
end))
local iconImageScale = Instance.new("NumberValue")
iconImageScale.Name = "IconImageScale"
iconImageScale.Parent = iconImage
iconImageScale:GetPropertyChangedSignal("Value"):Connect(function()
iconImage.Size = UDim2.new(iconImageScale.Value, 0, iconImageScale.Value, 0)
end)
local UIAspectRatioConstraint = Instance.new("UIAspectRatioConstraint")
UIAspectRatioConstraint.Name = "IconImageRatio"
UIAspectRatioConstraint.AspectType = Enum.AspectType.FitWithinMaxSize
UIAspectRatioConstraint.DominantAxis = Enum.DominantAxis.Height
UIAspectRatioConstraint.Parent = iconImage
local iconGradient = Instance.new("UIGradient")
iconGradient.Name = "IconGradient"
iconGradient.Enabled = true
iconGradient.Parent = button
local iconSpotGradient = Instance.new("UIGradient")
iconSpotGradient.Name = "IconSpotGradient"
iconSpotGradient.Enabled = true
iconSpotGradient.Parent = iconSpot
return widget
end