local a={}function ResizeTool.Equip()ShowUI()ShowHandles()BindShortcutKeys()end function ResizeTool.Unequip()HideUI()HideHandles()ClearConnections()SnapTracking.StopTracking()FinishSnapping()end function ClearConnections()for f,B in pairs(a)do B:disconnect()a[f]=nil end end function ClearConnection(f)local B=a[f]if B then B:disconnect()a[f]=nil end end function ShowUI()if ResizeTool.UI then ResizeTool.UI.Visible=true UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)return end ResizeTool.UI=Core.Tool.Interfaces.BTResizeToolGUI:Clone()ResizeTool.UI.Parent=Core.UI ResizeTool.UI.Visible=true local a=ResizeTool.UI.DirectionsOption a.Normal.Button.MouseButton1Down:connect(function()SetDirections("\078\111\114\109\097\108")end)a.Both.Button.MouseButton1Down:connect(function()SetDirections("\066\111\116\104")end)local f=ResizeTool.UI.IncrementOption.Increment.TextBox f.FocusLost:connect(function(a)ResizeTool.Increment=tonumber(f.Text)or ResizeTool.Increment f.Text=Support.Round(ResizeTool.Increment,4)end)local B=ResizeTool.UI.Info.SizeInfo.X.TextBox local l=ResizeTool.UI.Info.SizeInfo.Y.TextBox local e=ResizeTool.UI.Info.SizeInfo.Z.TextBox B.FocusLost:connect(function(a)local f=tonumber(B.Text)if f then SetAxisSize("\088",f)end end)l.FocusLost:connect(function(a)local f=tonumber(l.Text)if f then SetAxisSize("\089",f)end end)e.FocusLost:connect(function(a)local f=tonumber(e.Text)if f then SetAxisSize("\090",f)end end)UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)end function HideUI()if not ResizeTool.UI then return end ResizeTool.UI.Visible=false UIUpdater:Stop()end function UpdateUI()if not ResizeTool.UI then return end if#Selection.Items==0 then ResizeTool.UI.Info.Visible=false ResizeTool.UI.Size=UDim2.new(0,245,0,90)return else ResizeTool.UI.Info.Visible=true ResizeTool.UI.Size=UDim2.new(0,245,0,150)end local a,f,B={},{},{}for l,e in pairs(Selection.Items)do table.insert(a,Support.Round(e.Size.X,3))table.insert(f,Support.Round(e.Size.Y,3))table.insert(B,Support.Round(e.Size.Z,3))end local l=Support.IdentifyCommonItem(a)local e=Support.IdentifyCommonItem(f)local Q=Support.IdentifyCommonItem(B)local o=ResizeTool.UI.Info.SizeInfo.X.TextBox local d=ResizeTool.UI.Info.SizeInfo.Y.TextBox local Z=ResizeTool.UI.Info.SizeInfo.Z.TextBox if not o:IsFocused()then o.Text=l or"\042"end if not d:IsFocused()then d.Text=e or"\042"end if not Z:IsFocused()then Z.Text=Q or"\042"end end function SetDirections(a)ResizeTool.Directions=a if ResizeTool.UI then Core.ToggleSwitch(a,ResizeTool.UI.DirectionsOption)end end

local Connections = {};

function ResizeTool.Equip()
	-- Enables the tool's equipped functionality

	-- Start up our interface
	ShowUI();
	ShowHandles();
	BindShortcutKeys();

end;

function ResizeTool.Unequip()
	-- Disables the tool's equipped functionality

	-- Clear unnecessary resources
	HideUI();
	HideHandles();
	ClearConnections();
	SnapTracking.StopTracking();
	FinishSnapping();

end;

function ClearConnections()
	-- Clears out temporary connections

	for ConnectionKey, Connection in pairs(Connections) do
		Connection:disconnect();
		Connections[ConnectionKey] = nil;
	end;

end;

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

	local Connection = Connections[ConnectionKey];

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

end;

