rollout Lod2RollOut "LOD Adv." rolledUp:true (
	-- init GUI
	include "$scripts\\pteroTools\\_propGroupUI.ms"
	spinner		spnClipDist	"Clip Dist:"		range:[0, 65535, 0]	type:#float		FieldWidth:50	offset:[45,0]	align:#right	across:2
	checkbutton	btnTpeClip	"Tape"				images:(fnReturnFilename #tpeIcon)	width:18		height:16		align:#right	offset:[-3,0]		toolTip:"Measure Clip Distance ..."	enabled:false
	spinner		spnDolDist	"DOL Dist:"			range:[0, 65535, 0]	type:#float		FieldWidth:50	offset:[45,0]	align:#right	across:2
	checkbutton	btnTpeDol	"Tape"				images:(fnReturnFilename #tpeIcon)	width:18		height:16		align:#right	offset:[-3,0]		toolTip:"Measure DOL Distance ..."		enabled:false
	checkbox	cbxDolAlpha	"DOL No Alpha"		enabled:false														align:#center

	-- Init RollOut
	fn fnName		= Lod2RollOut
	fn fnLabel		= "LOD Adv."
 	fn fnKeyNames	= #( "ClipDist", "Dol", "DolNoAlpha" )
 	fn fnKeyUI		= #( #( spnClipDist ), #( spnDolDist ), #( cbxDolAlpha ) )
 	fn fnKeyDef		= #( #( 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 (
			----
			if keyIDX <= 2 do keys.values[keyIDX][1] = keys.controls[keyIDX][1].value
			if keyIDX == 3 do keys.values[keyIDX][1] = if keys.controls[keyIDX][1].checked then 1 else 0
			----
			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]
				----
				if keyIDX == 3 do keys.controls[3].enabled = false
				----
				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 <= 2 do if keyArray.values[1] as float > 0 do keys.controls[keyIDX][1].value = keyArray.values[1] as float
			if keyIDX == 3 do keys.controls[keyIDX][1].checked = if keyArray.values[1] as integer == 1 then true else false
			if keyIDX == 2 do if keys.controls[keyIDX][1].value == 0 then (
				keys.controls[keyIDX + 1].checked = false
				keys.controls[keyIDX + 1].enabled = false
			) else 
				keys.controls[keyIDX + 1].enabled = true
			----
			if type == #read do return true
			if btnAuto.checked do type = #write
		)

		-- WRITE
		if type == #write do (
			keyArray.keyName = keys.names[keyIDX]
			----
			if keyIDX <= 2 do if keys.controls[keyIDX][1].value == 0		then type = #delete else keyArray.values[1] = keys.controls[keyIDX][1].value as string
			if keyIDX == 3 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].value == 0		then (
				keys.controls[keyIDX + 1].checked = false
				keys.controls[keyIDX + 1].enabled = false
			) else 
				keys.controls[keyIDX + 1].enabled = true
			----
			if obj != undefined do (
				if type == #delete then (
					fnDeleteProp obj keys.names[keyIDX]
					----
					if keyIDX == 2 do fnDeleteProp obj keys.names[3]
					----
				) 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 spnClipDist		entered			do fnUpdate keyIDX:1
	on spnDolDist		entered			do fnUpdate keyIDX:2
	on cbxDolAlpha		changed state	do fnUpdate keyIDX:3
	on btnAcq			changed state	do fnAcquire (fnName()) keys:((fnKeyNames()).count)
	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 Lod2RollOut		open			do fnOpenRollOut (fnName())
	on Lod2RollOut		close			do fnCloseRollOut (fnName())
)
