rollout LodRollOut "LOD" rolledUp:true (
	-- init GUI
	include "$scripts\\pteroTools\\_propGroupUI.ms"
	spinner		spnLodDist	"Switch Dist.:"		range:[0, 65535, 0]	type:#float		FieldWidth:50				align:#right	offset:[45,0]	across:2
	checkbutton	btnTpeLod	"Tape"				images:(fnReturnFilename #tpeIcon)	width:18		height:16	align:#right	offset:[-3,0]	toolTip:"Measure LOD Distance ..."		enabled:false
	listbox		lbxDistList	""														width:49		height:5	align:#left		offset:[55,0]
	label		lbl1		""														width:30		height:65					offset:[-30,-75]
	button		btnAddDist	"Add"													width:30		height:16	align:#right	offset:[0,-71]	toolTip:"Add LOD Change Distance"
	button		btnChngDist	"Chng"													width:30		height:16	align:#right	offset:[0,0]	toolTip:"Change LOD Change Distance"
	button		btnRemDist	"Del"													width:30		height:16	align:#right	offset:[0,12]	toolTip:"Remove LOD Change Distance"
	checkbox	cbxLodHide	"Last LOD Hide"																						offset:[0,0]
	checkbox	cbxLodAlpha	"Fade Out"																			align:#left		offset:[24,0]
	spinner		spnAlphMult	"Alpha Mult.:"		range:[0.0, 1.0, 0.8]	type:#float	FieldWidth:35				align:#right	scale:0.1

	-- fnInitRollOut()
	fn fnName		= LodRollOut
	fn fnLabel		= "LOD"
 	fn fnKeyNames	= #( "Lod", "LastLodAlpha", "AlphaMult" )
 	fn fnKeyUI		= #( #(), #(cbxLodAlpha), #(spnAlphMult) )
 	fn fnKeyDef		= #( #(), #(0), #(0.8) )
	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")
		local check		= false
		local items		= #()
--		fnWritePropEnhanced $ strArray

		-- SAVE PRESSET
		if type == #save do (
			----
			if keyIDX == 1 do (
				keys.values[keyIDX]  = for n in lbxDistList.items collect n as float
				if not cbxLodHide.checked do append keys.values[keyIDX] -1
			)
			if keyIDX == 2 do keys.values[keyIDX][1] = if keys.controls[keyIDX][1].checked then 1 else 0
			if keyIDX == 3 do keys.values[keyIDX][1] = keys.controls[keyIDX][1].value
			----
			fnSetDefs (keys.names[keyIDX] as string) (keys.values[keyIDX] as string)
			return true
		)
		
		if obj != undefined do (
			-- DELETE
			if type == #delete do (
				if keyIDX == 1 then
					fnWritePropEnhanced obj keyArray delete:true
				else
					fnDeleteProp obj keys.names[keyIDX]
				type = #read
			)
			
			-- ACQUIRE or READ
			if keyIDX == 1 then
				keyArray = fnReadPropEnhanced (if type == #acquire then acqObj else obj) keys.names[keyIDX]
			else
				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 (
				if keyArray.values[1] != undefined do (
--					format "%\n" keyArray.values
					if keyArray.values[keyArray.values.count] as integer == -1 do (
						check = true
						deleteItem keyArray.values keyArray.values.count
					)
					for n in keyArray.values do
						if n > 0 do append items (n as string)
				)
				
				lbxDistList.items	= items
				cbxLodHide.checked	= not check
				spnLodDist.value	= 0
			)
			if keyIDX == 2 do keys.controls[keyIDX][1].checked = if keyArray.values[1] as integer == 1 then true else false
			if keyIDX == 3 do keys.controls[keyIDX][1].value   = keyArray.values[1] as float
			----
			if type == #read do return true
			if btnAuto.checked do type = #write
		)

		-- WRITE
		if type == #write do (
			for keyIDX = 1 to keys.names.count do (
				keyArray.keyName = keys.names[keyIDX]
				----
				if keyIDX == 1 do (
					keyArray.values = for n in lbxDistList.items collect n
					if not cbxLodHide.checked do append keyArray.values "-1"
					if lbxDistList.items.count == 0 do type = #delete
				)
				if keyIDX == 2 do if keys.controls[keyIDX][1].checked == false or not cbxLodAlpha.enabled then type = #delete else keyArray.values = #("1")
				if keyIDX == 3 do if keys.controls[keyIDX][1].value == 0.8 or not spnAlphMult.enabled then type = #delete else keyArray.values = #(keys.controls[keyIDX][1].value as string)
				----
				if obj != undefined do
					if keyIDX == 1 then
						fnWritePropEnhanced obj keyArray delete:(if type == #delete then true else false)
					else
						if type == #delete then
							fnDeleteProp obj keys.names[keyIDX]
						else
							fnWriteprop obj keyArray
			)
			return true
		)
	)
	
	-- fnUtilizeList()
	fn fnUtilizeList type = (
		local items = for n in lbxDistList.items collect n as float
		local sel	= lbxDistList.selection
		local dupli	= findItem items spnLodDist.value
		local txt	= ""
		
		if spnLodDist.value == 0 do dupli = 1
		case type of (
			#add:		if dupli == 0 do append items spnLodDist.value
			#change:	if sel > 0 and dupli == 0 do items[sel] = spnLodDist.value
			#remove:	if sel > 0 do deleteItem items sel
			default:	()
		)
		
		items = sort items
		items = for n in items collect n as string
		sel   = if type == #remove then
					if sel > items.count then items.count else sel
				else
					findItem items (spnLodDist.value as string)
		for n = 1 to (if items.count <= 5 then items.count else 5) do txt += "Lod =\n"
		
		lbxDistList.items	  = items
		lbxDistList.selection = sel
		lbl1.text			  = txt
	)
	
	-- fnUpdate()
	fn fnUpdate keyIDX:1 type:#none acqObj:undefined inner:true = (
		if btnAuto.checked and type == #none do type = #write

		cbxLodAlpha.enabled = cbxLodHide.checked
		spnAlphMult.enabled = if cbxLodAlpha.enabled then cbxLodAlpha.checked else false

		case selection.count of (
			0:		 fnApply undefined keyIDX type acqObj
			default: for obj in selection do fnApply obj keyIDX type acqObj
		)
		
		if type != #write and type != #save do fnUtilizeList #none
		if inner or type == #acquire do _mainRollout.name.fnUtilize (fnName()) info:true
	)
	
	-- Event Handler
	on lbxDistList	selected state	do spnLodDist.value = lbxDistList.selected as float
	on cbxLodHide	changed state	do fnUpdate keyIDX:1
	on cbxLodAlpha	changed state	do fnUpdate keyIDX:2
	on spnAlphMult	entered			do fnUpdate keyIDX:3
	on btnAddDist	pressed			do (fnUtilizeList #add; fnUpdate keyIDX:1)
	on btnChngDist	pressed			do (fnUtilizeList #change; fnUpdate keyIDX:1)
	on btnRemDist	pressed			do (fnUtilizeList #remove; fnUpdate keyIDX:1)
	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 LodRollOut	open			do fnOpenRollOut (fnName())
	on LodRollOut	close			do fnCloseRollOut (fnName())
)
