local C={}function TextureTool.Equip()ShowUI()EnableSurfaceClickSelection()SetTextureType(TextureTool.Type)SetFace(TextureTool.Face)end function TextureTool.Unequip()HideUI()ClearConnections()end function ClearConnections()for c,r in pairs(C)do r:disconnect()C[c]=nil end end function ShowUI()if UI then UI.Visible=true UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)return end UI=Core.Tool.Interfaces.BTTextureToolGUI:Clone()UI.Parent=Core.UI UI.Visible=true local C=UI.AddButton local c=UI.RemoveButton local r=UI.ModeOption.Decal.Button local O=UI.ModeOption.Texture.Button local p=UI.ImageIDOption.TextBox local b=UI.TransparencyOption.Input.TextBox local t=UI.RepeatOption.XInput.TextBox local B=UI.RepeatOption.YInput.TextBox r.MouseButton1Click:connect(function()SetTextureType("\068\101\099\097\108")end)O.MouseButton1Click:connect(function()SetTextureType("\084\101\120\116\117\114\101")end)local Y={"\084\111\112","\066\111\116\116\111\109","\070\114\111\110\116","\066\097\099\107";"\076\101\102\116","\082\105\103\104\116"}FaceDropdown=(Core.Cheer(UI.SideOption.Dropdown)).Start(Y,"",function(C)SetFace(Enum.NormalId[C])end)p.FocusLost:connect(function(C)SetTextureId(TextureTool.Type,TextureTool.Face,ParseAssetId(p.Text))end)SyncInputToProperty("\084\114\097\110\115\112\097\114\101\110\099\121",b)SyncInputToProperty("\083\116\117\100\115\080\101\114\084\105\108\101\085",t)SyncInputToProperty("\083\116\117\100\115\080\101\114\084\105\108\101\086",B)C.Button.MouseButton1Click:connect(function()AddTextures(TextureTool.Type,TextureTool.Face)end)c.Button.MouseButton1Click:connect(function()RemoveTextures(TextureTool.Type,TextureTool.Face)end)UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)end function SyncInputToProperty(C,c)c.FocusLost:connect(function()SetProperty(TextureTool.Type,TextureTool.Face,C,tonumber(c.Text))end)end function EnableSurfaceClickSelection()if C.SurfaceClickSelection then C.SurfaceClickSelection:disconnect()C.SurfaceClickSelection=nil end C.SurfaceClickSelection=UserInputService.InputEnded:connect(function(C,c)if not c and(C.UserInputType==Enum.UserInputType.MouseButton1 and Selection.IsSelected(Core.Mouse.Target))then SetFace(Core.Mouse.TargetSurface)end end)end function HideUI()if not UI then return end UI.Visible=false UIUpdater:Stop()end function GetTextures(C,c)local r={}for O,p in pairs(Selection.Items)do for O,p in pairs(p:GetChildren())do if p.ClassName==C and p.Face==c then table.insert(r,p)end end end return r end

local Connections = {};

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

	-- Start up our interface
	ShowUI();
	EnableSurfaceClickSelection();

	-- Set our current texture type and face
	SetTextureType(TextureTool.Type);
	SetFace(TextureTool.Face);

end;

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

	-- Clear unnecessary resources
	HideUI();
	ClearConnections();

end;

function ClearConnections()
	-- Clears out temporary connections

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

end;

function ShowUI()
	-- Creates and reveals the UI

	-- Reveal UI if already created
	if UI then

		-- Reveal the UI
		UI.Visible = true;

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

		-- Skip UI creation
		return;

	end;

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

	-- References to UI elements
	local AddButton = UI.AddButton;
	local RemoveButton = UI.RemoveButton;
	local DecalModeButton = UI.ModeOption.Decal.Button;
	local TextureModeButton = UI.ModeOption.Texture.Button;
	local ImageIdInput = UI.ImageIDOption.TextBox;
	local TransparencyInput = UI.TransparencyOption.Input.TextBox;
	local RepeatXInput = UI.RepeatOption.XInput.TextBox;
	local RepeatYInput = UI.RepeatOption.YInput.TextBox;

	-- Enable the texture type switch
	DecalModeButton.MouseButton1Click:connect(function ()
		SetTextureType 'Decal';
	end);
	TextureModeButton.MouseButton1Click:connect(function ()
		SetTextureType 'Texture';
	end);

	-- Create the face selection dropdown
	local Faces = { 'Top', 'Bottom', 'Front', 'Back', 'Left', 'Right' };
	FaceDropdown = Core.Cheer(UI.SideOption.Dropdown).Start(Faces, '', function (Face)
		SetFace(Enum.NormalId[Face]);
	end);

	-- Enable the image ID input
	ImageIdInput.FocusLost:connect(function (EnterPressed)
		SetTextureId(TextureTool.Type, TextureTool.Face, ParseAssetId(ImageIdInput.Text));
	end);

	-- Enable other inputs
	SyncInputToProperty('Transparency', TransparencyInput);
	SyncInputToProperty('StudsPerTileU', RepeatXInput);
	SyncInputToProperty('StudsPerTileV', RepeatYInput);

	-- Enable the texture adding button
	AddButton.Button.MouseButton1Click:connect(function ()
		AddTextures(TextureTool.Type, TextureTool.Face);
	end);
	RemoveButton.Button.MouseButton1Click:connect(function ()
		RemoveTextures(TextureTool.Type, TextureTool.Face);
	end);

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

end;

function SyncInputToProperty(Property, Input)
	-- Enables `Input` to change the given property

	-- Enable inputs
	Input.FocusLost:connect(function ()
		SetProperty(TextureTool.Type, TextureTool.Face, Property, tonumber(Input.Text));
	end);

end;

function EnableSurfaceClickSelection()
	-- Allows for the setting of the current face by clicking

	-- Clear out any existing connection
	if Connections.SurfaceClickSelection then
		Connections.SurfaceClickSelection:disconnect();
		Connections.SurfaceClickSelection = nil;
	end;

	-- Add the new click connection
	Connections.SurfaceClickSelection = UserInputService.InputEnded:connect(function (Input, GameProcessedEvent)
		if not GameProcessedEvent and Input.UserInputType == Enum.UserInputType.MouseButton1 and Selection.IsSelected(Core.Mouse.Target) then
			SetFace(Core.Mouse.TargetSurface);
		end;
	end);

end;

function HideUI()
	-- Hides the tool UI

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

	-- Hide the UI
	UI.Visible = false;

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

end;

function GetTextures(TextureType, Face)
	-- Returns all the textures in the selection

	local Textures = {};

	-- Get any textures from any selected parts
	for _, Part in pairs(Selection.Items) do
		for _, Child in pairs(Part:GetChildren()) do

			-- If this child is texture we're looking for, collect it
			if Child.ClassName == TextureType and Child.Face == Face then
				table.insert(Textures, Child);
			end;

		end;
	end;

	-- Return the found textures
	return Textures;

end;
