root / branches / simulator / projects / simulator / simulator / gui / gtk_environment_view.c @ 1089
History | View | Annotate | Download (14.3 KB)
1 |
#include <math.h> |
---|---|
2 |
#include <stdlib.h> |
3 |
#include <stdio.h> |
4 |
#include <sys/time.h> |
5 |
#include <string.h> |
6 |
|
7 |
#include <gtk/gtkmain.h> |
8 |
#include <gtk/gtksignal.h> |
9 |
#include <gdk/gdkkeysyms.h> |
10 |
|
11 |
#include "gtk_environment_view.h" |
12 |
|
13 |
#include "robot.h" |
14 |
#include "world.h" |
15 |
|
16 |
#define ENVIRONMENT_VIEW_MIN_WIDTH 100 |
17 |
#define ENVIRONMENT_VIEW_MIN_HEIGHT 50 |
18 |
|
19 |
#define ROBOT_DIAMETER 30 |
20 |
|
21 |
static GtkWidgetClass* parent_class = NULL; |
22 |
|
23 |
static void gtk_environment_view_class_init(GtkEnvironmentViewClass* environmentClass); |
24 |
static void gtk_environment_view_init(GtkEnvironmentView* view); |
25 |
static void gtk_environment_view_destroy(GtkObject* object); |
26 |
|
27 |
static void gtk_environment_view_size_request(GtkWidget* widget, |
28 |
GtkRequisition* req); |
29 |
static void gtk_environment_view_realize(GtkWidget* widget); |
30 |
static void gtk_environment_view_size_allocate(GtkWidget* widget, GtkAllocation* a); |
31 |
static gboolean gtk_environment_view_expose(GtkWidget* widget,
|
32 |
GdkEventExpose* event); |
33 |
|
34 |
static gboolean gtk_environment_view_mouse_down(GtkWidget* widget,
|
35 |
GdkEventButton* event); |
36 |
static gboolean gtk_environment_view_mouse_release(GtkWidget* widget,
|
37 |
GdkEventButton* event); |
38 |
static gboolean gtk_environment_view_mouse_move(GtkWidget* widget,
|
39 |
GdkEventMotion* event); |
40 |
static gboolean gtk_environment_view_key_press(GtkWidget* widget,
|
41 |
GdkEventKey* event); |
42 |
|
43 |
|
44 |
static void draw_world(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, world_t* world); |
45 |
static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, |
46 |
float x, float y, float angle, int selected); |
47 |
|
48 |
static void to_world_coordinates(GtkEnvironmentView* view, int x, int y, float* world_x, float* world_y); |
49 |
static void to_window_coordinates(GtkEnvironmentView* view, float x, float y, int* win_x, int* win_y); |
50 |
static void to_window_scale(GtkEnvironmentView* view, float v, int* win_v); |
51 |
|
52 |
|
53 |
GtkType gtk_environment_view_get_type(void)
|
54 |
{ |
55 |
static GtkType environment_view_type = 0; |
56 |
if (!environment_view_type)
|
57 |
{ |
58 |
static const GtkTypeInfo environment_view_info = |
59 |
{ |
60 |
"GtkEnvironmentView",
|
61 |
sizeof(GtkEnvironmentView),
|
62 |
sizeof(GtkEnvironmentViewClass),
|
63 |
(GtkClassInitFunc)gtk_environment_view_class_init, |
64 |
(GtkObjectInitFunc)gtk_environment_view_init, |
65 |
NULL, NULL, (GtkClassInitFunc)NULL |
66 |
}; |
67 |
|
68 |
environment_view_type = gtk_type_unique(GTK_TYPE_WIDGET, |
69 |
&environment_view_info); |
70 |
} |
71 |
|
72 |
return environment_view_type;
|
73 |
} |
74 |
|
75 |
static void gtk_environment_view_class_init(GtkEnvironmentViewClass* environmentClass) |
76 |
{ |
77 |
GtkObjectClass* object_class; |
78 |
GtkWidgetClass* widget_class; |
79 |
|
80 |
object_class = (GtkObjectClass*)environmentClass; |
81 |
widget_class = (GtkWidgetClass*)environmentClass; |
82 |
|
83 |
parent_class = gtk_type_class(gtk_widget_get_type()); |
84 |
|
85 |
object_class->destroy = gtk_environment_view_destroy; |
86 |
|
87 |
widget_class->realize = gtk_environment_view_realize; |
88 |
widget_class->expose_event = gtk_environment_view_expose; |
89 |
widget_class->size_request = gtk_environment_view_size_request; |
90 |
widget_class->size_allocate = gtk_environment_view_size_allocate; |
91 |
widget_class->button_press_event = gtk_environment_view_mouse_down; |
92 |
widget_class->button_release_event = gtk_environment_view_mouse_release; |
93 |
widget_class->motion_notify_event = gtk_environment_view_mouse_move; |
94 |
widget_class->key_press_event = gtk_environment_view_key_press; |
95 |
} |
96 |
|
97 |
GtkWidget* gtk_environment_view_new(void)
|
98 |
{ |
99 |
GtkEnvironmentView* view; |
100 |
view = gtk_type_new(gtk_environment_view_get_type()); |
101 |
|
102 |
return GTK_WIDGET(view);
|
103 |
} |
104 |
|
105 |
static void gtk_environment_view_init(GtkEnvironmentView* view) |
106 |
{ |
107 |
view->topLeftX = -2.0; |
108 |
view->topLeftY = -1.3; |
109 |
view->scale = 2.0 / 800.0; |
110 |
view->width = -1;
|
111 |
view->height = -1;
|
112 |
view->selected_robot = -1;
|
113 |
|
114 |
GTK_WIDGET_SET_FLAGS(&(view->widget), GTK_DOUBLE_BUFFERED | GTK_CAN_FOCUS); |
115 |
|
116 |
view->mouseDown = 0;
|
117 |
} |
118 |
|
119 |
static void gtk_environment_view_destroy(GtkObject* object) |
120 |
{ |
121 |
GtkEnvironmentView* view; |
122 |
g_return_if_fail(object != NULL);
|
123 |
g_return_if_fail(GTK_IS_ENVIRONMENT_VIEW(object)); |
124 |
|
125 |
view = GTK_ENVIRONMENT_VIEW(object); |
126 |
|
127 |
if (GTK_OBJECT_CLASS(parent_class)->destroy)
|
128 |
(* GTK_OBJECT_CLASS(parent_class)->destroy) (object); |
129 |
} |
130 |
|
131 |
static void gtk_environment_view_realize(GtkWidget* widget) |
132 |
{ |
133 |
GtkEnvironmentView* view; |
134 |
GdkWindowAttr attributes; |
135 |
gint attributes_mask; |
136 |
|
137 |
g_return_if_fail( widget != NULL);
|
138 |
g_return_if_fail( GTK_IS_ENVIRONMENT_VIEW(widget)); |
139 |
|
140 |
GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED); |
141 |
view = GTK_ENVIRONMENT_VIEW(widget); |
142 |
|
143 |
attributes.x = widget->allocation.x; |
144 |
attributes.y = widget->allocation.y; |
145 |
attributes.width = widget->allocation.width; |
146 |
attributes.height = widget->allocation.height; |
147 |
attributes.wclass = GDK_INPUT_OUTPUT; |
148 |
attributes.window_type = GDK_WINDOW_CHILD; |
149 |
attributes.event_mask = gtk_widget_get_events(widget) | |
150 |
GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | |
151 |
GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK | |
152 |
GDK_POINTER_MOTION_HINT_MASK; |
153 |
attributes.visual = gtk_widget_get_visual(widget); |
154 |
attributes.colormap = gtk_widget_get_colormap(widget); |
155 |
|
156 |
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; |
157 |
widget->window = gdk_window_new(widget->parent->window, &attributes, |
158 |
attributes_mask); |
159 |
gdk_window_set_user_data(widget->window, widget); |
160 |
widget->style = gtk_style_attach(widget->style, widget->window); |
161 |
gtk_style_set_background(widget->style, widget->window, |
162 |
GTK_STATE_NORMAL); |
163 |
} |
164 |
|
165 |
static void gtk_environment_view_size_request(GtkWidget* widget, |
166 |
GtkRequisition* req) |
167 |
{ |
168 |
if (req->width < ENVIRONMENT_VIEW_MIN_WIDTH)
|
169 |
req->width = ENVIRONMENT_VIEW_MIN_WIDTH; |
170 |
if (req->height < ENVIRONMENT_VIEW_MIN_HEIGHT)
|
171 |
req->height = ENVIRONMENT_VIEW_MIN_HEIGHT; |
172 |
} |
173 |
|
174 |
static void gtk_environment_view_size_allocate(GtkWidget* widget, GtkAllocation* a) |
175 |
{ |
176 |
GtkEnvironmentView* view; |
177 |
|
178 |
g_return_if_fail(widget != NULL);
|
179 |
g_return_if_fail(GTK_IS_ENVIRONMENT_VIEW(widget)); |
180 |
g_return_if_fail(a != NULL);
|
181 |
|
182 |
view = GTK_ENVIRONMENT_VIEW(widget); |
183 |
widget->allocation = *a; |
184 |
|
185 |
view->width = a->width; |
186 |
view->height = a->height; |
187 |
|
188 |
if (GTK_WIDGET_REALIZED(widget))
|
189 |
{ |
190 |
gdk_window_move_resize(widget->window, a->x, a->y, a->width, a->height); |
191 |
} |
192 |
} |
193 |
|
194 |
static gboolean gtk_environment_view_expose(GtkWidget* widget,
|
195 |
GdkEventExpose* event) |
196 |
{ |
197 |
GtkEnvironmentView* view; |
198 |
|
199 |
if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL) |
200 |
return FALSE;
|
201 |
if (event->count > 0) |
202 |
return FALSE;
|
203 |
|
204 |
view = GTK_ENVIRONMENT_VIEW(widget); |
205 |
|
206 |
draw_world(view, widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], &world); |
207 |
|
208 |
robot_iterator_reset(); |
209 |
Robot* r; |
210 |
while ((r = robot_iterator_next()) != NULL) { |
211 |
if (view->mouseDown && view->selected_robot == r->id)
|
212 |
{ |
213 |
float x, y;
|
214 |
to_world_coordinates(view, view->mouseX, view->mouseY, &x, &y); |
215 |
draw_robot(view, widget->window, |
216 |
widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
217 |
x, y, r->pose.theta, 1);
|
218 |
} |
219 |
else
|
220 |
draw_robot(view, widget->window, |
221 |
widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
222 |
r->pose.x, r->pose.y, r->pose.theta, (view->selected_robot == r->id)); |
223 |
} |
224 |
|
225 |
return FALSE;
|
226 |
} |
227 |
|
228 |
static gboolean gtk_environment_view_mouse_down(GtkWidget* widget,
|
229 |
GdkEventButton* event) |
230 |
{ |
231 |
GtkEnvironmentView* view; |
232 |
Robot* r; |
233 |
float closestdist;
|
234 |
int closest;
|
235 |
float x, y;
|
236 |
|
237 |
if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL) |
238 |
return FALSE;
|
239 |
|
240 |
view = GTK_ENVIRONMENT_VIEW(widget); |
241 |
|
242 |
if (event->button != 1) |
243 |
return FALSE;
|
244 |
|
245 |
view->mouseDownX = event->x; |
246 |
view->mouseDownY = event->y; |
247 |
view->mouseX = event->x; |
248 |
view->mouseY = event->y; |
249 |
view->mouseDown = 1;
|
250 |
|
251 |
closestdist = 10e8; |
252 |
to_world_coordinates(view, event->x, event->y, &x, &y); |
253 |
robot_iterator_reset(); |
254 |
while ((r = robot_iterator_next()) != NULL) |
255 |
{ |
256 |
float dx = r->pose.x - x;
|
257 |
float dy = r->pose.y - y;
|
258 |
float dist = dx * dx + dy * dy;
|
259 |
if (dist < closestdist)
|
260 |
{ |
261 |
closestdist = dist; |
262 |
closest = r->id; |
263 |
} |
264 |
} |
265 |
|
266 |
view->selected_robot = closest; |
267 |
|
268 |
return FALSE;
|
269 |
} |
270 |
|
271 |
static gboolean gtk_environment_view_mouse_release(GtkWidget* widget,
|
272 |
GdkEventButton* event) |
273 |
{ |
274 |
GtkEnvironmentView* view; |
275 |
Robot* r; |
276 |
|
277 |
if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL) |
278 |
return FALSE;
|
279 |
|
280 |
view = GTK_ENVIRONMENT_VIEW(widget); |
281 |
|
282 |
if (event->button != 1 && event->button != 3) |
283 |
return FALSE;
|
284 |
|
285 |
if (event->button == 1) |
286 |
{ |
287 |
view->mouseDown = 0;
|
288 |
robots_pause(); |
289 |
r = robot_get(view->selected_robot); |
290 |
to_world_coordinates(view, view->mouseX, view->mouseY, |
291 |
&r->pose.x, &r->pose.y); |
292 |
robots_resume(); |
293 |
} |
294 |
view->mouseX = event->x; |
295 |
view->mouseY = event->y; |
296 |
/*w = abs(view->mouseX - view->mouseDownX);
|
297 |
h = abs(view->mouseY - view->mouseDownY);
|
298 |
|
299 |
// if the box is big, zoom to the box
|
300 |
if (event->button == 1 && (w >= 10 || h >= 10))
|
301 |
{
|
302 |
// we need to make sure the box is to scale
|
303 |
// scale to height
|
304 |
if (fabs((double)w / h) <= fabs((double)view->width / view->height))
|
305 |
{
|
306 |
w = (int)((double)view->width / view->height * h);
|
307 |
if (view->mouseX < view->mouseDownX)
|
308 |
view->mouseX = view->mouseDownX - w;
|
309 |
else
|
310 |
view->mouseX = view->mouseDownX + w;
|
311 |
}
|
312 |
// scale to width
|
313 |
else
|
314 |
{
|
315 |
h = (int)((double)view->height / view->width * w);
|
316 |
if (view->mouseY < view->mouseDownY)
|
317 |
view->mouseY = view->mouseDownY - h;
|
318 |
else
|
319 |
view->mouseY = view->mouseDownY + h;
|
320 |
}
|
321 |
|
322 |
x1 = view->mouseX * view->scale + view->topLeftX;
|
323 |
y1 = view->mouseY * view->scale + view->topLeftY;
|
324 |
x2 = view->mouseDownX * view->scale + view->topLeftX;
|
325 |
y2 = view->mouseDownY * view->scale + view->topLeftY;
|
326 |
if (x2 < x1)
|
327 |
x1 = x2;
|
328 |
if (y2 < y1)
|
329 |
y1 = y2;
|
330 |
|
331 |
view->scale = (w * view->scale) / view->width;
|
332 |
view->topLeftX = x1;
|
333 |
view->topLeftY = y1;
|
334 |
}
|
335 |
else
|
336 |
{
|
337 |
x1 = view->mouseX * view->scale + view->topLeftX;
|
338 |
y1 = view->mouseY * view->scale + view->topLeftY;
|
339 |
|
340 |
// zoom in on left click, out on right click
|
341 |
if (event->button == 1)
|
342 |
view->scale /= 2.0;
|
343 |
else
|
344 |
view->scale *= 2.0;
|
345 |
view->topLeftX = x1 - view->width / 2 * view->scale;
|
346 |
view->topLeftY = y1 - view->height / 2 * view->scale;
|
347 |
}*/
|
348 |
|
349 |
return FALSE;
|
350 |
} |
351 |
|
352 |
static gboolean gtk_environment_view_mouse_move(GtkWidget* widget,
|
353 |
GdkEventMotion* event) |
354 |
{ |
355 |
GtkEnvironmentView* view; |
356 |
|
357 |
if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL) |
358 |
return FALSE;
|
359 |
|
360 |
view = GTK_ENVIRONMENT_VIEW(widget); |
361 |
|
362 |
view->mouseX = event->x; |
363 |
view->mouseY = event->y; |
364 |
|
365 |
if (!view->mouseDown)
|
366 |
return FALSE;
|
367 |
|
368 |
// we need to make sure the new view is to scale
|
369 |
/*int w = abs(view->mouseX - view->mouseDownX);
|
370 |
int h = abs(view->mouseY - view->mouseDownY);
|
371 |
// scale to height
|
372 |
if (fabs((double)w / h) <= fabs((double)view->width / view->height))
|
373 |
{
|
374 |
w = (int)((double)view->width / view->height * h);
|
375 |
if (view->mouseX < view->mouseDownX)
|
376 |
view->mouseX = view->mouseDownX - w;
|
377 |
else
|
378 |
view->mouseX = view->mouseDownX + w;
|
379 |
}
|
380 |
// scale to width
|
381 |
else
|
382 |
{
|
383 |
h = (int)((double)view->height / view->width * w);
|
384 |
if (view->mouseY < view->mouseDownY)
|
385 |
view->mouseY = view->mouseDownY - h;
|
386 |
else
|
387 |
view->mouseY = view->mouseDownY + h;
|
388 |
}*/
|
389 |
|
390 |
gtk_widget_queue_draw_area(widget, 0, 0, view->width, view->height); |
391 |
|
392 |
return FALSE;
|
393 |
} |
394 |
|
395 |
gboolean gtk_environment_view_key_press(GtkWidget* widget, |
396 |
GdkEventKey* event) |
397 |
{ |
398 |
GtkEnvironmentView* view; |
399 |
|
400 |
if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL) |
401 |
return FALSE;
|
402 |
|
403 |
view = GTK_ENVIRONMENT_VIEW(widget); |
404 |
|
405 |
if (event->keyval == GDK_Delete)
|
406 |
{ |
407 |
printf("Deleting.\n");
|
408 |
robots_pause(); |
409 |
if (view->selected_robot != -1) |
410 |
{ |
411 |
robot_destroy(view->selected_robot); |
412 |
} |
413 |
robots_resume(); |
414 |
} |
415 |
return FALSE;
|
416 |
} |
417 |
|
418 |
void gtk_environment_view_refresh(GtkWidget* view)
|
419 |
{ |
420 |
gdk_threads_enter(); |
421 |
gtk_widget_queue_draw(view); |
422 |
gdk_threads_leave(); |
423 |
} |
424 |
|
425 |
void to_world_coordinates(GtkEnvironmentView* view, int x, int y, float* world_x, float* world_y) |
426 |
{ |
427 |
double leftx = world.win.p1.x;
|
428 |
double upy = world.win.p1.x;
|
429 |
double xwidth = world.win.p2.x - leftx;
|
430 |
double yheight = world.win.p2.y - upy;
|
431 |
int width = view->width, height = view->height;
|
432 |
if (xwidth / width < yheight / height)
|
433 |
width = (int)(height / yheight * xwidth);
|
434 |
else
|
435 |
height = (int)(width / xwidth * yheight);
|
436 |
|
437 |
*world_x = (float)x / width * xwidth + leftx;
|
438 |
*world_y = (float)y / height * yheight + upy;
|
439 |
} |
440 |
|
441 |
void to_window_coordinates(GtkEnvironmentView* view, float x, float y, int* win_x, int* win_y) |
442 |
{ |
443 |
double leftx = world.win.p1.x;
|
444 |
double upy = world.win.p1.x;
|
445 |
double xwidth = world.win.p2.x - leftx;
|
446 |
double yheight = world.win.p2.y - upy;
|
447 |
int width = view->width, height = view->height;
|
448 |
if (xwidth / width < yheight / height)
|
449 |
width = (int)(height / yheight * xwidth);
|
450 |
else
|
451 |
height = (int)(width / xwidth * yheight);
|
452 |
|
453 |
*win_x = (int)((x - leftx) / xwidth * width);
|
454 |
*win_y = (int)((y - upy) / yheight * height);
|
455 |
} |
456 |
|
457 |
void to_window_scale(GtkEnvironmentView* view, float v, int* win_v) |
458 |
{ |
459 |
double leftx = world.win.p1.x;
|
460 |
double upy = world.win.p1.x;
|
461 |
double xwidth = world.win.p2.x - leftx;
|
462 |
double yheight = world.win.p2.y - upy;
|
463 |
int width = view->width, height = view->height;
|
464 |
if (xwidth / width < yheight / height)
|
465 |
width = (int)(height / yheight * xwidth);
|
466 |
else
|
467 |
height = (int)(width / xwidth * yheight);
|
468 |
|
469 |
*win_v = (int)(v / xwidth * width);
|
470 |
} |
471 |
|
472 |
static void draw_world(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, world_t* world) |
473 |
{ |
474 |
int i, j;
|
475 |
GdkPoint* points; |
476 |
poly_t* p; |
477 |
|
478 |
if (!drawable || !gc || !world)
|
479 |
return;
|
480 |
|
481 |
for (i = 0 ; i < world->max_objs; i++) |
482 |
{ |
483 |
object_t* o = &(world->objs[i]); |
484 |
switch (o->id)
|
485 |
{ |
486 |
case ID_POLY:
|
487 |
p = (poly_t*)o->props; |
488 |
points = (GdkPoint*)malloc(sizeof(GdkPoint) * p->num_pts);
|
489 |
for (j = 0; j < p->num_pts; j++) |
490 |
to_window_coordinates(view, p->pts[j].x, p->pts[j].y, &(points[j].x), &(points[j].y)); |
491 |
switch(p->type)
|
492 |
{ |
493 |
case POLY_CONNECTED:
|
494 |
gdk_draw_polygon(drawable, gc, 1, points, p->num_pts);
|
495 |
break;
|
496 |
default:
|
497 |
fprintf(stderr, "Detected unexpected type of polygon.\n");
|
498 |
break;
|
499 |
} |
500 |
free(points); |
501 |
break;
|
502 |
// no object
|
503 |
case ID_NULL:
|
504 |
break;
|
505 |
default:
|
506 |
fprintf(stderr, "Unexpected object type %d in world.\n", o->id);
|
507 |
break;
|
508 |
} |
509 |
} |
510 |
} |
511 |
|
512 |
static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, |
513 |
float x, float y, float angle, int selected) |
514 |
{ |
515 |
int rx, ry;
|
516 |
int rox, roy;
|
517 |
if (!drawable || !gc)
|
518 |
return;
|
519 |
|
520 |
to_window_scale(view, ROBOT_DIAMETER, &rx); |
521 |
to_window_scale(view, ROBOT_DIAMETER, &ry); |
522 |
to_window_coordinates(view, x, y, &rox, &roy); |
523 |
|
524 |
if (selected)
|
525 |
gdk_gc_set_fill(gc, GDK_TILED); |
526 |
gdk_draw_arc(drawable, gc, selected, (int)(rox - (float)rx / 2), (int)(roy - (float)ry / 2), |
527 |
rx, ry, 0, 360*64); |
528 |
if (selected)
|
529 |
gdk_gc_set_fill(gc, GDK_SOLID); |
530 |
|
531 |
gdk_draw_line(drawable, gc, rox, roy, |
532 |
rox + (rx / 1.8) * cos(-angle), |
533 |
roy - (ry / 1.8) * sin(-angle)); |
534 |
} |
535 |
|