[go: up one dir, main page]

File: gui.c

package info (click to toggle)
stage 2.0.3-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,676 kB
  • ctags: 1,725
  • sloc: ansic: 10,192; sh: 8,357; cpp: 3,676; makefile: 199
file content (1192 lines) | stat: -rw-r--r-- 31,253 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
/*
CVS: $Id: gui.c,v 1.108.2.1 2006/09/25 17:49:49 gerkey Exp $
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>

//#define DEBUG
//#undef DEBUG

// new canvas layer
#include "stage_internal.h"
#include "gui.h"

#if INCLUDE_GNOME
#include "gnome.h"
#endif

#define STG_DEFAULT_WINDOW_WIDTH 700
#define STG_DEFAULT_WINDOW_HEIGHT 740

// only models that have fewer rectangles than this get matrix
// rendered when dragged
#define STG_POLY_THRESHOLD 10

// single static application visible to all funcs in this file
static stg_rtk_app_t *app = NULL; 

// some global figures anyone can draw in for debug purposes
stg_rtk_fig_t* fig_debug_rays = NULL;
stg_rtk_fig_t* fig_debug_geom = NULL;
stg_rtk_fig_t* fig_debug_matrix = NULL;
stg_rtk_fig_t* fig_trails = NULL;

int _render_matrix_deltas = FALSE;

/** @addtogroup stage 
    @{
*/

/** @defgroup window Window

The Stage window consists of a menu bar, a view of the simulated
world, and a status bar.

The world view shows part of the simulated world. You can zoom the
view in and out, and scroll it to see more of the world. Simulated
robot devices, obstacles, etc., are rendered as colored polygons. The
world view can also show visualizations of the data and configuration
of various sensor and actuator models. The View menu has options to
control which data and configurations are rendered.

<h2>Worldfile Properties</h2>

@par Summary and default values

@verbatim
window
(
  # gui properties
  center [0 0]
  size [700 740]
  scale 1.0

  # model properties do not apply to the gui window
)
@endverbatim

@par Details
- size [width:int width:int]
  - size of the window in pixels
- center [x:float y:float]
  - location of the center of the window in world coordinates (meters)
  - scale [?:double]
  - ratio of world to pixel coordinates (window zoom)


<h2>Using the Stage window</h2>


<h3>Scrolling the view</h3>

<p>Left-click and drag on the background to move your view of the world.

<h3>Zooming the view</h3>

<p>Right-click and drag on the background to zoom your view of the
world. When you press the right mouse button, a circle appears. Moving
the mouse adjusts the size of the circle; the current view is scaled
with the circle.

<h3>Saving the world</h3>

<P>You can save the current pose of everything in the world, using the
File/Save menu item. <b>Warning: the saved poses overwrite the current
world file.</b> Make a copy of your world file before saving if you
want to keep the old poses.


<h3>Saving a screenshot</h3>

<p> The File/Export menu allows you to export a screenshot of the
current world view in JPEG or PNG format. The frame is saved in the
current directory with filename in the format "stage-(frame
number).(jpg/png)". 

 You can also save sequences of screen shots. To start saving a
sequence, select the desired time interval from the same menu, then
select File/Export/Sequence of frames. The frames are saved in the
current directory with filenames in the format "stage-(sequence
number)-(frame number).(jpg/png)".

The frame and sequence numbers are reset to zero every time you run
Stage, so be careful to rename important frames before they are
overwritten.

<h3>Pausing and resuming the clock</h3>

<p>The Clock/Pause menu item allows you to stop the simulation clock,
freezing the world. Selecting this item again re-starts the clock.


<h3>View options</h3>

<p>The View menu allows you to toggle rendering of a 1m grid, to help
you line up objects (View/Grid). You can control whether polygons are
filled (View/Fill polygons); turning this off slightly improves
graphics performance. The rest of the view menu contains options for
rendering of data and configuration for each type of model, and a
debug menu that enables visualization of some of the innards of Stage.

*/

/** @} */

void gui_load( gui_window_t* win, int section )
{
  // remember the section for saving later
  win->wf_section = section;

  int window_width = 
    (int)wf_read_tuple_float(section, "size", 0, STG_DEFAULT_WINDOW_WIDTH);
  int window_height = 
    (int)wf_read_tuple_float(section, "size", 1, STG_DEFAULT_WINDOW_HEIGHT);
  
  double window_center_x = 
    wf_read_tuple_float(section, "center", 0, 0.0 );
  double window_center_y = 
    wf_read_tuple_float(section, "center", 1, 0.0 );
  
  double window_scale = 
    wf_read_float(section, "scale", 1.0 );
  
  win->fill_polygons = wf_read_int(section, "fill_polygons", 1 );
  win->show_grid = wf_read_int(section, "show_grid", 1 );
  
  PRINT_DEBUG2( "window width %d height %d", window_width, window_height );
  PRINT_DEBUG2( "window center (%.2f,%.2f)", window_center_x, window_center_y );
  PRINT_DEBUG1( "window scale %.2f", window_scale );
  
  // ask the canvas to comply
  gtk_window_resize( GTK_WINDOW(win->frame), window_width, window_height );
  stg_rtk_canvas_scale( win->canvas, window_scale, window_scale );
  stg_rtk_canvas_origin( win->canvas, window_center_x, window_center_y );

}

