/*
2014-06-05 fix "** system exception **" by ecximer
*/

macroScript CoronaMeshExport category:"Corona" tooltip:"Corona mesh export"

(

	try
	(
		destroydialog CorMeshExport
	)
	catch()
	start_selection = ""

	rollout CorMeshExport "Corona mesh export" width:375 height:140
	(
		
		
		editText edt_folder "Folder:" pos:[10,10] width:290 height:21
		button btn_folder "Browse" pos:[306,11] width:60 height:21
		editText edt_file "File:" pos:[24,36] width:276 height:21 readOnly: true
		--button btn_file "Browse" pos:[306,37] width:60 height:21
		checkbox chk_replacesourse "Replace source" pos:[10,65] width:93 height:21
		dropDownList ddl_previewmethod "Preview method" items:#("Solid bounding box","Wire bounding box","Point cloud","Full mesh") pos:[113,65] width:150 height:40
		checkbox chk_cachinRAM "Cache in RAM" pos:[10,85] width:87 height:21
		label label_PCD "Point cloud displayed [%]" pos:[270,65] width:100 height:40
		spinner spn_PCD "" pos:[310,85] width:50 height:16 range:[0.01,100,1] type:#float scale:1
		button btn_start "Start" pos:[306,110] width:60 height:21
		progressBar prg_progress pos:[10,112] width:290 height:17
		
		on CorMeshExport open do
		(
			start_selection = selection as array
			if start_selection.count != 0 then
			(
				edt_file.text = start_selection[1].name + ".cproxy"
				edt_folder.text = maxFilePath
			)
			else
			(
				destroydialog CorMeshExport
			)
		)
		
		on btn_folder pressed do
		(
			try
			(
				edt_folder.text = getSavePath "choose dir" initialDir:edt_folder.text
			)
			catch()
		)
		
		on btn_start pressed do
		(
			undo "CorMeshExport" on
			(
				obj_count = start_selection.count
				obj_current = 0
				for obj in start_selection do
				(
					tmp_proxy = CoronaProxy cacheInRam:(chk_cachinRAM.checked) previzType:(ddl_previewmethod.selection-1) displayVertexFraction:(spn_PCD.value) --displayIconSize:()
					CoronaProxy.ProxyFp.fromScene tmp_proxy obj (edt_folder.text+"\\"+obj.name+".cproxy")
					tmp_proxy.name = "crProxy_" + obj.name
					if (obj.material != undefined) then
					(
						tmp_proxy.material = obj.material
					)
					if chk_replacesourse.checked then
					(
						delete obj
					)
					
					obj_current += 1
					prg_progress.value = obj_current/(obj_count as float)*100
				)
			)
		)
	)

	createdialog CorMeshExport
)



if menuMan.registerMenuContext 425390867 then
(
	local quadMenu = menuMan.getViewportRightClickMenu #nonePressed
	local menu = quadMenu.getMenu 1
	local CoronaItem = menuMan.createActionItem  "CoronaMeshExport" "Corona"
	tmp_separator = menuMan.createSeparatorItem()
	menu.addItem tmp_separator -1
	menu.addItem CoronaItem -1
)