/****************************************************************
 * Filename  :	Gauge.c
 * Functions :	
 *		Gauge_Display
 * Purpose   : 	Functions for the Gauge class
 * Comment   : 	This class inherit from Item
 $Revision: 1.1 $
 ***************************************************************/

#include "dmvideo.h"			/* data manager definitions           */
#include "OVtypes.h"                    /* video overlay type definitions     */
#include "dy4std.h"                     /* DY4 standard definitions           */
#include "mygraphics.h"			/* graphical alignment definitions    */
#include "dy4driver.h"			/* DY4 driver definitions             */
#include "Item.h"			/* Item class definitions             */
#include "Gauge.h"			/* Gauge class definitions            */

static	char *array_graduations[] = { 
"N", "E", "S", "W"
};


/*---------------------------------------------------------------
 | Function  :	Gauge_Display
 | Purpose   : 	Display the Gauge on the screen
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 10/3/96
 |                to check xorg2 in range for big and small tick marks
 |              tarrant 7/1/97 replaced globals with alignment structure  
 ---------------------------------------------------------------*/
    void
Gauge_Display(OV_alignment *alignment, OV_Item *p_item)
{
	OV_int	 kerr ;		/* error code */
	OV_int	 kaxe ;		/* axis type */
	OV_int	 klin ;		/* line type */
	OV_int	 nb_grd	;	/* number of graduations */
	OV_int	 nb_tic	;	/* number of tic marks */
	OV_float dist_tic ;	/* distance betw. 2 tic.  */
	OV_float dist_tic2 ;	/* distance betw. 2 tic.  */
	OV_float tic_size ;	/* size of a tic mark (mm) */
	OV_float xorg,yorg ;	/* coord. of axe's origin (uu) */
	OV_float xorg2 ;	/* coord. of axe's origin (uu) */
	OV_float xorg1,yorg1;	/* coord. of axe's origin (mm) */
	OV_float xscl1,yscl1;	/* scale factors */
	OV_float modulo;	/* max user graduation */
	OV_float heading;	/* current heading (deg) */
	OV_float x_min, x_max;	/* X bounds (uu) */
	OV_float x_length;	/* length of X axis (mm) */
	OV_float dist_grd;	/* distance between grad. (uu) */
	OV_float x_pnt_a[3];	/* X coordinates array (mm) */
	OV_float y_pnt_a[3];	/* Y coordinates array (mm) */
	OV_float xpnt1,ypnt1;	/* X,Y coord.of fixed point (mm)*/
	OV_int	 nb_pnt;	/* nb of coordinates */
	OV_int	 i;
	OV_float ylin;		/* Y coord. of heading line (uu) */

	x_min    = (float) p_item->p_curve->x_min;
	x_max    = (float) p_item->p_curve->x_max;
	dist_tic = (float) p_item->p_curve->x_grad;
	x_length = (float) p_item->p_curve->x_length;
	kaxe     = 0;
	yorg     = 0.;
	nb_grd   = 4;

/*debug
printf (" x_min  = %f \n", x_min);
printf (" x_max  = %f \n", x_max);
printf (" dist_tic  = %f \n", dist_tic);
enddebug*/

	/* write the value of item on screen */

     	  /* shift origin in millimeters */
	  xorg1 = p_item->x;
	  yorg1 = p_item->y;
	  My_shift_origin(alignment, xorg1, yorg1);

	  /* draw a fixed cursor indicating current position */
	  if (p_item->draw_cursor == 1)
	  {
	     p_item->draw_cursor = 0;
	     nb_pnt = 3;
	     xpnt1 = x_length / 2.;
	     ypnt1 = 4.;
	     x_pnt_a[0] = xpnt1;
	     y_pnt_a[0] = ypnt1;
	     x_pnt_a[1] = xpnt1 + 1.;
	     y_pnt_a[1] = ypnt1 + 3.;
	     x_pnt_a[2] = xpnt1 - 1.;
	     y_pnt_a[2] = ypnt1 + 3.;
	     My_wri_polyline(alignment, nb_pnt, x_pnt_a, y_pnt_a, FILLED);
	  }

/*debug
	  x_length = SCREEN_WIDTH - (2. * xorg1);
	  printf ("x_length = %f\n",x_length);
enddebug*/

	  /* change scale factors to be in user units */
	  xscl1 = x_length / (x_max - x_min);
	  yscl1 = 1.0;
	  My_set_scale(alignment, xscl1, yscl1);

	  /* calculate origin of current heading */
	  modulo = x_max;
	  sscanf(p_item->value, "%f", &heading);
	  xorg = (modulo / 2.) - heading;
	  if (xorg < 0.) xorg = xorg + modulo;
	  if (xorg > 360.) xorg = xorg - modulo;

	  /* draw big tic marks */
	  klin = 0;
	  xorg2 = xorg + 45.;
	  if (xorg2 < 0.) xorg2 = xorg2 + modulo;
	  if (xorg2 > 360.) xorg2 = xorg2 - modulo;
	  dist_tic2 = 90.;
	  nb_tic = (int) ((x_max - x_min) / dist_tic2);
	  tic_size = -10.;
	  kerr = My_cir_axis(alignment, kaxe, xorg2, yorg, modulo, dist_tic2,
			     nb_tic, tic_size, klin);

	  /* draw small tic marks */
	  klin = 0;
	  dist_tic2 = 90.;
	  nb_tic = (int) ((x_max - x_min) / dist_tic2);
	  tic_size = -4.;
	  xorg2 = xorg + 22.5;
	  if (xorg2 < 0.) xorg2 = xorg2 + modulo;
	  if (xorg2 > 360.) xorg2 = xorg2 - modulo;
	  kerr = My_cir_axis(alignment, kaxe, xorg2, yorg, modulo, dist_tic2,
			     nb_tic, tic_size, klin);
	  xorg2 = xorg + 67.5;
	  if (xorg2 < 0.) xorg2 = xorg2 + modulo;
	  if (xorg2 > 360.) xorg2 = xorg2 - modulo;
	  kerr = My_cir_axis(alignment, kaxe, xorg2, yorg, modulo, dist_tic2,
			     nb_tic, tic_size, klin);

	  /* draw graduations */
	  tic_size = +5.;	
	  dist_grd = (x_max - x_min) / (float) nb_grd;
	  kerr = My_cir_grad(alignment, kaxe, xorg, yorg, modulo, dist_grd,
			     nb_grd, tic_size, array_graduations);

	  /* draw cursor */
/*
	  Gauge_cursor(alignment, p_item, xorg, yorg);  
*/
	
	  /* reset scale factors and origin */
	  My_reset_scale(alignment);
	  My_reset_origin(alignment);
 
        return;

} /* Gauge_Display */