void gui_save( gui_window_t* win )
{
  int width, height;
  gtk_window_get_size(  GTK_WINDOW(win->frame), &width, &height );
  
  wf_write_tuple_float( win->wf_section, "size", 0, width );
  wf_write_tuple_float( win->wf_section, "size", 1, height );

  wf_write_tuple_float( win->wf_section, "center", 0, win->canvas->ox );
  wf_write_tuple_float( win->wf_section, "center", 1, win->canvas->oy );

  wf_write_float( win->wf_section, "scale", win->canvas->sx );

  wf_write_int( win->wf_section, "fill_polygons", win->fill_polygons );
  wf_write_int( win->wf_section, "show_grid", win->show_grid );

  // save the state of the property toggles
  for( GList* list = win->toggle_list; list; list = g_list_next( list ) )
    {
      stg_property_toggle_args_t* tog = 
	(stg_property_toggle_args_t*)list->data;
      assert( tog );
      printf( "toggle item path: %s\n", tog->path );

      wf_write_int( win->wf_section, tog->path, 
		   gtk_toggle_action_get_active(  GTK_TOGGLE_ACTION(tog->action) ));
    }
}

void gui_startup( int* argc, char** argv[] )
{
  PRINT_DEBUG( "gui startup" );

  stg_rtk_initxx(argc, argv);
  
  app = stg_rtk_app_create();
  stg_rtk_app_main_init( app );

  
}

void gui_poll( void )
{
  //PRINT_DEBUG( "gui poll" );
  if( stg_rtk_app_main_loop( app ) )
    stg_quit_request();
}

void gui_shutdown( void )
{
  PRINT_DEBUG( "gui shutdown" );
  stg_rtk_app_main_term( app );  
}

void  signal_destroy( GtkObject *object,
		      gpointer user_data )
{
  PRINT_MSG( "Window destroyed." );
  stg_quit_request();
}

gboolean quit_dialog( GtkWindow* parent )
{
  GtkMessageDialog *dlg = 
    (GtkMessageDialog*)gtk_message_dialog_new( parent,
					       GTK_DIALOG_DESTROY_WITH_PARENT,
					       GTK_MESSAGE_QUESTION,
					       GTK_BUTTONS_YES_NO,
					       "Are you sure you want to quit Stage?" );
  
  
  gint result = gtk_dialog_run( GTK_DIALOG(dlg));
  gtk_widget_destroy(GTK_WIDGET(dlg));
  
  // return TRUE if use clicked YES
  return( result == GTK_RESPONSE_YES );
}


gboolean  signal_delete( GtkWidget *widget,
		     GdkEvent *event,
		     gpointer user_data )
{
  PRINT_MSG( "Request close window." );


  gboolean confirm = quit_dialog( GTK_WINDOW(widget) );

  if( confirm )
    {
      PRINT_MSG( "Confirmed" );
      stg_quit_request();
    }
  else
      PRINT_MSG( "Cancelled" );
  
  return TRUE;
}

