rollout condenseMats "Condese Materials" width:275
(
	dotNetControl tv_matMatches "treeview" width:250 height:700
	button btn_condenseMats "Condense Materials" width:250 height:30
	
	STRUCT materialMatch
	(
		name,
		materials,
		mainMat,
		node,
		objects
	)
	
	local materialNames = #()
	local matMatches = #()
	local checkFlag = false
	
	fn findMatches =
	(
		FOR theObject in objects where theObject.material != undefined DO
		(
			local foundIndex = (findItem materialNames theObject.material.name)
			IF foundIndex == 0 THEN
			(
				local newMatch = materialMatch name:theObject.material.name materials:#(theObject.material) objects:#(#(theObject.inode.handle))
				append matMatches newMatch
				append materialNames theObject.material.name
			)
			ELSE
			(
				local theMatch = matMatches[foundIndex]
				local matchFound = false
				FOR theMatIndex = 1 to theMatch.materials.count DO
				(
					IF theMatch.materials[theMatIndex] == theObject.material DO
					(
						append theMatch.objects[theMatIndex] theObject.inode.handle
						matchFound = true
					)
				)
				IF matchFound == false DO
				(
					append theMatch.materials theObject.material
					append theMatch.objects #(theObject.inode.handle)
				)
			)
		)
	)
	
	fn populateTree =
	(
		tv_matMatches.font = (dotNetObject "System.Drawing.Font" "Microsoft Sans Serif" 11 (dotNetClass "System.Drawing.FontStyle").Bold (dotNetClass "System.Drawing.GraphicsUnit").Pixel)
		FOR theMatch in matMatches DO
		(
			local newNode = tv_matMatches.nodes.add theMatch.name
			newNode.checked = true
			newNode.nodeFont = tv_matMatches.font
		)
		tv_matMatches.font = (dotNetObject "System.Drawing.Font" "Microsoft Sans Serif" 11 (dotNetClass "System.Drawing.GraphicsUnit").Pixel)	
		FOR theMatchIndex = 1 to matMatches.count DO
		(
			FOR theMatIndex = 1 to matMatches[theMatchIndex].materials.count DO
			(
				local newSubNode = tv_matMatches.nodes.item[theMatchIndex-1].nodes.add "Sub Node" (classOf matMatches[theMatchIndex].materials[theMatIndex] as string+" - Objects: "+matMatches[theMatchIndex].objects[theMatIndex].count as string)
				IF theMatIndex == 1 DO newSubNode.checked = true
				newSubNode.tag = matMatches[theMatchIndex].objects[theMatIndex]
			)
		)
		tv_matMatches.expandAll()
	)
	
	ON condenseMats OPEN DO
	(
		local cursors = dotNetClass "System.Windows.Forms.Cursors"
		local cursor = dotNetClass "System.Windows.Forms.Cursor"
		cursor.current = cursors.WaitCursor
		tv_matMatches.checkBoxes = true
		
		findMatches()
		populateTree()
		checkFlag = true
		cursor.current = cursors.Arrow
	)
	
	ON tv_matMatches AFTERCHECK arg DO
	(
		IF checkFlag DO
		(
			checkFlag = false
			IF arg.node.level == 1 DO
			(
				IF arg.node.checked THEN
				(
					arg.node.parent.checked = true
					matMatches[arg.node.parent.index+1].mainMat = matMatches[arg.node.parent.index+1].materials[arg.node.index+1]
					FOR subNodeIndex = 0 to (arg.node.parent.nodes.count-1) DO
					(
						local theSubNode = arg.node.parent.nodes.item[subNodeIndex]
						IF theSubNode != arg.node DO theSubNode.checked = false
					)
				)
				ELSE
				(
					arg.node.parent.checked = false
				)
			)
			checkFlag= true
		)
	)
	
	ON tv_matMatches NodeMouseDoubleClick arg DO
	(
		IF arg.node.level == 1 DO
		(
			clearSelection()
			FOR theId in arg.node.tag DO selectMore (maxOps.getNodeByHandle theId)
		)
	)
	
	ON btn_condenseMats PRESSED DO
	(
		local cursors = dotNetClass "System.Windows.Forms.Cursors"
		local cursor = dotNetClass "System.Windows.Forms.Cursor"
		cursor.current = cursors.WaitCursor
		UNDO "Condense Materials" ON
		(
			FOR nodeIndex = 0 to (tv_matMatches.nodes.count-1) DO
			(
				local theNode = tv_matMatches.nodes.item[nodeIndex]
				IF theNode.checked DO
				(
					IF matMatches[nodeIndex+1].mainMat != undefined DO
					(
						FOR theObjArray in matMatches[nodeIndex+1].objects DO
						(
							FOR theObjID in theObjArray DO (maxOps.getNodeByHandle theObjID).material = matMatches[nodeIndex+1].mainMat
						)
					)
				)
			)
		)
		cursor.current = cursors.Arrow
		destroyDialog condenseMats
	)
)
createDialog condenseMats

