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