gui_window_t* gui_window_create( stg_world_t* world, int xdim, int ydim )
{
  gui_window_t* win = calloc( sizeof(gui_window_t), 1 );

  // Create a top-level window
  win->frame = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size( GTK_WINDOW(win->frame), xdim, ydim ); 

  win->layout = gtk_vbox_new(FALSE, 0);

  GtkHBox* hbox = GTK_HBOX(gtk_hbox_new( FALSE, 3 ));

  win->canvas = stg_rtk_canvas_create( app );

  win->status_bar = GTK_STATUSBAR(gtk_statusbar_new());
  gtk_statusbar_set_has_resize_grip( win->status_bar, FALSE );
  win->clock_label = GTK_LABEL(gtk_label_new( "clock" ));

  GtkWidget* contents = NULL;

#if INCLUDE_GNOME

  contents = gtk_scrolled_window_new( NULL, NULL );
  
  win->gcanvas = GNOME_CANVAS(gnome_canvas_new_aa());
  gtk_container_add( GTK_CONTAINER(contents), 
		     GTK_WIDGET(win->gcanvas) );

  GnomeCanvasItem* bg = 
    gnome_canvas_item_new(gnome_canvas_root(win->gcanvas),
			  gnome_canvas_rect_get_type(),
			  "x1",-world->width/2.0,
			  "y1",-world->height/2.0,
			  "x2", world->width/2.0,
			  "y2", world->height/2.0,
			  "fill_color", "white",
			  NULL);
  
  g_signal_connect( bg, 
		    "event", 
		    G_CALLBACK(background_event_callback), 
		    world );
  
  GnomeCanvasItem* grp = 
    gnome_canvas_item_new( gnome_canvas_root(win->gcanvas),
			   gnome_canvas_group_get_type(),
			   NULL );
  
  gnome_canvas_item_lower_to_bottom( grp );
  gnome_canvas_item_move( grp, world->width, 0 );


  //GnomeCanvasItem *it = 
    gnome_canvas_item_new( GNOME_CANVAS_GROUP(grp),
			   gnome_canvas_widget_get_type(),
			   "width", world->width,
			   "height", world->height,
			   "anchor", GTK_ANCHOR_CENTER,
			   "widget", win->canvas->canvas,
			   NULL);
  

  // flip the vertical axis for the root group
  double flip[6];  
  art_affine_identity( flip );
  art_affine_flip( flip, flip, FALSE, TRUE );
  gnome_canvas_item_affine_relative( GNOME_CANVAS_ITEM(gnome_canvas_root(win->gcanvas)),
				     flip );

  win->zoom = 50.0;

  gnome_canvas_set_pixels_per_unit( win->gcanvas, win->zoom  );
  gnome_canvas_set_center_scroll_region( win->gcanvas, TRUE ); 
  gnome_canvas_set_scroll_region ( win->gcanvas, 
				   1.1 * -world->width/2.0, 
				   1.1 * -world->height/2.0, 
				   1.1 * world->width + world->width/2.0, 
				   1.1 * world->height/2.0  );

#else
  // no GnomeCanvas, so add the RTK window directly into the frame
  //gtk_container_add( GTK_WINDOW(win->scrolled_win), 
  //				 GTK_WIDGET(win->canvas->canvas) );  

  contents = win->canvas->canvas;
#endif

  // Put it all together
  gtk_container_add(GTK_CONTAINER(win->frame), win->layout);

  
  g_signal_connect(GTK_OBJECT(win->frame),
		   "destroy",
		   GTK_SIGNAL_FUNC(signal_destroy),
		   NULL);

  g_signal_connect(GTK_OBJECT(win->frame),
		   "delete-event",
		   GTK_SIGNAL_FUNC(signal_delete),
		   NULL);

  gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(win->clock_label), FALSE, FALSE, 5);

  gtk_box_pack_start(GTK_BOX(hbox), 
  	   GTK_WIDGET(win->status_bar), TRUE, TRUE, 5);

 
  // we'll add these backwards so we can stick the menu in later
  gtk_box_pack_end(GTK_BOX(win->layout), 
		   GTK_WIDGET(hbox), FALSE, TRUE, 0);
  gtk_box_pack_end( GTK_BOX(win->layout),
		    contents, TRUE, TRUE, 0);
  

  win->world = world;
  
  // enable all objects on the canvas to find our window object
  win->canvas->userdata = (void*)win; 
  
  char txt[256];
  snprintf( txt, 256, " Stage v%s", VERSION );
  gtk_statusbar_push( win->status_bar, 0, txt ); 

  snprintf( txt, 256, " Stage: %s", world->token );
  gtk_window_set_title( GTK_WINDOW(win->frame), txt );
  
  win->bg = stg_rtk_fig_create( win->canvas, NULL, 0 );
  
  // draw a mark for the origin
  stg_rtk_fig_color_rgb32( win->bg, stg_lookup_color(STG_GRID_MAJOR_COLOR) );    
  double mark_sz = 0.4;
  stg_rtk_fig_ellipse( win->bg, 0,0,0, mark_sz/3, mark_sz/3, 0 );
  stg_rtk_fig_arrow( win->bg, -mark_sz/2, 0, 0, mark_sz, mark_sz/4 );
  stg_rtk_fig_arrow( win->bg, 0, -mark_sz/2, M_PI/2, mark_sz, mark_sz/4 );
  //stg_rtk_fig_grid( win->bg, 0,0, 100.0, 100.0, 1.0 );
  
  win->show_geom = TRUE;
  win->fill_polygons = TRUE;
  win->show_polygons = TRUE;
  win->frame_interval = 500; // ms
  win->frame_format = STK_IMAGE_FORMAT_PNG;

  win->poses = stg_rtk_fig_create( win->canvas, NULL, 0 );

  
  //gint canvas_w, canvas_h;
  //gtk_layout_get_size( GTK_LAYOUT(win->gcanvas), &canvas_w, &canvas_h );

  /* g_object_get( win->gcanvas,  */
/* 		"width", &canvas_w,  */
/* 		"height", &canvas_h,  */
/* 		NULL ); */

/*   stg_rtk_canvas_size( win->canvas, canvas_w, canvas_h ); */
  
