local y={[Enum.Material.SmoothPlastic]="\083\109\111\111\116\104 \080\108\097\115\116\105\099";[Enum.Material.Plastic]="\080\108\097\115\116\105\099",[Enum.Material.Brick]="\066\114\105\099\107",[Enum.Material.Cobblestone]="\067\111\098\098\108\101\115\116\111\110\101",[Enum.Material.Concrete]="\067\111\110\099\114\101\116\101";[Enum.Material.CorrodedMetal]="\067\111\114\114\111\100\101\100 \077\101\116\097\108";[Enum.Material.DiamondPlate]="\068\105\097\109\111\110\100 \080\108\097\116\101",[Enum.Material.Fabric]="\070\097\098\114\105\099",[Enum.Material.Foil]="\070\111\105\108";[Enum.Material.ForceField]="\070\111\114\099\101\102\105\101\108\100",[Enum.Material.Granite]="\071\114\097\110\105\116\101";[Enum.Material.Grass]="\071\114\097\115\115";[Enum.Material.Ice]="\073\099\101",[Enum.Material.Marble]="\077\097\114\098\108\101";[Enum.Material.Metal]="\077\101\116\097\108",[Enum.Material.Neon]="\078\101\111\110",[Enum.Material.Pebble]="\080\101\098\098\108\101";[Enum.Material.Sand]="\083\097\110\100",[Enum.Material.Slate]="\083\108\097\116\101";[Enum.Material.Wood]="\087\111\111\100";[Enum.Material.WoodPlanks]="\087\111\111\100 \080\108\097\110\107\115";[Enum.Material.Glass]="\071\108\097\115\115"}function MaterialTool.ShowUI(c)if c.UI then c.UI.Visible=true c.StopUpdatingUI=Support.Loop(.1,function()c:UpdateUI()end)return end c.UI=Core.Tool.Interfaces.BTMaterialToolGUI:Clone()c.UI.Parent=Core.UI c.UI.Visible=true local T=c.UI.TransparencyOption.Input.TextBox local F=c.UI.ReflectanceOption.Input.TextBox local E=Support.Values(y)table.sort(E)local function H()return Roact.createElement(Dropdown,{Position=UDim2.new(0,50,0,0);Size=UDim2.new(0,130,0,25);Options=E,MaxRows=6;CurrentOption=c.CurrentMaterial and c.CurrentMaterial.Name;OnOptionSelected=function(c)SetProperty("\077\097\116\101\114\105\097\108",Support.FindTableOccurrence(y,c))end})end local l=Roact.mount(H(),c.UI.MaterialOption,"\068\114\111\112\100\111\119\110")c.OnMaterialChanged:Connect(function()Roact.update(l,H())end)SyncInputToProperty("\084\114\097\110\115\112\097\114\101\110\099\121",T)SyncInputToProperty("\082\101\102\108\101\099\116\097\110\099\101",F)local n=(c.UI:WaitForChild("\084\105\116\108\101")):WaitForChild("\083\105\103\110\097\116\117\114\101")ListenForManualWindowTrigger(MaterialTool.ManualText,MaterialTool.Color.Color,n)c.StopUpdatingUI=Support.Loop(.1,function()c:UpdateUI()end)end function MaterialTool.HideUI(y)if not y.UI then return end y.UI.Visible=false y.StopUpdatingUI()end function SyncInputToProperty(y,c)c.FocusLost:Connect(function()SetProperty(y,tonumber(c.Text))end)end function SetProperty(y,c)if c==nil then return end TrackChange()for T,F in pairs(Selection.Parts)do table.insert(HistoryRecord.Before,{Part=F,[y]=F[y]})table.insert(HistoryRecord.After,{Part=F,[y]=c})end RegisterChange()end function UpdateDataInputs(y)for y,c in pairs(y)do if not y:IsFocused()then y.Text=tostring(c)end end end
-- Designate a friendly name to each material
local Materials = {
	[Enum.Material.SmoothPlastic] = "Smooth Plastic",
	[Enum.Material.Plastic] = "Plastic",
	[Enum.Material.Brick] = "Brick",
	[Enum.Material.Cobblestone] = "Cobblestone",
	[Enum.Material.Concrete] = "Concrete",
	[Enum.Material.CorrodedMetal] = "Corroded Metal",
	[Enum.Material.DiamondPlate] = "Diamond Plate",
	[Enum.Material.Fabric] = "Fabric",
	[Enum.Material.Foil] = "Foil",
	[Enum.Material.ForceField] = "Forcefield",
	[Enum.Material.Granite] = "Granite",
	[Enum.Material.Grass] = "Grass",
	[Enum.Material.Ice] = "Ice",
	[Enum.Material.Marble] = "Marble",
	[Enum.Material.Metal] = "Metal",
	[Enum.Material.Neon] = "Neon",
	[Enum.Material.Pebble] = "Pebble",
	[Enum.Material.Sand] = "Sand",
	[Enum.Material.Slate] = "Slate",
	[Enum.Material.Wood] = "Wood",
	[Enum.Material.WoodPlanks] = "Wood Planks",
	[Enum.Material.Glass] = "Glass",
}