/*---------------------------------------------------------------
 | Function  :	Gauge_cursor
 | Purpose   : 	
 | Inputs    :	none
 | Outputs   :	none
 | Side FX   :	none
 | comments  :
 | Author    :	sylvain.coudray MBARI-IFREMER/diti/dsi/ie
 | Modified  :  tarrant 7/1/97 replaced globals with alignment structure  
 ---------------------------------------------------------------*/
    void
Gauge_cursor(OV_alignment *alignment, OV_Item *p_item, OV_float xorg,
	     OV_float yorg)
{
	OV_float heading;	/* current heading (deg) */
	OV_float ylin;		/* Y coord. of heading line (uu) */

	/* get current heading */
	sscanf(p_item->value, "%f", &heading);

	/* vertical position relatively to graduations */
	ylin = yorg + 2.;

	/* draw a line from current position to origin */

	My_mov_point(alignment, 180., ylin);
	if ( heading > 180.) {
	    My_wri_line(alignment, 0., ylin);
	    My_mov_point(alignment, 360., ylin);
	    My_wri_line(alignment, 360. + 180. - heading, ylin);
	} else {
	    if ( heading < -180.) {
	      My_wri_line(alignment, 360., ylin);
	      My_mov_point(alignment, 0., ylin);
	      My_wri_line(alignment, 0. - (heading + 180.), ylin);
	    } else {  	    
	      My_wri_line(alignment, 180. - heading, ylin);
	    }
	}

        return;

} /* Gauge_cursor */


/*---------------------------------------------------------------
 | Function  :	Gauge_line
 | Purpose   : 	Draws a line between the current gauge value and
 |              the setpoint value.
 | Inputs    :	Item pointer.
 | Outputs   :	None.
 | Author    :	Janice Tarrant
 ---------------------------------------------------------------*/
    void
Gauge_Line(OV_alignment *alignment, OV_Item *p_item)
{
    OV_float x_min, x_max;		/* X bounds in user units             */
    OV_float x_length;			/* length of X axis (mm)              */
    OV_float xscale,yscale;		/* scale factors                      */
    OV_int   delta;			/* setpoint error                     */

/* get the line length                                                        */
    if (p_item->value != NULL)
    {
    	sscanf(p_item->value, "%d", &delta);
    	if (abs(delta) > 0)
    	{

    	    x_min = (float) p_item->p_curve->x_min;
    	    x_max = (float) p_item->p_curve->x_max;
    	    x_length = (float) p_item->p_curve->x_length;

/* shift origin in millimeters */
    	    My_shift_origin(alignment, p_item->x, p_item->y);

/* change scale factors to be in user units */
    	    xscale = x_length / (x_max - x_min);
    	    yscale = 1.0;
    	    My_set_scale(alignment, xscale, yscale);

/* draw the setpoint error */
    	    My_mov_point(alignment, 180.0, 0.0);
    	    My_wri_line(alignment, 180.0 + delta, 0.0);

/* reset scale factors and origin */
    	    My_reset_scale(alignment);
    	    My_reset_origin(alignment);
    	}
    }

    return;

}  /* Gauge_Line */

