//********VARIABLES***************
//Note: the variables in this zscript are not used outside their local scope
//so it's not actually necessary to declare them here
[VarDef,result,0]
[VarDef,perCent,0]
[VarDef,mapSize,0]
[VarDef,tmpTool,0]
[VarDef,tmpTxtr,0]
[VarDef,tmpCol,0]
[VarDef,txtrName,""]
[VarDef,totalPolys,""]
[VarDef,maxMesh,0]
[VarDef,maxMeshStr,""]
[VarDef,milStr,""]
[VarDef,thouStr,""]
[VarDef,newStr,""]


//********ROUTINES***************

//routine to check mesh is in Edit mode
[RoutineDef,CheckEdit,
	[If,[IsEnabled,Transform:Edit],
		[If,[IGet,Transform:Edit],,
		 //no mesh in edit mode - exit		
		[Exit]
		]
		,		
		//no mesh selected - exit
		[Exit]
	]
]//end routine

//routine to check if model has UVs and if a texture is assigned
[RoutineDef,CheckTxtr,
  [If,[IExists,Tool:UV Map:Delete UV],//if this 3D object can have UVs
		[If,[IsEnabled,Tool:UV Map:Delete UV],,
		[Note,"Mesh does not have UVs assigned",,,4737096]
		[Exit]
		]
		,		
		//not a polymesh
		[Exit]
	]
	[If,[IsEnabled,Tool:Texture Map:Texture On],//if there's a map on the model
	  [NoteIButton,"\CffffffOK"][NoteIButton,"\Cff9923Cancel"]
		[VarSet,result,[Note,"\Cff9923\n WARNING: \CffffffThe mesh has a texture applied. \Cff9923The map will be lost if you continue.\n",,,4737096]]
		[If,result!=1,[Exit]]				
	]	
]

//********INTERFACE***************

//create a sub-palette (the Misc Utilities should already be there
//so this is just a safety precaution)
[ISubPalette,"ZPlugin:Misc Utilities"]

//create our plugin button
[IButton,"ZPlugin:Misc Utilities:Max Detail","Get the maximum detail in polygons that the current UVs & map size will support",
  [VarSet,perCent,0]//for note output
  //check edit mode
  [RoutineCall,CheckEdit]
  //check if tool has a texture assigned
   [RoutineCall,CheckTxtr]
   //get current settings
  [VarSet,mapSize,[IGet,Tool:UV Map:UV Map Size]]
  [VarSet,tmpTool,[IGet,Tool:Item Info]]
  [VarSet,tmpTxtr,[IGet,Texture:Item Info]]
  [VarSet,tmpCol,[IGet,Color:Main Color]]
  //get a texture map we can use
  [IPress,Tool:Texture Map:New From UV Check]
  [IPress,Tool:Texture Map:Clone Txtr]
  //turn off the texture map
  [IPress,Tool:Texture Map:Texture Map]
  [IClick,"Popup:Texture Off"] 
  //now work with our texture
  //make sure it's selected   
  [ISet,Texture:Item Info,[IGetMax,Texture:Item Info]]
  //change the gray to white
  [IColorSet,255,255,255]
  [IPress,Texture:Grad]
  //reset active color
  [ISet,Color:Main Color,tmpCol]
  //resord the texture name so we can select it
  [VarSet,txtrName,[IGetTitle,Texture:Item Info]]
  //reset texture in Texture palette
  [ISet,Texture:Item Info,tmpTxtr]
  //trim off the period that [IGetTitle] used above gives us
  [VarSet,txtrName,[StrExtract,txtrName,0,[StrLength,txtrName]-2]]
  //load our plane if necessary
  [If,[IExists,Tool:MaxDetailPlane],    
    [IPress,Tool:MaxDetailPlane]
    ,
    [If,[FileExists,"MaxDetailData/MaxDetailPlane.ztl"],
      [FileNameSetNext,"MaxDetailData/MaxDetailPlane.ztl"]
      [IPress,Tool:Load Tool]
      //divide so we have enough detail
      [Loop,5,
        [IPress,Tool:Geometry:Divide]
      ]
    ,//our plane ztl is missing - exit
      [Note,"The MaxDetailPlane.ztl could not be found. Please re-install the plugin.",,,4737096]
		  [Exit]
    ]
  ]
  //assign our texture to the plane  
  [IPress,Tool:Texture Map:Texture Map]
  [IClick,[StrMerge,"Popup:",#txtrName]]
  //use it to mask the plane  
  [IPress,Tool:Masking:Mask By Intensity]
  //get the total number of polys our plane has
  [Mesh3DGet,1,,,totalFaces]
  //hide the masked bit
  [IPress,Tool:Visibility:Hide Pt]
  //find out how many polys are hidden by reading the pop-up info
  [VarSet,totalPolys,[StrExtract,[IGetInfo,Tool:Current Tool],12,256]]	
	//find the string we want
	[If,[StrFind,"HiddenPolys=",totalPolys]>-1,
		[VarSet,totalPolys,[StrExtract,totalPolys,([StrFind,"HiddenPolys=",totalPolys]+12),([StrFind,"HiddenPoints=",totalPolys]-2)]]
		//use the result to set our percentage
		[If,[Val,totalPolys] > 0,
		  [VarSet,perCent,(totalPolys/totalFaces)]			  
		]
	]
	//find out how many polys we can use
	[VarSet,maxMesh,INT((mapSize*mapSize)*perCent)]
	//convert it to a readable string with commas for millions/thousands
	[VarSet,maxMeshStr,maxMesh]	
	[If,[StrLength,maxMeshStr]>3,
	  [VarSet,thouStr,[StrMerge,[StrExtract,maxMeshStr,[StrLength,maxMeshStr]-6,[StrLength,maxMeshStr]-4],[StrFromAsc,44],[StrExtract,maxMeshStr,[StrLength,maxMeshStr]-3,255]]]
	 	[If,[StrLength,maxMeshStr]>6,  		 
		  [VarSet,milStr,[StrMerge,[StrExtract,maxMeshStr,0,[StrLength,maxMeshStr]-7],[StrFromAsc,44]]]
		  [VarSet,newStr,[StrMerge,milStr,thouStr]]
		,
		  [VarSet,newStr,thouStr]
		]
		, 
		[VarSet,newStr,maxMesh]		 
	]
	//convert percentage to whole number		  
	[VarSet,perCent,perCent*100]
	[VarSet,perCent,INT(perCent+0.5)]
	//reset plane
	[IPress,Tool:Visibility:ShowPt]
	[IPress,Tool:Masking:Clear]
	//reset tool
	[ISet,Tool:Item Info,tmpTool]
	//display results
	[Note,[StrMerge,"\Cff9923UV map size = \Cffffff",#mapSize," x ",#mapSize,"\Cff9923 pixels\n"],,-1]
	[Note,[StrMerge,"\Cff9923UV space used = \Cffffff",#perCent,"%\Cff9923 of total map area\n"],,-1]
	[Note,[StrMerge,"\Cff9923Maximum detail at highest level = \Cffffff",#newStr,"\Cff9923 polys"],,,4737096]

,,.5]//end button (.5 is half a palette width)
