local l={}function TextureTool.Equip()ShowUI()EnableSurfaceClickSelection()SetTextureType(TextureTool.Type)SetFace(TextureTool.Face)end function TextureTool.Unequip()HideUI()ClearConnections()end function ClearConnections()for O,h in pairs(l)do h:Disconnect()l[O]=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 l=UI.AddButton local O=UI.RemoveButton local h=UI.ModeOption.Decal.Button local A=UI.ModeOption.Texture.Button local K=UI.ImageIDOption.TextBox local Q=UI.TransparencyOption.Input.TextBox local f=UI.RepeatOption.XInput.TextBox local C=UI.RepeatOption.YInput.TextBox h.MouseButton1Click:Connect(function()SetTextureType("\068\101\099\097\108")end)A.MouseButton1Click:Connect(function()SetTextureType("\084\101\120\116\117\114\101")end)local V={"\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(V,"",function(l)SetFace(Enum.NormalId[l])end)K.FocusLost:Connect(function(l)SetTextureId(TextureTool.Type,TextureTool.Face,ParseAssetId(K.Text))end)SyncInputToProperty("\084\114\097\110\115\112\097\114\101\110\099\121",Q)SyncInputToProperty("\083\116\117\100\115\080\101\114\084\105\108\101\085",f)SyncInputToProperty("\083\116\117\100\115\080\101\114\084\105\108\101\086",C)l.Button.MouseButton1Click:Connect(function()AddTextures(TextureTool.Type,TextureTool.Face)end)O.Button.MouseButton1Click:Connect(function()RemoveTextures(TextureTool.Type,TextureTool.Face)end)UIUpdater=Support.ScheduleRecurringTask(UpdateUI,.1)end function SyncInputToProperty(l,O)O.FocusLost:Connect(function()SetProperty(TextureTool.Type,TextureTool.Face,l,tonumber(O.Text))end)end function EnableSurfaceClickSelection()if l.SurfaceClickSelection then l.SurfaceClickSelection:Disconnect()l.SurfaceClickSelection=nil end l.SurfaceClickSelection=Core.Mouse.Button1Down:Connect(function()local l,O=Core.Targeting:UpdateTarget()if Selection.IsSelected(O)then SetFace(Core.Mouse.TargetSurface)end end)end function HideUI()if not UI then return end UI.Visible=false UIUpdater:Stop()end function GetTextures(l,O)local h={}for A,K in pairs(Selection.Parts)do for A,K in pairs(K:GetChildren())do if K.ClassName==l and K.Face==O then table.insert(h,K)end end end return h end
-- Container for temporary connections (disconnected automatically)
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 = Core.Mouse.Button1Down:Connect(function()
		local _, ScopeTarget = Core.Targeting:UpdateTarget()
		if Selection.IsSelected(ScopeTarget) 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.Parts) 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
