Revision 1086
Reorganized coordinate transforms in the GUI.
gtk_environment_view.c | ||
---|---|---|
38 | 38 |
GdkEventMotion* event); |
39 | 39 |
|
40 | 40 |
|
41 |
static void draw_world(GdkDrawable* drawable, GdkGC* gc, world_t* world); |
|
42 |
static void draw_robot(GdkDrawable* drawable, GdkGC* gc, |
|
41 |
static void draw_world(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, world_t* world);
|
|
42 |
static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc,
|
|
43 | 43 |
float x, float y, float angle); |
44 | 44 |
|
45 |
static void to_world_coordinates(GtkEnvironmentView* view, int x, int y, float* world_x, float* world_y); |
|
46 |
static void to_window_coordinates(GtkEnvironmentView* view, float x, float y, int* win_x, int* win_y); |
|
47 |
static void to_window_scale(GtkEnvironmentView* view, float v, int* win_v); |
|
48 |
|
|
49 |
|
|
45 | 50 |
GtkType gtk_environment_view_get_type(void) |
46 | 51 |
{ |
47 | 52 |
static GtkType environment_view_type = 0; |
... | ... | |
193 | 198 |
|
194 | 199 |
view = GTK_ENVIRONMENT_VIEW(widget); |
195 | 200 |
|
196 |
draw_world(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], &world); |
|
201 |
draw_world(view, widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)], &world);
|
|
197 | 202 |
|
198 | 203 |
robot_iterator_reset(); |
199 | 204 |
Robot* r; |
200 | 205 |
while ((r = robot_iterator_next()) != NULL) { |
201 |
draw_robot(widget->window, |
|
206 |
draw_robot(view, widget->window,
|
|
202 | 207 |
widget->style->fg_gc[GTK_WIDGET_STATE(widget)], |
203 | 208 |
r->pose.x, r->pose.y, r->pose.theta); |
204 | 209 |
} |
... | ... | |
210 | 215 |
GdkEventButton* event) |
211 | 216 |
{ |
212 | 217 |
GtkEnvironmentView* view; |
218 |
Robot* r; |
|
219 |
float closestdist; |
|
220 |
int closest; |
|
213 | 221 |
|
214 | 222 |
if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL) |
215 | 223 |
return FALSE; |
... | ... | |
224 | 232 |
view->mouseX = event->x; |
225 | 233 |
view->mouseY = event->y; |
226 | 234 |
view->mouseDown = 1; |
235 |
|
|
236 |
/*closestdist = 10e8; |
|
237 |
robot_iterator_reset(); |
|
238 |
while ((r = robot_iterator_next()) != NULL) { |
|
239 |
{ |
|
240 |
float dist = r->pose.x - |
|
241 |
}*/ |
|
227 | 242 |
|
228 | 243 |
return FALSE; |
229 | 244 |
} |
... | ... | |
320 | 335 |
return FALSE; |
321 | 336 |
|
322 | 337 |
// we need to make sure the new view is to scale |
323 |
int w = abs(view->mouseX - view->mouseDownX); |
|
338 |
/*int w = abs(view->mouseX - view->mouseDownX);
|
|
324 | 339 |
int h = abs(view->mouseY - view->mouseDownY); |
325 | 340 |
// scale to height |
326 | 341 |
if (fabs((double)w / h) <= fabs((double)view->width / view->height)) |
... | ... | |
339 | 354 |
view->mouseY = view->mouseDownY - h; |
340 | 355 |
else |
341 | 356 |
view->mouseY = view->mouseDownY + h; |
342 |
} |
|
357 |
}*/
|
|
343 | 358 |
|
344 | 359 |
gtk_widget_queue_draw_area(widget, 0, 0, view->width, view->height); |
345 | 360 |
|
... | ... | |
353 | 368 |
gdk_threads_leave(); |
354 | 369 |
} |
355 | 370 |
|
356 |
static void draw_world(GdkDrawable* drawable, GdkGC* gc, world_t* world)
|
|
371 |
void to_world_coordinates(GtkEnvironmentView* view, int x, int y, float* world_x, float* world_y)
|
|
357 | 372 |
{ |
373 |
double leftx = world.win.p1.x; |
|
374 |
double upy = world.win.p1.x; |
|
375 |
double xwidth = world.win.p2.x - leftx; |
|
376 |
double yheight = world.win.p2.y - upy; |
|
377 |
int width = view->width, height = view->height; |
|
378 |
if (xwidth / width < yheight / height) |
|
379 |
width = (int)(height / yheight * xwidth); |
|
380 |
else |
|
381 |
height = (int)(width / xwidth * yheight); |
|
382 |
|
|
383 |
*world_x = (float)x / width * xwidth + leftx; |
|
384 |
*world_y = (float)y / height * yheight + upy; |
|
385 |
} |
|
386 |
|
|
387 |
void to_window_coordinates(GtkEnvironmentView* view, float x, float y, int* win_x, int* win_y) |
|
388 |
{ |
|
389 |
double leftx = world.win.p1.x; |
|
390 |
double upy = world.win.p1.x; |
|
391 |
double xwidth = world.win.p2.x - leftx; |
|
392 |
double yheight = world.win.p2.y - upy; |
|
393 |
int width = view->width, height = view->height; |
|
394 |
if (xwidth / width < yheight / height) |
|
395 |
width = (int)(height / yheight * xwidth); |
|
396 |
else |
|
397 |
height = (int)(width / xwidth * yheight); |
|
398 |
|
|
399 |
*win_x = (int)((x - leftx) / xwidth * width); |
|
400 |
*win_y = (int)((y - upy) / yheight * height); |
|
401 |
} |
|
402 |
|
|
403 |
void to_window_scale(GtkEnvironmentView* view, float v, int* win_v) |
|
404 |
{ |
|
405 |
double leftx = world.win.p1.x; |
|
406 |
double upy = world.win.p1.x; |
|
407 |
double xwidth = world.win.p2.x - leftx; |
|
408 |
double yheight = world.win.p2.y - upy; |
|
409 |
int width = view->width, height = view->height; |
|
410 |
if (xwidth / width < yheight / height) |
|
411 |
width = (int)(height / yheight * xwidth); |
|
412 |
else |
|
413 |
height = (int)(width / xwidth * yheight); |
|
414 |
|
|
415 |
*win_v = (int)(v / xwidth * width); |
|
416 |
} |
|
417 |
|
|
418 |
static void draw_world(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, world_t* world) |
|
419 |
{ |
|
358 | 420 |
int i, j; |
359 | 421 |
GdkPoint* points; |
360 | 422 |
poly_t* p; |
... | ... | |
362 | 424 |
if (!drawable || !gc || !world) |
363 | 425 |
return; |
364 | 426 |
|
365 |
double leftx = world->win.p1.x; |
|
366 |
double upy = world->win.p1.x; |
|
367 |
double xwidth = world->win.p2.x - leftx; |
|
368 |
double yheight = world->win.p2.y - upy; |
|
369 |
int width, height; |
|
370 |
gdk_drawable_get_size(drawable, &width, &height); |
|
371 |
if (xwidth / width < yheight / height) |
|
372 |
width = (int)(height / yheight * xwidth); |
|
373 |
else |
|
374 |
height = (int)(width / xwidth * yheight); |
|
375 |
|
|
376 | 427 |
for (i = 0 ; i < world->max_objs; i++) |
377 | 428 |
{ |
378 | 429 |
object_t* o = &(world->objs[i]); |
... | ... | |
382 | 433 |
p = (poly_t*)o->props; |
383 | 434 |
points = (GdkPoint*)malloc(sizeof(GdkPoint) * p->num_pts); |
384 | 435 |
for (j = 0; j < p->num_pts; j++) |
385 |
{ |
|
386 |
points[j].x = (int)((p->pts[j].x - leftx) / xwidth * width); |
|
387 |
points[j].y = (int)((p->pts[j].y - upy) / yheight * height); |
|
388 |
} |
|
436 |
to_window_coordinates(view, p->pts[j].x, p->pts[j].y, &(points[j].x), &(points[j].y)); |
|
389 | 437 |
switch(p->type) |
390 | 438 |
{ |
391 | 439 |
case POLY_CONNECTED: |
... | ... | |
407 | 455 |
} |
408 | 456 |
} |
409 | 457 |
|
410 |
static void draw_robot(GdkDrawable* drawable, GdkGC* gc, |
|
458 |
static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc,
|
|
411 | 459 |
float x, float y, float angle) |
412 | 460 |
{ |
413 |
double leftx = world.win.p1.x; |
|
414 |
double upy = world.win.p1.x; |
|
415 |
double xwidth = world.win.p2.x - leftx; |
|
416 |
double yheight = world.win.p2.y - upy; |
|
417 |
double rx, ry; |
|
418 |
int width, height; |
|
419 |
|
|
461 |
int rx, ry; |
|
462 |
int rox, roy; |
|
420 | 463 |
if (!drawable || !gc) |
421 | 464 |
return; |
422 |
gdk_drawable_get_size(drawable, &width, &height); |
|
423 |
if (xwidth / width < yheight / height) |
|
424 |
width = (int)(height / yheight * xwidth); |
|
425 |
else |
|
426 |
height = (int)(width / xwidth * yheight); |
|
427 | 465 |
|
428 |
rx = (double)ROBOT_DIAMETER / xwidth * width; |
|
429 |
ry = (double)ROBOT_DIAMETER / yheight * height; |
|
466 |
to_window_scale(view, ROBOT_DIAMETER, &rx); |
|
467 |
to_window_scale(view, ROBOT_DIAMETER, &ry); |
|
468 |
to_window_coordinates(view, x, y, &rox, &roy); |
|
430 | 469 |
|
431 |
x = ((x - leftx) / xwidth * width); |
|
432 |
y = ((y - upy) / yheight * height); |
|
433 |
|
|
434 |
gdk_draw_arc(drawable, gc, FALSE, (int)(x - rx / 2), (int)(y - ry / 2), |
|
470 |
gdk_draw_arc(drawable, gc, FALSE, (int)(rox - (float)rx / 2), (int)(roy - (float)ry / 2), |
|
435 | 471 |
rx, ry, 0, 360*64); |
436 | 472 |
|
437 |
gdk_draw_line(drawable, gc, x, y,
|
|
438 |
x + (rx / 1.8) * cos(-angle), |
|
439 |
y - (ry / 1.8) * sin(-angle)); |
|
473 |
gdk_draw_line(drawable, gc, rox, roy,
|
|
474 |
rox + (rx / 1.8) * cos(-angle),
|
|
475 |
roy - (ry / 1.8) * sin(-angle));
|
|
440 | 476 |
} |
441 | 477 |
|
Also available in: Unified diff