diff -r 20b26ee5812b -r 6706c788ebb5 src/work/peter/canvas-test.cc --- a/src/work/peter/canvas-test.cc Wed Mar 16 16:40:21 2005 +0000 +++ b/src/work/peter/canvas-test.cc Wed Mar 16 17:31:04 2005 +0000 @@ -12,53 +12,166 @@ typedef Gnome::Canvas::CanvasAA Parent; public: - CanvasExample(); + CanvasExample(double *, int); virtual ~CanvasExample(); private: + int noe; + + bool isbutton; + double clicked_x, clicked_y; + + ///this variable is needed, because + ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault + ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution + Gnome::Canvas::Item * active_item; + bool event_handler(GdkEvent* e, bool b); - Gnome::Canvas::Points coos; + bool tri_mover(GdkEvent* e, bool b); Gnome::Art::Point * wp; Gnome::Canvas::Ellipse ** nodes; Gnome::Canvas::Polygon * sides; Gnome::Canvas::Group triangle; }; -bool CanvasExample::event_handler(GdkEvent* e, bool b) +bool CanvasExample::tri_mover(GdkEvent* e, bool b) { - bool isbutton=true; switch(e->type) { - case GDK_BUTTON_PRESS: printf("Node is pressed!\n"); break; - //case GDK_BUTTON_RELEASE: printf("Node is released!\n"); break; - default: isbutton=false; break; - } - if(isbutton) - { - //(get_item_at(e->button.x, e->button.y))->move(5,5); - (get_item_at(e->button.x, e->button.y))->hide(); + case GDK_BUTTON_PRESS: + clicked_x=e->button.x; + clicked_y=e->button.y; + isbutton=true; + break; + case GDK_BUTTON_RELEASE: + isbutton=false; + active_item=NULL; + break; + case GDK_MOTION_NOTIFY: + if(isbutton) + { + //double x1, y1, x2, y2; + //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); + //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); + //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); + double dx=e->motion.x-clicked_x; + double dy=e->motion.y-clicked_y; + for(int i=0;i<=noe/2;i++) + { + nodes[i]->move(dx,dy); + } + clicked_x=e->motion.x; + clicked_y=e->motion.y; + + Gnome::Canvas::Points coos; + for(int i=0;iget_bounds(x1,y1,x2,y2); + double x=(x1+x2)/2; + double y=(y1+y2)/2; + coos.push_back(Gnome::Art::Point(x, y)); + } + sides->property_points().set_value(coos); + + } + default: break; } } -CanvasExample::CanvasExample():triangle(*(root()), 0, 0) +bool CanvasExample::event_handler(GdkEvent* e, bool b) { - double ax=100; - double ay=100; - double bx=-100; - double by=100; - double cx=0; - double cy=-100; - coos.push_back(Gnome::Art::Point(100, 100)); - coos.push_back(Gnome::Art::Point(-100, 100)); - coos.push_back(Gnome::Art::Point(0, -100)); + switch(e->type) + { + case GDK_BUTTON_PRESS: + clicked_x=e->button.x; + clicked_y=e->button.y; + active_item=(get_item_at(e->button.x, e->button.y)); + isbutton=true; + break; + case GDK_BUTTON_RELEASE: + isbutton=false; + active_item=NULL; + break; + case GDK_MOTION_NOTIFY: + if(isbutton) + { + //double x1, y1, x2, y2; + //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); + //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); + //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); + double dx=e->motion.x-clicked_x; + double dy=e->motion.y-clicked_y; + active_item->move(dx, dy); + + Gnome::Canvas::Points coos; + + double x_wp=0; + double y_wp=0; + + for(int i=0;iget_bounds(x1,y1,x2,y2); + double x=(x1+x2)/2; + double y=(y1+y2)/2; + coos.push_back(Gnome::Art::Point(x, y)); + + if(i<3) + { + x_wp+=x; + y_wp+=y; + } + } + + sides->property_points().set_value(coos); + + x_wp/=3; + y_wp/=3; + + double x1,y1,x2,y2; + nodes[noe/2]->get_bounds(x1,y1,x2,y2); + double x=(x1+x2)/2; + double y=(y1+y2)/2; + + dx=x_wp-x; + dy=y_wp-y; + nodes[noe/2]->move(dx, dy); + + clicked_x=e->motion.x; + clicked_y=e->motion.y; + } + default: break; + } +} + +CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL) +{ + noe=numofels; + + int j=0; + double ax=coosarray[j++]; + double ay=coosarray[j++]; + double bx=coosarray[j++]; + double by=coosarray[j++]; + double cx=coosarray[j++]; + double cy=coosarray[j++]; + + Gnome::Canvas::Points coos; + for(int i=0;iproperty_width_pixels().set_value(10); - nodes=new ( Gnome::Canvas::Ellipse * ) [4]; + nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1]; - for(int i=0; i<3;i++) + for(int i=0; iget_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20); - *(nodes[3]) << Gnome::Canvas::Properties::fill_color("blue"); - *(nodes[3]) << Gnome::Canvas::Properties::outline_color("black"); + nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20); + *(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue"); + *(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black"); + (nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true)); } @@ -84,14 +198,14 @@ class MainWin : public Gtk::Window { public: - MainWin(const std::string& title); + MainWin(const std::string& title, double *, int); protected: //Member widgets: CanvasExample m_canvas; }; -MainWin::MainWin(const std::string& title) +MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe) { set_title (title); add(m_canvas); @@ -104,11 +218,23 @@ int main(int argc, char *argv[]) { - Gnome::Canvas::init(); - Gtk::Main app(argc, argv); + if((argc>=7)&&( (argc/2)!=( (argc+1)/2 ) ) ) + { + double * coosarray=new double[argc]; - MainWin mainwin("Gnome::Canvas Example"); - app.run(mainwin); + for(int i=1;i