rollout LadderRollOut "Ladder" rolledUp:true (
	-- init GUI
	include "$scripts\\pteroTools\\_propGroupUI.ms"
	checkbox	cbxLadder	"Is Ladder"

	-- Init RollOut
	fn fnName		= LadderRollOut
	fn fnLabel		= "Ladder"
 	fn fnKeyNames	= #( fnLabel() )
 	fn fnKeyUI		= #( #( cbxLadder ) )
 	fn fnKeyDef		= #( #( 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 == 1 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]
				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 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 then keyArray.values[1] = "1" else type = #delete
			----
			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 cbxLadder		changed state	do fnUpdate keyIDX:1
	on btnAcq			changed state	do fnAcquire (fnName())
	on btnSet			pressed			do fnUpdate keyIDX:1 type:#write
	on btnDef			pressed			do fnUpdate keyIDX:1 type:#load
	on btnSav			pressed			do fnUpdate keyIDX:1 type:#save
	on btnDel			pressed			do fnUpdate keyIDX:1 type:#delete
	on LadderRollOut	open			do fnOpenRollOut (fnName())
	on LadderRollOut	close			do fnCloseRollOut (fnName())
	
)
