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