[Lemon-commits] [lemon_svn] hegyi: r1641 - hugo/trunk/src/work/peter
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:46:44 CET 2006
Author: hegyi
Date: Wed Mar 16 18:31:04 2005
New Revision: 1641
Modified:
hugo/trunk/src/work/peter/Makefile
hugo/trunk/src/work/peter/canvas-test.cc
Log:
Magic triangle is READY.
Modified: hugo/trunk/src/work/peter/Makefile
==============================================================================
--- hugo/trunk/src/work/peter/Makefile (original)
+++ hugo/trunk/src/work/peter/Makefile Wed Mar 16 18:31:04 2005
@@ -1,6 +1,2 @@
-hier: hierarchygraph.h hierarchygraph_test.cc
- g++ -Wall -W -I../.. -I../klao -I../../lemon hierarchygraph_test.cc -o test
-
-edge: edgepathgraph_test.cc edgepathgraph.h
- g++ -Wall -W -I../.. -I../klao -I../../lemon edgepathgraph_test.cc -o test
-
+triangle: canvas-test.cc
+ g++ canvas-test.cc -o triangle `pkg-config libgnomecanvasmm-2.6 --cflags --libs`
Modified: hugo/trunk/src/work/peter/canvas-test.cc
==============================================================================
--- hugo/trunk/src/work/peter/canvas-test.cc (original)
+++ hugo/trunk/src/work/peter/canvas-test.cc Wed Mar 16 18:31:04 2005
@@ -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;
+ 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;i<noe/2;i++)
+ {
+ double x1,y1,x2,y2;
+ nodes[i]->get_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;
}
- if(isbutton)
+}
+
+bool CanvasExample::event_handler(GdkEvent* e, bool b)
+{
+ switch(e->type)
{
- //(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;
+ 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;i<noe/2;i++)
+ {
+ double x1,y1,x2,y2;
+ nodes[i]->get_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():triangle(*(root()), 0, 0)
+CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
{
- 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));
+ 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;i<noe;i++)
+ {
+ double x=coosarray[i++];
+ double y=coosarray[i];
+ coos.push_back(Gnome::Art::Point(x, y));
+ }
sides=new Gnome::Canvas::Polygon(triangle, coos);
*sides << Gnome::Canvas::Properties::outline_color("green");
sides->property_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; i<noe/2;i++)
{
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);
*(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
@@ -68,9 +181,10 @@
wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3);
- nodes[3]= new Gnome::Canvas::Ellipse(triangle, wp->get_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];
+
+ for(int i=1;i<argc;i++)
+ {
+ coosarray[i-1]=atof(argv[i]);
+ }
+
+ Gnome::Canvas::init();
+ Gtk::Main app(argc, argv);
+
+ MainWin mainwin("Magic Triangle",coosarray,argc-1);
+ app.run(mainwin);
+ }
- MainWin mainwin("Gnome::Canvas Example");
- app.run(mainwin);
+ printf("Usage:\n./triangle x1 y1 x2 y2 x3 y3\nWhere xi and yi are the coordinates of the points of the triangle\n");
return 0;
}
More information about the Lemon-commits
mailing list