-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathHandleDragging.lua
More file actions
188 lines (147 loc) · 5.39 KB
/
Copy pathHandleDragging.lua
File metadata and controls
188 lines (147 loc) · 5.39 KB
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
local Tool = script.Parent.Parent.Parent
-- API
local Core = require(Tool.Core)
local Selection = Core.Selection
local Security = Core.Security
local BoundingBox = require(Tool.Core.BoundingBox)
-- Libraries
local Libraries = Tool:WaitForChild 'Libraries'
local MoveUtil = require(script.Parent:WaitForChild 'Util')
local JointUtils = require(Libraries:WaitForChild("JointUtils"))
-- Create class
local HandleDragging = {}
HandleDragging.__index = HandleDragging
-- Directions of movement for each handle's dragged face
local AxisMultipliers = {
[Enum.NormalId.Top] = Vector3.new(0, 1, 0);
[Enum.NormalId.Bottom] = Vector3.new(0, -1, 0);
[Enum.NormalId.Front] = Vector3.new(0, 0, -1);
[Enum.NormalId.Back] = Vector3.new(0, 0, 1);
[Enum.NormalId.Left] = Vector3.new(-1, 0, 0);
[Enum.NormalId.Right] = Vector3.new(1, 0, 0);
}
function HandleDragging.new(Tool)
local self = {
Tool = Tool;
-- Handle state
IsHandleDragging = false;
Handles = nil;
-- Selection state
InitialExtentsSize = nil;
InitialExtentsCFrame = nil;
InitialState = nil;
InitialFocusCFrame = nil;
}
return setmetatable(self, HandleDragging)
end
function HandleDragging:AttachHandles(Part, Autofocus)
-- Creates and attaches handles to `Part`, and optionally automatically attaches to the focused part
-- Enable autofocus if requested and not already on
if Autofocus and not self.Tool.Maid.AutofocusHandle then
self.Tool.Maid.AutofocusHandle = Selection.FocusChanged:Connect(function ()
self:AttachHandles(Selection.Focus, true)
end)
-- Disable autofocus if not requested and on
elseif not Autofocus and self.Tool.Maid.AutofocusHandle then
self.Tool.Maid.AutofocusHandle = nil
end
-- Just attach and show the handles if they already exist
if self.Handles then
self.Handles:BlacklistObstacle(BoundingBox.GetBoundingBox())
self.Handles:SetAdornee(Part)
return
end
local AreaPermissions
local function OnHandleDragStart()
-- Prepare for moving parts when the handle is clicked
-- Prevent selection
Core.Targeting.CancelSelecting()
-- Indicate dragging via handles
self.IsHandleDragging = true
-- Freeze bounding box extents while dragging
if BoundingBox.GetBoundingBox() then
local InitialExtentsSize, InitialExtentsCFrame =
BoundingBox.CalculateExtents(Selection.Parts, BoundingBox.StaticExtents)
self.InitialExtentsSize = InitialExtentsSize
self.InitialExtentsCFrame = InitialExtentsCFrame
BoundingBox.PauseMonitoring()
end
-- Stop parts from moving, and capture the initial state of the parts
local InitialPartStates, InitialModelStates, InitialFocusCFrame = self.Tool:PrepareSelectionForDragging()
self.InitialPartStates = InitialPartStates
self.InitialModelStates = InitialModelStates
self.InitialFocusCFrame = InitialFocusCFrame
-- Track the change
self.Tool:TrackChange()
-- Cache area permissions information
if Core.Mode == 'Tool' then
AreaPermissions = Security.GetPermissions(Security.GetSelectionAreas(Selection.Parts), Core.Player)
end
end
local function OnHandleDrag(Face, Distance)
-- Update parts when the handles are moved
-- Only drag if handle is enabled
if not self.IsHandleDragging then
return
end
-- Calculate the increment-aligned drag distance
Distance = MoveUtil.GetIncrementMultiple(Distance, self.Tool.Increment)
-- Move the parts along the selected axes by the calculated distance
self.Tool:MovePartsAlongAxesByFace(Face, Distance, self.InitialPartStates, self.InitialModelStates, self.InitialFocusCFrame)
-- Make sure we're not entering any unauthorized private areas
if Core.Mode == 'Tool' and Security.ArePartsViolatingAreas(Selection.Parts, Core.Player, false, AreaPermissions) then
local Part, InitialPartState = next(self.InitialPartStates)
Part.CFrame = InitialPartState.CFrame
MoveUtil.TranslatePartsRelativeToPart(Part, self.InitialPartStates, self.InitialModelStates)
Distance = 0
end
-- Signal out change in dragged distance
self.Tool.DragChanged:Fire(Distance)
-- Update bounding box if enabled in global axes movements
if self.Tool.Axes == 'Global' and BoundingBox.GetBoundingBox() then
BoundingBox.GetBoundingBox().CFrame = self.InitialExtentsCFrame + (AxisMultipliers[Face] * Distance)
end
end
local function OnHandleDragEnd()
if not self.IsHandleDragging then
return
end
-- Disable dragging
self.IsHandleDragging = false
-- Make joints, restore original anchor and collision states
for Part, State in pairs(self.InitialPartStates) do
JointUtils.RestoreJoints(State.Joints)
Part:MakeJoints()
Part.CanCollide = State.CanCollide
Part.Anchored = State.Anchored
end
-- Register change
self.Tool:RegisterChange()
-- Resume bounding box updates
BoundingBox.RecalculateStaticExtents()
BoundingBox.ResumeMonitoring()
end
-- Create the handles
local Handles = require(Libraries:WaitForChild 'Handles')
self.Handles = Handles.new({
Color = self.Tool.Color.Color,
Parent = Core.UIContainer,
Adornee = Part,
ObstacleBlacklist = { BoundingBox.GetBoundingBox() },
OnDragStart = OnHandleDragStart,
OnDrag = OnHandleDrag,
OnDragEnd = OnHandleDragEnd
})
end
function HandleDragging:HideHandles()
-- Hides the resizing handles
-- Make sure handles exist and are visible
if not self.Handles then
return
end
-- Hide the handles
self.Handles = self.Handles:Destroy()
-- Disable handle autofocus
self.Tool.Maid.AutofocusHandle = nil
end
return HandleDragging