Advertisement
NebulaZorua

drag module

Apr 10th, 2020
1,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. -- Dragger
  2. -- I overcomplicated it but I don't care
  3. --[[ Written by
  4. Nebula the Zoroark#6969 (Discord, UserId 138946947842179072)
  5. Nebula_Zoroark (ROBLOX, UserId 5719877)
  6. ]]
  7.  
  8. --[[
  9. Module:Connect(GuiObject gui[, table<GuiObject> parts])
  10. When you drag on 'gui', it will move either the GUI itself or if parts is supplied then every GUI in that table instead.
  11. Returns DragInstance
  12.  
  13. DragInstance.GuiObject
  14. The GUI to be dragged.
  15.  
  16. DragInstance.Affected
  17. All GUIs that are moved when the GUI is dragged.
  18.  
  19. DragInstance:Disconnect() / DragInstance:disconnect()
  20. Disables dragging for the instance.
  21. Returns nil
  22.  
  23. DragInstance:SetDragTime(number time)
  24. If time is 0, the GUI(s) won't tween to the new position when tweened.
  25. If the time is above 0, the GUI(s) will tween to the new position over the specified time
  26. --]]
  27.  
  28. function Tween(obj,props,time,easing,direction,repeats,backwards)
  29. local info = TweenInfo.new(time or .5, easing or Enum.EasingStyle.Quad, direction or Enum.EasingDirection.Out, repeats or 0, backwards or false)
  30. local tween = game:service'TweenService':Create(obj, info, props)
  31.  
  32. tween:Play()
  33. end
  34.  
  35. local Drag = {Current=nil;Draggers={}}
  36. function Drag:Disconnect()
  37. local g = self;
  38. if(typeof(g)=='table' and g.GuiObject)then g = g.GuiObject end
  39. if(self.Draggers[g])then
  40. if(Drag.Current==g)then
  41. Drag.Current=nil
  42. end
  43. for i = 1,#self.Draggers[g].Connections do self.Draggers[g].Connections[i]:disconnect() end
  44. self.Draggers[g]=nil;
  45. else
  46. warn("Make sure to make "..g.Name.." draggable!")
  47. end
  48. end
  49. Drag.disconnect=Drag.Disconnect
  50.  
  51. function Drag:SetDragTime(t)
  52. local g = self;
  53. local t = tonumber(t)
  54. if(typeof(g)=='table' and g.GuiObject)then g = g.GuiObject end
  55. if(self.Draggers[g])then
  56. self.Draggers[g].DragTime=t
  57. else
  58. warn("Make sure to make "..g.Name.." draggable!")
  59. end
  60. end
  61.  
  62.  
  63. function Drag:Connect(g,parts)
  64. local parts = typeof(parts)=='table' and parts or {g}
  65. local partsToDrag = {}
  66. for i = 1,#parts do
  67. table.insert(partsToDrag,{GUI=parts[i],Position=parts[i].Position})
  68. end
  69. if(g:IsA'GuiObject')then
  70. local con1=g.InputBegan:connect(function(io,gpe)
  71. if(gpe)then return end
  72. local data = self.Draggers[g]
  73. if(data and io.UserInputType==Enum.UserInputType.MouseButton1 and not Drag.Current)then
  74. Drag.Current=g;
  75. data.LastPos=io.Position
  76. end
  77. end)
  78. local con2=g.InputEnded:connect(function(io,gpe)
  79. if(gpe)then return end
  80. local data = self.Draggers[g]
  81. if(data and io.UserInputType==Enum.UserInputType.MouseButton1 and Drag.Current==g)then
  82. Drag.Current=nil;
  83. end
  84. end)
  85. local con3=game:service'UserInputService'.InputChanged:connect(function(io,gpe)
  86. if(gpe)then return end
  87. local data = self.Draggers[g]
  88. if(data and Drag.Current==g and data.LastPos~=nil and io.UserInputType==Enum.UserInputType.MouseMovement)then
  89. local lp=data.LastPos
  90. local delta=lp-io.Position;
  91. for i = 1,#partsToDrag do
  92. local g = partsToDrag[i].GUI
  93. partsToDrag[i].Position = partsToDrag[i].Position+UDim2.new(0,-delta.x,0,-delta.y)
  94. if(data.DragTime>0)then
  95. Tween(g,{Position=partsToDrag[i].Position},data.DragTime,Enum.EasingStyle.Quad,Enum.EasingDirection.Out,0,false)
  96. else
  97. g.Position=partsToDrag[i].Position
  98. end
  99. end
  100. data.LastPos=io.Position;
  101. end
  102. end)
  103. self.Draggers[g]=setmetatable({
  104. DragTime=0;
  105. LastPos=Vector3.new();
  106. Connections={con1,con2,con3};
  107. },{__index=Drag})
  108.  
  109. return setmetatable({
  110. GuiObject=g;
  111. Affected=parts;
  112. },{__index=self.Draggers[g]})
  113. else
  114. error(g.ClassName.." is not a valid GuiObject")
  115. end
  116. end
  117.  
  118. return Drag;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement