1.1 --- a/src/work/peter/canvas-test.cc Thu Mar 17 11:45:05 2005 +0000
1.2 +++ b/src/work/peter/canvas-test.cc Thu Mar 17 17:20:37 2005 +0000
1.3 @@ -18,10 +18,10 @@
1.4 private:
1.5
1.6 ///Event handler function that handles dragging nodes of triangle
1.7 - bool event_handler(GdkEvent* e, bool b);
1.8 + bool event_handler(GdkEvent* e, int b);
1.9
1.10 ///Event handler function that handles dragging triangle
1.11 - bool tri_mover(GdkEvent* e, bool b);
1.12 + bool tri_mover(GdkEvent* e);
1.13
1.14 ///Coordinates of Weight Point of tirangle
1.15 Gnome::Art::Point * wp;
1.16 @@ -38,6 +38,9 @@
1.17 ///Number Of Elements - the number of nodes
1.18 int noe;
1.19
1.20 + ///Array of coordinates
1.21 + double * coordinates;
1.22 +
1.23 ///At this location was the mousebutton pressed.
1.24 ///It helps to calculate the distance of dragging.
1.25 double clicked_x, clicked_y;
1.26 @@ -52,9 +55,8 @@
1.27 };
1.28
1.29 ///When we click on the weight point we can drag the whole triangle. This function resolves it.
1.30 -bool CanvasExample::tri_mover(GdkEvent* e, bool b)
1.31 +bool CanvasExample::tri_mover(GdkEvent* e)
1.32 {
1.33 - b=b;
1.34 switch(e->type)
1.35 {
1.36 case GDK_BUTTON_PRESS:
1.37 @@ -71,24 +73,24 @@
1.38 {
1.39 double dx=e->motion.x-clicked_x;
1.40 double dy=e->motion.y-clicked_y;
1.41 +
1.42 + Gnome::Canvas::Points coos;
1.43 +
1.44 for(int i=0;i<=noe;i++)
1.45 {
1.46 nodes[i]->move(dx,dy);
1.47 +
1.48 + double x=(coordinates[2*i]+=dx);
1.49 + double y=(coordinates[2*i+1]+=dy);
1.50 +
1.51 + if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
1.52 +
1.53 }
1.54 +
1.55 clicked_x=e->motion.x;
1.56 clicked_y=e->motion.y;
1.57
1.58 - Gnome::Canvas::Points coos;
1.59 - for(int i=0;i<noe;i++)
1.60 - {
1.61 - double x1,y1,x2,y2;
1.62 - nodes[i]->get_bounds(x1,y1,x2,y2);
1.63 - double x=(x1+x2)/2;
1.64 - double y=(y1+y2)/2;
1.65 - coos.push_back(Gnome::Art::Point(x, y));
1.66 - }
1.67 sides->property_points().set_value(coos);
1.68 -
1.69 }
1.70 default: break;
1.71 }
1.72 @@ -98,9 +100,8 @@
1.73 ///This function moves only one node of triangle,
1.74 ///but recalculate the location of wight point,
1.75 ///and also redraw the sides of the planefigure.
1.76 -bool CanvasExample::event_handler(GdkEvent* e, bool b)
1.77 +bool CanvasExample::event_handler(GdkEvent* e, int b)
1.78 {
1.79 - b=b;
1.80 switch(e->type)
1.81 {
1.82 case GDK_BUTTON_PRESS:
1.83 @@ -124,6 +125,9 @@
1.84 double dy=e->motion.y-clicked_y;
1.85 active_item->move(dx, dy);
1.86
1.87 + coordinates[2*b]+=dx;
1.88 + coordinates[2*b+1]+=dy;
1.89 +
1.90 Gnome::Canvas::Points coos;
1.91
1.92 double x_wp=0;
1.93 @@ -131,14 +135,10 @@
1.94
1.95 for(int i=0;i<noe;i++)
1.96 {
1.97 - double x1,y1,x2,y2;
1.98 - nodes[i]->get_bounds(x1,y1,x2,y2);
1.99 - double x=(x1+x2)/2;
1.100 - double y=(y1+y2)/2;
1.101 - coos.push_back(Gnome::Art::Point(x, y));
1.102 + coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
1.103
1.104 - x_wp+=x;
1.105 - y_wp+=y;
1.106 + x_wp+=coordinates[2*i];
1.107 + y_wp+=coordinates[2*i+1];
1.108 }
1.109
1.110 sides->property_points().set_value(coos);
1.111 @@ -146,14 +146,12 @@
1.112 x_wp/=noe;
1.113 y_wp/=noe;
1.114
1.115 - double x1,y1,x2,y2;
1.116 - nodes[noe]->get_bounds(x1,y1,x2,y2);
1.117 - double x=(x1+x2)/2;
1.118 - double y=(y1+y2)/2;
1.119 + dx=x_wp-coordinates[noe*2];
1.120 + dy=y_wp-coordinates[noe*2+1];
1.121 + nodes[noe]->move(dx, dy);
1.122
1.123 - dx=x_wp-x;
1.124 - dy=y_wp-y;
1.125 - nodes[noe]->move(dx, dy);
1.126 + coordinates[noe*2]+=dx;
1.127 + coordinates[noe*2+1]+=dy;
1.128
1.129 clicked_x=e->motion.x;
1.130 clicked_y=e->motion.y;
1.131 @@ -167,18 +165,20 @@
1.132 {
1.133 noe=numofcoos/2;
1.134
1.135 + coordinates=new double [numofcoos+2];
1.136 +
1.137 double x_wp=0;
1.138 double y_wp=0;
1.139
1.140 Gnome::Canvas::Points coos;
1.141 for(int i=0;i<numofcoos;i++)
1.142 {
1.143 - double x=coosarray[i++];
1.144 - double y=coosarray[i];
1.145 - coos.push_back(Gnome::Art::Point(x, y));
1.146 + coordinates[i]=coosarray[i++];
1.147 + coordinates[i]=coosarray[i];
1.148 + coos.push_back(Gnome::Art::Point(coordinates[i-1], coordinates[i]));
1.149
1.150 - x_wp+=x;
1.151 - y_wp+=y;
1.152 + x_wp+=coordinates[i-1];
1.153 + y_wp+=coordinates[i];
1.154
1.155 }
1.156
1.157 @@ -193,15 +193,26 @@
1.158 nodes[i]= new Gnome::Canvas::Ellipse(triangle, coos[i].get_x()-20, coos[i].get_y()-20, coos[i].get_x()+20, coos[i].get_y()+20);
1.159 *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
1.160 *(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
1.161 - (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
1.162 + (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i));
1.163 }
1.164
1.165 - wp=new Gnome::Art::Point(x_wp/noe,y_wp/noe);
1.166 + coordinates[numofcoos]=x_wp/noe;
1.167 + coordinates[numofcoos+1]=y_wp/noe;
1.168
1.169 - nodes[noe]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
1.170 + wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]);
1.171 +
1.172 + nodes[noe]= new Gnome::Canvas::Ellipse
1.173 + (
1.174 + triangle,
1.175 + coordinates[numofcoos]-20,
1.176 + coordinates[numofcoos+1]-20,
1.177 + coordinates[numofcoos]+20,
1.178 + coordinates[numofcoos+1]+20
1.179 + );
1.180 *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
1.181 *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
1.182 - (nodes[noe])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
1.183 + (nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover));
1.184 +
1.185
1.186
1.187 }