gui/broken_edge.cc
author hegyi
Tue, 26 Jul 2005 21:20:01 +0000
changeset 1593 a8a007f1c01c
parent 1505 7121597da9ba
child 1601 70d169a8313a
permissions -rw-r--r--
EdgeMap and NodeMap creation is done, at last. Bach 4ever.
     1 #include <broken_edge.h>
     2 #include <math.h>
     3 
     4 BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false)
     5 {
     6   my_points=new Gnome::Art::Point[3];
     7 
     8   arrow=new Gnome::Canvas::Polygon(g);
     9   *arrow << Gnome::Canvas::Properties::fill_color("red");
    10   arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler));
    11   setPoints(p);
    12 }
    13 
    14 BrokenEdge::~BrokenEdge()
    15 {
    16   if(arrow)delete(arrow);
    17 }
    18 
    19 void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
    20 {
    21   bool set_arrow=false;
    22   if(!move)
    23     {
    24       if(p.size()==2)
    25 	{
    26 	  set_arrow=true;
    27 	  Gnome::Canvas::Points points_with_center;
    28 	  points_with_center.push_back(my_points[0]=p[0]);
    29 	  points_with_center.push_back(my_points[1]=Gnome::Art::Point( (p[0].get_x()+p[1].get_x())/2+0 , (p[0].get_y()+p[1].get_y())/2 )+0 );
    30 	  points_with_center.push_back(my_points[2]=p[1]);
    31 	  property_points().set_value(points_with_center);
    32 	}  
    33       if(p.size()==3)
    34 	{
    35 	  set_arrow=true;
    36 	  property_points().set_value(p);
    37 	  for(int i=0;i<3;i++)
    38 	    {
    39 	      my_points[i]=p[i];
    40 	    }
    41 	}
    42     }
    43   else
    44     {
    45       if(p.size()==2)
    46 	{
    47 	  Gnome::Canvas::Points points;
    48 	  my_points[0]=p[0];
    49 	  my_points[2]=p[1];
    50 	  for(int i=0;i<3;i++)
    51 	    {
    52 	      points.push_back(my_points[i]);
    53 	    }
    54 	  property_points().set_value(points);
    55 	}
    56     }
    57 
    58   if(set_arrow)
    59     {
    60       //calculating coordinates of the direction indicator arrow
    61 
    62       xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
    63       xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
    64 
    65       xy<gdouble> unit_vector_in_dir(target-center);
    66       //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
    67       unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
    68       //       std::cout << " = " << unit_vector_in_dir << std::endl;
    69 
    70       xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
    71       //       std::cout << unit_norm_vector << std::endl;
    72 
    73       {      
    74 	//       /\       // top
    75 	//      /  \      //
    76 	//      -  -      // c(enter)l(eft), ccl, ccr, cr
    77 	//       ||       //
    78 	//       ||       // b(ottom)l, br
    79       }
    80 
    81       double size=3;
    82 
    83       xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
    84       xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
    85       xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
    86       xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
    87       xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
    88       xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
    89       xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
    90 	 
    91       //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
    92 
    93       Gnome::Canvas::Points arrow_points;
    94       arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
    95       arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
    96       arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
    97       arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
    98       arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
    99       arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
   100       arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
   101 
   102       arrow->property_points().set_value(arrow_points);
   103     }
   104 }
   105 
   106 bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
   107 {
   108   switch(e->type)
   109     {
   110     case GDK_BUTTON_PRESS:
   111       //we mark the location of the event to be able to calculate parameters of dragging
   112       if(gdc.getActualTool()!=CREATE_NODE)
   113 	{
   114 	  gdc.toggleEdgeActivity(this, true);
   115 	  clicked_x=e->button.x;
   116 	  clicked_y=e->button.y;
   117 	  isbutton=true;
   118 	}
   119       break;
   120     case GDK_BUTTON_RELEASE:
   121       if(gdc.getActualTool()!=CREATE_NODE)
   122 	{
   123 	  gdc.toggleEdgeActivity(this, false);
   124 	  isbutton=false;
   125 	}
   126       break;
   127     case GDK_MOTION_NOTIFY:
   128       //we only have to do sg. if the mouse button is pressed
   129       if(isbutton)
   130 	{
   131 	  //new coordinates will be the old values,
   132 	  //because the item will be moved to the
   133 	  //new coordinate therefore the new movement
   134 	  //has to be calculated from here
   135 
   136 	  double dx=e->motion.x-clicked_x;
   137 	  double dy=e->motion.y-clicked_y;
   138 
   139 	  Gnome::Canvas::Points points_new;
   140 
   141 	  points_new.push_back(my_points[0]);
   142 	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
   143 	  points_new.push_back(my_points[2]);
   144 
   145 	  setPoints(points_new);
   146 	  gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
   147 
   148 	  clicked_x=e->motion.x;
   149 	  clicked_y=e->motion.y;
   150 
   151 	}
   152     default: break;
   153     }
   154 
   155   return true;
   156 }
   157 
   158 xy<double> BrokenEdge::getArrowPos()
   159 {
   160   xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
   161   return ret_val;
   162 }