nij55

Module - Draggify

Jun 3rd, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. --[[
  2.     @Author: Spynaz
  3.     @Description: Enables dragging on GuiObjects. Supports both mouse and touch.
  4.    
  5.     For instructions on how to use this module, go to this link:
  6.     https://devforum.roblox.com/t/simple-module-for-creating-draggable-gui-elements/230678
  7. --]]
  8. local UDim2_new = UDim2.new
  9. local UserInputService = game:GetService("UserInputService")
  10. local DraggableObject       = {}
  11. DraggableObject.__index     = DraggableObject
  12. -- Sets up a new draggable object
  13. function DraggableObject.new(Object)
  14.     local self          = {}
  15.     self.Object         = Object
  16.     self.DragStarted    = nil
  17.     self.DragEnded      = nil
  18.     self.Dragged        = nil
  19.     self.Dragging       = false
  20.    
  21.     setmetatable(self, DraggableObject)
  22.    
  23.     return self
  24. end
  25. -- Enables dragging
  26. function DraggableObject:Enable()
  27.     local object            = self.Object
  28.     local dragInput         = nil
  29.     local dragStart         = nil
  30.     local startPos          = nil
  31.     local preparingToDrag   = false
  32.    
  33.     -- Updates the element
  34.     local function update(input)
  35.         local delta         = input.Position - dragStart
  36.         local newPosition   = UDim2_new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
  37.         object.Position     = newPosition
  38.    
  39.         return newPosition
  40.     end
  41.    
  42.     self.InputBegan = object.InputBegan:Connect(function(input)
  43.         if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
  44.             preparingToDrag = true
  45.             --[[if self.DragStarted then
  46.                 self.DragStarted()
  47.             end
  48.            
  49.             dragging        = true
  50.             dragStart       = input.Position
  51.             startPos        = Element.Position
  52.             --]]
  53.            
  54.             local connection
  55.             connection = input.Changed:Connect(function()
  56.                 if input.UserInputState == Enum.UserInputState.End and (self.Dragging or preparingToDrag) then
  57.                     self.Dragging = false
  58.                     connection:Disconnect()
  59.                    
  60.                     if self.DragEnded and not preparingToDrag then
  61.                         self.DragEnded()
  62.                     end
  63.                    
  64.                     preparingToDrag = false
  65.                 end
  66.             end)
  67.         end
  68.     end)
  69.    
  70.     self.InputChanged = object.InputChanged:Connect(function(input)
  71.         if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
  72.             dragInput = input
  73.         end
  74.     end)
  75.    
  76.     self.InputChanged2 = UserInputService.InputChanged:Connect(function(input)
  77.         if object.Parent == nil then
  78.             self:Disable()
  79.             return
  80.         end
  81.        
  82.         if preparingToDrag then
  83.             preparingToDrag = false
  84.            
  85.             if self.DragStarted then
  86.                 self.DragStarted()
  87.             end
  88.            
  89.             self.Dragging   = true
  90.             dragStart       = input.Position
  91.             startPos        = object.Position
  92.         end
  93.        
  94.         if input == dragInput and self.Dragging then
  95.             local newPosition = update(input)
  96.            
  97.             if self.Dragged then
  98.                 self.Dragged(newPosition)
  99.             end
  100.         end
  101.     end)
  102. end
  103. -- Disables dragging
  104. function DraggableObject:Disable()
  105.     self.InputBegan:Disconnect()
  106.     self.InputChanged:Disconnect()
  107.     self.InputChanged2:Disconnect()
  108.    
  109.     if self.Dragging then
  110.         self.Dragging = false
  111.        
  112.         if self.DragEnded then
  113.             self.DragEnded()
  114.         end
  115.     end
  116. end
  117. return DraggableObject
Advertisement
Add Comment
Please, Sign In to add comment