src/work/peter/canvas-test.cc
changeset 1224 7f4f2855fa11
parent 1221 6706c788ebb5
child 1225 4401e1aeafcf
     1.1 --- a/src/work/peter/canvas-test.cc	Thu Mar 17 10:46:57 2005 +0000
     1.2 +++ b/src/work/peter/canvas-test.cc	Thu Mar 17 11:45:05 2005 +0000
     1.3 @@ -16,26 +16,45 @@
     1.4  	virtual ~CanvasExample();
     1.5  
     1.6  private:
     1.7 +
     1.8 +	///Event handler function that handles dragging nodes of triangle
     1.9 +	bool event_handler(GdkEvent* e, bool b);
    1.10 +
    1.11 +	///Event handler function that handles dragging triangle
    1.12 +	bool tri_mover(GdkEvent* e, bool b);
    1.13 +
    1.14 +	///Coordinates of Weight Point of tirangle
    1.15 +	Gnome::Art::Point * wp;
    1.16 +	///Array of nodes of planefigure
    1.17 +	Gnome::Canvas::Ellipse ** nodes;
    1.18 +	///Sides of planefigure
    1.19 +	Gnome::Canvas::Polygon * sides;
    1.20 +	///Group of graphical elements of triangle
    1.21 +	Gnome::Canvas::Group triangle;
    1.22 +
    1.23 +	///Indicates whether the button of mouse is pressed or not
    1.24 +	bool isbutton;
    1.25 +
    1.26 +	///Number Of Elements - the number of nodes
    1.27  	int noe;
    1.28  
    1.29 -	bool isbutton;
    1.30 +	///At this location was the mousebutton pressed.
    1.31 +	///It helps to calculate the distance of dragging.
    1.32  	double clicked_x, clicked_y;
    1.33  
    1.34 +	///Remembers which Gnome::Canvas::Item was pressed.
    1.35  	///this variable is needed, because
    1.36  	///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
    1.37  	///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
    1.38  	Gnome::Canvas::Item * active_item;
    1.39  
    1.40 -	bool event_handler(GdkEvent* e, bool b);
    1.41 -	bool tri_mover(GdkEvent* e, bool b);
    1.42 -	Gnome::Art::Point * wp;
    1.43 -	Gnome::Canvas::Ellipse ** nodes;
    1.44 -	Gnome::Canvas::Polygon * sides;
    1.45 -	Gnome::Canvas::Group triangle;
    1.46 +
    1.47  };
    1.48  
    1.49 +///When we click on the weight point we can drag the whole triangle. This function resolves it.
    1.50  bool CanvasExample::tri_mover(GdkEvent* e, bool b)
    1.51  {
    1.52 +	b=b;
    1.53  	switch(e->type)
    1.54  	{
    1.55  		case GDK_BUTTON_PRESS:
    1.56 @@ -50,13 +69,9 @@
    1.57  		case GDK_MOTION_NOTIFY:
    1.58  			if(isbutton)
    1.59  			{
    1.60 -				//double x1, y1, x2, y2;
    1.61 -				//(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
    1.62 -				//printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
    1.63 -				//printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
    1.64  				double dx=e->motion.x-clicked_x;
    1.65  				double dy=e->motion.y-clicked_y;
    1.66 -				for(int i=0;i<=noe/2;i++)
    1.67 +				for(int i=0;i<=noe;i++)
    1.68  				{
    1.69  					nodes[i]->move(dx,dy);
    1.70  				}
    1.71 @@ -64,7 +79,7 @@
    1.72  				clicked_y=e->motion.y;
    1.73  
    1.74  				Gnome::Canvas::Points coos;
    1.75 -				for(int i=0;i<noe/2;i++)
    1.76 +				for(int i=0;i<noe;i++)
    1.77  				{
    1.78  					double x1,y1,x2,y2;
    1.79  					nodes[i]->get_bounds(x1,y1,x2,y2);
    1.80 @@ -77,10 +92,15 @@
    1.81  			}
    1.82  		default: break;
    1.83  	}
    1.84 +	return true;
    1.85  }
    1.86  
    1.87 +///This function moves only one node of triangle,
    1.88 +///but recalculate the location of wight point,
    1.89 +///and also redraw the sides of the planefigure.
    1.90  bool CanvasExample::event_handler(GdkEvent* e, bool b)
    1.91  {
    1.92 +	b=b;
    1.93  	switch(e->type)
    1.94  	{
    1.95  		case GDK_BUTTON_PRESS:
    1.96 @@ -109,7 +129,7 @@
    1.97  				double x_wp=0;
    1.98  				double y_wp=0;
    1.99  
   1.100 -				for(int i=0;i<noe/2;i++)
   1.101 +				for(int i=0;i<noe;i++)
   1.102  				{
   1.103  					double x1,y1,x2,y2;
   1.104  					nodes[i]->get_bounds(x1,y1,x2,y2);
   1.105 @@ -117,61 +137,58 @@
   1.106  					double y=(y1+y2)/2;
   1.107  					coos.push_back(Gnome::Art::Point(x, y));
   1.108  
   1.109 -					if(i<3)
   1.110 -					{
   1.111 -						x_wp+=x;
   1.112 -						y_wp+=y;
   1.113 -					}
   1.114 +					x_wp+=x;
   1.115 +					y_wp+=y;
   1.116  				}
   1.117  
   1.118  				sides->property_points().set_value(coos);
   1.119  
   1.120 -				x_wp/=3;
   1.121 -				y_wp/=3;
   1.122 +				x_wp/=noe;
   1.123 +				y_wp/=noe;
   1.124  
   1.125  				double x1,y1,x2,y2;
   1.126 -				nodes[noe/2]->get_bounds(x1,y1,x2,y2);
   1.127 +				nodes[noe]->get_bounds(x1,y1,x2,y2);
   1.128  				double x=(x1+x2)/2;
   1.129  				double y=(y1+y2)/2;
   1.130  
   1.131  				dx=x_wp-x;
   1.132  				dy=y_wp-y;
   1.133 -				nodes[noe/2]->move(dx, dy);
   1.134 +				nodes[noe]->move(dx, dy);
   1.135  
   1.136  				clicked_x=e->motion.x;
   1.137  				clicked_y=e->motion.y;
   1.138  			}
   1.139  		default: break;
   1.140  	}
   1.141 +	return true;
   1.142  }
   1.143  
   1.144 -CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
   1.145 +CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
   1.146  {
   1.147 -	noe=numofels;
   1.148 +	noe=numofcoos/2;
   1.149  
   1.150 -	int j=0;
   1.151 -	double ax=coosarray[j++];
   1.152 -	double ay=coosarray[j++];
   1.153 -	double bx=coosarray[j++];
   1.154 -	double by=coosarray[j++];
   1.155 -	double cx=coosarray[j++];
   1.156 -	double cy=coosarray[j++];
   1.157 +	double x_wp=0;
   1.158 +	double y_wp=0;
   1.159  
   1.160  	Gnome::Canvas::Points coos;
   1.161 -	for(int i=0;i<noe;i++)
   1.162 +	for(int i=0;i<numofcoos;i++)
   1.163  	{
   1.164  		double x=coosarray[i++];
   1.165  		double y=coosarray[i];
   1.166  		coos.push_back(Gnome::Art::Point(x, y));
   1.167 +
   1.168 +		x_wp+=x;
   1.169 +		y_wp+=y;
   1.170 +
   1.171  	}
   1.172  
   1.173  	sides=new Gnome::Canvas::Polygon(triangle, coos);
   1.174  	*sides << Gnome::Canvas::Properties::outline_color("green");
   1.175  	sides->property_width_pixels().set_value(10);
   1.176  
   1.177 -	nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1];
   1.178 +	nodes=new Gnome::Canvas::Ellipse* [noe+1];
   1.179  
   1.180 -	for(int i=0; i<noe/2;i++)
   1.181 +	for(int i=0; i<noe;i++)
   1.182  	{
   1.183  		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.184  		*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
   1.185 @@ -179,12 +196,12 @@
   1.186  		(nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
   1.187  	}
   1.188  
   1.189 -	wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
   1.190 +	wp=new Gnome::Art::Point(x_wp/noe,y_wp/noe);
   1.191  
   1.192 -	nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
   1.193 -	*(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue");
   1.194 -	*(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black");
   1.195 -	(nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
   1.196 +	nodes[noe]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
   1.197 +	*(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
   1.198 +	*(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
   1.199 +	(nodes[noe])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
   1.200  
   1.201  
   1.202  }
   1.203 @@ -205,7 +222,7 @@
   1.204  	CanvasExample m_canvas;
   1.205  };
   1.206  
   1.207 -MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe)
   1.208 +MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
   1.209  {
   1.210  	set_title (title);
   1.211  	add(m_canvas);
   1.212 @@ -234,7 +251,5 @@
   1.213  		app.run(mainwin);
   1.214  	}
   1.215  
   1.216 -	printf("Usage:\n./triangle x1 y1 x2 y2 x3 y3\nWhere xi and yi are the coordinates of the points of the triangle\n");
   1.217 -
   1.218  	return 0;
   1.219  }