rollout PhysicsRollOut "Physics" rolledUp:true (
	-- init GUI
	include "$scripts\\pteroTools\\_propGroupUI.ms"
		spinner			spnWeight	"Weight:"			range:[0, 65535, 0]	type:#float		FieldWidth:50	offset:[-20,0]	align:#right
		spinner			spnDensity	"Density:"			range:[0, 65535, 0]	type:#float		FieldWidth:50	offset:[-20,0]	align:#right
		spinner			spnInertia	"MoI:"				range:[0, 65535, 0]	type:#float		FieldWidth:50	offset:[-20,0]	align:#right
		checkbox		cbxDefMoi	"Default Moment of Inertia"																align:#left
		dropdownlist	drpStatus	"Type:"													width:144		height:7		align:#center 	items:#("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789012345")

	-- Init RollOut
	fn fnName		= PhysicsRollOut
	fn fnLabel		= "Physics"
 	fn fnKeyNames	= #( "Phy_weight", "Phy_density", "Phy_moment", "Phy_status" )
 	fn fnKeyUI		= #( #( spnWeight ), #( spnDensity ), #( spnInertia ), #( drpStatus ) )
 	fn fnKeyDef		= #( #( 0 ), #( 0 ), #( 0 ), #( -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")

		-- SAVE PRESSET
		if type == #save do (
			----
			if keyIDX <= 3 do keys.values[keyIDX][1] = keys.controls[keyIDX][1].value
			if keyIDX == 4 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 <= 3 do if keyArray.values[1] as float > 0 do		keys.controls[keyIDX][1].value		= keyArray.values[1] as float
			if keyIDX == 4 do if keyArray.values[1] as integer > -2 do	keys.controls[keyIDX][1].selection	= keyArray.values[1] as integer + 2
			cbxDefMoi.checked = if ( spnWeight.value * 2 ) == spnInertia.value then true else false
			spnInertia.enabled = not cbxDefMoi.checked
			----
			if type == #read do return true
			if btnAuto.checked do type = #write
		)

		-- WRITE
		if type == #write do (
			keyArray.keyName = keys.names[keyIDX]
			----
			if keyIDX <= 3 do if keys.controls[keyIDX][1].value == 0		then type = #delete else keyArray.values[1] = keys.controls[keyIDX][1].value as string
			if keyIDX == 4 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
	)

	-- fnWeight()
	fn fnWeight value = (
		if cbxDefMoi.checked do (
			spnInertia.value = spnWeight.value * 2
			fnUpdate keyIDX:3
		)
		fnUpdate keyIDX:1
	)

	-- fnDefMoi()
	fn fnDefMoi state = (
		spnInertia.enabled	= not state
		spnInertia.value	= spnWeight.value * 2
		fnUpdate keyIDX:3
	)
	
	-- Event Handler
	on spnWeight		entered			do fnWeight value
	on spnDensity		entered			do fnUpdate keyIDX:2
	on spnInertia		entered			do fnUpdate keyIDX:3 type:(if spnInertia.value > 0 then #none else #delete)
	on cbxDefMoi		changed state	do fnDefMoi state
	on drpStatus		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 PhysicsRollOut	open			do (
-- TODO: zjistit jak to je doopravdy s 'Trash' Status
		drpStatus.items		= #("- None -", "+ Collision, - Synchronize", "+ Collision, + Synchronize", "Trash, + Collide with PC and NPC Only, - Synchronize", "Trash, - Collide, - Synchronize")
		fnOpenRollOut (fnName()) keys:(fnKeyNames()).count
	)
	on PhysicsRollOut	close			do fnCloseRollOut (fnName())
)
