Skip to content

Commit

Permalink
Catch handle releases at all times in Move, Resize, & Rotate tools
Browse files Browse the repository at this point in the history
This change moves logic for finalizing mouse-involving operations
from outside of temporary connections, in order to prevent issues
where parts are left in a mid-operation state because the tool was
switched or disabled too early.

Signed-off-by: Robert Chiquini <[email protected]>
  • Loading branch information
GigsD4X committed Apr 11, 2017
1 parent 3744c68 commit 451570e
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 127 deletions.
93 changes: 46 additions & 47 deletions tools/Move.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ function ClearConnections()

end;

function ClearConnection(ConnectionKey)
-- Clears the given specific connection

local Connection = Connections[ConnectionKey];

-- Disconnect the connection if it exists
if Connections[ConnectionKey] then
Connection:disconnect();
Connections[ConnectionKey] = nil;
end;

end;

function ShowUI()
-- Creates and reveals the UI

Expand Down Expand Up @@ -253,8 +266,7 @@ function AttachHandles(Part, Autofocus)

-- Disable autofocus if not requested and on
elseif not Autofocus and Connections.AutofocusHandle then
Connections.AutofocusHandle:disconnect();
Connections.AutofocusHandle = nil;
ClearConnection 'AutofocusHandle';
end;

-- Just attach and show the handles if they already exist
Expand All @@ -277,7 +289,6 @@ function AttachHandles(Part, Autofocus)
-- Prepare for moving parts when the handle is clicked
------------------------------------------------------

local InitialState = {};
local AreaPermissions;

Handles.MouseButton1Down:connect(function ()
Expand All @@ -299,36 +310,6 @@ function AttachHandles(Part, Autofocus)
AreaPermissions = Security.GetPermissions(Security.GetSelectionAreas(Selection.Items), Core.Player);
end;

------------------------------------------------------
-- Finalize changes to parts when the handle is let go
------------------------------------------------------

Connections.HandleRelease = UserInputService.InputEnded:connect(function (InputInfo, GameProcessedEvent)

-- Make sure this was button 1 being released, and dragging is ongoing
if not HandleDragging or (InputInfo.UserInputType ~= Enum.UserInputType.MouseButton1) then
return;
end;

-- Disable dragging
HandleDragging = false;

-- Clear this connection to prevent it from firing again
Connections.HandleRelease:disconnect();
Connections.HandleRelease = nil;

-- Make joints, restore original anchor and collision states
for _, Part in pairs(Selection.Items) do
Part:MakeJoints();
Part.CanCollide = InitialState[Part].CanCollide;
Part.Anchored = InitialState[Part].Anchored;
end;

-- Register the change
RegisterChange();

end);

end);

------------------------------------------
Expand Down Expand Up @@ -363,6 +344,32 @@ function AttachHandles(Part, Autofocus)

end;

-- Finalize changes to parts when the handle is let go
Support.AddUserInputListener('Ended', 'MouseButton1', true, function (Input)

-- Ensure handle dragging is ongoing
if not HandleDragging then
return;
end;

-- Disable dragging
HandleDragging = false;

-- Clear this connection to prevent it from firing again
ClearConnection 'HandleRelease';

-- Make joints, restore original anchor and collision states
for _, Part in pairs(Selection.Items) do
Part:MakeJoints();
Part.CanCollide = InitialState[Part].CanCollide;
Part.Anchored = InitialState[Part].Anchored;
end;

-- Register the change
RegisterChange();

end);

function HideHandles()
-- Hides the resizing handles

Expand All @@ -375,11 +382,8 @@ function HideHandles()
Handles.Visible = false;
Handles.Parent = nil;

-- Disable handle autofocus if enabled
if Connections.AutofocusHandle then
Connections.AutofocusHandle:disconnect();
Connections.AutofocusHandle = nil;
end;
-- Disable handle autofocus
ClearConnection 'AutofocusHandle';

end;

Expand Down Expand Up @@ -741,7 +745,7 @@ function EnableDragging()
SetUpDragging(DragStartTarget, SnapTracking.Enabled and SnappedPoint or nil);

-- Disable watching for potential dragging
Connections.WatchForDrag:disconnect();
ClearConnection 'WatchForDrag';

end;

Expand All @@ -757,10 +761,7 @@ function EnableDragging()
DragStartTarget = nil;

-- Disconnect dragging-start listeners
if Connections.WatchForDrag then
Connections.WatchForDrag:disconnect();
Connections.WatchForDrag = nil;
end;
ClearConnection 'WatchForDrag';

end);

Expand Down Expand Up @@ -1064,13 +1065,11 @@ function FinishDragging()
Dragging = false;

-- Stop the dragging action
Connections.Drag:disconnect()
Connections.Drag = nil;
ClearConnection 'Drag';

-- Stop, clean up snapping point tracking
SnapTracking.StopTracking();
Connections.DragSnapping:disconnect();
Connections.DragSnapping = nil;
ClearConnection 'DragSnapping';

-- Restore the original state of each part
for _, Part in pairs(Selection.Items) do
Expand Down
97 changes: 50 additions & 47 deletions tools/Resize.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function ResizeTool.Unequip()
HideHandles();
ClearConnections();
SnapTracking.StopTracking();
FinishSnapping();

end;

Expand Down Expand Up @@ -264,7 +265,6 @@ function ShowHandles()
-- Prepare for resizing parts when the handle is clicked
--------------------------------------------------------

local InitialState = {};
local AreaPermissions;

