/*************************************************************************
	File:	pcld_init.c

	FormZ unstructured translator sample.  Imports and exports point cloud data.

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


#include "pcld_prvt.h"




/************************************************************************
	Sets the default export options.
*************************************************************************/
fzrt_error_td pcld_write_opts_default(void)
{
	fzrt_error_td 				err = FZRT_NOERR;

	_export_scale = 1.0;

	return(err);
}

/************************************************************************
	Sets the default import options.
*************************************************************************/
fzrt_error_td pcld_read_opts_default(void)
{
	fzrt_error_td 				err = FZRT_NOERR;

	_import_scale = 1.0;

	return(err);
}

/************************************************************************
	Given an error id, returns to formZ an error string associated with a targa error code.
*************************************************************************/
fzrt_boolean pcld_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, PCLD_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);
}

/************************************************************************
	Creates the export options dialog.
*************************************************************************/
fzrt_error_td pcld_write_opts(fz_fuim_tmpl_ptr fuim_tmpl)
{
	short			g1;
	char			str[256];
	fzrt_error_td	err;

	/* Get the string form the export options dialog title. */
	_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, PCLD_STRINGS, PCLD_EXPT_OPTS_TITLE, str);

	/* Create a dialog. */
	if((err = _fuim_funcs.fz_fuim_tmpl_init(fuim_tmpl, str, 0, FZRT_UUID_NULL, 0)) == FZRT_NOERR)
	{
		/* Get the string form the export options Scale item. */
		_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, PCLD_STRINGS, PCLD_SCALE_STR, str);

		/* Create a text edit the Scale option item. */
		_fuim_funcs.fz_fuim_new_text_static_edit(fuim_tmpl, -1, FZ_FUIM_NONE, str, FZ_FUIM_NONE,FZ_FUIM_FLAG_BRDR, NULL, NULL, &g1);			

		/* Associate a double variable with the text edit. */
		_fuim_funcs.fz_fuim_item_range_double(fuim_tmpl, g1, &_export_scale, 0.0, 0.0, FZ_FUIM_FORMAT_FLOAT_DEFAULT, FZ_FUIM_RANGE_MIN);
	}
	
	return(err);
}

/************************************************************************
	Creates the import options dialog.
*************************************************************************/
fzrt_error_td pcld_read_opts(fz_fuim_tmpl_ptr fuim_tmpl)
{
	short			g1;
	char			str[256];
	fzrt_error_td	err;

	/* Get the string form the export options dialog title. */
	_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, PCLD_STRINGS, PCLD_IMPT_OPTS_TITLE, str);

	/* Create a dialog. */
	if((err = _fuim_funcs.fz_fuim_tmpl_init(fuim_tmpl, str, 0, FZRT_UUID_NULL, 0)) == FZRT_NOERR)
	{
		/* Get the string form the export options Scale item. */
		_fzrt_base_funcs.fzrt_fzr_get_string(_fz_rsrc_ref, PCLD_STRINGS, PCLD_SCALE_STR, str);

		/* Create a text edit the Scale option item. */
		_fuim_funcs.fz_fuim_new_text_static_edit(fuim_tmpl, -1, FZ_FUIM_NONE, str, FZ_FUIM_NONE, FZ_FUIM_FLAG_BRDR, NULL, NULL, &g1);			

		/* Associate a double variable with the text edit. */
		_fuim_funcs.fz_fuim_item_range_double(fuim_tmpl, g1, &_import_scale, 0.0, 0.0, FZ_FUIM_FORMAT_FLOAT_DEFAULT, FZ_FUIM_RANGE_MIN);
	}
	
	return(err);
}

/************************************************************************
	Save and restore options.
*************************************************************************/
fzrt_error_td pcld_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 = PCLD_FMT_PLUGIN_VERSION;
	}
	else	/* Read */
	{
		/* If the version is newer than this version of the translator, skip the block and return an error. */
		if(*version > PCLD_FMT_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_double(iost, &_import_scale, 1);
		if(err == FZRT_NOERR)	err = _iost_funcs.fz_iost_double(iost, &_export_scale, 1);
	}

	return(err);
}

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

/************************************************************************
	Returns the id of the translator to formZ.
*************************************************************************/
fzrt_error_td pcld_get_id(fzrt_UUID_td fmt_id)
{
	fzrt_error_td					err = FZRT_NOERR;

	/* Just use the plugin id for the translator's id. */
	_fzrt_base_funcs.fzrt_UUID_copy(PCLD_FMT_PLUGIN_ID, fmt_id);
	
	return(err);
}

/************************************************************************
	Returns other information about the translator to formZ.
*************************************************************************/
fzrt_error_td pcld_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
			)
{
	/* Set the string used to sort translators in menus. */
	strncpy(sortby, "point", sortby_max_len);

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

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

	/* Set the translator's attribute flags. */
	*attr_flags = 0;
	
	return(FZRT_NOERR);
}

/************************************************************************
	Returns the Macintosh file type and file creator to formZ.
*************************************************************************/
fzrt_error_td pcld_get_ftype(
			fzrt_ostype		*type,
			fzrt_ostype		*creator
			)
{
	/* Set the format's Macintosh file type. */
	*type = 'pcld';

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

/************************************************************************
	Returns the file formats icon to formZ.
*************************************************************************/
fzrt_error_td pcld_get_icon(
	fzrt_floc_ptr 			floc,				
	fzrt_int 					*hpos, 				
	fzrt_int 					*vpos, 				
	fzrt_floc_ptr 			floc_mask, 			
	fzrt_int 					*hpos_mask, 		
	fzrt_int 					*vpos_mask			
	)
{
	fzrt_error_td	err;

	/* Set the icon's file name. */
	err = _fzrt_file_funcs.fzrt_file_floc_set_name(floc,"pcld_col.tif");

	/* Set the horizontal and vertical pixel offset into the color image. */
	*hpos = 0;
	*vpos = 0;

	if(err == FZRT_NOERR)
	{
		/* Set the icon's mask file name. */
		err = _fzrt_file_funcs.fzrt_file_floc_set_name(floc_mask,"pcld_mask.tif");

		/* Set the horizontal and vertical pixel offset into the mask image. */
		*hpos_mask = 0;
		*vpos_mask = 0;
	}

	return(err);
}

/************************************************************************
	Returns the unstructured translators data type to formZ.
*************************************************************************/
fzrt_error_td pcld_type(fz_ffmt_basic_types_enum *type)
{
	*type = FZ_FFMT_BASIC_TYPE_DATA_MODEL;

	return(FZRT_NOERR);	
}