Magic triangle is READY.
authorhegyi
Wed, 16 Mar 2005 17:31:04 +0000
changeset 12216706c788ebb5
parent 1220 20b26ee5812b
child 1222 a3fb216a267d
Magic triangle is READY.
src/work/peter/Makefile
src/work/peter/canvas-test.cc
     1.1 --- a/src/work/peter/Makefile	Wed Mar 16 16:40:21 2005 +0000
     1.2 +++ b/src/work/peter/Makefile	Wed Mar 16 17:31:04 2005 +0000
     1.3 @@ -1,6 +1,2 @@
     1.4 -hier: hierarchygraph.h hierarchygraph_test.cc
     1.5 -	g++ -Wall -W -I../.. -I../klao -I../../lemon hierarchygraph_test.cc -o test
     1.6 -
     1.7 -edge: edgepathgraph_test.cc edgepathgraph.h
     1.8 -	g++ -Wall -W -I../.. -I../klao -I../../lemon edgepathgraph_test.cc -o test
     1.9 -
    1.10 +triangle: canvas-test.cc
    1.11 +	g++ canvas-test.cc -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
     2.1 --- a/src/work/peter/canvas-test.cc	Wed Mar 16 16:40:21 2005 +0000
     2.2 +++ b/src/work/peter/canvas-test.cc	Wed Mar 16 17:31:04 2005 +0000
     2.3 @@ -12,53 +12,166 @@
     2.4  	typedef Gnome::Canvas::CanvasAA Parent;
     2.5  
     2.6  public:
     2.7 -	CanvasExample();
     2.8 +	CanvasExample(double *, int);
     2.9  	virtual ~CanvasExample();
    2.10  
    2.11  private:
    2.12 +	int noe;
    2.13 +
    2.14 +	bool isbutton;
    2.15 +	double clicked_x, clicked_y;
    2.16 +
    2.17 +	///this variable is needed, because
    2.18 +	///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
    2.19 +	///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
    2.20 +	Gnome::Canvas::Item * active_item;
    2.21 +
    2.22  	bool event_handler(GdkEvent* e, bool b);
    2.23 -	Gnome::Canvas::Points coos;
    2.24 +	bool tri_mover(GdkEvent* e, bool b);
    2.25  	Gnome::Art::Point * wp;
    2.26  	Gnome::Canvas::Ellipse ** nodes;
    2.27  	Gnome::Canvas::Polygon * sides;
    2.28  	Gnome::Canvas::Group triangle;
    2.29  };
    2.30  
    2.31 -bool CanvasExample::event_handler(GdkEvent* e, bool b)
    2.32 +bool CanvasExample::tri_mover(GdkEvent* e, bool b)
    2.33  {
    2.34 -	bool isbutton=true;
    2.35  	switch(e->type)
    2.36  	{
    2.37 -		case GDK_BUTTON_PRESS: printf("Node is pressed!\n"); break;
    2.38 -		//case GDK_BUTTON_RELEASE: printf("Node is released!\n"); break;
    2.39 -		default: isbutton=false; break;
    2.40 -	}
    2.41 -	if(isbutton)
    2.42 -	{
    2.43 -		//(get_item_at(e->button.x, e->button.y))->move(5,5);
    2.44 -		(get_item_at(e->button.x, e->button.y))->hide();
    2.45 +		case GDK_BUTTON_PRESS:
    2.46 +			clicked_x=e->button.x;
    2.47 +			clicked_y=e->button.y;
    2.48 +			isbutton=true;
    2.49 +			break;
    2.50 +		case GDK_BUTTON_RELEASE:
    2.51 +			isbutton=false;
    2.52 +			active_item=NULL;
    2.53 +			break;
    2.54 +		case GDK_MOTION_NOTIFY:
    2.55 +			if(isbutton)
    2.56 +			{
    2.57 +				//double x1, y1, x2, y2;
    2.58 +				//(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
    2.59 +				//printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
    2.60 +				//printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
    2.61 +				double dx=e->motion.x-clicked_x;
    2.62 +				double dy=e->motion.y-clicked_y;
    2.63 +				for(int i=0;i<=noe/2;i++)
    2.64 +				{
    2.65 +					nodes[i]->move(dx,dy);
    2.66 +				}
    2.67 +				clicked_x=e->motion.x;
    2.68 +				clicked_y=e->motion.y;
    2.69 +
    2.70 +				Gnome::Canvas::Points coos;
    2.71 +				for(int i=0;i<noe/2;i++)
    2.72 +				{
    2.73 +					double x1,y1,x2,y2;
    2.74 +					nodes[i]->get_bounds(x1,y1,x2,y2);
    2.75 +					double x=(x1+x2)/2;
    2.76 +					double y=(y1+y2)/2;
    2.77 +					coos.push_back(Gnome::Art::Point(x, y));
    2.78 +				}
    2.79 +				sides->property_points().set_value(coos);
    2.80 +
    2.81 +			}
    2.82 +		default: break;
    2.83  	}
    2.84  }
    2.85  
    2.86 -CanvasExample::CanvasExample():triangle(*(root()), 0, 0)
    2.87 +bool CanvasExample::event_handler(GdkEvent* e, bool b)
    2.88  {
    2.89 -	double ax=100;
    2.90 -	double ay=100;
    2.91 -	double bx=-100;
    2.92 -	double by=100;
    2.93 -	double cx=0;
    2.94 -	double cy=-100;
    2.95 -	coos.push_back(Gnome::Art::Point(100, 100));
    2.96 -	coos.push_back(Gnome::Art::Point(-100, 100));
    2.97 -	coos.push_back(Gnome::Art::Point(0, -100));
    2.98 +	switch(e->type)
    2.99 +	{
   2.100 +		case GDK_BUTTON_PRESS:
   2.101 +			clicked_x=e->button.x;
   2.102 +			clicked_y=e->button.y;
   2.103 +			active_item=(get_item_at(e->button.x, e->button.y));
   2.104 +			isbutton=true;
   2.105 +			break;
   2.106 +		case GDK_BUTTON_RELEASE:
   2.107 +			isbutton=false;
   2.108 +			active_item=NULL;
   2.109 +			break;
   2.110 +		case GDK_MOTION_NOTIFY:
   2.111 +			if(isbutton)
   2.112 +			{
   2.113 +				//double x1, y1, x2, y2;
   2.114 +				//(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
   2.115 +				//printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
   2.116 +				//printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
   2.117 +				double dx=e->motion.x-clicked_x;
   2.118 +				double dy=e->motion.y-clicked_y;
   2.119 +				active_item->move(dx, dy);
   2.120 +
   2.121 +				Gnome::Canvas::Points coos;
   2.122 +
   2.123 +				double x_wp=0;
   2.124 +				double y_wp=0;
   2.125 +
   2.126 +				for(int i=0;i<noe/2;i++)
   2.127 +				{
   2.128 +					double x1,y1,x2,y2;
   2.129 +					nodes[i]->get_bounds(x1,y1,x2,y2);
   2.130 +					double x=(x1+x2)/2;
   2.131 +					double y=(y1+y2)/2;
   2.132 +					coos.push_back(Gnome::Art::Point(x, y));
   2.133 +
   2.134 +					if(i<3)
   2.135 +					{
   2.136 +						x_wp+=x;
   2.137 +						y_wp+=y;
   2.138 +					}
   2.139 +				}
   2.140 +
   2.141 +				sides->property_points().set_value(coos);
   2.142 +
   2.143 +				x_wp/=3;
   2.144 +				y_wp/=3;
   2.145 +
   2.146 +				double x1,y1,x2,y2;
   2.147 +				nodes[noe/2]->get_bounds(x1,y1,x2,y2);
   2.148 +				double x=(x1+x2)/2;
   2.149 +				double y=(y1+y2)/2;
   2.150 +
   2.151 +				dx=x_wp-x;
   2.152 +				dy=y_wp-y;
   2.153 +				nodes[noe/2]->move(dx, dy);
   2.154 +
   2.155 +				clicked_x=e->motion.x;
   2.156 +				clicked_y=e->motion.y;
   2.157 +			}
   2.158 +		default: break;
   2.159 +	}
   2.160 +}
   2.161 +
   2.162 +CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
   2.163 +{
   2.164 +	noe=numofels;
   2.165 +
   2.166 +	int j=0;
   2.167 +	double ax=coosarray[j++];
   2.168 +	double ay=coosarray[j++];
   2.169 +	double bx=coosarray[j++];
   2.170 +	double by=coosarray[j++];
   2.171 +	double cx=coosarray[j++];
   2.172 +	double cy=coosarray[j++];
   2.173 +
   2.174 +	Gnome::Canvas::Points coos;
   2.175 +	for(int i=0;i<noe;i++)
   2.176 +	{
   2.177 +		double x=coosarray[i++];
   2.178 +		double y=coosarray[i];
   2.179 +		coos.push_back(Gnome::Art::Point(x, y));
   2.180 +	}
   2.181  
   2.182  	sides=new Gnome::Canvas::Polygon(triangle, coos);
   2.183  	*sides << Gnome::Canvas::Properties::outline_color("green");
   2.184  	sides->property_width_pixels().set_value(10);
   2.185  
   2.186 -	nodes=new ( Gnome::Canvas::Ellipse * ) [4];
   2.187 +	nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1];
   2.188  
   2.189 -	for(int i=0; i<3;i++)
   2.190 +	for(int i=0; i<noe/2;i++)
   2.191  	{
   2.192  		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);
   2.193  		*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
   2.194 @@ -68,9 +181,10 @@
   2.195  
   2.196  	wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
   2.197  
   2.198 -	nodes[3]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
   2.199 -	*(nodes[3]) << Gnome::Canvas::Properties::fill_color("blue");
   2.200 -	*(nodes[3]) << Gnome::Canvas::Properties::outline_color("black");
   2.201 +	nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
   2.202 +	*(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue");
   2.203 +	*(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black");
   2.204 +	(nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
   2.205  
   2.206  
   2.207  }
   2.208 @@ -84,14 +198,14 @@
   2.209  class MainWin : public Gtk::Window
   2.210  {
   2.211  public:
   2.212 -	MainWin(const std::string& title);
   2.213 +	MainWin(const std::string& title, double *, int);
   2.214  
   2.215  protected:
   2.216  	//Member widgets:
   2.217  	CanvasExample m_canvas;
   2.218  };
   2.219  
   2.220 -MainWin::MainWin(const std::string& title)
   2.221 +MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe)
   2.222  {
   2.223  	set_title (title);
   2.224  	add(m_canvas);
   2.225 @@ -104,11 +218,23 @@
   2.226  
   2.227  int main(int argc, char *argv[])
   2.228  {
   2.229 -	Gnome::Canvas::init();
   2.230 -	Gtk::Main app(argc, argv);
   2.231 +	if((argc>=7)&&( (argc/2)!=( (argc+1)/2 ) ) )
   2.232 +	{
   2.233 +		double * coosarray=new double[argc];
   2.234  
   2.235 -	MainWin mainwin("Gnome::Canvas Example");
   2.236 -	app.run(mainwin);
   2.237 +		for(int i=1;i<argc;i++)
   2.238 +		{
   2.239 +			coosarray[i-1]=atof(argv[i]);
   2.240 +		}
   2.241 +
   2.242 +		Gnome::Canvas::init();
   2.243 +		Gtk::Main app(argc, argv);
   2.244 +
   2.245 +		MainWin mainwin("Magic Triangle",coosarray,argc-1);
   2.246 +		app.run(mainwin);
   2.247 +	}
   2.248 +
   2.249 +	printf("Usage:\n./triangle x1 y1 x2 y2 x3 y3\nWhere xi and yi are the coordinates of the points of the triangle\n");
   2.250  
   2.251  	return 0;
   2.252  }