[1212] | 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> |
---|
[1277] | 9 | #include <lemon/list_graph.h> |
---|
[1212] | 10 | |
---|
| 11 | class CanvasExample : public Gnome::Canvas::CanvasAA |
---|
| 12 | { |
---|
| 13 | typedef Gnome::Canvas::CanvasAA Parent; |
---|
| 14 | |
---|
| 15 | public: |
---|
[1221] | 16 | CanvasExample(double *, int); |
---|
[1212] | 17 | virtual ~CanvasExample(); |
---|
| 18 | |
---|
| 19 | private: |
---|
[1224] | 20 | |
---|
| 21 | ///Event handler function that handles dragging nodes of triangle |
---|
[1225] | 22 | bool event_handler(GdkEvent* e, int b); |
---|
[1224] | 23 | |
---|
| 24 | ///Event handler function that handles dragging triangle |
---|
[1225] | 25 | bool tri_mover(GdkEvent* e); |
---|
[1224] | 26 | |
---|
| 27 | ///Coordinates of Weight Point of tirangle |
---|
| 28 | Gnome::Art::Point * wp; |
---|
| 29 | ///Array of nodes of planefigure |
---|
| 30 | Gnome::Canvas::Ellipse ** nodes; |
---|
| 31 | ///Sides of planefigure |
---|
| 32 | Gnome::Canvas::Polygon * sides; |
---|
| 33 | ///Group of graphical elements of triangle |
---|
| 34 | Gnome::Canvas::Group triangle; |
---|
| 35 | |
---|
| 36 | ///Indicates whether the button of mouse is pressed or not |
---|
| 37 | bool isbutton; |
---|
| 38 | |
---|
| 39 | ///Number Of Elements - the number of nodes |
---|
[1221] | 40 | int noe; |
---|
| 41 | |
---|
[1225] | 42 | ///Array of coordinates |
---|
| 43 | double * coordinates; |
---|
| 44 | |
---|
[1224] | 45 | ///At this location was the mousebutton pressed. |
---|
| 46 | ///It helps to calculate the distance of dragging. |
---|
[1221] | 47 | double clicked_x, clicked_y; |
---|
| 48 | |
---|
[1224] | 49 | ///Remembers which Gnome::Canvas::Item was pressed. |
---|
[1221] | 50 | ///this variable is needed, because |
---|
| 51 | ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault |
---|
| 52 | ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution |
---|
| 53 | Gnome::Canvas::Item * active_item; |
---|
| 54 | |
---|
[1224] | 55 | |
---|
[1212] | 56 | }; |
---|
| 57 | |
---|
[1224] | 58 | ///When we click on the weight point we can drag the whole triangle. This function resolves it. |
---|
[1225] | 59 | bool CanvasExample::tri_mover(GdkEvent* e) |
---|
[1212] | 60 | { |
---|
| 61 | switch(e->type) |
---|
| 62 | { |
---|
[1221] | 63 | case GDK_BUTTON_PRESS: |
---|
| 64 | clicked_x=e->button.x; |
---|
| 65 | clicked_y=e->button.y; |
---|
| 66 | isbutton=true; |
---|
| 67 | break; |
---|
| 68 | case GDK_BUTTON_RELEASE: |
---|
| 69 | isbutton=false; |
---|
| 70 | active_item=NULL; |
---|
| 71 | break; |
---|
| 72 | case GDK_MOTION_NOTIFY: |
---|
| 73 | if(isbutton) |
---|
| 74 | { |
---|
| 75 | double dx=e->motion.x-clicked_x; |
---|
| 76 | double dy=e->motion.y-clicked_y; |
---|
[1225] | 77 | |
---|
| 78 | Gnome::Canvas::Points coos; |
---|
| 79 | |
---|
[1224] | 80 | for(int i=0;i<=noe;i++) |
---|
[1221] | 81 | { |
---|
| 82 | nodes[i]->move(dx,dy); |
---|
[1225] | 83 | |
---|
| 84 | double x=(coordinates[2*i]+=dx); |
---|
| 85 | double y=(coordinates[2*i+1]+=dy); |
---|
| 86 | |
---|
| 87 | if(i!=noe)coos.push_back(Gnome::Art::Point(x,y)); |
---|
| 88 | |
---|
[1221] | 89 | } |
---|
[1225] | 90 | |
---|
[1221] | 91 | clicked_x=e->motion.x; |
---|
| 92 | clicked_y=e->motion.y; |
---|
| 93 | |
---|
| 94 | sides->property_points().set_value(coos); |
---|
| 95 | } |
---|
| 96 | default: break; |
---|
[1212] | 97 | } |
---|
[1224] | 98 | return true; |
---|
[1212] | 99 | } |
---|
| 100 | |
---|
[1224] | 101 | ///This function moves only one node of triangle, |
---|
| 102 | ///but recalculate the location of wight point, |
---|
| 103 | ///and also redraw the sides of the planefigure. |
---|
[1225] | 104 | bool CanvasExample::event_handler(GdkEvent* e, int b) |
---|
[1212] | 105 | { |
---|
[1221] | 106 | switch(e->type) |
---|
| 107 | { |
---|
| 108 | case GDK_BUTTON_PRESS: |
---|
| 109 | clicked_x=e->button.x; |
---|
| 110 | clicked_y=e->button.y; |
---|
| 111 | active_item=(get_item_at(e->button.x, e->button.y)); |
---|
| 112 | isbutton=true; |
---|
| 113 | break; |
---|
| 114 | case GDK_BUTTON_RELEASE: |
---|
| 115 | isbutton=false; |
---|
| 116 | active_item=NULL; |
---|
| 117 | break; |
---|
| 118 | case GDK_MOTION_NOTIFY: |
---|
| 119 | if(isbutton) |
---|
| 120 | { |
---|
| 121 | //double x1, y1, x2, y2; |
---|
| 122 | //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); |
---|
| 123 | //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); |
---|
| 124 | //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); |
---|
| 125 | double dx=e->motion.x-clicked_x; |
---|
| 126 | double dy=e->motion.y-clicked_y; |
---|
| 127 | active_item->move(dx, dy); |
---|
| 128 | |
---|
[1225] | 129 | coordinates[2*b]+=dx; |
---|
| 130 | coordinates[2*b+1]+=dy; |
---|
| 131 | |
---|
[1221] | 132 | Gnome::Canvas::Points coos; |
---|
| 133 | |
---|
| 134 | double x_wp=0; |
---|
| 135 | double y_wp=0; |
---|
| 136 | |
---|
[1224] | 137 | for(int i=0;i<noe;i++) |
---|
[1221] | 138 | { |
---|
[1225] | 139 | coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1])); |
---|
[1221] | 140 | |
---|
[1225] | 141 | x_wp+=coordinates[2*i]; |
---|
| 142 | y_wp+=coordinates[2*i+1]; |
---|
[1221] | 143 | } |
---|
| 144 | |
---|
| 145 | sides->property_points().set_value(coos); |
---|
| 146 | |
---|
[1224] | 147 | x_wp/=noe; |
---|
| 148 | y_wp/=noe; |
---|
[1221] | 149 | |
---|
[1225] | 150 | dx=x_wp-coordinates[noe*2]; |
---|
| 151 | dy=y_wp-coordinates[noe*2+1]; |
---|
| 152 | nodes[noe]->move(dx, dy); |
---|
[1221] | 153 | |
---|
[1225] | 154 | coordinates[noe*2]+=dx; |
---|
| 155 | coordinates[noe*2+1]+=dy; |
---|
[1221] | 156 | |
---|
| 157 | clicked_x=e->motion.x; |
---|
| 158 | clicked_y=e->motion.y; |
---|
| 159 | } |
---|
| 160 | default: break; |
---|
| 161 | } |
---|
[1224] | 162 | return true; |
---|
[1221] | 163 | } |
---|
| 164 | |
---|
[1224] | 165 | CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL) |
---|
[1221] | 166 | { |
---|
[1224] | 167 | noe=numofcoos/2; |
---|
[1221] | 168 | |
---|
[1225] | 169 | coordinates=new double [numofcoos+2]; |
---|
| 170 | |
---|
[1224] | 171 | double x_wp=0; |
---|
| 172 | double y_wp=0; |
---|
[1221] | 173 | |
---|
| 174 | Gnome::Canvas::Points coos; |
---|
[1226] | 175 | for(int i=0;i<numofcoos;i+=2) |
---|
[1221] | 176 | { |
---|
[1225] | 177 | coordinates[i]=coosarray[i]; |
---|
[1226] | 178 | coordinates[i+1]=coosarray[i+1]; |
---|
| 179 | coos.push_back(Gnome::Art::Point(coordinates[i], |
---|
| 180 | coordinates[i+1])); |
---|
[1224] | 181 | |
---|
[1226] | 182 | x_wp+=coordinates[i]; |
---|
| 183 | y_wp+=coordinates[i+1]; |
---|
[1224] | 184 | |
---|
[1221] | 185 | } |
---|
[1212] | 186 | |
---|
| 187 | sides=new Gnome::Canvas::Polygon(triangle, coos); |
---|
| 188 | *sides << Gnome::Canvas::Properties::outline_color("green"); |
---|
| 189 | sides->property_width_pixels().set_value(10); |
---|
| 190 | |
---|
[1224] | 191 | nodes=new Gnome::Canvas::Ellipse* [noe+1]; |
---|
[1212] | 192 | |
---|
[1224] | 193 | for(int i=0; i<noe;i++) |
---|
[1212] | 194 | { |
---|
| 195 | 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); |
---|
| 196 | *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 197 | *(nodes[i]) << Gnome::Canvas::Properties::outline_color("black"); |
---|
[1225] | 198 | (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i)); |
---|
[1212] | 199 | } |
---|
| 200 | |
---|
[1225] | 201 | coordinates[numofcoos]=x_wp/noe; |
---|
| 202 | coordinates[numofcoos+1]=y_wp/noe; |
---|
[1212] | 203 | |
---|
[1225] | 204 | wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]); |
---|
| 205 | |
---|
| 206 | nodes[noe]= new Gnome::Canvas::Ellipse |
---|
| 207 | ( |
---|
| 208 | triangle, |
---|
| 209 | coordinates[numofcoos]-20, |
---|
| 210 | coordinates[numofcoos+1]-20, |
---|
| 211 | coordinates[numofcoos]+20, |
---|
| 212 | coordinates[numofcoos+1]+20 |
---|
| 213 | ); |
---|
[1224] | 214 | *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue"); |
---|
| 215 | *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black"); |
---|
[1225] | 216 | (nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover)); |
---|
| 217 | |
---|
[1212] | 218 | |
---|
| 219 | |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | CanvasExample::~CanvasExample() |
---|
| 223 | { |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | //MainWin: |
---|
| 227 | |
---|
| 228 | class MainWin : public Gtk::Window |
---|
| 229 | { |
---|
| 230 | public: |
---|
[1221] | 231 | MainWin(const std::string& title, double *, int); |
---|
[1212] | 232 | |
---|
| 233 | protected: |
---|
| 234 | //Member widgets: |
---|
| 235 | CanvasExample m_canvas; |
---|
| 236 | }; |
---|
| 237 | |
---|
[1224] | 238 | MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc) |
---|
[1212] | 239 | { |
---|
| 240 | set_title (title); |
---|
| 241 | add(m_canvas); |
---|
| 242 | set_default_size(900,600); |
---|
| 243 | |
---|
| 244 | show_all(); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | //main(): |
---|
| 248 | |
---|
| 249 | int main(int argc, char *argv[]) |
---|
| 250 | { |
---|
[1226] | 251 | if((argc>=7)&& (argc%2) ) |
---|
[1221] | 252 | { |
---|
| 253 | double * coosarray=new double[argc]; |
---|
[1212] | 254 | |
---|
[1221] | 255 | for(int i=1;i<argc;i++) |
---|
| 256 | { |
---|
| 257 | coosarray[i-1]=atof(argv[i]); |
---|
[1226] | 258 | printf("%g%c",coosarray[i-1],i%2?' ':'\n'); |
---|
[1221] | 259 | } |
---|
| 260 | |
---|
| 261 | Gnome::Canvas::init(); |
---|
| 262 | Gtk::Main app(argc, argv); |
---|
| 263 | |
---|
| 264 | MainWin mainwin("Magic Triangle",coosarray,argc-1); |
---|
| 265 | app.run(mainwin); |
---|
| 266 | } |
---|
| 267 | |
---|
[1212] | 268 | return 0; |
---|
| 269 | } |
---|