/*************************************************************************
	File:	wave_init.c

	FormZ data model translator sample.  Imports and exports Wavefront OBJ files.

	COPYRIGHT (C) 2001.  All Rights Reserved.
	auto*des*sys, Inc., Columbus, Ohio, USA.
*************************************************************************/


#include "wave_prvt.h"



/************************************************************************
	Set default values for custom export options and override default
	values for common export options.
*************************************************************************/
fzrt_error_td wave_write_opts_default(fz_ffmt_ref_td ffmt_id)
{
	fzrt_error_td 						err = FZRT_NOERR;
	fzrt_int								flags;
	fz_type_td 							fz_type;
	
	/* custom export options */
	_write_flags = WAVE_WRITE_USEABSOLUTEINDICES_FLAG;

	/* Override common export options */
	/* Get the options bit flags. */
	_ffmt_3d_funcs.fz_ffmt_data_model_write_opts_parm_get(ffmt_id, FZ_FFMT_DATA_MODEL_WRITE_OPTS_PARM_FLAGS, &fz_type);
	_type_funcs.fz_type_get_int(&fz_type, &flags);

	/* Set the Remove Holes flag. */
	FZ_SETBIT(flags, FZ_FFMT_DATA_MODEL_WRITE_OPTS_REMOVEHOLES_BIT);

	/* Set the modified options bit flags. */
	_type_funcs.fz_type_set_int(&flags, &fz_type);
	_ffmt_3d_funcs.fz_ffmt_data_model_write_opts_parm_set(ffmt_id, FZ_FFMT_DATA_MODEL_WRITE_OPTS_PARM_FLAGS, &fz_type);
	
	/* Override the transform options.  NOTE: The transform options are shared between import and export.
		So, the transform options defaults can be overridden either here or in the import options set
		defaults function.  But, the overriding of transform options does not have to be in both the 
		import and export set defaults functions. */
	/* Get the transform options bit flags. */
	_ffmt_3d_funcs.fz_ffmt_data_model_tform_opts_parm_get(ffmt_id, FZ_FFMT_DATA_MODEL_TFORM_OPTS_PARM_FLAGS, &fz_type);
	_type_funcs.fz_type_get_int(&fz_type, &flags);

	/* Set the Flip X flag. */
	FZ_SETBIT(flags, FZ_FFMT_DATA_MODEL_TFORM_OPTS_FLIPX_BIT);

	/* Set the Swap YZ flag. */
	FZ_SETBIT(flags, FZ_FFMT_DATA_MODEL_TFORM_OPTS_SWAPYZ_BIT);

	/* Set the Normals Point Outward flag. */
	FZ_SETBIT(flags, FZ_FFMT_DATA_MODEL_TFORM_OPTS_NORMALSOUTWARD_BIT);





												/* Enable the Normals Point Outward dialog item. */
												FZ_SETBIT(flags, FZ_FFMT_DATA_MODEL_TFORM_OPTS_ENABLENORMALSOUTWARD_BIT);





	/* Set the modified transform options bit flags. */
	_type_funcs.fz_type_set_int(&flags, &fz_type);
	_ffmt_3d_funcs.fz_ffmt_data_model_tform_opts_parm_set(ffmt_id, FZ_FFMT_DATA_MODEL_TFORM_OPTS_PARM_FLAGS, &fz_type);
	
	return(err);
}

