/*------------------------------------------------------------------------*
  Filename....: triangle_tool.c


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

#include "triangle.h"
#include "fz_tool_api.h"
#include "fz_cmnd_model_ids.h"

typedef struct
{

	fz_objt_ptr			obj;
	fz_xyz_td			points[3];
} triangle_track_data_td;

triangle_track_data_td		triangle_track_data;


/************************************************************************
	info callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_info(fz_tool_kind_enum *kind, fz_proj_level_enum *level)
{
	short			rv = FZRT_NOERR;

	*kind = FZ_TOOL_KIND_OPERATOR;
	*level = FZ_PROJ_LEVEL_MODEL;
	
	return (rv);
}


/************************************************************************
	name callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_name(char *name, fzrt_int max_len)
{
	char	str[256];
	
	plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.fz_rsrc_ref_func, TRIANGLE_STR_ID, TRIANGLE_STR_TOOL_NAME, str);
	strncpy(name, str, max_len);
	
	return (FZRT_NOERR);
}

/************************************************************************
	uuid callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_uuid(fzrt_UUID_td uuid)
{

	plugin_stuff.fzrt_base_funcs.fzrt_UUID_copy( TRIANGLE_TOOL_ID, uuid );
	
	return (FZRT_NOERR);
}

/************************************************************************
	icon file callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_icon_file(fzrt_floc_ptr floc)
{
	fzrt_error_td	err;

	err = plugin_stuff.fzrt_file_funcs.fzrt_file_floc_copy(plugin_stuff.floc,floc);
	err = plugin_stuff.fzrt_file_funcs.fzrt_file_floc_set_name(floc,"triangle.bmp");
	return(err);
}

/*************************************************************************
	info2 callback
 *************************************************************************/
static fzrt_boolean star_tool_info2(
									fzrt_int				windex,
									fz_tool_info_enum	which,
									fz_type_td			*info_data,
									fz_type_td			*data
									)
{
	fzrt_boolean		rv = TRUE;
	fzrt_int				value;
	
	switch(which)
	{
		case FZ_TOOL_INFO_PICK_STATE :
			value = 0;
			plugin_stuff.type_funcs.fz_type_set_int(&value, data);
			break;
			
		case FZ_TOOL_INFO_DRAW_STATE:
			/* AUTOPLANE ONLY FOR FIRST CLICK POINT */
			value = 1;
			plugin_stuff.type_funcs.fz_type_set_int(&value, data);
			break;
			
		default:
			rv = FALSE;
			break;
	}
	return(rv);
}


/************************************************************************
	icon adjacent callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_icon_adjacent(const fzrt_UUID_td tool_bar_id, fzrt_UUID_td adjacent_id, fz_fuim_icon_adjacent_enum *where)
{		

	
	plugin_stuff.fzrt_base_funcs.fzrt_UUID_copy(FZ_CMND_MODEL_RECT3, adjacent_id);
	*where = FZ_FUIM_ICON_ADJACENT_RIGHT;
	
	return(FZRT_NOERR);
}



/************************************************************************
	handles the first click when creating a triangle object
*************************************************************************/
static fzrt_error_td triangle_tool_click_first(fzrt_int windex, fz_xyz_td *where_3d)		
{
	fzrt_error_td		err;
	fz_objt_ptr			obj;
	char				prompt_str[256];
	fzrt_int				pindx[3];
	
	if((err = plugin_stuff.model_funcs.fz_objt_cnstr_objt_new(windex,&obj)) == FZRT_NOERR )
	{
		triangle_track_data.points[0] = *where_3d;
		triangle_track_data.points[1] = *where_3d;
		plugin_stuff.model_funcs.fz_objt_fact_add_pnts(windex,obj,triangle_track_data.points,2);
		
		pindx[0] = 0;
		pindx[1] = 1;
		plugin_stuff.model_funcs.fz_objt_fact_create_wire_face(windex,obj,pindx,2,NULL);
		
		err = plugin_stuff.model_funcs.fz_objt_add_objt_to_project(windex,obj);

		if ( err != FZRT_NOERR )
		{	plugin_stuff.model_funcs.fz_objt_edit_delete_objt(windex,obj);
		}
		else
		{
			plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.fz_rsrc_ref_func, TRIANGLE_STR_ID, TRIANGLE_STR_P2, prompt_str);
			plugin_stuff.fuim_funcs.fz_fuim_prompt_line(prompt_str, FZ_FUIM_PROMPT_LINE_NEXT, FZ_FUIM_PROMPT_EDIT_XYZ);
			
			triangle_track_data.obj = obj;
		}
	}


	return FZRT_NOERR;
}
/************************************************************************
	handles the second click when creating a triangle object
*************************************************************************/
static fzrt_error_td triangle_tool_click_second(fzrt_int windex, fz_xyz_td *where_3d, char do_prompt)		
{
	char				prompt_str[256];
	fzrt_int				pindx[3];
	
	
	plugin_stuff.model_funcs.fz_objt_fact_reset(windex, triangle_track_data.obj);
	triangle_track_data.points[1] = *where_3d;
	plugin_stuff.model_funcs.fz_objt_fact_add_pnts(windex,triangle_track_data.obj,triangle_track_data.points,2);
		
	pindx[0] = 0;
	pindx[1] = 1;
	plugin_stuff.model_funcs.fz_objt_fact_create_wire_face(windex,triangle_track_data.obj,pindx,2,NULL);
			
	if(do_prompt)
	{	plugin_stuff.fzrt_base_funcs.fzrt_fzr_get_string(plugin_stuff.fz_rsrc_ref_func, TRIANGLE_STR_ID, TRIANGLE_STR_P3, prompt_str);
		plugin_stuff.fuim_funcs.fz_fuim_prompt_line(prompt_str, FZ_FUIM_PROMPT_LINE_NEXT, FZ_FUIM_PROMPT_EDIT_XYZ);
	}
	return FZRT_NOERR;
}
/************************************************************************
	handles the third click when creating a triangle object
*************************************************************************/
static fzrt_error_td triangle_tool_click_third(fzrt_int windex, fz_xyz_td *where_3d)		
{
	fzrt_error_td		err;
	fzrt_int				pindx[3];
	
	plugin_stuff.model_funcs.fz_objt_fact_reset(windex, triangle_track_data.obj);
	triangle_track_data.points[2] = *where_3d;
	err = plugin_stuff.model_funcs.fz_objt_fact_add_pnts(windex,triangle_track_data.obj,triangle_track_data.points,3);
		
	pindx[0] = 0;
	pindx[1] = 1;
	pindx[2] = 2;
	err = plugin_stuff.model_funcs.fz_objt_fact_create_face(windex,triangle_track_data.obj,pindx,3,NULL);
	
	return FZRT_NOERR;
}


