MacroScript DiffuseMapper category:"Piranha-Bytes" tooltip:"Assign average DiffuseTextureColor to DiffuseColor" buttonText:"DiffuseMap" 
(
	rollout ro_DiffuseMapper "pb  DiffuseMapper"
	(
		checkbutton ui_FromScene "Scene" pos:[5,5] width:40 height:20 checked:true tooltip:"Use all Scene Materials"
		checkbutton ui_FromObj   "Object" pos:[45,5] width:50 height:20 checked:false tooltip:"Use Materials from selected Object"
		checkbutton ui_FromMat "Material" pos:[95,5] width:50 height:20 checked:false tooltip:"Use selected Material in MaterialEditor"
	
		label lbl_ValidMats "Valid Materials found:" pos:[5,30]
		button ui_Start "Apply Diffuse Colors" pos:[5,50] width:140 height:20 tooltip:"Get the average color of textures and set it as Diffuse Color"
		spinner ui_Resolution "Texture Resolution:" pos:[5,78] fieldwidth:36 scale:1 range:[1,1024,100] type:#integer	
		checkbox ui_Ambient "Ambient = Diffuse" pos:[5,100] checked:true
		colorpicker ui_AmbColor pos:[110,100] alpha:false fieldwidth:30 height:14 color:(color 100 100 100) 
		checkbutton ui_ToggleShow "Toggle ShowMaps" pos:[5,120] width:140 height:20
	
		progressbar ui_prog pos:[5,145] height:10 width:140
	
		local matList = #()
	
		function SetDiffuse cmat =
		(
			difMap = cmat.DiffuseMap
			if difMap != undefined do
			(
				cbitmap = rendermap difmap size:[ui_Resolution.value, ui_Resolution.value]
	
				bh = cbitmap.height
				bw = cbitmap.width
				numpix = bh * bw
				
				redline   = 0.0
				greenline = 0.0
				blueline  = 0.0
				
				for line = 1 to bh do
				(
					colorRow = getPixels cbitmap [0,(line-1)] bw
					for c=1 to bw do
					(
						redLine   = redLine   + colorRow[c].red
						greenLine = greenLine + colorRow[c].green
						blueLine  = blueLine  + colorRow[c].blue
					)
					colorRow = #()
				)
				avgRed   = RedLine   / numPix
				avgGreen = GreenLine / numPix
				avgBlue  = BlueLine  / numPix
				avgColor = color avgRed avgGreen avgBlue
				cmat.diffusecolor =  avgColor
				
				if ui_Ambient.checked == true then cmat.ambientcolor =  avgColor
				else  
				(
					cmat.adLock = off
					cmat.ambientcolor = ui_AmbColor.color
				)
				showTextureMap cmat false
				for tm = 1 to cmat.maps.count do if cmat.maps[tm] != undefined then showTextureMap cmat cmat.maps[tm] false
	
				close cbitmap
			)
		)
		
		function getMaterialList gMatList =
		(
			matList = #()
			
			for m=1 to gmatList.count do
			(
				matClass = classof gmatList[m]
				if matClass == MultiMaterial do
				(
					for sm = 1 to gmatList[m].count do
					(
						if (classOf gmatList[m][sm]) == StandardMaterial do
						(
							if (classOf gmatList[m][sm].diffuseMap) != undefined then append matList gmatList[m][sm]
						)
					)
				)
				if matClass == StandardMaterial do
				(
					if (classOf gmatList[m].diffuseMap) != undefined then append matList gmatList[m]
				)
			)
			lbl_ValidMats.caption = "Valid Materials found: " + (matList.count as string) 
			lbl_ValidMats.pos = [5,30]
		)
	
		on ui_start pressed do
		(
			progStep = 100.0 / (matList.count)
			for m = 1 to matList.count do 
			(
				SetDiffuse matList[m]
				ui_prog.value = m * progStep
			)
			ui_prog.value = 0
		)
		
		on ui_ToggleShow changed state do
		(
			progStep = 100.0 / (matList.count)
			if state == true do
			(
				for m = 1 to matList.count do 
				(
					showTextureMap matList[m] true
					ui_prog.value = m * progStep
				)
			)
			if state == false do
			(
				for m = 1 to matList.count do 
				(
					showTextureMap matList[m] false
					for tm = 1 to matList[m].maps.count do
					(
						if matList[m].maps[tm] != undefined then showTextureMap matList[m] matList[m].maps[tm] false
					)
					ui_prog.value = m * progStep
				)
			)
			ui_prog.value = 0
		)
		
		on ui_FromScene changed state do
		(
			ui_FromScene.state = true
			ui_FromObj.state = false
			ui_FromMat.state = false
			getMaterialList SceneMaterials
		)
		
		on ui_FromObj changed state do
		(
			obj = getCurrentSelection()
			if obj[1] != undefined then
			(
				ui_FromScene.state = false
				ui_FromObj.state = true
				ui_FromMat.state = false
				mat = obj[1].material
				newMatList = #()
				append newMatList mat
				getMaterialList newMatList
			)
			else ui_FromObj.state = false
		)
		
		on ui_FromMat changed state do
		(
			if matEditor.isOpen() == false then MatEditor.Open()
			
			ui_FromScene.state = false
			ui_FromObj.state = false
			ui_FromMat.state = true
			
			curSlot = medit.GetActiveMtlSlot()
			mat = getMeditMaterial curSlot
			newMatList = #()
			append newMatList mat
			getMaterialList newMatList
		)
	
		on ro_DiffuseMapper close do gc()
		on ro_DiffuseMapper open do getMaterialList SceneMaterials
	)
	
	createDialog ro_DiffuseMapper width:150
)