/************************************************************************
	Set default values for custom import options and override default
	values for common export options.
*************************************************************************/
fzrt_error_td wave_read_opts_default(fz_ffmt_ref_td ffmt_id)
{
	fzrt_error_td						err = FZRT_NOERR;
	fzrt_int								flags;
	fz_type_td 							fz_type;

	/* custom import options */
	_read_flags = 0;

	/* Override common import options */
	/* Get the options bit flags. */
	_ffmt_3d_funcs.fz_ffmt_data_model_read_opts_parm_get(ffmt_id, FZ_FFMT_DATA_MODEL_READ_OPTS_PARM_FLAGS, &fz_type);
	_type_funcs.fz_type_get_int(&fz_type, &flags);

	/* Set the Remove Holes flag. */
	FZ_SETBIT(flags, FZ_FFMT_DATA_MODEL_READ_OPTS_PRESERVECONTROLS_BIT);

	/* Set the modified options bit flags. */
	_type_funcs.fz_type_set_int(&flags, &fz_type);
	_ffmt_3d_funcs.fz_ffmt_data_model_read_opts_parm_set(ffmt_id, FZ_FFMT_DATA_MODEL_READ_OPTS_PARM_FLAGS, &fz_type);

	return err;
}

/************************************************************************
	Given an error id, returns to formZ an error string associated with a targa error code.
*************************************************************************/
fzrt_boolean wave_error_str_func(fzrt_int err, char *str, short str_len)
{
	char	msg[256];

	/* Get the string form the plugin's resource file. */
	_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, WAVE_MESSAGES, err, msg);
	strncpy(str, msg, str_len);
	
	/* If we sucessfully got the string return TRUE otherwise return FALSE. */
	return(msg[0] == '\0' ? FALSE : TRUE);
}

/************************************************************************
	Adds custom items to the export options dialog.
*************************************************************************/
fzrt_error_td wave_out_dlog_cust_func(fz_fuim_tmpl_ptr fuim_tmpl, short parent, fz_ffmt_ref_td ref_id)
{
	short		gindx;
	char		title[256];

	/* Get a string for the Use Absolute Indices custom option item. */
	_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, WAVE_STRINGS, WAVE_ABSINDX_STR, title);
	
	/* Create a check box the Use Absolute Indices custom option item. */
	if((gindx = _fuim_funcs.fz_fuim_new_check(fuim_tmpl, parent, 0, FZ_FUIM_FLAG_GFLT | FZ_FUIM_FLAG_HORZ, title, NULL, NULL)) != -1)
	{
		/* Associate a bit flag variable with the check box. */
		_fuim_funcs.fz_fuim_item_encod_int(fuim_tmpl, gindx, &_write_flags , TRUE, FZ_FUIM_BIT2_MASK(WAVE_WRITE_USEABSOLUTEINDICES_BIT));
	}
	
	return(FZRT_NOERR);
}

/************************************************************************
	Adds custom items to the import options dialog.
*************************************************************************/
fzrt_error_td wave_in_dlog_cust_func(fz_fuim_tmpl_ptr fuim_tmpl, short parent, fz_ffmt_ref_td ref_id)
/************************************************************************/
{
	short		gindx;
	char		title[256];

	/* Get a string for the Use Groups To Form Objects custom option item. */
	_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, WAVE_STRINGS, WAVE_GRPASOBJ_STR, title);
	
	/* Create a check box the e Groups To Form Objects custom option item. */
	if((gindx = _fuim_funcs.fz_fuim_new_check(fuim_tmpl, parent, 0, FZ_FUIM_FLAG_GFLT | FZ_FUIM_FLAG_HORZ, title, NULL, NULL)) != -1)
	{
		/* Associate a bit flag variable with the check box. */
		_fuim_funcs.fz_fuim_item_encod_int(fuim_tmpl, gindx, &_read_flags , TRUE, FZ_FUIM_BIT2_MASK(WAVE_READ_USE_SIGNG_BIT));
	}
	
	return(FZRT_NOERR);
}

