COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/peter/canvas-test.cc @ 1221:6706c788ebb5

Last change on this file since 1221:6706c788ebb5 was 1221:6706c788ebb5, checked in by Hegyi Péter, 19 years ago

Magic triangle is READY.

File size: 5.7 KB
Line 
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.
5
6#include <libgnomecanvasmm.h>
7#include <libgnomecanvasmm/polygon.h>
8#include <iostream>
9
10class CanvasExample : public Gnome::Canvas::CanvasAA
11{
12        typedef Gnome::Canvas::CanvasAA Parent;
13
14public:
15        CanvasExample(double *, int);
16        virtual ~CanvasExample();
17
18private:
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
29        bool event_handler(GdkEvent* e, bool b);
30        bool tri_mover(GdkEvent* e, bool b);
31        Gnome::Art::Point * wp;
32        Gnome::Canvas::Ellipse ** nodes;
33        Gnome::Canvas::Polygon * sides;
34        Gnome::Canvas::Group triangle;
35};
36
37bool 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
82bool CanvasExample::event_handler(GdkEvent* e, bool b)
83{
84        switch(e->type)
85        {
86                case GDK_BUTTON_PRESS:
87                        clicked_x=e->button.x;
88                        clicked_y=e->button.y;
89                        active_item=(get_item_at(e->button.x, e->button.y));
90                        isbutton=true;
91                        break;
92                case GDK_BUTTON_RELEASE:
93                        isbutton=false;
94                        active_item=NULL;
95                        break;
96                case GDK_MOTION_NOTIFY:
97                        if(isbutton)
98                        {
99                                //double x1, y1, x2, y2;
100                                //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
101                                //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
102                                //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
103                                double dx=e->motion.x-clicked_x;
104                                double dy=e->motion.y-clicked_y;
105                                active_item->move(dx, dy);
106
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
148CanvasExample::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        }
167
168        sides=new Gnome::Canvas::Polygon(triangle, coos);
169        *sides << Gnome::Canvas::Properties::outline_color("green");
170        sides->property_width_pixels().set_value(10);
171
172        nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1];
173
174        for(int i=0; i<noe/2;i++)
175        {
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);
177                *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
178                *(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
179                (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true));
180        }
181
182        wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
183
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);
185        *(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue");
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));
188
189
190}
191
192CanvasExample::~CanvasExample()
193{
194}
195
196//MainWin:
197
198class MainWin : public Gtk::Window
199{
200public:
201        MainWin(const std::string& title, double *, int);
202
203protected:
204        //Member widgets:
205        CanvasExample m_canvas;
206};
207
208MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe)
209{
210        set_title (title);
211        add(m_canvas);
212        set_default_size(900,600);
213
214        show_all();
215}
216
217//main():
218
219int main(int argc, char *argv[])
220{
221        if((argc>=7)&&( (argc/2)!=( (argc+1)/2 ) ) )
222        {
223                double * coosarray=new double[argc];
224
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");
238
239        return 0;
240}
Note: See TracBrowser for help on using the repository browser.