ladanyi@1606: #include "broken_edge.h" alpar@1632: #include hegyi@1497: hegyi@1501: BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Gnome::Canvas::Points p, GraphDisplayerCanvas & gc) : Line(g), gdc(gc), isbutton(false) hegyi@1497: { hegyi@1499: my_points=new Gnome::Art::Point[3]; hegyi@1499: hegyi@1499: arrow=new Gnome::Canvas::Polygon(g); hegyi@1499: *arrow << Gnome::Canvas::Properties::fill_color("red"); hegyi@1524: arrow->signal_event().connect(sigc::mem_fun(*this, &BrokenEdge::edgeFormerEventHandler)); ladanyi@1645: arrow->lower_to_bottom(); hegyi@1524: setPoints(p); hegyi@1499: } hegyi@1499: hegyi@1499: BrokenEdge::~BrokenEdge() hegyi@1499: { hegyi@1499: if(arrow)delete(arrow); hegyi@1499: } hegyi@1499: hegyi@1524: void BrokenEdge::setPoints(Gnome::Canvas::Points p, bool move) hegyi@1499: { hegyi@1499: bool set_arrow=false; hegyi@1601: //red arrow losts its position-right button hegyi@1500: if(!move) hegyi@1497: { hegyi@1500: if(p.size()==2) hegyi@1500: { hegyi@1500: set_arrow=true; hegyi@1500: Gnome::Canvas::Points points_with_center; hegyi@1500: points_with_center.push_back(my_points[0]=p[0]); hegyi@1500: 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: points_with_center.push_back(my_points[2]=p[1]); hegyi@1500: property_points().set_value(points_with_center); hegyi@1500: } hegyi@1500: if(p.size()==3) hegyi@1500: { hegyi@1500: set_arrow=true; hegyi@1500: property_points().set_value(p); hegyi@1500: for(int i=0;i<3;i++) hegyi@1500: { hegyi@1500: my_points[i]=p[i]; hegyi@1500: } hegyi@1500: } hegyi@1500: } hegyi@1500: else hegyi@1499: { hegyi@1601: //arrow keeps its position-left button hegyi@1601: hegyi@1714: // if(p.size()==2) hegyi@1714: // { hegyi@1714: // Gnome::Canvas::Points points; hegyi@1714: // my_points[0]=p[0]; hegyi@1714: // my_points[2]=p[1]; hegyi@1714: // for(int i=0;i<3;i++) hegyi@1714: // { hegyi@1714: // points.push_back(my_points[i]); hegyi@1714: // } hegyi@1714: // property_points().set_value(points); hegyi@1714: // } hegyi@1602: set_arrow=true; hegyi@1601: hegyi@1601: ////////////////////////////////////////////////////////////////////////////////////////////////////// hegyi@1714: /////////// keeps shape-with scalar multiplication - version 2. hegyi@1601: ////////////////////////////////////////////////////////////////////////////////////////////////////// hegyi@1601: hegyi@1714: if(p.size()==2) hegyi@1714: { hegyi@1714: //old vector from one to the other node - a hegyi@1714: xy a_v(my_points[2].get_x()-my_points[0].get_x(),my_points[2].get_y()-my_points[0].get_y()); hegyi@1714: //new vector from one to the other node - b hegyi@1714: xy b_v(p[1].get_x()-p[0].get_x(),p[1].get_y()-p[0].get_y()); hegyi@1601: hegyi@1714: double absa=sqrt(a_v.normSquare()); hegyi@1714: double absb=sqrt(b_v.normSquare()); hegyi@1601: hegyi@1714: //old vector from one node to the breakpoint - c hegyi@1714: xy c_v(my_points[1].get_x()-my_points[0].get_x(),my_points[1].get_y()-my_points[0].get_y()); hegyi@1601: hegyi@1714: //unit vector with the same direction to a_v hegyi@1714: xy a_v_u(a_v.x/absa,a_v.y/absa); hegyi@1601: hegyi@1714: //normal vector of unit vector with the same direction to a_v hegyi@1714: xy a_v_u_n(((-1)*a_v_u.y),a_v_u.x); hegyi@1601: hegyi@1714: //unit vector with the same direction to b_v hegyi@1714: xy b_v_u(b_v.x/absb,b_v.y/absb); hegyi@1601: hegyi@1714: //normal vector of unit vector with the same direction to b_v hegyi@1714: xy b_v_u_n(((-1)*b_v_u.y),b_v_u.x); hegyi@1601: hegyi@1714: //vector c in a_v_u and a_v_u_n co-ordinate system hegyi@1714: xy c_a(c_v*a_v_u,c_v*a_v_u_n); hegyi@1601: hegyi@1714: //new vector from one node to the breakpoint - d - we have to calculate this one hegyi@1714: xy d_v=absb/absa*(c_a.x*b_v_u+c_a.y*b_v_u_n); hegyi@1601: hegyi@1714: my_points[1]=Gnome::Art::Point(d_v.x+p[0].get_x(),d_v.y+p[0].get_y()); hegyi@1601: hegyi@1714: my_points[0]=p[0]; hegyi@1714: my_points[2]=p[1]; hegyi@1601: hegyi@1714: Gnome::Canvas::Points points; hegyi@1714: for(int i=0;i<3;i++) hegyi@1714: { hegyi@1714: points.push_back(my_points[i]); hegyi@1714: } hegyi@1714: property_points().set_value(points); hegyi@1714: } hegyi@1499: } hegyi@1499: if(set_arrow) hegyi@1499: { hegyi@1499: //calculating coordinates of the direction indicator arrow hegyi@1499: hegyi@1499: xy target( my_points[2].get_x(), my_points[2].get_y() ); hegyi@1499: xy center( my_points[1].get_x(), my_points[1].get_y() ); hegyi@1499: hegyi@1499: xy unit_vector_in_dir(target-center); hegyi@1499: // std::cout << target << " - " << center << " = " << unit_vector_in_dir << " / " <property_points().set_value(arrow_points); hegyi@1497: } hegyi@1497: } hegyi@1499: hegyi@1524: bool BrokenEdge::edgeFormerEventHandler(GdkEvent* e) hegyi@1499: { hegyi@1499: switch(e->type) hegyi@1499: { hegyi@1499: case GDK_BUTTON_PRESS: hegyi@1499: //we mark the location of the event to be able to calculate parameters of dragging hegyi@1524: if(gdc.getActualTool()!=CREATE_NODE) hegyi@1501: { hegyi@1524: gdc.toggleEdgeActivity(this, true); hegyi@1501: clicked_x=e->button.x; hegyi@1501: clicked_y=e->button.y; hegyi@1501: isbutton=true; hegyi@1501: } hegyi@1499: break; hegyi@1499: case GDK_BUTTON_RELEASE: hegyi@1524: if(gdc.getActualTool()!=CREATE_NODE) hegyi@1505: { hegyi@1524: gdc.toggleEdgeActivity(this, false); hegyi@1505: isbutton=false; hegyi@1505: } hegyi@1499: break; hegyi@1499: case GDK_MOTION_NOTIFY: hegyi@1499: //we only have to do sg. if the mouse button is pressed hegyi@1499: if(isbutton) hegyi@1499: { hegyi@1499: //new coordinates will be the old values, hegyi@1499: //because the item will be moved to the hegyi@1499: //new coordinate therefore the new movement hegyi@1499: //has to be calculated from here hegyi@1499: hegyi@1499: double dx=e->motion.x-clicked_x; hegyi@1499: double dy=e->motion.y-clicked_y; hegyi@1499: hegyi@1499: Gnome::Canvas::Points points_new; hegyi@1499: hegyi@1499: points_new.push_back(my_points[0]); hegyi@1499: points_new.push_back(my_points[1]=Gnome::Art::Point(my_points[1].get_x()+dx,my_points[1].get_y()+dy)); hegyi@1499: points_new.push_back(my_points[2]); hegyi@1499: hegyi@1524: setPoints(points_new); hegyi@1524: gdc.textReposition(xy(my_points[1].get_x(),my_points[1].get_y())); hegyi@1499: hegyi@1499: clicked_x=e->motion.x; hegyi@1499: clicked_y=e->motion.y; hegyi@1499: hegyi@1499: } hegyi@1499: default: break; hegyi@1499: } hegyi@1499: hegyi@1499: return true; hegyi@1499: } hegyi@1505: hegyi@1524: xy BrokenEdge::getArrowPos() hegyi@1505: { hegyi@1505: xy ret_val(my_points[1].get_x(),my_points[1].get_y()); hegyi@1505: return ret_val; hegyi@1505: }