/************************************************************************
	Save and restore custom options.
*************************************************************************/
fzrt_error_td wave_opts_io(fz_iost_ptr iost, fz_iost_dir_td_enum dir, fzpl_vers_td * const version, fzrt_unsigned_int size)
{
	fzrt_error_td	err = FZRT_NOERR;
	
	if(dir == FZ_IOST_WRITE)	/* Write */
	{
		/* Set the version of the translator */
		*version = WAVE_PLUGIN_VERSION;
	}
	else	/* Read */
	{
		/* If the version is newer than this version of the translator, skip the block and return an error. */
		if(*version > WAVE_PLUGIN_VERSION)
		{
			_iost_funcs.fz_iost_block_skip(iost, size);
			err = _fzrt_base_funcs.fzrt_error_set_with_detail(FZPL_BAD_VERSION_ERROR, FZRT_ERROR_SEVERITY_WARNING, FZRT_ERROR_CONTEXT_FZRT, FZPL_CONTEXT_ID, 0 );
		}
	}

	if(err == FZRT_NOERR)
	{
		/* Perform the read or write. */
		err = _iost_funcs.fz_iost_int(iost, &_write_flags, 1);
		if(err == FZRT_NOERR) err = _iost_funcs.fz_iost_int(iost, &_read_flags, 1);
	}

	return(err);
}

/************************************************************************
	Returns the name of the translator to formZ.
*************************************************************************/
fzrt_error_td wave_get_name(char *fmt_name, fzrt_int fmt_name_max_len)
{
	if(fmt_name != NULL)
	{
		strncpy(fmt_name, "OBJ - SDK Sample", fmt_name_max_len);
	}

	return(FZRT_NOERR);
}

/************************************************************************
	Returns the id of the translator to formZ.
*************************************************************************/
fzrt_error_td wave_get_id(fzrt_UUID_td fmt_id)
{
	/* Just use the plugin id for the translator's id. */
	_fzrt_base_funcs.fzrt_UUID_copy(WAVE_PLUGIN_ID, fmt_id);

	return(FZRT_NOERR);
}

/************************************************************************
	Returns other information about the translator to formZ.
*************************************************************************/
fzrt_error_td wave_get_info(
			char			*sortby,
			fzrt_int			sortby_max_len,
			char			*file_ext,
			fzrt_int			file_ext_max_len,
			char			*keywd,
			fzrt_int			keywd_max_len,
			fzrt_int			*attr_flags
			)
/************************************************************************/
{
	fzrt_error_td					err = FZRT_NOERR;

	/* Set the string used to sort translators in menus. */
	strncpy(sortby, "obj", sortby_max_len);

	/* Set the format's file extension. */
	strncpy(file_ext, "obj", file_ext_max_len);

	/* Set the format's key word. */
	strncpy(keywd, WAVE_PLUGIN_KEYWD, keywd_max_len);

	/* Set the translator's attribute flags. */
	FZ_SETBIT(*attr_flags, FZ_FFMT_FORMAT_ATTR_CANBETEXT_BIT);
	FZ_SETBIT(*attr_flags, FZ_FFMT_FORMAT_ATTR_SUPPORTS_NURBS_BIT);
	
	return(err);
}

/************************************************************************
	wave_get_ftype:

	TAH 11/26/03: NEW FUNCTION
*************************************************************************/
fzrt_error_td wave_get_ftype(
			fzrt_ostype		*type,
			fzrt_ostype		*creator
			)
{
	/* Set the format's Macintosh file type. */
	*type = 'TEXT';

	/* Set the format's Macintosh file creator. */
	*creator = 'VTVS';
	
	return(FZRT_NOERR);
}

/************************************************************************
	Returns the file format's icon to formZ.
*************************************************************************/
fzrt_error_td wave_get_icon(
			fzrt_icon_ptr 	*icon
			)
{
	fzrt_int							err = FZRT_NOERR;
	short							rlib_index;

	if(icon != NULL)
	{
		/* Get the plugin's library file's runtime id. */
		_plugin_query_funcs.fzpl_plugin_get_rlib_idx(_wave_plugin_runtime_id, &rlib_index);

		/* Get the file format icon from the library file's resources. */
		err = _fzrt_base_funcs.fzrt_rlib_load_icon(rlib_index, FZRT_LOAD_ICON_COLOR, WAVE_ICON, icon);
	}
	
	return(err);
}

