local w={}function ResizeTool.Equip()ShowUI()ShowHandles()BindShortcutKeys()end function ResizeTool.Unequip()HideUI()HideHandles()ClearConnections()SnapTracking.StopTracking()FinishSnapping()end function ClearConnections()for I,y in pairs(w)do y:disconnect()w[I]=nil end end function ClearConnection(I)local y=w[I]if y then y:disconnect()w[I]=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 w=ResizeTool.UI.DirectionsOption w.Normal.Button.MouseButton1Down:connect(function()SetDirections("\078\111\114\109\097\108")end)w.Both.Button.MouseButton1Down:connect(function()SetDirections("\066\111\116\104")end)local I=ResizeTool.UI.IncrementOption.Increment.TextBox I.FocusLost:connect(function(w)ResizeTool.Increment=tonumber(I.Text)or ResizeTool.Increment I.Text=Support.Round(ResizeTool.Increment,4)end)local y=ResizeTool.UI.Info.SizeInfo.X.TextBox local p=ResizeTool.UI.Info.SizeInfo.Y.TextBox local x=ResizeTool.UI.Info.SizeInfo.Z.TextBox y.FocusLost:connect(function(w)local I=tonumber(y.Text)if I then SetAxisSize("\088",I)end end)p.FocusLost:connect(function(w)local I=tonumber(p.Text)if I then SetAxisSize("\089",I)end end)x.FocusLost:connect(function(w)local I=tonumber(x.Text)if I then SetAxisSize("\090",I)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 w,I,y={},{},{}for p,x in pairs(Selection.Items)do table.insert(w,Support.Round(x.Size.X,3))table.insert(I,Support.Round(x.Size.Y,3))table.insert(y,Support.Round(x.Size.Z,3))end local p=Support.IdentifyCommonItem(w)local x=Support.IdentifyCommonItem(I)local X=Support.IdentifyCommonItem(y)local B=ResizeTool.UI.Info.SizeInfo.X.TextBox local m=ResizeTool.UI.Info.SizeInfo.Y.TextBox local l=ResizeTool.UI.Info.SizeInfo.Z.TextBox if not B:IsFocused()then B.Text=p or"\042"end if not m:IsFocused()then m.Text=x or"\042"end if not l:IsFocused()then l.Text=X or"\042"end end function SetDirections(w)ResizeTool.Directions=w if ResizeTool.UI then Core.ToggleSwitch(w,ResizeTool.UI.DirectionsOption)end end
-- Container for temporary connections (disconnected automatically)
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
