/* :ts=8 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "general.h"
#include "globals.h"
#include "intui.h"
#include "timer.h"
#include "spl_math.h"
#include "spl_util.h"
#include "spl_gfx.h"
#include "rotate_g.h"

/* Get Stringinfo buffer for the Rotate gadget with id 'G':	*/
#define Get_Gadget_String(G) (((struct StringInfo *) \
				(Rotate_GroupGadgets[G]->SpecialInfo))->Buffer)

	/* Set string to 'S' in string gadget 'G':	*/
#define Set_Gadget_String(G, S) \
		GT_SetGadgetAttrs(Rotate_GroupGadgets[G], \
			Windows[W_Rotate_Group].Window, NULL, \
			GTST_String, S)

static Boolean_T	Rotate_Group_Active;
static short		Rotate_Group_Axis = 0; /* 0 = X, 1 = Y, 2 = Z */
static short		Orig_Rotate_Group_X;
static short		Prev_Rotate_Group_X;
static double		Rotate_Group_Angle;
static Vector_T		Box_Min, Box_Max;

static
void Set_Rotate_Group_Angle_Value(double Angle)
/************************************************************************/
/*                                                                      */
/* Set rotate angle value gadget to 'Angle'.				*/
/*                                                                      */
/************************************************************************/
{
    char Buffer[Buffer_Length+1];

    if (Windows[W_Rotate_Group].Window == NULL) return;

    sprintf(Buffer, " %.0lf", Angle);
    Set_Gadget_String(GDX_Rotate_Group_Angle_Value, Buffer);

} /* Set_Rotate_Group_Angle_Value */

static
void Rotate_Group_Timeout()
/************************************************************************/
/*                                                                      */
/* Function called when timer expires: Draw all splines.		*/
/*                                                                      */
/************************************************************************/
{    
    Points_Rotate(Rotate_Group_Angle, Rotate_Group_Axis);
    Compute_Splines();
    Clear_All(What_All);
    Draw_All(What_All);

    Redraw_Mask = 0;

    Stop_Timer();

} /* Rotate_Group_Timeout */

static 
void Rotate_Group_Redraw(long Mask)
/************************************************************************/
/*                                                                      */
/* Function called to redraw screen while rotating points.		*/
/*                                                                      */
/************************************************************************/
{    
    Vector_T R_Vector;

    if (Windows[W_Rotate_Group].Window == NULL) return;

    R_Vector[0] = R_Vector[1] = R_Vector[2] = 0.0;
    R_Vector[Rotate_Group_Axis] = Rotate_Group_Angle;

    Set_Rotate_Group_Angle_Value(Rotate_Group_Angle);

    Clear_Plane(3, What_All);
    Draw_Box(Box_Min, Box_Max, R_Vector, DM_Plane, What_All);

    Start_Timer(Delay_Draw_Seconds, Delay_Draw_Micros);

    Redraw_Mask = 0;

} /* Rotate_Group_Redraw */

static
void Rotate_Group_Select_Up(short X, short Y)
/************************************************************************/
/*                                                                      */
/* X, Y are the actual coordinates in the active window.		*/
/*                                                                      */
/************************************************************************/
{
	/* If the timer hasn't expired, then call the timeout handler to*/
	/* compute and draw the splines.			 	*/

    if (!Check_Timer()) Rotate_Group_Timeout();

    Set_Rotate_Group_Angle_Value(0.0);
    Set_Mode_Normal();

} /* Rotate_Group_Select_Up */

static 
void Rotate_Group_Select_Down(short X, short Y)
/************************************************************************/
/*                                                                      */
/* X, Y are the actual coordinates in the active window.		*/
/*                                                                      */
/************************************************************************/
{

    if (X < 0) return;

    if (Get_Select_Bounding_Box(Box_Min, Box_Max)) {
	Display_Message("No points selected");
	return;
    }
    Draw_Box(Box_Min, Box_Max, NULL, DM_Plane, What_All);

    Rotate_Group_Active = TRUE;

    Orig_Rotate_Group_X = X;
    Prev_Rotate_Group_X = X;

    Rotate_Group_Angle = 0.0;

    if (MQ_Size_Rotate_G > 0) {
	SetMouseQueue(Windows[Id_Active_Window].Window, MQ_Size_Rotate_G); 
	Redraw_Always = TRUE;
    } else Redraw_Always = FALSE;

} /* Rotate_Group_Select_Down */