/*   printf( "canvas_w %d xdim %d world->width %.2f\n",  */
/* 	  canvas_w, xdim, world->width ); */

  //gnome_canvas_set_pixels_per_unit( win->gcanvas, 1.0 );//world->width /  canvas_w   );
  
  // start in the center, fully zoomed out
  stg_rtk_canvas_scale( win->canvas, 
  		0.02,
  		0.02 );

  //stg_rtk_canvas_origin( win->canvas, width/2.0, height/2.0 );

  win->selection_active = NULL;
  
  // show the limits of the world
  stg_rtk_canvas_bgcolor( win->canvas, 0.9, 0.9, 0.9 );
  stg_rtk_fig_color_rgb32( win->bg, 0xFFFFFF );    
  stg_rtk_fig_rectangle( win->bg, 0,0,0, world->width, world->height, 1 );
 
  gui_window_menus_create( win );
 
  win->toggle_list = NULL;

  return win;
}

void gui_window_destroy( gui_window_t* win )
{
  PRINT_DEBUG( "gui window destroy" );

  //g_hash_table_destroy( win->guimods );

  stg_rtk_canvas_destroy( win->canvas );
  stg_rtk_fig_destroy( win->bg );
  stg_rtk_fig_destroy( win->poses );
}

gui_window_t* gui_world_create( stg_world_t* world )
{
  PRINT_DEBUG( "gui world create" );
  
  assert(world);
  gui_window_t* win = gui_window_create( world, 
					 STG_DEFAULT_WINDOW_WIDTH,
					 STG_DEFAULT_WINDOW_HEIGHT );
  assert(win);

  // show the window
  gtk_widget_show_all(win->frame);

  return win;
}


void gui_world_render_cell( stg_rtk_fig_t* fig, stg_cell_t* cell )
{
  assert( fig );
  assert( cell );
  
  stg_rtk_fig_rectangle( fig,
			 cell->x, cell->y, 0.0,
			 cell->size, cell->size, 0 );
  
  // if this is a leaf node containing data, draw a little cross
  if( 0 )//cell->data && g_slist_length( (GSList*)cell->data ) )
    {
      stg_rtk_fig_line( fig, 
			cell->x-0.01, cell->y-0.01,
			cell->x+0.01, cell->y+0.01 );

      stg_rtk_fig_line( fig, 
			cell->x-0.01, cell->y+0.01,
			cell->x+0.01, cell->y-0.01 );
    }
  
  if( cell->children[0] )
    {
      int i;
      for( i=0; i<4; i++ )
	gui_world_render_cell( fig, cell->children[i] );  
    }
}

void gui_world_render_cell_occupied( stg_rtk_fig_t* fig, stg_cell_t* cell )
{
  assert( fig );
  assert( cell );
  
  // if this is a leaf node containing data, draw a 
  if( cell->data && g_slist_length( (GSList*)cell->data ) )
    stg_rtk_fig_rectangle( fig,
			   cell->x, cell->y, 0.0,
			   cell->size, cell->size, 0 );            
  
  if( cell->children[0] )
    {
      int i;
      for( i=0; i<4; i++ )
	gui_world_render_cell_occupied( fig, cell->children[i] );  
    }
}

void gui_world_render_cell_cb( gpointer cell, gpointer fig )
{
  gui_world_render_cell( (stg_rtk_fig_t*)fig, (stg_cell_t*)cell );
}


void render_matrix_object( gpointer key, gpointer value, gpointer user )
{
  // value is a list of cells
  GSList* list = (GSList*)value;
  
  while( list )
    {
      stg_cell_t* cell = (stg_cell_t*)list->data;
      stg_rtk_fig_rectangle( (stg_rtk_fig_t*)user,
			     cell->x, cell->y, 0.0,
			     cell->size, cell->size, 0 );
      
      list = list->next;
    }
}

// useful debug function allows plotting the matrix
void gui_world_matrix_table( stg_world_t* world, gui_window_t* win )
{
  if( win->matrix )
    {
      if( world->matrix->ptable )
	g_hash_table_foreach( world->matrix->ptable, 
			  render_matrix_object, 
			      win->matrix );   
    }
}


void gui_pose( stg_rtk_fig_t* fig, stg_model_t* mod )
{
  stg_rtk_fig_arrow_ex( fig, 0,0, mod->pose.x, mod->pose.y, 0.05 );
}


void gui_pose_cb( gpointer key, gpointer value, gpointer user )
{
  gui_pose( (stg_rtk_fig_t*)user, (stg_model_t*)value );
}