function ShowUI()
	-- Creates and reveals the UI

	-- Reveal UI if already created
	if ResizeTool.UI then

		-- Reveal the UI
		ResizeTool.UI.Visible = true;

		-- Update the UI every 0.1 seconds
		UIUpdater = Support.ScheduleRecurringTask(UpdateUI, 0.1);

		-- Skip UI creation
		return;

	end;

	-- Create the UI
	ResizeTool.UI = Core.Tool.Interfaces.BTResizeToolGUI:Clone();
	ResizeTool.UI.Parent = Core.UI;
	ResizeTool.UI.Visible = true;

	-- Add functionality to the directions option switch
	local DirectionsSwitch = ResizeTool.UI.DirectionsOption;
	DirectionsSwitch.Normal.Button.MouseButton1Down:connect(function ()
		SetDirections('Normal');
	end);
	DirectionsSwitch.Both.Button.MouseButton1Down:connect(function ()
		SetDirections('Both');
	end);

	-- Add functionality to the increment input
	local IncrementInput = ResizeTool.UI.IncrementOption.Increment.TextBox;
	IncrementInput.FocusLost:connect(function (EnterPressed)
		ResizeTool.Increment = tonumber(IncrementInput.Text) or ResizeTool.Increment;
		IncrementInput.Text = Support.Round(ResizeTool.Increment, 4);
	end);

	-- Add functionality to the size inputs
	local XInput = ResizeTool.UI.Info.SizeInfo.X.TextBox;
	local YInput = ResizeTool.UI.Info.SizeInfo.Y.TextBox;
	local ZInput = ResizeTool.UI.Info.SizeInfo.Z.TextBox;
	XInput.FocusLost:connect(function (EnterPressed)
		local NewSize = tonumber(XInput.Text);
		if NewSize then
			SetAxisSize('X', NewSize);
		end;
	end);
	YInput.FocusLost:connect(function (EnterPressed)
		local NewSize = tonumber(YInput.Text);
		if NewSize then
			SetAxisSize('Y', NewSize);
		end;
	end);
	ZInput.FocusLost:connect(function (EnterPressed)
		local NewSize = tonumber(ZInput.Text);
		if NewSize then
			SetAxisSize('Z', NewSize);
		end;
	end);

	-- Update the UI every 0.1 seconds
	UIUpdater = Support.ScheduleRecurringTask(UpdateUI, 0.1);

end;

function HideUI()
	-- Hides the tool UI

	-- Make sure there's a UI
	if not ResizeTool.UI then
		return;
	end;

	-- Hide the UI
	ResizeTool.UI.Visible = false;

	-- Stop updating the UI
	UIUpdater:Stop();

end;

function UpdateUI()
	-- Updates information on the UI

	-- Make sure the UI's on
	if not ResizeTool.UI then
		return;
	end;

	-- Only show and calculate selection info if it's not empty
	if #Selection.Items == 0 then
		ResizeTool.UI.Info.Visible = false;
		ResizeTool.UI.Size = UDim2.new(0, 245, 0, 90);
		return;
	else
		ResizeTool.UI.Info.Visible = true;
		ResizeTool.UI.Size = UDim2.new(0, 245, 0, 150);
	end;

	-----------------------------------------
	-- Update the size information indicators
	-----------------------------------------

	-- Identify common sizes across axes
	local XVariations, YVariations, ZVariations = {}, {}, {};
	for _, Part in pairs(Selection.Items) do
		table.insert(XVariations, Support.Round(Part.Size.X, 3));
		table.insert(YVariations, Support.Round(Part.Size.Y, 3));
		table.insert(ZVariations, Support.Round(Part.Size.Z, 3));
	end;
	local CommonX = Support.IdentifyCommonItem(XVariations);
	local CommonY = Support.IdentifyCommonItem(YVariations);
	local CommonZ = Support.IdentifyCommonItem(ZVariations);

	-- Shortcuts to indicators
	local XIndicator = ResizeTool.UI.Info.SizeInfo.X.TextBox;
	local YIndicator = ResizeTool.UI.Info.SizeInfo.Y.TextBox;
	local ZIndicator = ResizeTool.UI.Info.SizeInfo.Z.TextBox;

	-- Update each indicator if it's not currently being edited
	if not XIndicator:IsFocused() then
		XIndicator.Text = CommonX or '*';
	end;
	if not YIndicator:IsFocused() then
		YIndicator.Text = CommonY or '*';
	end;
	if not ZIndicator:IsFocused() then
		ZIndicator.Text = CommonZ or '*';
	end;

end;

function SetDirections(DirectionMode)
	-- Sets the given resizing direction mode

	-- Update setting
	ResizeTool.Directions = DirectionMode;

	-- Update the UI switch
	if ResizeTool.UI then
		Core.ToggleSwitch(DirectionMode, ResizeTool.UI.DirectionsOption);
	end;

end;