/************************************************************************
	Return to formZ an array of reference ids for all texture formats supported by this model format.
	formZ will call this function twice to get the array.  The first time the array (wave_list)
	will be NULL and this function needs to return the format count (wave_list_knt_ptr).
	formZ will use this count to allocate an array and call this function s second time
	to have the array filled.
*************************************************************************/
fzrt_error_td wave_2d_tmap_list(fz_ffmt_ref_td ffmt_id, fz_ffmt_ref_td *wave_list, fzrt_int *wave_list_knt_ptr)
{
	fzrt_int					knt, i, num, max;
	fz_ffmt_ref_td			*ffmt_ids = NULL;
	fzrt_error_td			err = FZRT_NOERR;

	knt = 0;
	max = 0;

	/* Use the TIFF keyword to get the count of tiff translators formZ has installed. */
	if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(TIFF_PLUGIN_KEYWD, NULL, &num))
	{
		knt += num;
		if(num > max)	max = num;
	}

	/* Use the Targa keyword to get the count of targa translators formZ has installed. */
	if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(TGA_PLUGIN_KEYWD, NULL, &num))
	{
		knt += num;
		if(num > max)	max = num;
	}

	/* Use the JPEG keyword to get the count of JPEG translators formZ has installed. */
	if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(JPEG_PLUGIN_KEYWD, NULL, &num))
	{
		knt += num;
		if(num > max)	max = num;
	}

	/* Use the PNG keyword to get the count of PNG translators formZ has installed. */
	if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(PNG_PLUGIN_KEYWD, NULL, &num))
	{
		knt += num;
		if(num > max)	max = num;
	}
	
	/* If the array is not NULL fill it. */
	if(wave_list != NULL && max > 0)
	{
		/* Allocate memory for a working translator array. */
		_fzrt_mem_funcs.fzrt_new_ptr(max * sizeof(fz_ffmt_ref_td), (fzrt_ptr*)&ffmt_ids);
		if(ffmt_ids != NULL)
		{
			/* Reset the count. */
			knt = 0;

			/* Get the TIFF translator format reference ids. */
			if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(TIFF_PLUGIN_KEYWD, ffmt_ids, &num))
			{
				/* Copy ther reference ids into the array. */
				for(i = 0; i < num; i++)
				{
					wave_list[knt + i] = ffmt_ids[i];
				}
				knt += num;
			}

			/* Get the Targa translator format reference ids. */
			if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(TGA_PLUGIN_KEYWD, ffmt_ids, &num))
			{
				/* Copy ther reference ids into the array. */
				for(i = 0; i < num; i++)
				{
					wave_list[knt + i] = ffmt_ids[i];
				}
				knt += num;
			}

			/* Get the JPEG translator format reference ids. */
			if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(JPEG_PLUGIN_KEYWD, ffmt_ids, &num))
			{
				/* Copy ther reference ids into the array. */
				for(i = 0; i < num; i++)
				{
					wave_list[knt + i] = ffmt_ids[i];
				}
				knt += num;
			}

			/* Get the PNG translator format reference ids. */
			if(_ffmt_funcs.fz_ffmt_keyword_to_ref_id_list(PNG_PLUGIN_KEYWD, ffmt_ids, &num))
			{
				/* Copy ther reference ids into the array. */
				for(i = 0; i < num; i++)
				{
					wave_list[knt + i] = ffmt_ids[i];
				}
				knt += num;
			}

			/* Free memory for a working translator array. */
			_fzrt_mem_funcs.fzrt_dispose_ptr((fzrt_ptr)ffmt_ids);
		}
	}

	/* Set the texture map translator count. */
	if(wave_list_knt_ptr != NULL)	*wave_list_knt_ptr = knt;
	
	return(err);
	
}