/************************************************************************
	click callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_click(
		fzrt_int 						windex, 
		fzrt_point 					*where, 
		fz_xyz_td 					*where_3d, 
		fz_map_plane_td 			*map_plane,
		fz_fuim_click_enum 			clicks, 
		fzrt_int 						click_count,  
		fzrt_boolean 				*click_handled,
		fz_fuim_click_wait_enum 	*click_next, 
		fzrt_boolean 				*done)		
{
	fzrt_error_td			err;
	
	switch(click_count)
	{	case 1 : 
			err = triangle_tool_click_first(windex, where_3d);
		break;
			
		case 2 : 
			err = triangle_tool_click_second(windex, where_3d, TRUE);
		break;
		
		case 3 : 
			err = triangle_tool_click_third(windex, where_3d);
			*done = 1;
		break;
	}

	return (err);
}


/************************************************************************
	track callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_track(
	fzrt_int				windex,				
	fzrt_point 			*where, 			
	fz_xyz_td	 		*where_3d, 			
	fz_map_plane_td 	*map_plane,
	fzrt_int			 	click_count 		
	)
{	
	fzrt_error_td			err = FZRT_NOERR;

	switch(click_count)
	{	case 1 : 
			err = triangle_tool_click_second(windex, where_3d, FALSE);
		break;
			
		case 2 : 
			err = triangle_tool_click_third(windex, where_3d);
		break;
		
	}
	return (err);
}

/************************************************************************
	cancel callback function for the triangle tool command
*************************************************************************/
static fzrt_error_td triangle_tool_cancel(
	fzrt_int				windex,				
	fzrt_int			 	click_count 		
	)
{	
	fzrt_error_td			err = FZRT_NOERR;

	if(click_count >= 1)
	{
		plugin_stuff.model_funcs.fz_objt_edit_delete_objt(windex,triangle_track_data.obj);
	}
	return (err);
}



/************************************************************************
	Fill the tool command function set with the triangle tool command callback
	functions.
*************************************************************************/
fzrt_error_td triangle_tool_get_callback_fset ( const fzpl_fset_def_ptr fset_def, fzpl_fset_td * const fset )
{
	fzrt_error_td							err = FZRT_NOERR;
	fz_tool_cbak_fset 				*cmds_func;

	err = plugin_stuff.fsf->fzpl_fset_def_check ( fset_def, FZ_TOOL_CBAK_FSET_VERSION, FZPL_TYPE_STRING(fz_tool_cbak_fset), sizeof ( cmds_func ), FZPL_VERSION_OP_NEWER );
	if ( err == FZRT_NOERR )
	{
		cmds_func = (fz_tool_cbak_fset *)fset;
		
		cmds_func->fz_tool_cbak_info					= triangle_tool_info;
		cmds_func->fz_tool_cbak_name					= triangle_tool_name;
		cmds_func->fz_tool_cbak_uuid					= triangle_tool_uuid;
		cmds_func->fz_tool_cbak_icon_file				= triangle_tool_icon_file;
		cmds_func->fz_tool_cbak_icon_menu_adjacent		= triangle_tool_icon_adjacent;
		cmds_func->fz_tool_cbak_click					= triangle_tool_click;
		cmds_func->fz_tool_cbak_track					= triangle_tool_track;
		cmds_func->fz_tool_cbak_cancel					= triangle_tool_cancel;
		
		cmds_func->fz_tool_cbak_info2					= star_tool_info2;
		
	}

	return err;
}