// returns 1 if the world should be destroyed
int gui_world_update( stg_world_t* world )
{
  //PRINT_DEBUG( "gui world update" );
  
  gui_window_t* win = world->win;
  
  if( stg_rtk_canvas_isclosed( win->canvas ) )
    {
      puts( "<window closed>" );
      //stg_world_destroy( world );
      return 1;
    }

  if( win->matrix )
    {
      stg_rtk_fig_clear( win->matrix );
      gui_world_render_cell_occupied( win->matrix, world->matrix->root );
      //gui_world_matrix_table( world, win );
    }  
  
  if( win->matrix_tree )
    {
      stg_rtk_fig_clear( win->matrix_tree );
      gui_world_render_cell( win->matrix_tree, world->matrix->root );
    }  

  char clock[256];
#ifdef DEBUG
  snprintf( clock, 255, "Time: %lu:%lu:%02lu:%02lu.%03lu (sim:%3d real:%3d ratio:%2.2f)\tsubs: %d  %s",
	    world->sim_time / (24*3600000), // days
	    world->sim_time / 3600000, // hours
	    (world->sim_time % 3600000) / 60000, // minutes
	    (world->sim_time % 60000) / 1000, // seconds
	    world->sim_time % 1000, // milliseconds
	    (int)world->sim_interval,
	    (int)world->real_interval_measured,
	    (double)world->sim_interval / (double)world->real_interval_measured,
	    world->subs,
	    world->paused ? "--PAUSED--" : "" );
#else

  snprintf( clock, 255, "Time: %lu:%lu:%02lu:%02lu.%03lu\t(sim/real:%2.2f)\tsubs: %d  %s",
	    world->sim_time / (24*3600000), // days
	    world->sim_time / 3600000, // hours
	    (world->sim_time % 3600000) / 60000, // minutes
	    (world->sim_time % 60000) / 1000, // seconds
	    world->sim_time % 1000, // milliseconds
	    (double)world->sim_interval / (double)world->real_interval_measured,
	    world->subs,
	    world->paused ? "--PAUSED--" : "" );
#endif
  

  // smooth the performance avg a little
/*   static double fraction_avg = 1.0; */
/*   fraction_avg = fraction_avg * 0.9 + */
/*     (double)world->wall_interval / (double)world->real_interval_measured * 0.1; */
/*   gtk_progress_bar_set_fraction( win->canvas->perf_bar, fraction_avg ); */
  
/*   static double rt_avg = 1.0; */
/*   rt_avg = rt_avg * 0.9 + */
/*     (double)world->real_interval_measured / */
/*     (double)world->sim_interval * 0.1; */
/*   gtk_progress_bar_set_fraction( win->canvas->rt_bar,  */
/* 				 rt_avg ); */
  
  if( win->show_geom )
    gui_world_geom( world );

  static int trail_interval = 5;
  
  if( fig_trails )
    if( trail_interval++ > 4 )
      {
    void gui_world_trails(stg_world_t *); // forward declaration
	gui_world_trails( world );
	trail_interval = 0;
      }
  
  if( win->selection_active )
    gui_model_display_pose( win->selection_active, "Selection:" );
  
  gtk_label_set_text( win->clock_label, clock );

  stg_rtk_canvas_render( win->canvas );      

  return 0;
}

void gui_world_destroy( stg_world_t* world )
{
  PRINT_DEBUG( "gui world destroy" );
  
  if( world->win && world->win->canvas ) 
    stg_rtk_canvas_destroy( world->win->canvas );
  else
    PRINT_WARN1( "can't find a window for world %d", world->id );
}


// draw trails behing moving models
void gui_model_trail( stg_model_t* mod )
{ 
  assert( fig_trails );

  if( mod->parent ) // only trail top-level objects
    return; 

  stg_rtk_fig_t* spot = stg_rtk_fig_create( mod->world->win->canvas,
					    fig_trails, STG_LAYER_BODY-1 );      
  
  stg_rtk_fig_color_rgb32( spot, mod->color );
  
  
  // draw the bounding box
  stg_pose_t bbox_pose;
  memcpy( &bbox_pose, &mod->geom.pose, sizeof(bbox_pose));
  stg_model_local_to_global( mod, &bbox_pose );
  stg_rtk_fig_rectangle( spot, 
			 bbox_pose.x, bbox_pose.y, bbox_pose.a, 
			 mod->geom.size.x,
			 mod->geom.size.y, 0 );  
}


// wrapper 
void gui_model_trail_cb( gpointer key, gpointer value, gpointer user )
{
  gui_model_trail( (stg_model_t*)value );
}

// render a trail cell for all models
void gui_world_trails( stg_world_t* world )
{
  assert( fig_trails );
  g_hash_table_foreach( world->models, gui_model_trail_cb, NULL ); 
}



const char* gui_model_describe(  stg_model_t* mod )
{
  static char txt[256];
  
  snprintf(txt, sizeof(txt), "model \"%s\" pose: [%.2f,%.2f,%.2f]",  
	   //stg_model_type_string(mod->type), 
	   mod->token, 
	   //mod->world->id, 
	   //mod->id,  
	   mod->pose.x, mod->pose.y, mod->pose.a  );
  
  return txt;
}


