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