broken_edge.cc
author hegyi
Thu, 06 Oct 2005 12:32:06 +0000
branchgui
changeset 80 231191361de5
parent 63 59768817442a
permissions -rw-r--r--
Shape keeping movement is implemented, at last. Many thanks to Alpar.
ladanyi@53
     1
#include "broken_edge.h"
alpar@59
     2
#include <cmath>
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));
ladanyi@63
    11
  arrow->lower_to_bottom();
hegyi@30
    12
  setPoints(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@30
    20
void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move)
hegyi@19
    21
{
hegyi@19
    22
  bool set_arrow=false;
hegyi@50
    23
  //red arrow losts its position-right button
hegyi@20
    24
  if(!move)
hegyi@17
    25
    {
hegyi@20
    26
      if(p.size()==2)
hegyi@20
    27
	{
hegyi@20
    28
	  set_arrow=true;
hegyi@20
    29
	  Gnome::Canvas::Points points_with_center;
hegyi@20
    30
	  points_with_center.push_back(my_points[0]=p[0]);
hegyi@20
    31
	  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
    32
	  points_with_center.push_back(my_points[2]=p[1]);
hegyi@20
    33
	  property_points().set_value(points_with_center);
hegyi@20
    34
	}  
hegyi@20
    35
      if(p.size()==3)
hegyi@20
    36
	{
hegyi@20
    37
	  set_arrow=true;
hegyi@20
    38
	  property_points().set_value(p);
hegyi@20
    39
	  for(int i=0;i<3;i++)
hegyi@20
    40
	    {
hegyi@20
    41
	      my_points[i]=p[i];
hegyi@20
    42
	    }
hegyi@20
    43
	}
hegyi@20
    44
    }
hegyi@20
    45
  else
hegyi@19
    46
    {
hegyi@50
    47
      //arrow keeps its position-left button
hegyi@50
    48
hegyi@80
    49
//       if(p.size()==2)
hegyi@80
    50
//       	{
hegyi@80
    51
//       	  Gnome::Canvas::Points points;
hegyi@80
    52
//       	  my_points[0]=p[0];
hegyi@80
    53
//       	  my_points[2]=p[1];
hegyi@80
    54
//       	  for(int i=0;i<3;i++)
hegyi@80
    55
//       	    {
hegyi@80
    56
//       	      points.push_back(my_points[i]);
hegyi@80
    57
//       	    }
hegyi@80
    58
//       	  property_points().set_value(points);
hegyi@80
    59
//       	}
hegyi@51
    60
      set_arrow=true;
hegyi@50
    61
hegyi@50
    62
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@80
    63
      /////////// keeps shape-with scalar multiplication - version 2.
hegyi@50
    64
      //////////////////////////////////////////////////////////////////////////////////////////////////////
hegyi@50
    65
hegyi@80
    66
      if(p.size()==2)
hegyi@80
    67
      	{
hegyi@80
    68
	  //old vector from one to the other node - a
hegyi@80
    69
	  xy<double> a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y());
hegyi@80
    70
	  //new vector from one to the other node - b
hegyi@80
    71
	  xy<double> b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y());
hegyi@50
    72
hegyi@80
    73
	  double absa=sqrt(a_v.normSquare());
hegyi@80
    74
	  double absb=sqrt(b_v.normSquare());
hegyi@50
    75
hegyi@80
    76
	  //old vector from one node to the breakpoint - c
hegyi@80
    77
	  xy<double> c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y());
hegyi@50
    78
hegyi@80
    79
	  //unit vector with the same direction to a_v
hegyi@80
    80
	  xy<double> a_v_u(a_v.x/absa,a_v.y/absa);
hegyi@50
    81
hegyi@80
    82
	  //normal vector of unit vector with the same direction to a_v
hegyi@80
    83
	  xy<double> a_v_u_n(((-1)*a_v_u.y),a_v_u.x);
hegyi@50
    84
hegyi@80
    85
	  //unit vector with the same direction to b_v
hegyi@80
    86
	  xy<double> b_v_u(b_v.x/absb,b_v.y/absb);
hegyi@50
    87
hegyi@80
    88
	  //normal vector of unit vector with the same direction to b_v
hegyi@80
    89
	  xy<double> b_v_u_n(((-1)*b_v_u.y),b_v_u.x);
hegyi@50
    90
hegyi@80
    91
	  //vector c in a_v_u and a_v_u_n co-ordinate system
hegyi@80
    92
	  xy<double> c_a(c_v*a_v_u,c_v*a_v_u_n);
hegyi@50
    93
hegyi@80
    94
	  //new vector from one node to the breakpoint - d - we have to calculate this one
hegyi@80
    95
	  xy<double> d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n);
hegyi@50
    96
hegyi@80
    97
	  my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y());
hegyi@50
    98
hegyi@80
    99
      	  my_points[0]=p[0];
hegyi@80
   100
      	  my_points[2]=p[1];
hegyi@50
   101
hegyi@80
   102
	  Gnome::Canvas::Points points;
hegyi@80
   103
	  for(int i=0;i<3;i++)
hegyi@80
   104
	    {
hegyi@80
   105
	      points.push_back(my_points[i]);
hegyi@80
   106
	    }
hegyi@80
   107
	  property_points().set_value(points);
hegyi@80
   108
	}
hegyi@19
   109
    }
hegyi@19
   110
  if(set_arrow)