Handles.MouseButton1Down:connect(function ()
Expand All @@ -286,38 +286,6 @@ function ShowHandles()
AreaPermissions = Security.GetPermissions(Security.GetSelectionAreas(Selection.Items), Core.Player);
end;

------------------------------------------------------
-- Finalize changes to parts when the handle is let go
------------------------------------------------------

Connections.HandleRelease = UserInputService.InputEnded:connect(function (InputInfo, GameProcessedEvent)

-- Make sure this was button 1 being released, and handle resizing is ongoing
if not HandleResizing or (InputInfo.UserInputType ~= Enum.UserInputType.MouseButton1) then
return;
end;

-- Disable resizing
HandleResizing = false;

-- Prevent selection
Core.Targeting.CancelSelecting();

-- Clear this connection to prevent it from firing again
ClearConnection 'HandleRelease';

-- Make joints, restore original anchor and collision states
for _, Part in pairs(Selection.Items) do
Part:MakeJoints();
Part.CanCollide = InitialState[Part].CanCollide;
Part.Anchored = InitialState[Part].Anchored;
end;

-- Register the change
RegisterChange();

end);

end);

------------------------------------------
Expand Down Expand Up @@ -359,6 +327,36 @@ function ShowHandles()

end;


-- Finalize changes to parts when the handle is let go
Support.AddUserInputListener('Ended', 'MouseButton1', true, function (Input)

-- Ensure handle resizing is ongoing
if not HandleResizing then
return;
end;

-- Disable resizing
HandleResizing = false;

-- Prevent selection
Core.Targeting.CancelSelecting();

-- Clear this connection to prevent it from firing again
ClearConnection 'HandleRelease';

-- Make joints, restore original anchor and collision states
for _, Part in pairs(Selection.Items) do
Part:MakeJoints();
Part.CanCollide = InitialState[Part].CanCollide;
Part.Anchored = InitialState[Part].Anchored;
end;

-- Register the change
RegisterChange();

end);

function HideHandles()
-- Hides the resizing handles

Expand Down Expand Up @@ -938,32 +936,37 @@ function StartSnapping()

end);

-- Listen for the end of the snapping
Connections.SnapDragEnd = Support.AddUserInputListener('Ended', 'MouseButton1', true, function (Input)
end;

-- If destination stage was reached, restore the selection's original state
if SnappingStage == 'Destination' then
for Part, PartState in pairs(SnappingStartSelectionState) do
Part:MakeJoints();
Part.CanCollide = PartState.CanCollide;
Part.Anchored = PartState.Anchored;
end;
end;
-- Stop snapping whenever mouse is released
Support.AddUserInputListener('Ended', 'MouseButton1', true, function (Input)

-- Finish snapping
FinishSnapping();
-- Ensure snapping is ongoing
if not SnappingStage then
return;
end;

end);
-- Finish snapping
FinishSnapping();

end);

end;

function FinishSnapping()
-- Cleans up and finalizes the snapping operation

-- Ensure snapping is ongoing
if not SnappingStage then
return;
end;

-- Restore the selection's original state
for Part, PartState in pairs(SnappingStartSelectionState) do
Part:MakeJoints();
Part.CanCollide = PartState.CanCollide;
Part.Anchored = PartState.Anchored;
end;

-- Disable any snapping stage
SnappingStage = nil;

Expand Down
62 changes: 29 additions & 33 deletions tools/Rotate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ function AttachHandles(Part, Autofocus)
-- Prepare for rotating parts when the handle is clicked
--------------------------------------------------------

local InitialState = {};
local AreaPermissions;

Handles.MouseButton1Down:connect(function ()
Expand Down Expand Up @@ -305,38 +304,6 @@ function AttachHandles(Part, Autofocus)
PivotPoint = InitialState[Selection.Focus].CFrame;
end;

------------------------------------------------------
-- Finalize changes to parts when the handle is let go
------------------------------------------------------

Connections.HandleRelease = UserInputService.InputEnded:connect(function (InputInfo, GameProcessedEvent)

-- Make sure this was button 1 being released, and rotating is ongoing
if not HandleRotating or (InputInfo.UserInputType ~= Enum.UserInputType.MouseButton1) then
return;
end;

-- Prevent selection
Core.Targeting.CancelSelecting();

-- Disable rotating
HandleRotating = false;

-- Clear this connection to prevent it from firing again
ClearConnection 'HandleRelease';

-- Make joints, restore original anchor and collision states
for _, Part in pairs(Selection.Items) do
Part:MakeJoints();
Part.CanCollide = InitialState[Part].CanCollide;
Part.Anchored = InitialState[Part].Anchored;
end;

-- Register the change
RegisterChange();

end);

end);

------------------------------------------
Expand Down Expand Up @@ -375,6 +342,35 @@ function AttachHandles(Part, Autofocus)

end;

-- Finalize changes to parts when the handle is let go
Support.AddUserInputListener('Ended', 'MouseButton1', true, function (Input)

-- Make sure rotating is ongoing
if not HandleRotating then
return;
end;

-- Prevent selection
Core.Targeting.CancelSelecting();

-- Disable rotating
HandleRotating = false;

-- Clear this connection to prevent it from firing again
ClearConnection 'HandleRelease';

-- Make joints, restore original anchor and collision states
for _, Part in pairs(Selection.Items) do
Part:MakeJoints();
Part.CanCollide = InitialState[Part].CanCollide;
Part.Anchored = InitialState[Part].Anchored;
end;

-- Register the change
RegisterChange();

end);

function HideHandles()
-- Hides the resizing handles

Expand Down

0 comments on commit 451570e

Please sign in to comment.