Magic triangle is a bit more DONE, and is already not only a triangle.
1 // This example was started by Guillaume Laurent.
2 // It has become a place to dump code that tests parts of the
3 // gnomemm canvas code. Little thought has been given to the
4 // actual on-screen output.
6 #include <libgnomecanvasmm.h>
7 #include <libgnomecanvasmm/polygon.h>
10 class CanvasExample : public Gnome::Canvas::CanvasAA
12 typedef Gnome::Canvas::CanvasAA Parent;
15 CanvasExample(double *, int);
16 virtual ~CanvasExample();
20 ///Event handler function that handles dragging nodes of triangle
21 bool event_handler(GdkEvent* e, bool b);
23 ///Event handler function that handles dragging triangle
24 bool tri_mover(GdkEvent* e, bool b);
26 ///Coordinates of Weight Point of tirangle
27 Gnome::Art::Point * wp;
28 ///Array of nodes of planefigure
29 Gnome::Canvas::Ellipse ** nodes;
30 ///Sides of planefigure
31 Gnome::Canvas::Polygon * sides;
32 ///Group of graphical elements of triangle
33 Gnome::Canvas::Group triangle;
35 ///Indicates whether the button of mouse is pressed or not
38 ///Number Of Elements - the number of nodes
41 ///At this location was the mousebutton pressed.
42 ///It helps to calculate the distance of dragging.
43 double clicked_x, clicked_y;
45 ///Remembers which Gnome::Canvas::Item was pressed.
46 ///this variable is needed, because
47 ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
48 ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
49 Gnome::Canvas::Item * active_item;
54 ///When we click on the weight point we can drag the whole triangle. This function resolves it.
55 bool CanvasExample::tri_mover(GdkEvent* e, bool b)
60 case GDK_BUTTON_PRESS:
61 clicked_x=e->button.x;
62 clicked_y=e->button.y;
65 case GDK_BUTTON_RELEASE:
69 case GDK_MOTION_NOTIFY:
72 double dx=e->motion.x-clicked_x;
73 double dy=e->motion.y-clicked_y;
74 for(int i=0;i<=noe;i++)
76 nodes[i]->move(dx,dy);
78 clicked_x=e->motion.x;
79 clicked_y=e->motion.y;
81 Gnome::Canvas::Points coos;
82 for(int i=0;i<noe;i++)
85 nodes[i]->get_bounds(x1,y1,x2,y2);
88 coos.push_back(Gnome::Art::Point(x, y));
90 sides->property_points().set_value(coos);
98 ///This function moves only one node of triangle,
99 ///but recalculate the location of wight point,
100 ///and also redraw the sides of the planefigure.
101 bool CanvasExample::event_handler(GdkEvent* e, bool b)
106 case GDK_BUTTON_PRESS:
107 clicked_x=e->button.x;
108 clicked_y=e->button.y;
109 active_item=(get_item_at(e->button.x, e->button.y));
112 case GDK_BUTTON_RELEASE:
116 case GDK_MOTION_NOTIFY:
119 //double x1, y1, x2, y2;
120 //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
121 //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
122 //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
123 double dx=e->motion.x-clicked_x;
124 double dy=e->motion.y-clicked_y;
125 active_item->move(dx, dy);
127 Gnome::Canvas::Points coos;
132 for(int i=0;i<noe;i++)
135 nodes[i]->get_bounds(x1,y1,x2,y2);
138 coos.push_back(Gnome::Art::Point(x, y));
144 sides->property_points().set_value(coos);
150 nodes[noe]->get_bounds(x1,y1,x2,y2);
156 nodes[noe]->move(dx, dy);
158 clicked_x=e->motion.x;
159 clicked_y=e->motion.y;
166 CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
173 Gnome::Canvas::Points coos;
174 for(int i=0;i<numofcoos;i++)
176 double x=coosarray[i++];
177 double y=coosarray[i];
178 coos.push_back(Gnome::Art::Point(x, y));
185 sides=new Gnome::Canvas::Polygon(triangle, coos);
186 *sides << Gnome::Canvas::Properties::outline_color("green");
187 sides->property_width_pixels().set_value(10);
189 nodes=new Gnome::Canvas::Ellipse* [noe+1];
191 for(int i=0; i<noe;i++)
193 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);
194 *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
195 *(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
196 (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
199 wp=new Gnome::Art::Point(x_wp/noe,y_wp/noe);
201 nodes[noe]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20);
202 *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
203 *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
204 (nodes[noe])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true));
209 CanvasExample::~CanvasExample()
215 class MainWin : public Gtk::Window
218 MainWin(const std::string& title, double *, int);
222 CanvasExample m_canvas;
225 MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
229 set_default_size(900,600);
236 int main(int argc, char *argv[])
238 if((argc>=7)&&( (argc/2)!=( (argc+1)/2 ) ) )
240 double * coosarray=new double[argc];
242 for(int i=1;i<argc;i++)
244 coosarray[i-1]=atof(argv[i]);
247 Gnome::Canvas::init();
248 Gtk::Main app(argc, argv);
250 MainWin mainwin("Magic Triangle",coosarray,argc-1);