1.1 --- a/broken_edge.cc Wed Jun 15 13:05:32 2005 +0000
1.2 +++ b/broken_edge.cc Thu Jun 16 18:08:04 2005 +0000
1.3 @@ -1,14 +1,131 @@
1.4 #include <broken_edge.h>
1.5 +#include <lemon/xy.h>
1.6 +#include <math.h>
1.7
1.8 -BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p) : Line(g)
1.9 +BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p) : Line(g), isbutton(false)
1.10 {
1.11 + my_points=new Gnome::Art::Point[3];
1.12 +
1.13 + arrow=new Gnome::Canvas::Polygon(g);
1.14 + *arrow << Gnome::Canvas::Properties::fill_color("red");
1.15 + arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edge_former_event_handler));
1.16 + set_points(p);
1.17 +}
1.18 +
1.19 +BrokenEdge::~BrokenEdge()
1.20 +{
1.21 + if(arrow)delete(arrow);
1.22 +}
1.23 +
1.24 +void BrokenEdge::set_points(Gnome::Canvas::Points p)
1.25 +{
1.26 + bool set_arrow=false;
1.27 if(p.size()==2)
1.28 {
1.29 + set_arrow=true;
1.30 Gnome::Canvas::Points points_with_center;
1.31 - points_with_center.push_back(p[0]);
1.32 - points_with_center.push_back(Gnome::Art::Point( (p[0].get_x()+p[1].get_x())/2+30 , (p[0].get_y()+p[1].get_y())/2 )+30 );
1.33 - points_with_center.push_back(p[1]);
1.34 -
1.35 + points_with_center.push_back(my_points[0]=p[0]);
1.36 + 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 );
1.37 + points_with_center.push_back(my_points[2]=p[1]);
1.38 property_points().set_value(points_with_center);
1.39 + }
1.40 + if(p.size()==3)
1.41 + {
1.42 + set_arrow=true;
1.43 + property_points().set_value(p);
1.44 + for(int i=0;i<3;i++)
1.45 + {
1.46 + my_points[i]=p[i];
1.47 + }
1.48 + }
1.49 +
1.50 + if(set_arrow)
1.51 + {
1.52 + //calculating coordinates of the direction indicator arrow
1.53 +
1.54 + xy<gdouble> target( my_points[2].get_x(), my_points[2].get_y() );
1.55 + xy<gdouble> center( my_points[1].get_x(), my_points[1].get_y() );
1.56 +
1.57 + xy<gdouble> unit_vector_in_dir(target-center);
1.58 + // std::cout << target << " - " << center << " = " << unit_vector_in_dir << " / " <<unit_vector_in_dir.normSquare() ;
1.59 + unit_vector_in_dir/=sqrt( unit_vector_in_dir.normSquare() );
1.60 + // std::cout << " = " << unit_vector_in_dir << std::endl;
1.61 +
1.62 + xy<gdouble> unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x);
1.63 + // std::cout << unit_norm_vector << std::endl;
1.64 +
1.65 + {
1.66 + // /\ top
1.67 + // / \
1.68 + // - - c(enter)l(eft), ccl, ccr, cr
1.69 + // ||
1.70 + // || b(ottom)l, br
1.71 + }
1.72 +
1.73 + double size=3;
1.74 +
1.75 + xy<gdouble> bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size );
1.76 + xy<gdouble> br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size );
1.77 + xy<gdouble> ccl(center + unit_vector_in_dir * size + unit_norm_vector * size );
1.78 + xy<gdouble> ccr(center + unit_vector_in_dir * size - unit_norm_vector * size );
1.79 + xy<gdouble> cl (center + unit_vector_in_dir * size + unit_norm_vector * 2 * size );
1.80 + xy<gdouble> cr (center + unit_vector_in_dir * size - unit_norm_vector * 2 * size );
1.81 + xy<gdouble> top(center + unit_vector_in_dir * 3 * size);
1.82 +
1.83 + // std::cout << bl << " " << br << " " << ccl << " " << ccr << " " << cl << " " << cr << " " << top << std::endl;
1.84 +
1.85 + Gnome::Canvas::Points arrow_points;
1.86 + arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y ) );
1.87 + arrow_points.push_back(Gnome::Art::Point( br.x , br.y ) );
1.88 + arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) );
1.89 + arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y ) );
1.90 + arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) );
1.91 + arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y ) );
1.92 + arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) );
1.93 +
1.94 + arrow->property_points().set_value(arrow_points);
1.95 }
1.96 }
1.97 +
1.98 +bool BrokenEdge::edge_former_event_handler(GdkEvent* e)
1.99 +{
1.100 + switch(e->type)
1.101 + {
1.102 + case GDK_BUTTON_PRESS:
1.103 + //we mark the location of the event to be able to calculate parameters of dragging
1.104 + clicked_x=e->button.x;
1.105 + clicked_y=e->button.y;
1.106 + isbutton=true;
1.107 + break;
1.108 + case GDK_BUTTON_RELEASE:
1.109 + isbutton=false;
1.110 + break;
1.111 + case GDK_MOTION_NOTIFY:
1.112 + //we only have to do sg. if the mouse button is pressed
1.113 + if(isbutton)
1.114 + {
1.115 + //new coordinates will be the old values,
1.116 + //because the item will be moved to the
1.117 + //new coordinate therefore the new movement
1.118 + //has to be calculated from here
1.119 +
1.120 + double dx=e->motion.x-clicked_x;
1.121 + double dy=e->motion.y-clicked_y;
1.122 +
1.123 + Gnome::Canvas::Points points_new;
1.124 +
1.125 + points_new.push_back(my_points[0]);
1.126 + points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy));
1.127 + points_new.push_back(my_points[2]);
1.128 +
1.129 + set_points(points_new);
1.130 +
1.131 + clicked_x=e->motion.x;
1.132 + clicked_y=e->motion.y;
1.133 +
1.134 + }
1.135 + default: break;
1.136 + }
1.137 +
1.138 + return true;
1.139 +}