broken_edge.cc
author hegyi
Thu, 16 Jun 2005 18:58:15 +0000
branchgui
changeset 20 a3bd39d50930
parent 19 164783ceb9be
child 21 44bb92014108
permissions -rw-r--r--
First of all: revision 1981 is mine, what is important me because I was born in 1981. But what is new in my revision? If you drag nodes with left button, edge-breakpoints do not change location. If you drag nodes by right button, they do, they take up their base situation at the halfpoint of the edge.
hegyi@17
     1
#include <broken_edge.h>
hegyi@19
     2
#include <lemon/xy.h>
hegyi@19
     3
#include <math.h>
hegyi@17
     4
hegyi@19
     5
BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p) : Line(g), isbutton(false)
hegyi@17
     6
{
hegyi@19
     7
  my_points=new Gnome::Art::Point[3];
hegyi@19
     8
hegyi@19
     9
  arrow=new Gnome::Canvas::Polygon(g);
hegyi@19
    10
  *arrow << Gnome::Canvas::Properties::fill_color("red");
hegyi@19
    11
  arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edge_former_event_handler));
hegyi@19
    12
  set_points(p);
hegyi@19
    13
}
hegyi@19
    14
hegyi@19
    15
BrokenEdge::~BrokenEdge()
hegyi@19
    16
{
hegyi@19
    17
  if(arrow)delete(arrow);
hegyi@19
    18
}
hegyi@19
    19
hegyi@20
    20
void BrokenEdge::set_points(Gnome::Canvas::Points p, bool move)
hegyi@19
    21
{
hegyi@19
    22
  bool set_arrow=false;
hegyi@20
    23
  if(!move)
hegyi@17
    24
    {
hegyi@20
    25
      if(p.size()==2)
hegyi@20
    26
	{
hegyi@20
    27
	  set_arrow=true;
hegyi@20
    28
	  Gnome::Canvas::Points points_with_center;
hegyi@20
    29
	  points_with_center.push_back(my_points[0]=p[0]);
hegyi@20
    30
	  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 );
hegyi@20
    31
	  points_with_center.push_back(my_points[2]=p[1]);
hegyi@20
    32
	  property_points().set_value(points_with_center);
hegyi@20
    33
	}  
hegyi@20
    34
      if(p.size()==3)
hegyi@20
    35
	{
hegyi@20
    36
	  set_arrow=true;
hegyi@20
    37
	  property_points().set_value(p);
hegyi@20
    38
	  for(int i=0;i<3;i++)
hegyi@20
    39
	    {
hegyi@20
    40
	      my_points[i]=p[i];
hegyi@20
    41
	    }
hegyi@20
    42
	}
hegyi@20
    43
    }
hegyi@20
    44
  else
hegyi@19
    45
    {
hegyi@20
    46
      if(p.size()==2)
hegyi@19
    47
	{
hegyi@20
    48
	  Gnome::Canvas::Points points;
hegyi@20
    49
	  my_points[0]=p[0];
hegyi@20
    50
	  my_points[2]=p[1];
hegyi@20
    51
	  for(int i=0;i<3;i++)
hegyi@20
    52
	    {
hegyi@20
    53
	      points.push_back(my_points[i]);
hegyi@20
    54
	    }
hegyi@20
    55
	  property_points().set_value(points);
hegyi@19
    56
	}
hegyi@19
    57
    }
hegyi@19
    58
hegyi@19
    59
  if(set_arrow)
hegyi@19
    60
    {
hegyi@19
    61
      //calculating coordinates of the direction indicator arrow
hegyi@19
    62
hegyi@19
    63
      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
hegyi@19
    64
      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
hegyi@19
    65
hegyi@19
    66
      xy<gdouble> unit_vector_in_dir(target-center);
hegyi@19
    67
      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
hegyi@19
    68
      unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
hegyi@19
    69
      //       std::cout << " = " << unit_vector_in_dir << std::endl;
hegyi@19
    70
hegyi@19
    71
      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
hegyi@19
    72
      //       std::cout << unit_norm_vector << std::endl;
hegyi@19
    73
hegyi@19
    74
      {      
hegyi@19
    75
	//       /\             top
hegyi@19
    76
	//      /  \
hegyi@19
    77
	//      -  -      c(enter)l(eft), ccl, ccr, cr
hegyi@19
    78
	//       ||
hegyi@19
    79
	//       ||       b(ottom)l, br
hegyi@19
    80
      }
hegyi@19
    81
hegyi@19
    82
      double size=3;
hegyi@19
    83
hegyi@19
    84
      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
hegyi@19
    85
      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
hegyi@19
    86
      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
hegyi@19
    87
      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
hegyi@19
    88
      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
hegyi@19
    89
      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
hegyi@19
    90
      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
hegyi@19
    91
	 
hegyi@19
    92
      //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
hegyi@19
    93
hegyi@19
    94
      Gnome::Canvas::Points arrow_points;
hegyi@19
    95
      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
hegyi@19
    96
      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
hegyi@19
    97
      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
hegyi@19
    98
      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
hegyi@19
    99
      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
hegyi@19
   100
      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
hegyi@19
   101
      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
hegyi@19
   102
hegyi@19
   103
      arrow->property_points().set_value(arrow_points);
hegyi@17
   104
    }
hegyi@17
   105
}
hegyi@19
   106
hegyi@19
   107
bool BrokenEdge::edge_former_event_handler(GdkEvent* e)
hegyi@19
   108
{
hegyi@19
   109
  switch(e->type)
hegyi@19
   110
    {
hegyi@19
   111
    case GDK_BUTTON_PRESS:
hegyi@19
   112
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@19
   113
      clicked_x=e->button.x;
hegyi@19
   114
      clicked_y=e->button.y;
hegyi@19
   115
      isbutton=true;
hegyi@19
   116
      break;
hegyi@19
   117
    case GDK_BUTTON_RELEASE:
hegyi@19
   118
      isbutton=false;
hegyi@19
   119
      break;
hegyi@19
   120
    case GDK_MOTION_NOTIFY:
hegyi@19
   121
      //we only have to do sg. if the mouse button is pressed
hegyi@19
   122
      if(isbutton)
hegyi@19
   123
	{
hegyi@19
   124
	  //new coordinates will be the old values,
hegyi@19
   125
	  //because the item will be moved to the
hegyi@19
   126
	  //new coordinate therefore the new movement
hegyi@19
   127
	  //has to be calculated from here
hegyi@19
   128
hegyi@19
   129
	  double dx=e->motion.x-clicked_x;
hegyi@19
   130
	  double dy=e->motion.y-clicked_y;
hegyi@19
   131
hegyi@19
   132
	  Gnome::Canvas::Points points_new;
hegyi@19
   133
hegyi@19
   134
	  points_new.push_back(my_points[0]);
hegyi@19
   135
	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
hegyi@19
   136
	  points_new.push_back(my_points[2]);
hegyi@19
   137
hegyi@19
   138
	  set_points(points_new);
hegyi@19
   139
hegyi@19
   140
	  clicked_x=e->motion.x;
hegyi@19
   141
	  clicked_y=e->motion.y;
hegyi@19
   142
hegyi@19
   143
	}
hegyi@19
   144
    default: break;
hegyi@19
   145
    }
hegyi@19
   146
hegyi@19
   147
  return true;
hegyi@19
   148
}