I made a UI module for my game that creates this effect:
Here is the code for the module that I made:
local CollectionService = game:GetService("CollectionService")
local SlideUIEffect = {}
SlideUIEffect.__index = SlideUIEffect
function SlideUIEffect:AddSlide(GuiObject, Trigger, Parent)
if not Parent then
Parent = self.rootGuiObject
end
self.GuiObjectsInfo[GuiObject] = {open = false, Parent = Parent}
local callBack = function(state)
local info = self.GuiObjectsInfo[GuiObject]
if state == Enum.TweenStatus.Canceled and info.open == true then
info.open = false
self.GuiObjectsInfo[GuiObject] = info
self.OpenGuiObject = nil
GuiObject:TweenPosition(
Parent.Position,
Enum.EasingDirection.In,
Enum.EasingStyle.Sine,
1,
true
)
end
end
local connection = Trigger:Connect(function()
local info = self.GuiObjectsInfo[GuiObject]
local tweenSettings = self.TweenSettings
if info.open == false then
info.open = true
self.GuiObjectsInfo[GuiObject] = info
if self.OpenGuiObject then
self:SetOpen(self.OpenGuiObject, false)
end
self.OpenGuiObject = GuiObject
GuiObject:TweenPosition(
Parent.Position + UDim2.new(Parent.Size.X.Scale, Parent.Size.X.Offset + self.Offset, 0, 0),
tweenSettings.EasingDirection,
tweenSettings.EasingStyle,
tweenSettings.Time,
true,
callBack
)
elseif info.open == true then
info.open = false
self.GuiObjectsInfo[GuiObject] = info
self.OpenGuiObject = nil
GuiObject:TweenPosition(
Parent.Position,
tweenSettings.EasingDirection,
tweenSettings.EasingStyle,
tweenSettings.Time,
true
)
end
end)
table.insert(self.TriggerConnections, connection)
end
function SlideUIEffect:SetOpen(GuiObject, open)
local info = self.GuiObjectsInfo[GuiObject]
local tweenSettings = self.TweenSettings
if open and info.open == false then
info.open = true
self.GuiObjectsInfo[GuiObject] = info
GuiObject:TweenPosition(
info.Parent.Position + UDim2.new(info.Parent.Size.X.Scale, info.Parent.Size.X.Offset + self.Offset, 0, 0),
tweenSettings.EasingDirection,
tweenSettings.EasingStyle,
tweenSettings.Time,
true
)
elseif not open and info.open == true then
info.open = false
self.GuiObjectsInfo[GuiObject] = info
GuiObject:TweenPosition(
info.Parent.Position,
tweenSettings.EasingDirection,
tweenSettings.EasingStyle,
tweenSettings.Time,
true
)
end
end
function SlideUIEffect:Destroy()
for _, connection in ipairs(self.TriggerConnections) do
connection:Disconnect()
end
for GuiObject, isOpen in pairs(self.GuiObjectsInfo) do
GuiObject.Position = self.rootGuiObject.Position
end
self.TriggerConnections = nil
end
return {
new = function(rootGuiObject, tweenSettings, offset)
local self = setmetatable({}, SlideUIEffect)
self.rootGuiObject = rootGuiObject
self.TriggerConnections = {}
self.GuiObjectsInfo = {}
local easingDirection = tweenSettings.EasingDirection or Enum.EasingDirection.Out
local easingStyle = tweenSettings.EasingStyle or Enum.EasingStyle.Sine
local Time = tweenSettings.Time or 1
self.TweenSettings = {
EasingDirection = easingDirection,
EasingStyle = easingStyle,
Time = Time
}
self.Offset = offset
self.OpenGuiObject = nil
return self
end,
}
I would love some feedback on this module. Is there anything I can improve? Is the way I made the Destroy() function a good way of doing things? Feedback would be greatly appreciated.