hegyi@19
   111
    {
hegyi@19
   112
      //calculating coordinates of the direction indicator arrow
hegyi@19
   113
hegyi@19
   114
      xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
hegyi@19
   115
      xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
hegyi@19
   116
hegyi@19
   117
      xy<gdouble> unit_vector_in_dir(target-center);
hegyi@19
   118
      //       std::cout << target << " - " << center << " = " << unit_vector_in_dir << "    / " <<unit_vector_in_dir.normSquare() ;
hegyi@19
   119
      unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
hegyi@19
   120
      //       std::cout << " = " << unit_vector_in_dir << std::endl;
hegyi@19
   121
hegyi@19
   122
      xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
hegyi@19
   123
      //       std::cout << unit_norm_vector << std::endl;
hegyi@19
   124
hegyi@19
   125
      {      
hegyi@24
   126
	//       /\       // top
hegyi@24
   127
	//      /  \      //
hegyi@24
   128
	//      -  -      // c(enter)l(eft), ccl, ccr, cr
hegyi@24
   129
	//       ||       //
hegyi@24
   130
	//       ||       // b(ottom)l, br
hegyi@19
   131
      }
hegyi@19
   132
hegyi@19
   133
      double size=3;
hegyi@19
   134
hegyi@19
   135
      xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
hegyi@19
   136
      xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
hegyi@19
   137
      xy<gdouble> ccl(center + unit_vector_in_dir *  size + unit_norm_vector * size );
hegyi@19
   138
      xy<gdouble> ccr(center + unit_vector_in_dir *  size - unit_norm_vector * size );
hegyi@19
   139
      xy<gdouble> cl (center + unit_vector_in_dir *  size + unit_norm_vector * 2 * size );
hegyi@19
   140
      xy<gdouble> cr (center + unit_vector_in_dir *  size - unit_norm_vector * 2 * size );
hegyi@19
   141
      xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
hegyi@19
   142
	 
hegyi@19
   143
      //       std::cout << bl << " " << br << " " << ccl << " "  << ccr << " " << cl << " " << cr << " " << top << std::endl;
hegyi@19
   144
hegyi@19
   145
      Gnome::Canvas::Points arrow_points;
hegyi@19
   146
      arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y  ) );
hegyi@19
   147
      arrow_points.push_back(Gnome::Art::Point( br.x , br.y  ) );
hegyi@19
   148
      arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
hegyi@19
   149
      arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y  ) );
hegyi@19
   150
      arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
hegyi@19
   151
      arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y  ) );
hegyi@19
   152
      arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
hegyi@19
   153
hegyi@19
   154
      arrow->property_points().set_value(arrow_points);
hegyi@17
   155
    }
hegyi@17
   156
}
hegyi@19
   157
hegyi@30
   158
bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e)
hegyi@19
   159
{
hegyi@19
   160
  switch(e->type)
hegyi@19
   161
    {
hegyi@19
   162
    case GDK_BUTTON_PRESS:
hegyi@19
   163
      //we mark the location of the event to be able to calculate parameters of dragging
hegyi@30
   164
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@21
   165
	{
hegyi@30
   166
	  gdc.toggleEdgeActivity(this, true);
hegyi@21
   167
	  clicked_x=e->button.x;
hegyi@21
   168
	  clicked_y=e->button.y;
hegyi@21
   169
	  isbutton=true;
hegyi@21
   170
	}
hegyi@19
   171
      break;
hegyi@19
   172
    case GDK_BUTTON_RELEASE:
hegyi@30
   173
      if(gdc.getActualTool()!=CREATE_NODE)
hegyi@25
   174
	{
hegyi@30
   175
	  gdc.toggleEdgeActivity(this, false);
hegyi@25
   176
	  isbutton=false;
hegyi@25
   177
	}
hegyi@19
   178
      break;
hegyi@19
   179
    case GDK_MOTION_NOTIFY:
hegyi@19
   180
      //we only have to do sg. if the mouse button is pressed
hegyi@19
   181
      if(isbutton)
hegyi@19
   182
	{
hegyi@19
   183
	  //new coordinates will be the old values,
hegyi@19
   184
	  //because the item will be moved to the
hegyi@19
   185
	  //new coordinate therefore the new movement
hegyi@19
   186
	  //has to be calculated from here
hegyi@19
   187
hegyi@19
   188
	  double dx=e->motion.x-clicked_x;
hegyi@19
   189
	  double dy=e->motion.y-clicked_y;
hegyi@19
   190
hegyi@19
   191
	  Gnome::Canvas::Points points_new;
hegyi@19
   192
hegyi@19
   193
	  points_new.push_back(my_points[0]);
hegyi@19
   194
	  points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
hegyi@19
   195
	  points_new.push_back(my_points[2]);
hegyi@19
   196
hegyi@30
   197
	  setPoints(points_new);
hegyi@30
   198
	  gdc.textReposition(xy<double>(my_points[1].get_x(),my_points[1].get_y()));
hegyi@19
   199
hegyi@19
   200
	  clicked_x=e->motion.x;
hegyi@19
   201
	  clicked_y=e->motion.y;
hegyi@19
   202
hegyi@19
   203
	}
hegyi@19
   204
    default: break;
hegyi@19
   205
    }
hegyi@19
   206
hegyi@19
   207
  return true;
hegyi@19
   208
}
hegyi@25
   209
hegyi@30
   210
xy<double> BrokenEdge::getArrowPos()
hegyi@25
   211
{
hegyi@25
   212
  xy<double> ret_val(my_points[1].get_x(),my_points[1].get_y());
hegyi@25
   213
  return ret_val;
hegyi@25
   214
}