rollout CollisionsRollOut "Collisions" rolledUp:true (
	-- init GUI
	include "$scripts\\pteroTools\\_propGroupUI.ms"
	checkbox		cbxWPObst	"Way Points Obstacle"							align:#left
	dropdownlist	drpDefMat	"Default Surface:"		width:144	height:20	align:#center	items:#("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789012345")
	dropdownlist	drpColShp	"Shape for Player/AI:"	width:144	height:10	align:#center		
	dropdownlist	drpMslShp	"Shape for Missiles:"	width:144	height:7	align:#center

	-- Init RollOut
	fn fnName		= CollisionsRollOut
	fn fnLabel		= "Collisions"
 	fn fnKeyNames	= #( "Phy_WPObstacle", "Phy_defmat", "Phy_colshp", "Phy_misshp" )
 	fn fnKeyUI		= #( #( cbxWPObst ), #( drpDefMat ), #( drpColShp ), #( drpMslShp ) )
 	fn fnKeyDef		= #( #( 0 ), #( "- " ), #( -1 ), #( -1 ) )
	fn fnInitRollOut = (
		fnAppendRollOut (fnName()) (fnLabel())
	)
 	
	-- fnApply()
	fn fnApply obj keyIDX type acqObj = (
		local keys		= _keyTableDef (fnKeyNames()) (fnKeyUI()) (fnKeyDef())
		local keyArray	= _keyDef keys.names[keyIDX] (keys.names[keyIDX] + "=undefined") #("undefined")

		---- fnFindMat()
		fn fnFindMat val keyUI:keys.controls keyIDX:keyIDX = (
			for n = 1 to (keyUI[keyIDX][1].items).count do
				if (subString keyUI[keyIDX][1].items[n] 1 2) == val do return n
			return 1
		)
				
		-- SAVE PRESSET
		if type == #save do (
			----
			if keyIDX == 1 do keys.values[keyIDX][1] = if keys.controls[keyIDX][1].checked then 1 else 0
			if keyIDX == 2 do keys.values[keyIDX][1] = substring keys.controls[keyIDX][1].selected 1 2
			if keyIDX >= 3 do keys.values[keyIDX][1] = keys.controls[keyIDX][1].selection - 2
			----
			fnSetDefs (keys.names[keyIDX] as string) (keys.values[keyIDX] as string)
			return true
		)

		if obj != undefined do (
			-- DELETE
			if type == #delete do (
				fnDeleteProp obj keys.names[keyIDX]
				type = #read
			)
			-- ACQUIRE or READ
			keyArray = fnReadProp (if type == #acquire then acqObj else obj) keys.names[keyIDX]
		)
			
		-- UPDATE UI
		if type == #read or type == #load or type == #acquire do (
			-- LOAD PRESSET
			if type == #load do keyArray.values = fnGetDefs keys.names[keyIDX]
			----
			if (keyArray.values[1] == "") or (keyArray.values[1] as string == "undefined") do
				keyArray.values = keys.values[keyIDX]
			if keyIDX == 1 do keys.controls[keyIDX][1].checked		= if keyArray.values[1] == 1.0 then true else false
			if keyIDX == 2 do keys.controls[keyIDX][1].selection	= fnFindMat keyArray.values[1]
			if keyIDX >= 3 do if keyArray.values[1] as integer > -2 do keys.controls[keyIDX][1].selection	= keyArray.values[1] as integer + 2
			----
			if type == #read do return true
			if btnAuto.checked do type = #write
		)

		-- WRITE
		if type == #write do (
			keyArray.keyName = keys.names[keyIDX]
			----
			if keyIDX == 1 do if keys.controls[keyIDX][1].checked == false		then type = #delete else keyArray.values[1] = "1"
			if keyIDX == 2 do if keys.controls[keyIDX][1].selection == 1		then type = #delete else keyArray.values[1] = substring keys.controls[keyIDX][1].selected 1 2
			if keyIDX >= 3 do if keys.controls[keyIDX][1].selection == 1		then type = #delete else keyArray.values[1] = ( keys.controls[keyIDX][1].selection - 2 ) as string
			----
			if obj != undefined do
				if type == #delete then
					fnDeleteProp obj keys.names[keyIDX]
				else
					fnWriteprop obj keyArray
			return true
		)
	)
	
	-- fnUpdate()
	fn fnUpdate keyIDX:1 type:#none acqObj:undefined inner:true = (
		if btnAuto.checked and type == #none do type = #write

		case selection.count of (
			0:		 fnApply undefined keyIDX type acqObj
			default: for obj in selection do fnApply obj keyIDX type acqObj
		)
			
		if inner or type == #acquire do _mainRollout.name.fnUtilize (fnName()) info:true
	)

	-- Event Handler
	on cbxWPobst			changed state	do fnUpdate keyIDX:1
	on drpDefMat			selected item	do fnUpdate keyIDX:2
	on drpColShp			selected item	do fnUpdate keyIDX:3
	on drpMslShp			selected item	do fnUpdate keyIDX:4
	on btnSet				pressed			do for n = 1 to (fnKeyNames()).count do fnUpdate keyIDX:n type:#write
	on btnDef				pressed			do for n = 1 to (fnKeyNames()).count do fnUpdate keyIDX:n type:#load
	on btnSav				pressed			do for n = 1 to (fnKeyNames()).count do fnUpdate keyIDX:n type:#save
	on btnDel				pressed			do for n = 1 to (fnKeyNames()).count do fnUpdate keyIDX:n type:#delete
	on btnAcq				changed state	do fnAcquire (fnName()) keys:((fnKeyNames()).count)
	on CollisionsRollOut	open			do (
		drpDefMat.items		= _colMatTable --fnLoadMats()
		drpColShp.items		= #("- None -", "Linked", "Bounding Box", "Render Faces", "Bounding Sphere", "Crazy Bounding Sphere", "Point", "Crazy point")
		drpMslShp.items		= #("- None -", "Linked", "Bounding Box", "Render Faces", "Bounding Sphere")
		fnOpenRollOut (fnName()) keys:((fnKeyNames()).count)
	)
	on CollisionsRollOut	close			do fnCloseRollOut (fnName())
)
