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