function MaterialTool: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.BTMaterialToolGUI:Clone()
	self.UI.Parent = Core.UI
	self.UI.Visible = true

	-- References to inputs
	local TransparencyInput = self.UI.TransparencyOption.Input.TextBox
	local ReflectanceInput = self.UI.ReflectanceOption.Input.TextBox

	-- Sort the material list
	local MaterialList = Support.Values(Materials)
	table.sort(MaterialList)

	-- Create material dropdown
	local function BuildMaterialDropdown()
		return Roact.createElement(Dropdown, {
			Position = UDim2.new(0, 50, 0, 0),
			Size = UDim2.new(0, 130, 0, 25),
			Options = MaterialList,
			MaxRows = 6,
			CurrentOption = self.CurrentMaterial and self.CurrentMaterial.Name,
			OnOptionSelected = function(Option)
				SetProperty("Material", Support.FindTableOccurrence(Materials, Option))
			end,
		})
	end

	-- Mount surface dropdown
	local MaterialDropdownHandle = Roact.mount(BuildMaterialDropdown(), self.UI.MaterialOption, "Dropdown")
	self.OnMaterialChanged:Connect(function()
		Roact.update(MaterialDropdownHandle, BuildMaterialDropdown())
	end)

	-- Enable the transparency and reflectance inputs
	SyncInputToProperty("Transparency", TransparencyInput)
	SyncInputToProperty("Reflectance", ReflectanceInput)

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

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

function MaterialTool: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 SyncInputToProperty(Property, Input)
	-- Enables `Input` to change the given property

	-- Enable inputs
	Input.FocusLost:Connect(function()
		SetProperty(Property, tonumber(Input.Text))
	end)
end

function SetProperty(Property, Value)
	-- Make sure the given value is valid
	if Value == nil then
		return
	end

	-- Start a history record
	TrackChange()

	-- Go through each part
	for _, Part in pairs(Selection.Parts) do
		-- Store the state of the part before modification
		table.insert(HistoryRecord.Before, { Part = Part, [Property] = Part[Property] })

		-- Create the change request for this part
		table.insert(HistoryRecord.After, { Part = Part, [Property] = Value })
	end

	-- Register the changes
	RegisterChange()
end

function UpdateDataInputs(Data)
	-- Updates the data in the given TextBoxes when the user isn't typing in them

	-- Go through the inputs and data
	for Input, UpdatedValue in pairs(Data) do
		-- Makwe sure the user isn't typing into the input
		if not Input:IsFocused() then
			-- Set the input's value
			Input.Text = tostring(UpdatedValue)
		end
	end
end
