1.1 --- a/src/work/peter/magic_plane_figure.cc Sun Apr 17 18:57:22 2005 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,269 +0,0 @@
1.4 -// This example was started by Guillaume Laurent.
1.5 -// It has become a place to dump code that tests parts of the
1.6 -// gnomemm canvas code. Little thought has been given to the
1.7 -// actual on-screen output.
1.8 -
1.9 -#include <libgnomecanvasmm.h>
1.10 -#include <libgnomecanvasmm/polygon.h>
1.11 -#include <iostream>
1.12 -#include <lemon/list_graph.h>
1.13 -
1.14 -class CanvasExample : public Gnome::Canvas::CanvasAA
1.15 -{
1.16 - typedef Gnome::Canvas::CanvasAA Parent;
1.17 -
1.18 -public:
1.19 - CanvasExample(double *, int);
1.20 - virtual ~CanvasExample();
1.21 -
1.22 -private:
1.23 -
1.24 - ///Event handler function that handles dragging nodes of triangle
1.25 - bool event_handler(GdkEvent* e, int b);
1.26 -
1.27 - ///Event handler function that handles dragging triangle
1.28 - bool tri_mover(GdkEvent* e);
1.29 -
1.30 - ///Coordinates of Weight Point of tirangle
1.31 - Gnome::Art::Point * wp;
1.32 - ///Array of nodes of planefigure
1.33 - Gnome::Canvas::Ellipse ** nodes;
1.34 - ///Sides of planefigure
1.35 - Gnome::Canvas::Polygon * sides;
1.36 - ///Group of graphical elements of triangle
1.37 - Gnome::Canvas::Group triangle;
1.38 -
1.39 - ///Indicates whether the button of mouse is pressed or not
1.40 - bool isbutton;
1.41 -
1.42 - ///Number Of Elements - the number of nodes
1.43 - int noe;
1.44 -
1.45 - ///Array of coordinates
1.46 - double * coordinates;
1.47 -
1.48 - ///At this location was the mousebutton pressed.
1.49 - ///It helps to calculate the distance of dragging.
1.50 - double clicked_x, clicked_y;
1.51 -
1.52 - ///Remembers which Gnome::Canvas::Item was pressed.
1.53 - ///this variable is needed, because
1.54 - ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
1.55 - ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
1.56 - Gnome::Canvas::Item * active_item;
1.57 -
1.58 -
1.59 -};
1.60 -
1.61 -///When we click on the weight point we can drag the whole triangle. This function resolves it.
1.62 -bool CanvasExample::tri_mover(GdkEvent* e)
1.63 -{
1.64 - switch(e->type)
1.65 - {
1.66 - case GDK_BUTTON_PRESS:
1.67 - clicked_x=e->button.x;
1.68 - clicked_y=e->button.y;
1.69 - isbutton=true;
1.70 - break;
1.71 - case GDK_BUTTON_RELEASE:
1.72 - isbutton=false;
1.73 - active_item=NULL;
1.74 - break;
1.75 - case GDK_MOTION_NOTIFY:
1.76 - if(isbutton)
1.77 - {
1.78 - double dx=e->motion.x-clicked_x;
1.79 - double dy=e->motion.y-clicked_y;
1.80 -
1.81 - Gnome::Canvas::Points coos;
1.82 -
1.83 - for(int i=0;i<=noe;i++)
1.84 - {
1.85 - nodes[i]->move(dx,dy);
1.86 -
1.87 - double x=(coordinates[2*i]+=dx);
1.88 - double y=(coordinates[2*i+1]+=dy);
1.89 -
1.90 - if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
1.91 -
1.92 - }
1.93 -
1.94 - clicked_x=e->motion.x;
1.95 - clicked_y=e->motion.y;
1.96 -
1.97 - sides->property_points().set_value(coos);
1.98 - }
1.99 - default: break;
1.100 - }
1.101 - return true;
1.102 -}
1.103 -
1.104 -///This function moves only one node of triangle,
1.105 -///but recalculate the location of wight point,
1.106 -///and also redraw the sides of the planefigure.
1.107 -bool CanvasExample::event_handler(GdkEvent* e, int b)
1.108 -{
1.109 - switch(e->type)
1.110 - {
1.111 - case GDK_BUTTON_PRESS:
1.112 - clicked_x=e->button.x;
1.113 - clicked_y=e->button.y;
1.114 - active_item=(get_item_at(e->button.x, e->button.y));
1.115 - isbutton=true;
1.116 - break;
1.117 - case GDK_BUTTON_RELEASE:
1.118 - isbutton=false;
1.119 - active_item=NULL;
1.120 - break;
1.121 - case GDK_MOTION_NOTIFY:
1.122 - if(isbutton)
1.123 - {
1.124 - //double x1, y1, x2, y2;
1.125 - //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
1.126 - //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
1.127 - //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
1.128 - double dx=e->motion.x-clicked_x;
1.129 - double dy=e->motion.y-clicked_y;
1.130 - active_item->move(dx, dy);
1.131 -
1.132 - coordinates[2*b]+=dx;
1.133 - coordinates[2*b+1]+=dy;
1.134 -
1.135 - Gnome::Canvas::Points coos;
1.136 -
1.137 - double x_wp=0;
1.138 - double y_wp=0;
1.139 -
1.140 - for(int i=0;i<noe;i++)
1.141 - {
1.142 - coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
1.143 -
1.144 - x_wp+=coordinates[2*i];
1.145 - y_wp+=coordinates[2*i+1];
1.146 - }
1.147 -
1.148 - sides->property_points().set_value(coos);
1.149 -
1.150 - x_wp/=noe;
1.151 - y_wp/=noe;
1.152 -
1.153 - dx=x_wp-coordinates[noe*2];
1.154 - dy=y_wp-coordinates[noe*2+1];
1.155 - nodes[noe]->move(dx, dy);
1.156 -
1.157 - coordinates[noe*2]+=dx;
1.158 - coordinates[noe*2+1]+=dy;
1.159 -
1.160 - clicked_x=e->motion.x;
1.161 - clicked_y=e->motion.y;
1.162 - }
1.163 - default: break;
1.164 - }
1.165 - return true;
1.166 -}
1.167 -
1.168 -CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
1.169 -{
1.170 - noe=numofcoos/2;
1.171 -
1.172 - coordinates=new double [numofcoos+2];
1.173 -
1.174 - double x_wp=0;
1.175 - double y_wp=0;
1.176 -
1.177 - Gnome::Canvas::Points coos;
1.178 - for(int i=0;i<numofcoos;i+=2)
1.179 - {
1.180 - coordinates[i]=coosarray[i];
1.181 - coordinates[i+1]=coosarray[i+1];
1.182 - coos.push_back(Gnome::Art::Point(coordinates[i],
1.183 - coordinates[i+1]));
1.184 -
1.185 - x_wp+=coordinates[i];
1.186 - y_wp+=coordinates[i+1];
1.187 -
1.188 - }
1.189 -
1.190 - sides=new Gnome::Canvas::Polygon(triangle, coos);
1.191 - *sides << Gnome::Canvas::Properties::outline_color("green");
1.192 - sides->property_width_pixels().set_value(10);
1.193 -
1.194 - nodes=new Gnome::Canvas::Ellipse* [noe+1];
1.195 -
1.196 - for(int i=0; i<noe;i++)
1.197 - {
1.198 - 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);
1.199 - *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
1.200 - *(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
1.201 - (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i));
1.202 - }
1.203 -
1.204 - coordinates[numofcoos]=x_wp/noe;
1.205 - coordinates[numofcoos+1]=y_wp/noe;
1.206 -
1.207 - wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]);
1.208 -
1.209 - nodes[noe]= new Gnome::Canvas::Ellipse
1.210 - (
1.211 - triangle,
1.212 - coordinates[numofcoos]-20,
1.213 - coordinates[numofcoos+1]-20,
1.214 - coordinates[numofcoos]+20,
1.215 - coordinates[numofcoos+1]+20
1.216 - );
1.217 - *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
1.218 - *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
1.219 - (nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover));
1.220 -
1.221 -
1.222 -
1.223 -}
1.224 -
1.225 -CanvasExample::~CanvasExample()
1.226 -{
1.227 -}
1.228 -
1.229 -//MainWin:
1.230 -
1.231 -class MainWin : public Gtk::Window
1.232 -{
1.233 -public:
1.234 - MainWin(const std::string& title, double *, int);
1.235 -
1.236 -protected:
1.237 - //Member widgets:
1.238 - CanvasExample m_canvas;
1.239 -};
1.240 -
1.241 -MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
1.242 -{
1.243 - set_title (title);
1.244 - add(m_canvas);
1.245 - set_default_size(900,600);
1.246 -
1.247 - show_all();
1.248 -}
1.249 -
1.250 -//main():
1.251 -
1.252 -int main(int argc, char *argv[])
1.253 -{
1.254 - if((argc>=7)&& (argc%2) )
1.255 - {
1.256 - double * coosarray=new double[argc];
1.257 -
1.258 - for(int i=1;i<argc;i++)
1.259 - {
1.260 - coosarray[i-1]=atof(argv[i]);
1.261 - printf("%g%c",coosarray[i-1],i%2?' ':'\n');
1.262 - }
1.263 -
1.264 - Gnome::Canvas::init();
1.265 - Gtk::Main app(argc, argv);
1.266 -
1.267 - MainWin mainwin("Magic Triangle",coosarray,argc-1);
1.268 - app.run(mainwin);
1.269 - }
1.270 -
1.271 - return 0;
1.272 -}