static 
void  Rotate_Group_Move(short X, short Y)
/************************************************************************/
/*                                                                      */
/* Function called when mouse is moved to rotate points.		*/
/* X, Y are the actual coordinates in the active window.		*/
/*                                                                      */
/************************************************************************/
{
    if (X < 0) return;

    if (!Rotate_Group_Active) return;
    if (ABS(X - Prev_Rotate_Group_X) < 5) return;

    Rotate_Group_Angle = (X - Orig_Rotate_Group_X);
    Rotate_Group_Angle = 1.0 + (Rotate_Group_Angle/4.0);

    Prev_Rotate_Group_X = X;

    if (Redraw_Always) Rotate_Group_Redraw(What_All);
    else               Redraw_Mask = What_All;

} /* Rotate_Group_Move */

static
Boolean_T Rotate_Group_Handle_Event(struct IntuiMessage *Msg)
/************************************************************************/
/*                                                                      */
/* Event handler routine for the 'ROTATE GROUP' mode.			*/
/* Events handled:							*/
/*	Select down: Start rotation.					*/
/*	Select up: Stop rotation.					*/
/*	Mouse move: Change rotation angle.				*/
/*                                                                      */
/************************************************************************/
{

    switch (Msg->Class) {


    case IDCMP_MOUSEBUTTONS:
		/* Msg->Code contain id of button pressed 		*/
		/* Msg->MouseX and Msg->MouseY contain mouse position 			*/

	switch (Msg->Code) {

	case SELECTDOWN:
	    Rotate_Group_Select_Down(Msg->MouseX, Msg->MouseY);
	    return(TRUE);

	case SELECTUP:
	    Rotate_Group_Select_Up(Msg->MouseX, Msg->MouseY);
	    return(TRUE);

	} /* switch (Msg->Code) */
	break;

    case IDCMP_MOUSEMOVE:
	Rotate_Group_Move(Msg->MouseX, Msg->MouseY);
	return(TRUE);

    } /* switch (Msg->Class) */

    return(FALSE);

} /* Rotate_Group_Handle_Event */

void Handle_Rotate_Group_Angle()
/************************************************************************/
/*                                                                      */
/* Change to ROTATE GROUP mode.						*/
/*                                                                      */
/************************************************************************/
{
    if (Windows[W_Rotate_Group].Window == NULL) return;

    Rotate_Group_Active		= FALSE;
    State.Handle_Event		= Rotate_Group_Handle_Event;
    State.Timeout		= Rotate_Group_Timeout;
    State.Redraw		= Rotate_Group_Redraw;

    sprintf(Error_Msg, "Rotate group");
    Display_Status(Error_Msg);

    Points_Save();

} /* Handle_Rotate_Group_Angle */

void Handle_Rotate_Group_Angle_Value()
/************************************************************************/
/*                                                                      */
/* Handle a Rotate_Group_Angle_Value gadget event.				*/
/*                                                                      */
/************************************************************************/
{
    if (Windows[W_Rotate_Group].Window == NULL) return;

    Points_Save();
    Rotate_Group_Angle = atof(Get_Gadget_String(GDX_Rotate_Group_Angle_Value));

    Rotate_Group_Timeout(); /* Compute and draw splines */

    Set_Rotate_Group_Angle_Value(0.0);

} /* Handle_Rotate_Group_Angle_Value */


void Handle_Rotate_Group_Axis(short Axis)
/************************************************************************/
/*                                                                      */
/* Handle a Rotate_Group_Axis gadget event.				*/
/*                                                                      */
/************************************************************************/
{
    Rotate_Group_Axis = Axis;

} /* Handle_Rotate_Group_Axis */

