local c function MaterialTool.ChangeLayout(Z,g)if c==g then return end c=g for c,g in pairs(UIElements)do local e=Z.UI[g]e.Visible=false end local e=0 for c,g in ipairs(g)do local E=Z.UI[g]E.Visible=true E.Position=UDim2.new(0,0,0,20)+UDim2.new(E.Position.X.Scale,E.Position.X.Offset,0,e+10)e=(e+10)+E.AbsoluteSize.Y end Z.UI.Size=UDim2.new(0,200,0,40+e)end function MaterialTool.UpdateUI(c)if not c.UI then return end local g=c.UI.TransparencyOption.Input.TextBox local Z=c.UI.ReflectanceOption.Input.TextBox if#Selection.Parts==0 then c:ChangeLayout(Layouts.EmptySelection)return else c:ChangeLayout(Layouts.Normal)end local e=Support.IdentifyCommonProperty(Selection.Parts,"\077\097\116\101\114\105\097\108")local E=Support.IdentifyCommonProperty(Selection.Parts,"\084\114\097\110\115\112\097\114\101\110\099\121")local R=Support.IdentifyCommonProperty(Selection.Parts,"\082\101\102\108\101\099\116\097\110\099\101")if c.CurrentMaterial~=e then c.CurrentMaterial=e c.OnMaterialChanged:Fire(e)end UpdateDataInputs({[g]=E and Support.Round(E,2)or"\042";[Z]=R and Support.Round(R,2)or"\042"})end function TrackChange()HistoryRecord={Before={},After={};Selection=Selection.Items;Unapply=function(c)Selection.Replace(c.Selection)Core.SyncAPI:Invoke("\083\121\110\099\077\097\116\101\114\105\097\108",c.Before)end,Apply=function(c)Selection.Replace(c.Selection)Core.SyncAPI:Invoke("\083\121\110\099\077\097\116\101\114\105\097\108",c.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

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;