void gui_model_display_pose( stg_model_t* mod, char* verb )
{
  char txt[256];
  gui_window_t* win = mod->world->win;

  // display the pose
  snprintf(txt, sizeof(txt), "%s %s", 
	   verb, gui_model_describe(mod)); 
  guint cid = gtk_statusbar_get_context_id( win->status_bar, "on_mouse" );
  gtk_statusbar_pop( win->status_bar, cid ); 
  gtk_statusbar_push( win->status_bar, cid, txt ); 
  //printf( "STATUSBAR: %s\n", txt );
}

#define DEBUG 1

// Process mouse events 
void gui_model_mouse(stg_rtk_fig_t *fig, int event, int mode)
{  
  // each fig links back to the model that owns it
  stg_model_t* mod = (stg_model_t*)fig->userdata;
  assert( mod );

  //printf( "ON MOUSE CALLED BACK for model %s fig %p with userdata %p mask %d\n", 
  //	mod->token, fig, fig->userdata, mod->gui_mask );

  gui_window_t* win = mod->world->win;
  assert(win);

  guint cid=0;
  
  static stg_velocity_t capture_vel;
  stg_pose_t pose;  
  stg_velocity_t zero_vel;
  memset( &capture_vel, 0, sizeof(capture_vel) );
  memset( &zero_vel, 0, sizeof(zero_vel) );

  switch (event)
    {
    case STK_EVENT_MOUSE_OVER:
      win->selection_active = mod;      
      break;

    case STK_EVENT_MOUSE_NOT_OVER:
      win->selection_active = NULL;      
      
      // get rid of the selection info
      cid = gtk_statusbar_get_context_id( win->status_bar, "on_mouse" );
      gtk_statusbar_pop( win->status_bar, cid ); 
      break;
      
    case STK_EVENT_PRESS:
      {
	// store the velocity at which we grabbed the model (if it has a velocity)
	stg_model_get_velocity( mod, &capture_vel );

	stg_velocity_t zv;
	memset( &zv, 0, sizeof(zv));
	stg_model_set_velocity( mod, &zv );     
      }
      // DELIBERATE NO-BREAK      

    case STK_EVENT_MOTION:       
      // move the object to follow the mouse
      stg_rtk_fig_get_origin(fig, &pose.x, &pose.y, &pose.a );
      
      // TODO - if there are more motion events pending, do nothing.
      //if( !gtk_events_pending() )
      
      // only update simple objects on drag
      //if( mod->polygons->len < STG_POLY_THRESHOLD )
      //stg_model_set_pose( mod, &pose );

      stg_model_set_pose( mod, &pose );
      
      // display the pose
      //gui_model_display_pose( mod, "Dragging:" );
      break;
      
    case STK_EVENT_RELEASE:
      // move the entity to its final position
      stg_rtk_fig_get_origin(fig, &pose.x, &pose.y, &pose.a );
      stg_model_set_pose( mod, &pose );
      
      // and restore the velocity at which we grabbed it
      stg_model_set_velocity( mod, &capture_vel );
      break;      
      
    default:
      break;
    }

  return;
}

int stg_fig_clear_cb(  stg_model_t* mod, char* name, 
		       void* data, size_t len, void* userp )
{
  printf( "clear CB %p\n", userp );
  
  if( userp )
    stg_rtk_fig_clear((stg_rtk_fig_t*)userp);
  
  return 1; // cancels callback
}



void gui_model_create( stg_model_t* mod )
{
  PRINT_DEBUG( "gui model create" );
  
  //gui_window_t* win = mod->world->win;  

  stg_rtk_fig_t* parent = mod->world->win->bg;
  
  if( mod->parent )
    parent = stg_model_get_fig( mod->parent, "top" );
  
  stg_rtk_fig_t* top = stg_rtk_fig_create(  mod->world->win->canvas, parent, STG_LAYER_BODY );
  
  top->userdata = mod;
		     
  // install the figure 
  g_datalist_set_data( &mod->figs, "top", top ); 

  int ch;
  for(ch=0; ch < mod->children->len; ch++ )
    {
      stg_model_t* child = g_ptr_array_index( mod->children, ch );
      gui_model_create( child );
    }

  // XX
  //stg_model_property_refresh( mod, "polygons" );
}

void gui_model_destroy( stg_model_t* mod )
{
  PRINT_DEBUG( "gui model destroy" );
  
  // TODO - It's too late to kill the figs - the canvas is gone! fix this?
  
  stg_rtk_fig_t* fig = stg_model_get_fig( mod, "top" );
  
  if( fig ) 
    stg_rtk_fig_and_descendents_destroy( fig );

  fig = NULL;
}


