Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Dragger
- -- I overcomplicated it but I don't care
- --[[ Written by
- Nebula the Zoroark#6969 (Discord, UserId 138946947842179072)
- Nebula_Zoroark (ROBLOX, UserId 5719877)
- ]]
- --[[
- Module:Connect(GuiObject gui[, table<GuiObject> parts])
- When you drag on 'gui', it will move either the GUI itself or if parts is supplied then every GUI in that table instead.
- Returns DragInstance
- DragInstance.GuiObject
- The GUI to be dragged.
- DragInstance.Affected
- All GUIs that are moved when the GUI is dragged.
- DragInstance:Disconnect() / DragInstance:disconnect()
- Disables dragging for the instance.
- Returns nil
- DragInstance:SetDragTime(number time)
- If time is 0, the GUI(s) won't tween to the new position when tweened.
- If the time is above 0, the GUI(s) will tween to the new position over the specified time
- --]]
- function Tween(obj,props,time,easing,direction,repeats,backwards)
- local info = TweenInfo.new(time or .5, easing or Enum.EasingStyle.Quad, direction or Enum.EasingDirection.Out, repeats or 0, backwards or false)
- local tween = game:service'TweenService':Create(obj, info, props)
- tween:Play()
- end
- local Drag = {Current=nil;Draggers={}}
- function Drag:Disconnect()
- local g = self;
- if(typeof(g)=='table' and g.GuiObject)then g = g.GuiObject end
- if(self.Draggers[g])then
- if(Drag.Current==g)then
- Drag.Current=nil
- end
- for i = 1,#self.Draggers[g].Connections do self.Draggers[g].Connections[i]:disconnect() end
- self.Draggers[g]=nil;
- else
- warn("Make sure to make "..g.Name.." draggable!")
- end
- end
- Drag.disconnect=Drag.Disconnect
- function Drag:SetDragTime(t)
- local g = self;
- local t = tonumber(t)
- if(typeof(g)=='table' and g.GuiObject)then g = g.GuiObject end
- if(self.Draggers[g])then
- self.Draggers[g].DragTime=t
- else
- warn("Make sure to make "..g.Name.." draggable!")
- end
- end
- function Drag:Connect(g,parts)
- local parts = typeof(parts)=='table' and parts or {g}
- local partsToDrag = {}
- for i = 1,#parts do
- table.insert(partsToDrag,{GUI=parts[i],Position=parts[i].Position})
- end
- if(g:IsA'GuiObject')then
- local con1=g.InputBegan:connect(function(io,gpe)
- if(gpe)then return end
- local data = self.Draggers[g]
- if(data and io.UserInputType==Enum.UserInputType.MouseButton1 and not Drag.Current)then
- Drag.Current=g;
- data.LastPos=io.Position
- end
- end)
- local con2=g.InputEnded:connect(function(io,gpe)
- if(gpe)then return end
- local data = self.Draggers[g]
- if(data and io.UserInputType==Enum.UserInputType.MouseButton1 and Drag.Current==g)then
- Drag.Current=nil;
- end
- end)
- local con3=game:service'UserInputService'.InputChanged:connect(function(io,gpe)
- if(gpe)then return end
- local data = self.Draggers[g]
- if(data and Drag.Current==g and data.LastPos~=nil and io.UserInputType==Enum.UserInputType.MouseMovement)then
- local lp=data.LastPos
- local delta=lp-io.Position;
- for i = 1,#partsToDrag do
- local g = partsToDrag[i].GUI
- partsToDrag[i].Position = partsToDrag[i].Position+UDim2.new(0,-delta.x,0,-delta.y)
- if(data.DragTime>0)then
- Tween(g,{Position=partsToDrag[i].Position},data.DragTime,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false)
- else
- g.Position=partsToDrag[i].Position
- end
- end
- data.LastPos=io.Position;
- end
- end)
- self.Draggers[g]=setmetatable({
- DragTime=0;
- LastPos=Vector3.new();
- Connections={con1,con2,con3};
- },{__index=Drag})
- return setmetatable({
- GuiObject=g;
- Affected=parts;
- },{__index=self.Draggers[g]})
- else
- error(g.ClassName.." is not a valid GuiObject")
- end
- end
- return Drag;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement