Revision 1089 branches/simulator/projects/simulator/simulator/gui/gtk_environment_view.c

View differences:

gtk_environment_view.c
6 6

  
7 7
#include <gtk/gtkmain.h>
8 8
#include <gtk/gtksignal.h>
9
#include <gdk/gdkkeysyms.h>
9 10

  
10 11
#include "gtk_environment_view.h"
11 12

  
......
36 37
				GdkEventButton* event);
37 38
static gboolean gtk_environment_view_mouse_move(GtkWidget* widget,
38 39
				GdkEventMotion* event);
40
static gboolean gtk_environment_view_key_press(GtkWidget* widget,
41
				GdkEventKey* event);
39 42

  
40 43

  
41 44
static void draw_world(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, world_t* world);
42 45
static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, 
43
                        float x, float y, float angle);
46
                        float x, float y, float angle, int selected);
44 47

  
45 48
static void to_world_coordinates(GtkEnvironmentView* view, int x, int y, float* world_x, float* world_y);
46 49
static void to_window_coordinates(GtkEnvironmentView* view, float x, float y, int* win_x, int* win_y);
......
88 91
	widget_class->button_press_event = gtk_environment_view_mouse_down;
89 92
	widget_class->button_release_event = gtk_environment_view_mouse_release;
90 93
	widget_class->motion_notify_event = gtk_environment_view_mouse_move;
94
	widget_class->key_press_event = gtk_environment_view_key_press;
91 95
}
92 96

  
93 97
GtkWidget* gtk_environment_view_new(void)
......
105 109
	view->scale = 2.0 / 800.0;
106 110
	view->width = -1;
107 111
	view->height = -1;
112
	view->selected_robot = -1;
108 113

  
109
	GTK_WIDGET_SET_FLAGS(&(view->widget), GTK_DOUBLE_BUFFERED);
114
	GTK_WIDGET_SET_FLAGS(&(view->widget), GTK_DOUBLE_BUFFERED | GTK_CAN_FOCUS);
110 115

  
111 116
	view->mouseDown = 0;
112 117
}
......
203 208
	robot_iterator_reset();
204 209
	Robot* r;
205 210
	while ((r = robot_iterator_next()) != NULL) {
206
		draw_robot(view, widget->window, 
207
			widget->style->fg_gc[GTK_WIDGET_STATE(widget)], 
208
            r->pose.x, r->pose.y, r->pose.theta);
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));
209 223
    }
210 224

  
211 225
	return FALSE;
......
218 232
	Robot* r;
219 233
	float closestdist;
220 234
	int closest;
235
	float x, y;
221 236

  
222 237
	if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL)
223 238
		return FALSE;
......
233 248
	view->mouseY = event->y;
234 249
	view->mouseDown = 1;
235 250
	
236
	/*closestdist = 10e8;
251
	closestdist = 10e8;
252
	to_world_coordinates(view, event->x, event->y, &x, &y);
237 253
	robot_iterator_reset();
238
	while ((r = robot_iterator_next()) != NULL) {
254
	while ((r = robot_iterator_next()) != NULL)
239 255
	{
240
		float dist = r->pose.x - 
241
	}*/
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
	}
242 265

  
266
	view->selected_robot = closest;
267

  
243 268
	return FALSE;
244 269
}
245 270

  
......
247 272
				GdkEventButton* event)
248 273
{
249 274
	GtkEnvironmentView* view;
250
	double x1, x2, y1, y2;
251
	int w, h;
275
	Robot* r;
252 276

  
253 277
	if (widget == NULL || !GTK_IS_ENVIRONMENT_VIEW(widget) || event == NULL)
254 278
		return FALSE;
......
259 283
		return FALSE;
260 284
	
261 285
	if (event->button == 1)
286
	{
262 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
	}
263 294
	view->mouseX = event->x;
264 295
	view->mouseY = event->y;
265
	w = abs(view->mouseX - view->mouseDownX);
296
	/*w = abs(view->mouseX - view->mouseDownX);
266 297
	h = abs(view->mouseY - view->mouseDownY);
267 298
	
268 299
	// if the box is big, zoom to the box
......
313 344
			view->scale *= 2.0;
314 345
		view->topLeftX = x1 - view->width / 2 * view->scale;
315 346
		view->topLeftY = y1 - view->height / 2 * view->scale;
316
	}
347
	}*/
317 348

  
318 349
	return FALSE;
319 350
}
......
361 392
	return FALSE;
362 393
}
363 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

  
364 418
void gtk_environment_view_refresh(GtkWidget* view)
365 419
{
366 420
	gdk_threads_enter();
......
456 510
}
457 511

  
458 512
static void draw_robot(GtkEnvironmentView* view, GdkDrawable* drawable, GdkGC* gc, 
459
                        float x, float y, float angle)
513
                        float x, float y, float angle, int selected)
460 514
{
461 515
	int rx, ry;
462 516
	int rox, roy;
......
467 521
	to_window_scale(view, ROBOT_DIAMETER, &ry);
468 522
	to_window_coordinates(view, x, y, &rox, &roy);
469 523

  
470
	gdk_draw_arc(drawable, gc, FALSE, (int)(rox - (float)rx / 2), (int)(roy - (float)ry / 2), 
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), 
471 527
		rx, ry, 0, 360*64);
528
	if (selected)
529
		gdk_gc_set_fill(gc, GDK_SOLID);
472 530

  
473 531
	gdk_draw_line(drawable, gc, rox, roy, 
474 532
		rox + (rx / 1.8) * cos(-angle),

Also available in: Unified diff