/// render a model's global pose vector
void gui_model_render_geom_global( stg_model_t* mod, stg_rtk_fig_t* fig )
{
  stg_pose_t glob;
  stg_model_get_global_pose( mod, &glob );
  
  stg_rtk_fig_color_rgb32( fig, 0x0000FF ); // blue
  //stg_rtk_fig_line( fig, 0, 0, glob.x, glob.y );
  stg_rtk_fig_arrow( fig, glob.x, glob.y, glob.a, 0.15, 0.05 );  
  
  if( mod->parent )
    {
      stg_pose_t parentpose;
      stg_model_get_global_pose( mod->parent, &parentpose );
      stg_rtk_fig_line( fig, parentpose.x, parentpose.y, glob.x, parentpose.y );
      stg_rtk_fig_line( fig, glob.x, parentpose.y, glob.x, glob.y );
    }
  else
    {
      stg_rtk_fig_line( fig, 0, 0, glob.x, 0 );
      stg_rtk_fig_line( fig, glob.x, 0, glob.x, glob.y );
    }
  
  stg_geom_t geom;
  stg_model_get_geom( mod, &geom );
  
  stg_pose_t localpose;
  memcpy( &localpose, &geom.pose, sizeof(localpose));
  stg_model_local_to_global( mod, &localpose );
  
  // draw the local offset
  stg_rtk_fig_line( fig, 
		glob.x, glob.y, 
		localpose.x, glob.y );

  stg_rtk_fig_line( fig, 
		localpose.x, glob.y, 
		localpose.x, localpose.y );
  
  // draw the bounding box
  stg_pose_t bbox_pose;
  memcpy( &bbox_pose, &geom.pose, sizeof(bbox_pose));
  stg_model_local_to_global( mod, &bbox_pose );
  stg_rtk_fig_rectangle( fig, 
		     bbox_pose.x, bbox_pose.y, bbox_pose.a, 
		     geom.size.x,
		     geom.size.y, 0 );  
}

/// move a model's figure to the model's current location
int gui_model_move( stg_model_t* mod, void* userp )
{ 
  
  stg_rtk_fig_origin( stg_model_get_fig(mod,"top"), 
		      mod->pose.x, mod->pose.y, mod->pose.a );   
  
#if INCLUDE_GNOME
  double r[6], t[6];
  art_affine_rotate(r,RTOD(pose->a));
  gnome_canvas_item_affine_absolute( GNOME_CANVAS_ITEM(mod->grp), r );
  gnome_canvas_item_set( GNOME_CANVAS_ITEM(mod->grp),
			 "x", mod->pose.x,
			 "y", mod->pose.y,
			 NULL );
#endif			

  return 0;
}

///  render a model's geometry if geom viewing is enabled
void gui_model_render_geom( stg_model_t* mod )
{
  if( fig_debug_geom )
    gui_model_render_geom_global( mod, fig_debug_geom ); 
}

/// wrapper for gui_model_render_geom for use in callbacks
void gui_model_render_geom_cb( gpointer key, gpointer value, gpointer user )
{
  gui_model_render_geom( (stg_model_t*)value );
}

/// render the geometry of all models
void gui_world_geom( stg_world_t* world )
{
  if( fig_debug_geom )
    {
      stg_rtk_fig_clear( fig_debug_geom );
      g_hash_table_foreach( world->models, gui_model_render_geom_cb, NULL ); 
    }
}


void stg_model_fig_clear( stg_model_t* mod, const char* figname )
{
  stg_rtk_fig_t* fig = stg_model_get_fig( mod, figname );
  if( fig )
    stg_rtk_fig_clear( fig );
}

int stg_model_fig_clear_cb( stg_model_t* mod, void* data, size_t len, 
			    void* userp )
{
  if( userp )
    stg_model_fig_clear( mod, (char*)userp );
  return 1;  // remove callback
}


stg_rtk_fig_t* stg_model_get_fig( stg_model_t* mod, const char* figname ) 
{
  return( (stg_rtk_fig_t*)g_datalist_get_data( &mod->figs, figname ));  
}


stg_rtk_fig_t* stg_model_fig_get_or_create( stg_model_t* mod, 
					    const char* figname, 
					    const char* parentname, 
					    int layer )
{
  stg_rtk_fig_t* fig = stg_model_get_fig( mod, figname );
  
  if( fig ) 
    return fig;
  else
    return stg_model_fig_create( mod, figname, parentname, layer );
}

stg_rtk_fig_t* stg_model_fig_create( stg_model_t* mod, 
				     const char* figname, 
				     const char* parentname, 
				     int layer )
{
  stg_rtk_fig_t* parent = NULL;
  
  if( parentname )
    parent = stg_model_get_fig( mod, parentname );
  
  stg_rtk_fig_t* fig = stg_rtk_fig_create( mod->world->win->canvas, parent, layer );  
  g_datalist_set_data( &mod->figs, figname, (void*)fig );

  return fig;
}

