local T={}function TextureTool.Equip(T)T:ShowUI()T:EnableSurfaceClickSelection()T:SetTextureType(T.Type)T:SetFace(T.Face)end function TextureTool.Unequip(T)T:HideUI()ClearConnections()end function ClearConnections()for z,b in pairs(T)do b:Disconnect()T[z]=nil end end function TextureTool.ShowUI(T)if T.UI then T.UI.Visible=true T.StopUpdatingUI=Support.Loop(.1,function()T:UpdateUI()end)return end T.UI=Core.Tool.Interfaces.BTTextureToolGUI:Clone()T.UI.Parent=Core.UI T.UI.Visible=true local z=T.UI.AddButton local b=T.UI.RemoveButton local W=T.UI.ModeOption.Decal.Button local J=T.UI.ModeOption.Texture.Button local K=T.UI.ImageIDOption.TextBox local I=T.UI.TransparencyOption.Input.TextBox local L=T.UI.RepeatOption.XInput.TextBox local A=T.UI.RepeatOption.YInput.TextBox W.MouseButton1Click:Connect(function()T:SetTextureType("\068\101\099\097\108")end)J.MouseButton1Click:Connect(function()T:SetTextureType("\084\101\120\116\117\114\101")end)local i={"\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"}local function d()return Roact.createElement(Dropdown,{Position=UDim2.new(0,30,0,0),Size=UDim2.new(1,-45,0,25),Options=i,MaxRows=6,CurrentOption=T.Face and T.Face.Name,OnOptionSelected=function(z)T:SetFace(Enum.NormalId[z])end})end local e=Roact.mount(d(),T.UI.SideOption,"\068\114\111\112\100\111\119\110")T.OnFaceChanged:Connect(function()Roact.update(e,d())end)K.FocusLost:Connect(function(T)SetTextureId(TextureTool.Type,TextureTool.Face,ParseAssetId(K.Text))end)SyncInputToProperty("\084\114\097\110\115\112\097\114\101\110\099\121",I)SyncInputToProperty("\083\116\117\100\115\080\101\114\084\105\108\101\085",L)SyncInputToProperty("\083\116\117\100\115\080\101\114\084\105\108\101\086",A)z.Button.MouseButton1Click:Connect(function()AddTextures(TextureTool.Type,TextureTool.Face)end)b.Button.MouseButton1Click:Connect(function()RemoveTextures(TextureTool.Type,TextureTool.Face)end)local q=(T.UI:WaitForChild("\084\105\116\108\101")):WaitForChild("\083\105\103\110\097\116\117\114\101")ListenForManualWindowTrigger(TextureTool.ManualText,TextureTool.Color.Color,q)T.StopUpdatingUI=Support.Loop(.1,function()T:UpdateUI()end)end function SyncInputToProperty(T,z)z.FocusLost:Connect(function()SetProperty(TextureTool.Type,TextureTool.Face,T,tonumber(z.Text))end)end function TextureTool.EnableSurfaceClickSelection(z)if T.SurfaceClickSelection then T.SurfaceClickSelection:Disconnect()T.SurfaceClickSelection=nil end T.SurfaceClickSelection=Core.Mouse.Button1Down:Connect(function()local T,b=Core.Targeting:UpdateTarget()if Selection.IsSelected(b)then z:SetFace(Core.Mouse.TargetSurface)end end)end function TextureTool.HideUI(T)if not T.UI then return end T.UI.Visible=false T.StopUpdatingUI()end function GetTextures(T,z)local b={}for W,J in pairs(Selection.Parts)do for W,J in pairs(J:GetChildren())do if J.ClassName==T and J.Face==z then table.insert(b,J)end end end return b end
-- Container for temporary connections (disconnected automatically)
local Connections = {}

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

	-- Start up our interface
	self:ShowUI()
	self:EnableSurfaceClickSelection()

	-- Set our current texture type and face
	self:SetTextureType(self.Type)
	self:SetFace(self.Face)
end

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

	-- Clear unnecessary resources
	self:HideUI()
	ClearConnections()
end

function ClearConnections()
	-- Clears out temporary connections

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

function TextureTool:ShowUI()
	-- Creates and reveals the UI

	-- Reveal UI if already created
	if self.UI then
		-- Reveal the UI
		self.UI.Visible = true

		-- Update the UI every 0.1 seconds
		self.StopUpdatingUI = Support.Loop(0.1, function()
			self:UpdateUI()
		end)

		-- Skip UI creation
		return
	end

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

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

	-- Enable the texture type switch
	DecalModeButton.MouseButton1Click:Connect(function()
		self:SetTextureType("Decal")
	end)
	TextureModeButton.MouseButton1Click:Connect(function()
		self:SetTextureType("Texture")
	end)

	-- Create the face selection dropdown
	local Faces = {
		"Top",
		"Bottom",
		"Front",
		"Back",
		"Left",
		"Right",
	}
	local function BuildFaceDropdown()
		return Roact.createElement(Dropdown, {
			Position = UDim2.new(0, 30, 0, 0),
			Size = UDim2.new(1, -45, 0, 25),
			Options = Faces,
			MaxRows = 6,
			CurrentOption = self.Face and self.Face.Name,
			OnOptionSelected = function(Option)
				self:SetFace(Enum.NormalId[Option])
			end,
		})
	end

	-- Mount type dropdown
	local FaceDropdownHandle = Roact.mount(BuildFaceDropdown(), self.UI.SideOption, "Dropdown")
	self.OnFaceChanged:Connect(function()
		Roact.update(FaceDropdownHandle, BuildFaceDropdown())
	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)

	-- Hook up manual triggering
	local SignatureButton = self.UI:WaitForChild("Title"):WaitForChild("Signature")
	ListenForManualWindowTrigger(TextureTool.ManualText, TextureTool.Color.Color, SignatureButton)

	-- Update the UI every 0.1 seconds
	self.StopUpdatingUI = Support.Loop(0.1, function()
		self:UpdateUI()
	end)
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 TextureTool: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
			self:SetFace(Core.Mouse.TargetSurface)
		end
	end)
end

function TextureTool:HideUI()
	-- Hides the tool UI

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

	-- Hide the UI
	self.UI.Visible = false

	-- Stop updating the UI
	self.StopUpdatingUI()
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