/************************************************************************
	Returns the translator's import capability flags to formZ.
*************************************************************************/
fzrt_error_td wave_read_opts_get_flags(fz_ffmt_ref_td ffmt_id, fzrt_int *flags, fzrt_int *opts_flags)
{
	fzrt_error_td					err = FZRT_NOERR;

	if(flags != NULL)
	{
		/* Set the bit flag to enable the formZ Units item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLEFORMZUNITS_BIT);

		/* Set the bit flag to enable the formZ Units item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLEFFMTUNITS_BIT);

		/* Set the bit flag to enable the Preserve Nurbz item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLEPRESERVENURBS_BIT);

		/* Set the bit flag to enable the Construct 3D Solids item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLECONSTRUCT3DSOLIDS_BIT);

		/* Set the bit flag to enable the Same Color Surfaces item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLESAMECOLOR_BIT);

		/* Set the bit flag to enable the Same Layer Surfaces item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLESAMELAYER_BIT);

		/* Set the bit flag to enable the Join Adjacent Coplanar Faces item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLEJOINCOPLANAR_BIT);

		/* Set the bit flag to enable the Color item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLECOLOR_BIT);

		/* Set the bit flag to enable the Import Texture Maps item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLEIMPORTTEXTUREMAPS_BIT);

		/* Set the bit flag to enable the Texture Options button. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLETEXTUREOPTS_BIT);

		/* Set the bit flag to enable the Parametric Options button. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_READ_IFACE_OPTS_ENABLEPARAMETRIC_BIT);
	}

	if(opts_flags != NULL)
	{
		/* Set the bit flag to enable the Options button on the standard file open dialog. */
		FZ_SETBIT(*opts_flags,	FZ_FFMT_OPTS_INIT_HAS_READ_OPTS_BIT);
	}

	return(err);
}

/************************************************************************
	Returns the translator's export capability bit flags to formZ.
*************************************************************************/
fzrt_error_td wave_write_opts_get_flags(fz_ffmt_ref_td ffmt_id, fzrt_int *flags, fzrt_int *opts_flags)
{
	fzrt_error_td					err = FZRT_NOERR;

	if(flags != NULL)
	{
		/* Set the bit flag to enable the Platform item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLELINETERMINATION_BIT);

		/* Set the bit flag to enable the Grouping Method item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_GROUPINGMETHOD_BIT);

		/* Set the bit flag to enable the Separate Files item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLESEPARATEFILES_BIT);

		/* Set the bit flag to enable the Transform button. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLETRANSFORM_BIT);

		/* Set the bit flag to enable the Visible Layers Only item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLEVISIBLELAYERS_BIT);

		/* Set the bit flag to enable the Subdivide Concave Faces item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLESUBDIVIDECONCAVES_BIT);

		/* Set the bit flag to enable the Triangulate Faces item and the Triangulation Options button. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLETRIANGULATE_BIT);

		/* Set the bit flag to enable the Preserve Controls item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLEPRESERVEFACECOLORS_BIT);

		/* Set the bit flag to enable the Include Normals item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLEINCLUDENORMALS_BIT);

		/* Set the bit flag to enable the Fix Smooth Shading item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLEFIXSMOOTHSHADING_BIT);

		/* Set the bit flag to enable the Angle item. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLEANGLE_BIT);

		/* Set the bit flag to enable the Textures item and the texture Options button. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLETEXTUREMAPS_BIT);

		/* Set the bit flag to enable the Non Planar Faces Only and the Triangulate All Faces items on the Triangulate Options dialog. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLETRIANGWHICH_BIT);

		/* Set the bit flag to enable the Strict Planarity Test item on the Triangulate Options dialog. */
		FZ_SETBIT(*flags,	FZ_FFMT_DATA_MODEL_WRITE_IFACE_OPTS_ENABLETRIANGSTRICT_BIT);
	}

	if(opts_flags != NULL)
	{
		/* Set the bit flag to enable the Options button on the standard file open dialog. */
		FZ_SETBIT(*opts_flags,	FZ_FFMT_OPTS_INIT_HAS_WRITE_OPTS_BIT);
	}

	return(err);
}