int gui_model_lines( stg_model_t* mod, void* userp )
{
 /*  puts( "GUI MODEL LINES" ); */

/*   printf( "mod %s rendering %d lines at %p\n", */
/* 	  mod->token, mod->lines_count, mod->lines); */

  stg_rtk_fig_t* fig = stg_model_get_fig( mod, "model_lines_fig" );  
  
  if( ! fig )
    {
      fig = stg_model_fig_create( mod, "model_lines_fig", "top", 40 );  
      stg_rtk_fig_color_rgb32( fig, mod->color );
    }
  
  stg_rtk_fig_clear( fig );
  
  int p,q;
  if( mod->lines && mod->lines_count > 0 )
    for( p=0; p<mod->lines_count; p++ )
      {
	stg_polyline_t* polyline = &mod->lines[p];
	
	if( polyline->points_count > 1 )
	  for( q=0; q<polyline->points_count-1; q++ )
	    {
	      /* printf( "drawing line from %.2f,%.2f to %.2f,%.2f\n", */
/* 				polyline->points[q].x, */
/* 				polyline->points[q].y, */
/* 				polyline->points[q+1].x, */
/* 				polyline->points[q+1].y ); */
		      
	      stg_rtk_fig_line( fig, 
				polyline->points[q].x,
				polyline->points[q].y,
				polyline->points[q+1].x,
				polyline->points[q+1].y );
	    }
      }
  return 0;
}

int gui_model_polygons( stg_model_t* mod, void* userp )
{
  //puts( "model render polygons" );

  stg_rtk_fig_t* fig = stg_model_get_fig( mod, "top" );
  assert( fig );
  
  stg_rtk_fig_clear( fig );
  
  size_t count = mod->polygons_count;
  stg_polygon_t* polys = mod->polygons;
  
  stg_geom_t geom;
  stg_model_get_geom(mod, &geom);
  
  stg_rtk_fig_color_rgb32( fig, mod->color );

  if( polys && count > 0 )
    {
      

	if( ! mod->world->win->fill_polygons )
	{
	  
	  int p;
	  for( p=0; p<count; p++ )
	    stg_rtk_fig_polygon( fig,
				 geom.pose.x,
				 geom.pose.y,
				 geom.pose.a,
				 polys[p].points->len,
				 polys[p].points->data,
				 0 );
	}
      else
	{
	  int p;
	  for( p=0; p<count; p++ )
	    stg_rtk_fig_polygon( fig,
				 geom.pose.x,
				 geom.pose.y,
				 geom.pose.a,
				 polys[p].points->len,
				 polys[p].points->data,
				 ! polys[p].unfilled );
	  
	  if( mod->gui_outline )
	    {
	      stg_rtk_fig_color_rgb32( fig, 0 ); // black
	      
	      for( p=0; p<count; p++ )
		stg_rtk_fig_polygon( fig,
				     geom.pose.x,
				     geom.pose.y,
				     geom.pose.a,
				     polys[p].points->len,
				     polys[p].points->data,
				     0 );

	      stg_rtk_fig_color_rgb32( fig, mod->color ); // change back
	    }
	  
	}
    }
  
  if( mod->boundary )
    {      
      stg_rtk_fig_rectangle( fig, 
			     geom.pose.x, geom.pose.y, geom.pose.a, 
			     geom.size.x, geom.size.y, 0 ); 
    }

  if( mod->gui_nose )
    {       
      // draw an arrow from the center to the front of the model
      stg_rtk_fig_arrow( fig, geom.pose.x, geom.pose.y, geom.pose.a, 
			 geom.size.x/2.0, 0.05 );
    }
    
  return 0;
}


int gui_model_mask( stg_model_t* mod, void* userp )
{
  
  stg_rtk_fig_t* fig = stg_model_get_fig(mod, "top" );
  assert(fig);
  
  stg_rtk_fig_movemask( fig, mod->gui_mask );  
  
  // only install a mouse handler if the object needs one
  //(TODO can we remove mouse handlers dynamically?)
  if( mod->gui_mask )    
    {
      //printf( "adding mouse handler for model %s\n", mod->token );
      stg_rtk_fig_add_mouse_handler( fig, gui_model_mouse );
    }
  return 0;
}
  
int gui_model_grid( stg_model_t* mod, void* userp )
{
  stg_geom_t geom;
  stg_model_get_geom(mod, &geom);
  
  stg_rtk_fig_t* grid = stg_model_get_fig(mod,"grid"); 
  
  // if we need a grid and don't have one, make one
  if( mod->gui_grid  )
    {    
      if( ! grid ) 
	grid = stg_model_fig_create( mod, "grid", "top", STG_LAYER_GRID); 
      
      stg_rtk_fig_clear( grid );
      stg_rtk_fig_color_rgb32( grid, stg_lookup_color(STG_GRID_MAJOR_COLOR ) );      
      stg_rtk_fig_grid( grid, 
			geom.pose.x, geom.pose.y, 
			geom.size.x, geom.size.y, 1.0  ) ;
    }
  else
    if( grid ) // if we have a grid and don't need one, clear it but keep the fig around      
      stg_rtk_fig_clear( grid );
  
  return 0;
}