src/work/peter/canvas-test.cc
changeset 1221 6706c788ebb5
parent 1212 d89e184cc24e
child 1224 7f4f2855fa11
equal deleted inserted replaced
0:6045272bc1e9 1:338b56c3315d
    10 class CanvasExample : public Gnome::Canvas::CanvasAA
    10 class CanvasExample : public Gnome::Canvas::CanvasAA
    11 {
    11 {
    12 	typedef Gnome::Canvas::CanvasAA Parent;
    12 	typedef Gnome::Canvas::CanvasAA Parent;
    13 
    13 
    14 public:
    14 public:
    15 	CanvasExample();
    15 	CanvasExample(double *, int);
    16 	virtual ~CanvasExample();
    16 	virtual ~CanvasExample();
    17 
    17 
    18 private:
    18 private:
       
    19 	int noe;
       
    20 
       
    21 	bool isbutton;
       
    22 	double clicked_x, clicked_y;
       
    23 
       
    24 	///this variable is needed, because
       
    25 	///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
       
    26 	///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
       
    27 	Gnome::Canvas::Item * active_item;
       
    28 
    19 	bool event_handler(GdkEvent* e, bool b);
    29 	bool event_handler(GdkEvent* e, bool b);
    20 	Gnome::Canvas::Points coos;
    30 	bool tri_mover(GdkEvent* e, bool b);
    21 	Gnome::Art::Point * wp;
    31 	Gnome::Art::Point * wp;
    22 	Gnome::Canvas::Ellipse ** nodes;
    32 	Gnome::Canvas::Ellipse ** nodes;
    23 	Gnome::Canvas::Polygon * sides;
    33 	Gnome::Canvas::Polygon * sides;
    24 	Gnome::Canvas::Group triangle;
    34 	Gnome::Canvas::Group triangle;
    25 };
    35 };
    26 
    36 
       
    37 bool CanvasExample::tri_mover(GdkEvent* e, bool b)
       
    38 {
       
    39 	switch(e->type)
       
    40 	{
       
    41 		case GDK_BUTTON_PRESS:
       
    42 			clicked_x=e->button.x;
       
    43 			clicked_y=e->button.y;
       
    44 			isbutton=true;
       
    45 			break;
       
    46 		case GDK_BUTTON_RELEASE:
       
    47 			isbutton=false;
       
    48 			active_item=NULL;
       
    49 			break;
       
    50 		case GDK_MOTION_NOTIFY:
       
    51 			if(isbutton)
       
    52 			{
       
    53 				//double x1, y1, x2, y2;
       
    54 				//(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
       
    55 				//printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
       
    56 				//printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
       
    57 				double dx=e->motion.x-clicked_x;
       
    58 				double dy=e->motion.y-clicked_y;
       
    59 				for(int i=0;i<=noe/2;i++)
       
    60 				{
       
    61 					nodes[i]->move(dx,dy);
       
    62 				}
       
    63 				clicked_x=e->motion.x;
       
    64 				clicked_y=e->motion.y;
       
    65 
       
    66 				Gnome::Canvas::Points coos;
       
    67 				for(int i=0;i<noe/2;i++)
       
    68 				{
       
    69 					double x1,y1,x2,y2;
       
    70 					nodes[i]->get_bounds(x1,y1,x2,y2);
       
    71 					double x=(x1+x2)/2;
       
    72 					double y=(y1+y2)/2;
       
    73 					coos.push_back(Gnome::Art::Point(x, y));
       
    74 				}
       
    75 				sides->property_points().set_value(coos);
       
    76 
       
    77 			}
       
    78 		default: break;
       
    79 	}
       
    80 }
       
    81 
    27 bool CanvasExample::event_handler(GdkEvent* e, bool b)
    82 bool CanvasExample::event_handler(GdkEvent* e, bool b)
    28 {
    83 {
    29 	bool isbutton=true;
       
    30 	switch(e->type)
    84 	switch(e->type)
    31 	{
    85 	{
    32 		case GDK_BUTTON_PRESS: printf("Node is pressed!\n"); break;
    86 		case GDK_BUTTON_PRESS:
    33 		//case GDK_BUTTON_RELEASE: printf("Node is released!\n"); break;
    87 			clicked_x=e->button.x;
    34 		default: isbutton=false; break;
    88 			clicked_y=e->button.y;
    35 	}
    89 			active_item=(get_item_at(e->button.x, e->button.y));
    36 	if(isbutton)
    90 			isbutton=true;
    37 	{
    91 			break;
    38 		//(get_item_at(e->button.x, e->button.y))->move(5,5);
    92 		case GDK_BUTTON_RELEASE:
    39 		(get_item_at(e->button.x, e->button.y))->hide();
    93 			isbutton=false;
    40 	}
    94 			active_item=NULL;
    41 }
    95 			break;
    42 
    96 		case GDK_MOTION_NOTIFY:
    43 CanvasExample::CanvasExample():triangle(*(root()), 0, 0)
    97 			if(isbutton)
    44 {
    98 			{
    45 	double ax=100;
    99 				//double x1, y1, x2, y2;
    46 	double ay=100;
   100 				//(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
    47 	double bx=-100;
   101 				//printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
    48 	double by=100;
   102 				//printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
    49 	double cx=0;
   103 				double dx=e->motion.x-clicked_x;
    50 	double cy=-100;
   104 				double dy=e->motion.y-clicked_y;
    51 	coos.push_back(Gnome::Art::Point(100, 100));
   105 				active_item->move(dx, dy);
    52 	coos.push_back(Gnome::Art::Point(-100, 100));
   106 
    53 	coos.push_back(Gnome::Art::Point(0, -100));
   107 				Gnome::Canvas::Points coos;
       
   108 
       
   109 				double x_wp=0;
       
   110 				double y_wp=0;
       
   111 
       
   112 				for(int i=0;i<noe/2;i++)
       
   113 				{
       
   114 					double x1,y1,x2,y2;
       
   115 					nodes[i]->get_bounds(x1,y1,x2,y2);
       
   116 					double x=(x1+x2)/2;
       
   117 					double y=(y1+y2)/2;
       
   118 					coos.push_back(Gnome::Art::Point(x, y));
       
   119 
       
   120 					if(i<3)
       
   121 					{
       
   122 						x_wp+=x;
       
   123 						y_wp+=y;
       
   124 					}
       
   125 				}
       
   126 
       
   127 				sides->property_points().set_value(coos);
       
   128 
       
   129 				x_wp/=3;
       
   130 				y_wp/=3;
       
   131 
       
   132 				double x1,y1,x2,y2;
       
   133 				nodes[noe/2]->get_bounds(x1,y1,x2,y2);
       
   134 				double x=(x1+x2)/2;
       
   135 				double y=(y1+y2)/2;
       
   136 
       
   137 				dx=x_wp-x;
       
   138 				dy=y_wp-y;
       
   139 				nodes[noe/2]->move(dx, dy);
       
   140 
       
   141 				clicked_x=e->motion.x;
       
   142 				clicked_y=e->motion.y;
       
   143 			}
       
   144 		default: break;
       
   145 	}
       
   146 }
       
   147 
       
   148 CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
       
   149 {
       
   150 	noe=numofels;
       
   151 
       
   152 	int j=0;
       
   153 	double ax=coosarray[j++];
       
   154 	double ay=coosarray[j++];
       
   155 	double bx=coosarray[j++];
       
   156 	double by=coosarray[j++];
       
   157 	double cx=coosarray[j++];
       
   158 	double cy=coosarray[j++];
       
   159 
       
   160 	Gnome::Canvas::Points coos;
       
   161 	for(int i=0;i<noe;i++)
       
   162 	{
       
   163 		double x=coosarray[i++];
       
   164 		double y=coosarray[i];
       
   165 		coos.push_back(Gnome::Art::Point(x, y));
       
   166 	}
    54 
   167 
    55 	sides=new Gnome::Canvas::Polygon(triangle, coos);
   168 	sides=new Gnome::Canvas::Polygon(triangle, coos);
    56 	*sides << Gnome::Canvas::Properties::outline_color("green");
   169 	*sides << Gnome::Canvas::Properties::outline_color("green");
    57 	sides->property_width_pixels().set_value(10);
   170 	sides->property_width_pixels().set_value(10);
    58 
   171 
    59 	nodes=new ( Gnome::Canvas::Ellipse * ) [4];
   172 	nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1];
    60 
   173 
    61 	for(int i=0; i<3;i++)
   174 	for(int i=0; i<noe/2;i++)
    62 	{
   175 	{
    63 		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);
   176 		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);
    64 		*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
   177 		*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
    65 		*(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
   178 		*(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
    66 		(nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
   179 		(nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
    67 	}
   180 	}
    68 
   181 
    69 	wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
   182 	wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
    70 
   183 
    71 	nodes[3]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
   184 	nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
    72 	*(nodes[3]) << Gnome::Canvas::Properties::fill_color("blue");
   185 	*(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue");
    73 	*(nodes[3]) << Gnome::Canvas::Properties::outline_color("black");
   186 	*(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black");
       
   187 	(nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
    74 
   188 
    75 
   189 
    76 }
   190 }
    77 
   191 
    78 CanvasExample::~CanvasExample()
   192 CanvasExample::~CanvasExample()
    82 //MainWin:
   196 //MainWin:
    83 
   197 
    84 class MainWin : public Gtk::Window
   198 class MainWin : public Gtk::Window
    85 {
   199 {
    86 public:
   200 public:
    87 	MainWin(const std::string& title);
   201 	MainWin(const std::string& title, double *, int);
    88 
   202 
    89 protected:
   203 protected:
    90 	//Member widgets:
   204 	//Member widgets:
    91 	CanvasExample m_canvas;
   205 	CanvasExample m_canvas;
    92 };
   206 };
    93 
   207 
    94 MainWin::MainWin(const std::string& title)
   208 MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe)
    95 {
   209 {
    96 	set_title (title);
   210 	set_title (title);
    97 	add(m_canvas);
   211 	add(m_canvas);
    98 	set_default_size(900,600);
   212 	set_default_size(900,600);
    99 
   213 
   102 
   216 
   103 //main():
   217 //main():
   104 
   218 
   105 int main(int argc, char *argv[])
   219 int main(int argc, char *argv[])
   106 {
   220 {
   107 	Gnome::Canvas::init();
   221 	if((argc>=7)&&( (argc/2)!=( (argc+1)/2 ) ) )
   108 	Gtk::Main app(argc, argv);
   222 	{
   109 
   223 		double * coosarray=new double[argc];
   110 	MainWin mainwin("Gnome::Canvas Example");
   224 
   111 	app.run(mainwin);
   225 		for(int i=1;i<argc;i++)
       
   226 		{
       
   227 			coosarray[i-1]=atof(argv[i]);
       
   228 		}
       
   229 
       
   230 		Gnome::Canvas::init();
       
   231 		Gtk::Main app(argc, argv);
       
   232 
       
   233 		MainWin mainwin("Magic Triangle",coosarray,argc-1);
       
   234 		app.run(mainwin);
       
   235 	}
       
   236 
       
   237 	printf("Usage:\n./triangle x1 y1 x2 y2 x3 y3\nWhere xi and yi are the coordinates of the points of the triangle\n");
   112 
   238 
   113 	return 0;
   239 	return 0;
   114 }
   240 }