broken_edge.cc
author hegyi
Thu, 16 Jun 2005 18:08:04 +0000
branchgui
changeset 19 164783ceb9be
parent 17 c95aff79e893
child 20 a3bd39d50930
permissions -rw-r--r--
Little red arrows appear in breakpoints of edges.
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@19
    20
void BrokenEdge::set_points(Gnome::Canvas::Points p)
hegyi@19
    21
{
hegyi@19
    22
  bool set_arrow=false;
hegyi@17
    23
  if(p.size()==2)
hegyi@17
    24
    {
hegyi@19
    25
      set_arrow=true;
hegyi@17
    26
      Gnome::Canvas::Points points_with_center;
hegyi@19
    27
      points_with_center.push_back(my_points[0]=p[0]);
hegyi@19
    28
      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@19
    29
      points_with_center.push_back(my_points[2]=p[1]);
hegyi@17
    30
      property_points().set_value(points_with_center);
hegyi@19
    31
    }  
hegyi@19
    32
  if(p.size()==3)
hegyi@19
    33
    {
hegyi@19
    34
      set_arrow=true;
hegyi@19
    35
      property_points().set_value(p);
hegyi@19
    36
      for(int i=0;i<3;i++)
hegyi@19
    37
	{
hegyi@19
    38
	  my_points[i]=p[i];
hegyi@19
    39
	}
hegyi@19
    40
    }
hegyi@19
    41
hegyi@19
    42
  if(set_arrow)
hegyi@19
    43
    {
hegyi@19
    44
      //calculating coordinates of the direction indicator arrow
hegyi@19
    45
hegyi@19
    46
      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
hegyi@19
    47
      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
hegyi@19
    48
hegyi@19
    49
      xy<gdouble> unit_vector_in_dir(target-center);
hegyi@19
    50
      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
hegyi@19
    51
      unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
hegyi@19
    52
      //       std::cout << " = " << unit_vector_in_dir << std::endl;
hegyi@19
    53
hegyi@19
    54
      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
hegyi@19
    55
      //       std::cout << unit_norm_vector << std::endl;
hegyi@19
    56
hegyi@19
    57
      {      
hegyi@19
    58
	//       /\             top
hegyi@19
    59
	//      /  \
hegyi@19
    60
	//      -  -      c(enter)l(eft), ccl, ccr, cr
hegyi@19
    61
	//       ||
hegyi@19
    62
	//       ||       b(ottom)l, br
hegyi@19
    63
      }
hegyi@19
    64
hegyi@19
    65
      double size=3;
hegyi@19
    66
hegyi@19
    67
      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
hegyi@19
    68
      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
hegyi@19
    69
      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
hegyi@19
    70
      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
hegyi@19
    71
      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
hegyi@19
    72
      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
hegyi@19
    73
      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
hegyi@19
    74
	 
hegyi@19
    75
      //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
hegyi@19
    76
hegyi@19
    77
      Gnome::Canvas::Points arrow_points;
hegyi@19
    78
      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
hegyi@19
    79
      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
hegyi@19
    80
      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
hegyi@19
    81
      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
hegyi@19
    82
      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
hegyi@19
    83
      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
hegyi@19
    84
      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
hegyi@19
    85
hegyi@19
    86
      arrow->property_points().set_value(arrow_points);
hegyi@17
    87
    }
hegyi@17
    88
}
hegyi@19
    89
hegyi@19
    90
bool BrokenEdge::edge_former_event_handler(GdkEvent* e)
hegyi@19
    91
{
hegyi@19
    92
  switch(e->type)
hegyi@19
    93
    {
hegyi@19
    94
    case GDK_BUTTON_PRESS:
hegyi@19
    95
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@19
    96
      clicked_x=e->button.x;
hegyi@19
    97
      clicked_y=e->button.y;
hegyi@19
    98
      isbutton=true;
hegyi@19
    99
      break;
hegyi@19
   100
    case GDK_BUTTON_RELEASE:
hegyi@19
   101
      isbutton=false;
hegyi@19
   102
      break;
hegyi@19
   103
    case GDK_MOTION_NOTIFY:
hegyi@19
   104
      //we only have to do sg. if the mouse button is pressed
hegyi@19
   105
      if(isbutton)
hegyi@19
   106
	{
hegyi@19
   107
	  //new coordinates will be the old values,
hegyi@19
   108
	  //because the item will be moved to the
hegyi@19
   109
	  //new coordinate therefore the new movement
hegyi@19
   110
	  //has to be calculated from here
hegyi@19
   111
hegyi@19
   112
	  double dx=e->motion.x-clicked_x;
hegyi@19
   113
	  double dy=e->motion.y-clicked_y;
hegyi@19
   114
hegyi@19
   115
	  Gnome::Canvas::Points points_new;
hegyi@19
   116
hegyi@19
   117
	  points_new.push_back(my_points[0]);
hegyi@19
   118
	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
hegyi@19
   119
	  points_new.push_back(my_points[2]);
hegyi@19
   120
hegyi@19
   121
	  set_points(points_new);
hegyi@19
   122
hegyi@19
   123
	  clicked_x=e->motion.x;
hegyi@19
   124
	  clicked_y=e->motion.y;
hegyi@19
   125
hegyi@19
   126
	}
hegyi@19
   127
    default: break;
hegyi@19
   128
    }
hegyi@19
   129
hegyi@19
   130
  return true;
hegyi@19
   131
}