/* :ts=8 */
/************************************************************************/
/*                                                                      */
/* This file contains code to handle the ADD mode.			*/
/*                                                                      */
/************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

#include "general.h"
#include "globals.h"
#include "intui.h"
#include "spl_math.h"
#include "spl_util.h"
#include "spl_gfx.h"
#include "normal.h"
#include "add.h"

static 
void  Add_Select_Down(short X, short Y)
/************************************************************************/
/*                                                                      */
/* Select has been pressed in add mode, this means that the user wants	*/
/* to add a new point here.						*/
/*                                                                      */
/************************************************************************/
{
    short	 Point_Id;
    Knot_T	 *Knot;
    Spline_T	 *Spline;

    Select_View_Id = Screen_To_World(X, Y, Current_Pos);
    if (Select_View_Id < 0) return;

	/* Try to locate a point near the cursor */
    Point_Id = Point_Find(Current_Pos, Select_View_Id);


    if (Point_Id < 0) {

	/* The cursor wasn't near a point so add a new spline, ie. 2	*/
	/* points at the same time.					*/

	/* Deselect all, and draw prev. selected spline in normal mode */
    	Spline = Select_Spline;
    	Deselect_All();
        Draw_Spline(Spline, DM_Normal, What_All);
        
    	if (Grid_Snap_Active) Snap_To_Grid(Current_Pos);

        Spline = Spline_Add(Current_Pos);
        if (Spline == NULL) return;

    } else {
	
	/* Add a knot to an existing spline 				*/

	if (Point_Id != Select_Point_Id || Select_Knot == NULL) {

	   /* The chosen point isn't the currently selected point, so	*/
	   /* deselect all, and select the chosen point.		*/

    	    Spline = Select_Spline;
    	    Deselect_All();
            Draw_Spline(Spline, DM_Normal, What_All);

	    Select_Point(Point_Id);

	}

	/* Undraw the spline as it will be modified:			 */
        Draw_Spline(Select_Spline, DM_Erase, What_All);

	/* Add a new knot after the selected knot at Current_Pos */

        Knot = Knot_Add(Select_Spline, Select_Knot, Current_Pos); 

	/* Draw the modified spline again.             	*/
        Draw_Spline(Select_Spline, DM_Normal, What_All);

        if (Knot == NULL) return;

    } /* if .. else .. */

    Set_Window_Title(NULL);

	/* Enter move mode so the user can move the new knot */
    Set_Mode_Move(X, Y);

} /* Add_Select_Down */

static 
Boolean_T Add_Handle_Event(struct IntuiMessage *Msg)
/************************************************************************/
/*                                                                      */
/* Event handler routine for the 'ADD' mode.				*/
/*                                                                      */
/* Events handled:							*/
/*	Select down: Add a knot or a spline.				*/
/*                                                                      */
/************************************************************************/
{

    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:
	    Add_Select_Down(Msg->MouseX, Msg->MouseY);
	    return(TRUE);

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


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

    return(FALSE);

} /* Add_Handle_Event */

void Set_Mode_Add()
/************************************************************************/
/*                                                                      */
/* Enter add mode, ie. add a new knot next time the user presses left	*/
/* button.								*/
/*                                                                      */
/************************************************************************/
{

    State.Handle_Event		= Add_Handle_Event;

    Display_Status("Add");

} /* Set_Mode_Add */
