local r function MaterialTool.ChangeLayout(p,B)if r==B then return end r=B for r,B in pairs(UIElements)do local y=p.UI[B]y.Visible=false end local y=0 for r,B in ipairs(B)do local h=p.UI[B]h.Visible=true h.Position=UDim2.new(0,0,0,20)+UDim2.new(h.Position.X.Scale,h.Position.X.Offset,0,y+10)y=(y+10)+h.AbsoluteSize.Y end p.UI.Size=UDim2.new(0,200,0,40+y)end function MaterialTool.UpdateUI(r)if not r.UI then return end local B=r.UI.TransparencyOption.Input.TextBox local p=r.UI.ReflectanceOption.Input.TextBox if#Selection.Parts==0 then r:ChangeLayout(Layouts.EmptySelection)return else r:ChangeLayout(Layouts.Normal)end local y=Support.IdentifyCommonProperty(Selection.Parts,"\077\097\116\101\114\105\097\108")local h=Support.IdentifyCommonProperty(Selection.Parts,"\084\114\097\110\115\112\097\114\101\110\099\121")local K=Support.IdentifyCommonProperty(Selection.Parts,"\082\101\102\108\101\099\116\097\110\099\101")if r.CurrentMaterial~=y then r.CurrentMaterial=y r.OnMaterialChanged:Fire(y)end UpdateDataInputs({[B]=h and Support.Round(h,2)or"\042";[p]=K and Support.Round(K,2)or"\042"})end function TrackChange()HistoryRecord={Before={},After={};Selection=Selection.Items,Unapply=function(r)Selection.Replace(r.Selection)Core.SyncAPI:Invoke("\083\121\110\099\077\097\116\101\114\105\097\108",r.Before)end;Apply=function(r)Selection.Replace(r.Selection)Core.SyncAPI:Invoke("\083\121\110\099\077\097\116\101\114\105\097\108",r.After)end}end function RegisterChange()if not HistoryRecord then return end Core.SyncAPI:Invoke("\083\121\110\099\077\097\116\101\114\105\097\108",HistoryRecord.After)Core.History.Add(HistoryRecord)HistoryRecord=nil end
-- Current UI layout
local CurrentLayout

function MaterialTool:ChangeLayout(Layout)
	-- Sets the UI to the given layout

	-- Make sure the new layout isn't already set
	if CurrentLayout == Layout then
		return
	end

	-- Set this as the current layout
	CurrentLayout = Layout

	-- Reset the UI
	for _, ElementName in pairs(UIElements) do
		local Element = self.UI[ElementName]
		Element.Visible = false
	end

	-- Keep track of the total vertical extents of all items
	local Sum = 0

	-- Go through each layout element
	for ItemIndex, ItemName in ipairs(Layout) do
		local Item = self.UI[ItemName]

		-- Make the item visible
		Item.Visible = true

		-- Position this item underneath the past items
		Item.Position = UDim2.new(0, 0, 0, 20) + UDim2.new(Item.Position.X.Scale, Item.Position.X.Offset, 0, Sum + 10)

		-- Update the sum of item heights
		Sum = Sum + 10 + Item.AbsoluteSize.Y
	end

	-- Resize the container to fit the new layout
	self.UI.Size = UDim2.new(0, 200, 0, 40 + Sum)
end

function MaterialTool:UpdateUI()
	-- Updates information on the UI

	-- Make sure the UI's on
	if not self.UI then
		return
	end

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

	-----------------------
	-- Update the UI layout
	-----------------------

	-- Figure out the necessary UI layout
	if #Selection.Parts == 0 then
		self:ChangeLayout(Layouts.EmptySelection)
		return

	-- When the selection isn't empty
	else
		self:ChangeLayout(Layouts.Normal)
	end

	-- Get the common properties
	local Material = Support.IdentifyCommonProperty(Selection.Parts, "Material")
	local Transparency = Support.IdentifyCommonProperty(Selection.Parts, "Transparency")
	local Reflectance = Support.IdentifyCommonProperty(Selection.Parts, "Reflectance")

	-- Update the material dropdown
	if self.CurrentMaterial ~= Material then
		self.CurrentMaterial = Material
		self.OnMaterialChanged:Fire(Material)
	end

	-- Update inputs
	UpdateDataInputs({
		[TransparencyInput] = Transparency and Support.Round(Transparency, 2) or "*",
		[ReflectanceInput] = Reflectance and Support.Round(Reflectance, 2) or "*",
	})
end

function TrackChange()
	-- Start the record
	HistoryRecord = {
		Before = {},
		After = {},
		Selection = Selection.Items,

		Unapply = function(Record)
			-- Reverts this change

			-- Select the changed parts
			Selection.Replace(Record.Selection)

			-- Send the change request
			Core.SyncAPI:Invoke("SyncMaterial", Record.Before)
		end,

		Apply = function(Record)
			-- Applies this change

			-- Select the changed parts
			Selection.Replace(Record.Selection)

			-- Send the change request
			Core.SyncAPI:Invoke("SyncMaterial", Record.After)
		end,
	}
end

function RegisterChange()
	-- Finishes creating the history record and registers it

	-- Make sure there's an in-progress history record
	if not HistoryRecord then
		return
	end

	-- Send the change to the server
	Core.SyncAPI:Invoke("SyncMaterial", HistoryRecord.After)

	-- Register the record and clear the staging
	Core.History.Add(HistoryRecord)
	HistoryRecord = nil
end
