rollout LightingRollOut "Lighting" rolledUp:true (
	-- init GUI
	include "$scripts\\pteroTools\\_propGroupUI.ms"
	spinner		spnAmbMult	"Ambient Mult.:"		range:[0, 99, 0]	type:#float		FieldWidth:35					align:#right
	spinner		spnDirMult	"Directional Mult.:"	range:[0, 99, 0]	type:#float		FieldWidth:35					align:#right
	colorPicker clrLight	""															width:60		height:60		align:#left
	spinner		spnClrR		"R:"					range:[0, 254, 0]	type:#integer	FieldWidth:35	offset:[0,-64]	align:#right
	spinner		spnClrG		"G:"					range:[0, 254, 0]	type:#integer	FieldWidth:35					align:#right
	spinner		spnClrB		"B:"					range:[0, 254, 0]	type:#integer	FieldWidth:35					align:#right

	-- Init RollOut
	fn fnName		 = LightingRollOut
	fn fnLabel		 = "Lighting"
 	fn fnKeyNames	 = #( fnLabel() )
 	fn fnKeyUI		 = #( #(spnAmbMult, spnDirMult, spnClrR, spnClrG, spnClrB) )
 	fn fnKeyDef		 = #( #( 0, 0, 0, 0, 0 ) )
	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")
		
		-- SAVE PRESSET
		if type == #save do (
			----
			for n = 1 to 5 do keys.values[keyIDX][n] = keys.controls[keyIDX][n].value
			----
			fnSetDefs ( keys.names[keyIDX] as string ) ( keys.values[keyIDX] as string )
			return true
		)

		if obj != undefined do (
			-- DELETE
			if type == #delete do (
				clrLight.color = [ 0, 0, 0 ] as color
				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]
			----
			for n = 1 to 5 do (
				if (keyArray.values[n] == "") or (keyArray.values[n] as string == "undefined") do
					keyArray.values[n] = keys.values[keyIDX][n]
				if n <= 2 do keys.controls[keyIDX][n].value = keyArray.values[n] as float
				if n >= 3 do keys.controls[keyIDX][n].value = keyArray.values[n] as integer
			)
			----
			if type == #read do return true
			if btnAuto.checked do type = #write
		)

		-- WRITE
		if type == #write do (
			keyArray.keyName = keys.names[keyIDX]
			----
			type = #delete
			for n = 1 to 5 do (
				keyArray.values[n] = keys.controls[keyIDX][n].value as string
				if keys.controls[keyIDX][n].value > 0 do type = #write
			)
			----
			if obj != undefined do
				if type == #delete then (
					clrLight.color = [ 0, 0, 0 ] as color
					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

		fnColor (fnName()) type:false keyIDX:keyIDX
	)

	-- Event Handler
	on spnAmbMult	entered 		do fnUpdate()
	on spnDirMult	entered 		do fnUpdate()
	on spnClrR		entered 		do (fnColor (fnName()) type:false; fnUpdate())
	on spnClrG		entered 		do (fnColor (fnName()) type:false; fnUpdate())
	on spnClrB		entered 		do (fnColor (fnName()) type:false; fnUpdate())
	on clrLight		changed value	do (fnColor (fnName()) val:value; fnUpdate())
	on btnSet		pressed			do fnUpdate type:#write
	on btnDef		pressed			do fnUpdate type:#load
	on btnSav		pressed			do fnUpdate type:#save
	on btnDel		pressed			do fnUpdate type:#delete
	on btnAcq		changed state	do fnAcquire (fnName())
	on LightingRollOut	open		do (
		fnColor (fnName()) type:false
		fnOpenRollOut (fnName())
	)
	on LightingRollOut	close		do fnCloseRollOut (fnName())
)
