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

function 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 = 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 = 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
	UI.Size = UDim2.new(0, 200, 0, 40 + Sum)
end

function UpdateUI()
	-- Updates information on the UI

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

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

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

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

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

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

	-- Update the material dropdown
	MaterialDropdown.SetOption(Material and Materials[Material] or "*")

	-- 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 = {},

		Unapply = function(Record)
			-- Reverts this change

			-- Select the changed parts
			Selection.Replace(Support.GetListMembers(Record.Before, "Part"))

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

		Apply = function(Record)
			-- Applies this change

			-- Select the changed parts
			Selection.Replace(Support.GetListMembers(Record.After, "Part"))

			-- 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
