/* $Log: boundary.c,v $
 * Revision 1.5  1995/03/20  14:53:58  martenl
 * *** empty log message ***
 *
 * Revision 1.4  1995/03/12  14:30:22  martenl
 * Added new types of boundaries
 *
 * Revision 1.3  1995/02/23  09:43:33  martenl
 * minor typographical changes
 * */
/*
C----------------------------------------------------------------------
C /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\  /\
C   \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/  \/
C----------------------------------------------------------------------
C	                  		             
C     +------------------------------------+        .          	  
C     |                                    |       / \ 	        	 
C     |     Author:                        |       | |		 
C     |                           	   |       |.|		 
C     |     Marten Levenstam               |  	   |.|
C     |     Mekanik/Hydromekanik           |       |:|      __
C     |     Kungliga Tekniska Hogskolan    |     ,_|:|_,   /  )
C     |     S-100 44 Stockholm             |  	   (Oo    / _I_
C     |     SWEDEN                         |   	    +\ \  || __|
C     |     marten@mech.kth.se             |   	       \ \||___|
C     |                       	       	   |             \ /.:.\-\
C     |        	      \|/              	   |              |.:. /-----\
C     +---------------( )------------------+              |___|::oOo::|
C                    (@ @)     	       	                  /   |:<_T_>:|
C     +------o00------(	)------00o---------+             |_____\ ::: /
C     |	               o       	       	   |              | |  \ \:/
C     |	               	       	       	   |              | |   | |  	     
C     |	         what comes free       	   |              \ /   | \___
C     |      comes without a guarantee     |              / |   \_____\
C     +------------------------------------+	      	  `-'
C     +----------------------------------------------------------------+
C     |                                                                |
C     |    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS |
C     |    OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE   |
C     |    IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A     |
C     |    PARTICULAR PURPOSE.                                         |
C     |                                                                |
C     +----------------------------------------------------------------+
C======================================================================
C   4 6  10        20        30        40        50        60        70
C   ]|{   |    |    |    |    |    |    |    |    |    |    |    |    |}
C======================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "libsx.h"
#include "globals.h"
#include "geodata.h"
#include "post.h"
#include "crosshair.h"
#include "kraftwerk.h"
#include "graph.h"
#include "grid.h"

void deleteEqData();

int thisBoundary = 0;
int x1, y1;
int xstart, ystart;
int xlast, ylast;
int mb = 0;

PTS_NODE *p0, *p1, *p2, *mv_pts;
GEO_NODE *g;

void boundaryMoveMouseDown(Widget w, int button, int xp, int yp, void *data);


void boundaryMouseMotion(Widget w, int x, int y, void *data)
{
  crossHair(x, y);

  if(mb == 1){
    SetDrawMode(SANE_XOR);
    myDrawLine(xstart, ystart, xlast, ylast);
    myDrawLine(xstart, ystart, x, y);
    SetDrawMode(GXcopy);
  }

  xlast = x;
  ylast = y;
}
void boundaryMouseUp(Widget w, int button, int x, int y, void *data)
{

}
int isOnPoint(int *x, int *y)
{
  FOR_ALL_POINTS( p1 ){
    if( (*x-p1->x)*(*x-p1->x)+(*y-p1->y)*(*y-p1->y) < CLICK_TOL){
      *x = p1->x ;
      *y = p1->y ;
      return TRUE;
    }
  }
  return FALSE;
}


void boundaryMouseDown(Widget w, int button, int x, int y, void *data)
{
  int hh=0, ww=0;
  int n, nb;
  float xp, yp;

  xstart = x;
  ystart = y;
  xlast = x;
  ylast = y;

  if( mb == 0){
    n = 0;
    x1 = x;
    y1 = y;
    mb = 1;
    myDrawFilledBox(x-2,y-2,4,4);
    pix2phys(x, y, &xp, &yp);
    g = new_bnd();
    g->type = boundaryType;
    if(isOnPoint(&x, &y) == FALSE){
      pix2phys(x, y, &xp, &yp);
      p1 = add_pnt(xp, yp);
      p1->x = x;
      p1->y = y;
    }
    p0 = p1;
/* new stuff */
  }
  else{
    xstart = x;
    ystart = y;
    xlast = x;
    ylast = y;

/*  Check to see if we have reach the starting point */

    if( (x-x1)*(x-x1) + (y-y1)*(y-y1) < CLICK_TOL) {
      myDrawLine(xstart, ystart, x1, y1);
      SetButtonUpCB(drawWindow, NULL);
      SetButtonDownCB(drawWindow, NULL);
      SetMouseMotionCB(drawWindow, NULL);    
      deleteCrossHair();
      mb = 0;
      state = HAVE_BOUNDARY;
      SetButtonDownCB(drawWindow, boundaryMouseDown);
      SetMouseMotionCB(drawWindow, boundaryMouseMotion);   
      add_seg(p1, p0, g);
      redisplay(w, hh, ww, data);
    }

/*  Nope, just another segment */

    else{
      myDrawLine(xstart, ystart, x, y);
      myDrawFilledBox(x-2,y-2,4,4);
      pix2phys(x, y, &xp, &yp);
      p2 = p1;
      if(isOnPoint(&x, &y) == FALSE){
	pix2phys(x, y, &xp, &yp);
	p1 = add_pnt(xp, yp);
	p1->x = x;
	p1->y = y;
	add_seg(p2, p1, g);
      }
      else{

/* Ok hit an existing point, stop here should only work for internal bnd*/

	p1->x = x;
	p1->y = y;
	add_seg(p2, p1, g);
	mb = 0;
	SetButtonUpCB(drawWindow, NULL);
	SetButtonDownCB(drawWindow, NULL);
	SetMouseMotionCB(drawWindow, NULL);    
	deleteCrossHair();
	state = HAVE_BOUNDARY;
	SetButtonDownCB(drawWindow, boundaryMouseDown);
	SetMouseMotionCB(drawWindow, boundaryMouseMotion);   
	redisplay(w, hh, ww, data);
      }
    }
  }
}
/* Things to move a boundary point */

