-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSnackbar.lua
266 lines (216 loc) · 8.14 KB
/
Snackbar.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
-- Snackbar PseudoInstance
-- @documentation https://rostrap.github.io/Libraries/RoStrapUI/Snackbar/
-- @author Validark
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local TextService = game:GetService("TextService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Resources = require(ReplicatedStorage:WaitForChild("Resources"))
local Color = Resources:LoadLibrary("Color")
local Debug = Resources:LoadLibrary("Debug")
local Tween = Resources:LoadLibrary("Tween")
local Typer = Resources:LoadLibrary("Typer")
local Shadow = Resources:LoadLibrary("Shadow")
local RippleButton = Resources:LoadLibrary("RippleButton")
local Enumeration = Resources:LoadLibrary("Enumeration")
local PseudoInstance = Resources:LoadLibrary("PseudoInstance")
local RoStrapPriorityUI = Resources:LoadLibrary("RoStrapPriorityUI")
local ReplicatedPseudoInstance = Resources:LoadLibrary("ReplicatedPseudoInstance")
local TEXT_SIZE = 20
local FONT = Enum.Font.SourceSans.Value
local BUTTON_FONT = Enum.Font.SourceSansSemibold.Value
local BUTTON_SIZE = 18
local CORNER_OFFSET = 8
local HEIGHT = 48
local SMALLEST_WIDTH = 294
local TweenCompleted = Enum.TweenStatus.Completed
local Deceleration = Enumeration.EasingFunction.Deceleration.Value
local Acceleration = Enumeration.EasingFunction.Acceleration.Value
Enumeration.SnackbarPosition = { "Left", "Right", "Center" }
local StatePosition = {
[Enumeration.SnackbarPosition.Left.Value] = {
AnchorPoint = Vector2.new(0, 0);
ExitPosition = UDim2.new(0, 7, 1, 0);
EnterPosition = UDim2.new(0, 7, 1, -HEIGHT - CORNER_OFFSET);
};
[Enumeration.SnackbarPosition.Right.Value] = {
AnchorPoint = Vector2.new(1, 0);
ExitPosition = UDim2.new(1, -7, 1, 0);
EnterPosition = UDim2.new(1, -7, 1, -HEIGHT - CORNER_OFFSET);
};
[Enumeration.SnackbarPosition.Center.Value] = {
AnchorPoint = Vector2.new(0.5, 0);
ExitPosition = UDim2.new(0.5, 0, 1, 0);
EnterPosition = UDim2.new(0.5, 0, 1, -HEIGHT - CORNER_OFFSET);
};
}
local SnackbarImage = Instance.new("ImageLabel")
SnackbarImage.BackgroundTransparency = 1
SnackbarImage.Name = "Snackbar"
SnackbarImage.ZIndex = 2
SnackbarImage.Image = "rbxassetid://1934624205"
SnackbarImage.ImageColor3 = Color3.fromRGB(50, 50, 50)
SnackbarImage.ScaleType = Enum.ScaleType.Slice.Value
SnackbarImage.SliceCenter = Rect.new(4, 4, 252, 252)
SnackbarImage.ZIndex = 3
local SnackbarText = Instance.new("TextLabel")
SnackbarText.AnchorPoint = Vector2.new(0, 0.5)
SnackbarText.BackgroundTransparency = 1
SnackbarText.Name = "SnackbarText"
SnackbarText.Position = UDim2.new(0, 16, 0.5, 0)
SnackbarText.Size = UDim2.new(1, 0, 1, -12)
SnackbarText.ZIndex = 3
SnackbarText.Font = FONT
SnackbarText.TextColor3 = Color3.fromRGB(255, 255, 255)
SnackbarText.TextSize = TEXT_SIZE
SnackbarText.TextXAlignment = Enum.TextXAlignment.Left.Value
SnackbarText.Parent = SnackbarImage
local Shadow = PseudoInstance.new("Shadow")
Shadow.Elevation = 6
Shadow.Parent = SnackbarImage
local LocalPlayer, PlayerGui do
if RunService:IsClient() then
repeat LocalPlayer = Players.LocalPlayer until LocalPlayer or not wait()
repeat PlayerGui = LocalPlayer:FindFirstChildOfClass("PlayerGui") until PlayerGui or not wait()
end
end
local function OnActionPressed(self)
if not self.Dismissed then
self:Dismiss()
self.OnAction:Fire(LocalPlayer)
end
end
local LARGE_FRAME_SIZE = Vector2.new(32767, 32767)
local Storage = {}
local function IsInputting(CurrentlyInputting)
for _, Bool in next, CurrentlyInputting do
if Bool == true then
return true
end
end
return false
end
return PseudoInstance:Register("Snackbar", {
Storage = Storage;
WrappedProperties = {
Object = { "Active", "LayoutOrder", "NextSelectionDown", "NextSelectionLeft", "NextSelectionRight", "NextSelectionUp" },
};
Methods = {
Enter = function(self)
self.Dismissed = false
local SnackbarFrame = self.Object
SnackbarFrame.Parent = self.SCREEN
SnackbarFrame.Position = self.ExitPosition
if Storage.OpenSnackbar then
Storage.OpenSnackbar:Dismiss()
end
Storage.OpenSnackbar = self
local CurrentlyInputting = {}
SnackbarFrame.InputBegan:Connect(function(InputObject)
CurrentlyInputting[InputObject.UserInputType.Value] = true
end)
SnackbarFrame.InputEnded:Connect(function(InputObject)
CurrentlyInputting[InputObject.UserInputType.Value] = false
end)
Tween(SnackbarFrame, "Position", self.EnterPosition, Deceleration, self.ENTER_TIME, false, function(Completed)
if Completed == TweenCompleted and wait(self.DisplayTime) then
while IsInputting(CurrentlyInputting) do
repeat wait() until not IsInputting(CurrentlyInputting)
wait(self.DisplayTime)
end
self:Dismiss()
end
end)
end;
Dismiss = function(self)
if not self.Dismissed then
self.Dismissed = true
local SnackbarFrame = self.Object
SnackbarFrame.ZIndex = SnackbarFrame.ZIndex - 1
Tween(SnackbarFrame, "Position", self.ExitPosition, Acceleration, self.ENTER_TIME, true, function(Completed)
if Completed == TweenCompleted then
SnackbarFrame.Parent = nil
if Storage.OpenSnackbar == self then
Storage.OpenSnackbar = nil
end
end
end)
end
end;
};
Events = {
"OnAction";
};
Internals = {
"SnackbarText", "SnackbarAction", "RegisteredRippleInputs", "EnterPosition", "ExitPosition";
SHOULD_BLUR = false;
ActionButtonWidth = 0;
TextWidth = 0;
ENTER_TIME = 0.275;
AdjustSnackbarSize = function(self)
local Width = self.ActionButtonWidth + self.TextWidth + 16*3
self.Object.Size = UDim2.new(0, Width > SMALLEST_WIDTH and Width or SMALLEST_WIDTH, 0, HEIGHT)
end;
};
Properties = {
SnackbarPosition = Typer.AssignSignature(2, Typer.EnumerationOfTypeSnackbarPosition, function(self, Position)
local State = StatePosition[Position.Value]
self.Object.AnchorPoint = State.AnchorPoint
self.ExitPosition = State.ExitPosition
self.EnterPosition = State.EnterPosition
self:rawset("SnackbarPosition", Position)
end);
ActionText = Typer.AssignSignature(2, Typer.String, function(self, ActionText)
if ActionText == "" then
self.SnackbarAction.Parent = nil
self.ActionButtonWidth = 0
else
self.SnackbarAction.Text = ActionText
local Width = TextService:GetTextSize(ActionText, BUTTON_SIZE, BUTTON_FONT, LARGE_FRAME_SIZE).X + 16
self.SnackbarAction.Size = UDim2.new(0, Width, 1, -12)
self.ActionButtonWidth = Width
self.SnackbarAction.Parent = self.Object
self:rawset("ActionText", ActionText)
end
self:AdjustSnackbarSize()
end);
ActionColor3 = Typer.AssignSignature(2, Typer.Color3, function(self, Color)
if self.SnackbarAction.Parent ~= nil then
self.SnackbarAction.PrimaryColor3 = Color
self:rawset("ActionColor3", Color)
end
end);
Text = Typer.AssignSignature(2, Typer.String, function(self, Text)
-- Assign Text to SnackbarText.Text
-- Update Size according to TextBounds, which shouldn't be a property of Snackbar
self.TextWidth = TextService:GetTextSize(Text, TEXT_SIZE, FONT, LARGE_FRAME_SIZE).X
self.SnackbarText.Text = Text
self:AdjustSnackbarSize()
self:rawset("Text", Text)
end);
DisplayTime = Typer.AssignSignature(2, Typer.Number, function(self, DisplayTime)
self:rawset("DisplayTime", DisplayTime)
end)
},
Init = function(self, ...)
self:rawset("Object", SnackbarImage:Clone())
self.SnackbarText = self.Object.SnackbarText
self:rawset("DisplayTime", 5)
local SnackbarAction = PseudoInstance.new("RippleButton")
SnackbarAction.AnchorPoint = Vector2.new(1, 0.5)
SnackbarAction.Name = "SnackbarAction"
SnackbarAction.Position = UDim2.new(1, -8, 0.5, 0)
SnackbarAction.ZIndex = 4
SnackbarAction.Font = BUTTON_FONT
SnackbarAction.PrimaryColor3 = Color.Purple[300]
SnackbarAction.TextSize = BUTTON_SIZE
SnackbarAction.Style = Enumeration.ButtonStyle.Flat.Value
self.Janitor:Add(SnackbarAction.OnPressed:Connect(OnActionPressed, self), "Disconnect")
self.SnackbarAction = SnackbarAction
self.SnackbarPosition = Enumeration.SnackbarPosition.Center
self.Janitor:Add(self.Object, "Destroy")
self.Janitor:Add(self.SnackbarText, "Destroy")
self.Janitor:Add(SnackbarAction, "Destroy")
self:superinit(...)
end;
}, RoStrapPriorityUI)