[89] | 1 | #include "graph_displayer_canvas.h" |
---|
[59] | 2 | #include <cmath> |
---|
[17] | 3 | |
---|
[98] | 4 | GraphDisplayerCanvas::BrokenEdge::BrokenEdge(Gnome::Canvas::Group & g, Edge _edge, GraphDisplayerCanvas & gc) : Line(g), edge(_edge), gdc(gc), isbutton(false) |
---|
[17] | 5 | { |
---|
[19] | 6 | arrow=new Gnome::Canvas::Polygon(g); |
---|
| 7 | *arrow << Gnome::Canvas::Properties::fill_color("red"); |
---|
[89] | 8 | arrow->signal_event().connect(sigc::mem_fun(*this, &GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler)); |
---|
[63] | 9 | arrow->lower_to_bottom(); |
---|
[98] | 10 | draw(); |
---|
[19] | 11 | } |
---|
| 12 | |
---|
[89] | 13 | GraphDisplayerCanvas::BrokenEdge::~BrokenEdge() |
---|
[19] | 14 | { |
---|
| 15 | if(arrow)delete(arrow); |
---|
| 16 | } |
---|
| 17 | |
---|
[98] | 18 | void GraphDisplayerCanvas::BrokenEdge::draw() |
---|
[19] | 19 | { |
---|
[98] | 20 | MapStorage& ms = gdc.mytab.mapstorage; |
---|
| 21 | |
---|
| 22 | // update the edge |
---|
| 23 | { |
---|
| 24 | Gnome::Canvas::Points points; |
---|
| 25 | Node source = ms.graph.source(edge); |
---|
| 26 | Node target = ms.graph.target(edge); |
---|
| 27 | points.push_back(Gnome::Art::Point(ms.coords[source].x, |
---|
| 28 | ms.coords[source].y)); |
---|
| 29 | points.push_back(Gnome::Art::Point(ms.arrow_pos[edge].x, |
---|
| 30 | ms.arrow_pos[edge].y)); |
---|
| 31 | points.push_back(Gnome::Art::Point(ms.coords[target].x, |
---|
| 32 | ms.coords[target].y)); |
---|
| 33 | property_points().set_value(points); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | // update the arrow |
---|
| 37 | { |
---|
| 38 | //calculating coordinates of the direction indicator arrow |
---|
| 39 | XY target(ms.coords[ms.graph.target(edge)]); |
---|
| 40 | XY center(ms.arrow_pos[edge]); |
---|
| 41 | |
---|
| 42 | XY unit_vector_in_dir(target-center); |
---|
| 43 | double length=sqrt( unit_vector_in_dir.normSquare() ); |
---|
| 44 | |
---|
| 45 | // std::cout << target << " - " << center << " = " << unit_vector_in_dir << " / " <<unit_vector_in_dir.normSquare() ; |
---|
| 46 | unit_vector_in_dir/=length; |
---|
| 47 | // std::cout << " = " << unit_vector_in_dir << std::endl; |
---|
| 48 | |
---|
| 49 | XY unit_norm_vector(0-unit_vector_in_dir.y, unit_vector_in_dir.x); |
---|
| 50 | // std::cout << unit_norm_vector << std::endl; |
---|
| 51 | |
---|
| 52 | { |
---|
| 53 | // /\ // top |
---|
| 54 | // / \ // |
---|
| 55 | // - - // c(enter)l(eft), ccl, ccr, cr |
---|
| 56 | // || // |
---|
| 57 | // || // b(ottom)l, br |
---|
[20] | 58 | } |
---|
[50] | 59 | |
---|
[98] | 60 | double size=3; |
---|
[50] | 61 | |
---|
[98] | 62 | XY bl (center - unit_vector_in_dir * 3 * size + unit_norm_vector * size ); |
---|
| 63 | XY br (center - unit_vector_in_dir * 3 * size - unit_norm_vector * size ); |
---|
| 64 | XY ccl(center + unit_vector_in_dir * size + unit_norm_vector * size ); |
---|
| 65 | XY ccr(center + unit_vector_in_dir * size - unit_norm_vector * size ); |
---|
| 66 | XY cl (center + unit_vector_in_dir * size + unit_norm_vector * 2 * size ); |
---|
| 67 | XY cr (center + unit_vector_in_dir * size - unit_norm_vector * 2 * size ); |
---|
| 68 | XY top(center + unit_vector_in_dir * 3 * size); |
---|
[50] | 69 | |
---|
[98] | 70 | //std::cout << bl << " " << br << " " << ccl << " " << ccr << " " << cl << " " << cr << " " << top << std::endl; |
---|
[50] | 71 | |
---|
[98] | 72 | Gnome::Canvas::Points arrow_points; |
---|
| 73 | arrow_points.push_back(Gnome::Art::Point( bl.x , bl.y ) ); |
---|
| 74 | arrow_points.push_back(Gnome::Art::Point( br.x , br.y ) ); |
---|
| 75 | arrow_points.push_back(Gnome::Art::Point( ccr.x, ccr.y ) ); |
---|
| 76 | arrow_points.push_back(Gnome::Art::Point( cr.x , cr.y ) ); |
---|
| 77 | arrow_points.push_back(Gnome::Art::Point( top.x, top.y ) ); |
---|
| 78 | arrow_points.push_back(Gnome::Art::Point( cl.x , cl.y ) ); |
---|
| 79 | arrow_points.push_back(Gnome::Art::Point( ccl.x, ccl.y ) ); |
---|
[50] | 80 | |
---|
[98] | 81 | arrow->property_points().set_value(arrow_points); |
---|
| 82 | } |
---|
[17] | 83 | } |
---|
[19] | 84 | |
---|
[89] | 85 | bool GraphDisplayerCanvas::BrokenEdge::edgeFormerEventHandler(GdkEvent* e) |
---|
[19] | 86 | { |
---|
| 87 | switch(e->type) |
---|
| 88 | { |
---|
| 89 | case GDK_BUTTON_PRESS: |
---|
| 90 | //we mark the location of the event to be able to calculate parameters of dragging |
---|
[30] | 91 | if(gdc.getActualTool()!=CREATE_NODE) |
---|
[21] | 92 | { |
---|
[30] | 93 | gdc.toggleEdgeActivity(this, true); |
---|
[21] | 94 | clicked_x=e->button.x; |
---|
| 95 | clicked_y=e->button.y; |
---|
| 96 | isbutton=true; |
---|
| 97 | } |
---|
[19] | 98 | break; |
---|
| 99 | case GDK_BUTTON_RELEASE: |
---|
[30] | 100 | if(gdc.getActualTool()!=CREATE_NODE) |
---|
[25] | 101 | { |
---|
[30] | 102 | gdc.toggleEdgeActivity(this, false); |
---|
[25] | 103 | isbutton=false; |
---|
| 104 | } |
---|
[19] | 105 | break; |
---|
| 106 | case GDK_MOTION_NOTIFY: |
---|
| 107 | //we only have to do sg. if the mouse button is pressed |
---|
| 108 | if(isbutton) |
---|
| 109 | { |
---|
| 110 | //new coordinates will be the old values, |
---|
| 111 | //because the item will be moved to the |
---|
| 112 | //new coordinate therefore the new movement |
---|
| 113 | //has to be calculated from here |
---|
| 114 | |
---|
| 115 | double dx=e->motion.x-clicked_x; |
---|
| 116 | double dy=e->motion.y-clicked_y; |
---|
| 117 | |
---|
| 118 | Gnome::Canvas::Points points_new; |
---|
| 119 | |
---|
[98] | 120 | gdc.mytab.mapstorage.arrow_pos.set(edge, gdc.mytab.mapstorage.arrow_pos[edge] + XY(dx, dy)); |
---|
[19] | 121 | |
---|
[98] | 122 | draw(); |
---|
| 123 | gdc.textReposition(gdc.mytab.mapstorage.arrow_pos[edge]); |
---|
[19] | 124 | |
---|
| 125 | clicked_x=e->motion.x; |
---|
| 126 | clicked_y=e->motion.y; |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | default: break; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return true; |
---|
| 133 | } |
---|