void boundaryMoveMouseMotion(Widget w, int xp, int yp, void *data)
{
  crossHair(xp,yp);
}

void boundaryMoveDrop(Widget w, int xp, int yp, void *data)
{
  int i1, i2;
  int n;
  GEO_NODE *g;
  BND_NODE *s;
  crossHair(xp,yp);
  SetDrawMode(SANE_XOR);

/* Now find all segments sharing the point that is moved */
  FOR_ALL_BOUNDARIES(g){
    FOR_ALL_SEGMENTS(g, s){
      if( s->seg->pts1 == mv_pts){
	myDrawLine(s->seg->pts2->x, s->seg->pts2->y, xlast, ylast);
	myDrawLine(s->seg->pts2->x, s->seg->pts2->y, xp, yp);
      }
      if( s->seg->pts2 == mv_pts){
	myDrawLine(s->seg->pts1->x, s->seg->pts1->y, xlast, ylast);
	myDrawLine(s->seg->pts1->x, s->seg->pts1->y, xp, yp);
      }
    }
  }
  SetDrawMode(GXcopy);

  xlast = xp;
  ylast = yp;
  
}
void boundaryMoveMouseUp(Widget w, int button, int xp, int yp, void *data)
{
  int hh=0, ww=0;
  int n, nb;
  float x, y;

  pix2phys(xp, yp, &x, &y);
  mv_pts->x = xp;
  mv_pts->y = yp;
  mv_pts->physicalX = x;
  mv_pts->physicalY = y;

  SetDrawMode(GXcopy);
  myDrawFilledBox(xp-2,yp-2,4,4);
  SetMouseMotionCB(drawWindow, NULL);    
  SetButtonDownCB(drawWindow, NULL);
  SetButtonUpCB(drawWindow, NULL );
  deleteCrossHair();
  state = HAVE_BOUNDARY;
  redisplay(w, hh, ww, data);
  SetButtonDownCB(drawWindow, boundaryMoveMouseDown);
  SetMouseMotionCB(drawWindow, boundaryMoveMouseMotion);
}

void boundaryMoveMouseDown(Widget w, int button, int xp, int yp, void *data)
{
  int i, j, xs, ys;
  int n, nb;
  PTS_NODE *p;


  crossHair(xp,yp);

  FOR_ALL_POINTS(p){
    xs = p->x;
    ys = p->y;
    if( (xp - xs)*(xp - xs) + (yp - ys)*(yp - ys) < CLICK_TOL){
/*
 *  OK user has hit an existing point
 */
      mv_pts = p;
      xlast = xs;
      ylast = ys;
      SetMouseMotionCB(drawWindow, boundaryMoveDrop);    
      SetButtonDownCB(drawWindow, NULL);
      SetButtonUpCB(drawWindow, boundaryMoveMouseUp );
      break;
    }
  }
}

void plotBoundary()
{
  int i,j;
  int n, nb;
  int x1, y1;
  int x2, y2;
  float xx1, yy1;
  float xx2, yy2;
  float xx[2], yy[2];
  GEO_NODE *g;
  BND_NODE *s;
  PTS_NODE *p;

  getDrawBox();

  FOR_ALL_BOUNDARIES(g){
    FOR_ALL_SEGMENTS(g, s){
      xx1 = s->seg->pts1->physicalX;
      yy1 = s->seg->pts1->physicalY;
      xx2 = s->seg->pts2->physicalX;
      yy2 = s->seg->pts2->physicalY;
      phys2pix(&x1, &y1, xx1, yy1);
      phys2pix(&x2, &y2, xx2, yy2);
      s->seg->pts1->x = x1;
      s->seg->pts1->y = y1;
      s->seg->pts2->x = x2;
      s->seg->pts2->y = y2;
      myDrawLine(x1, y1, x2, y2);
    }
  }
  FOR_ALL_POINTS(p)
    myDrawFilledBox(p->x-2,p->y-2,4,4);

}






syntax highlighted by Code2HTML, v. 0.9.1