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