]> git.cworth.org Git - grrobot/blobdiff - src/grrobot.c
Fix message scrolling. Add NOTICE TURN support. Flush notices.
[grrobot] / src / grrobot.c
index a71a319fdb04da788e959e53a84d13adf97c4544..332cca55206501cda7a77790f9ec1ad1cfaf6132 100644 (file)
@@ -44,15 +44,19 @@ typedef struct grr_game {
     GtkTextBuffer *message_buffer;
     GtkWidget *message_view;
     GtkWidget *command_entry;
+    GtkTextIter message_iter;
 } grr_game_t;
 
 static int
-do_gui (grr_game_t *game);
+grr_game_start_gui (grr_game_t *game);
 
-void
-game_callback (gpointer data,
-              gint fd,
-              GdkInputCondition condition);
+static void
+grr_game_read_callback (gpointer data,
+                       gint fd,
+                       GdkInputCondition condition);
+
+static void
+grr_game_read_notices (grr_game_t *game);
 
 int 
 main (int argc, char **argv)
@@ -83,26 +87,34 @@ main (int argc, char **argv)
 
     gdk_input_add (rr_client_fd (game.client),
                   GDK_INPUT_READ,
-                  game_callback,
+                  grr_game_read_callback,
                   &game);
 
     rr_client_show (game.client, &diagram);
     rr_board_parse (game.board, diagram);
     free (diagram);
 
-    return do_gui (&game);
+    return grr_game_start_gui (&game);
 }
 
-void
-game_callback (gpointer data,
-              gint fd,
-              GdkInputCondition condition)
+static void
+grr_game_read_callback (gpointer data,
+                     gint fd,
+                     GdkInputCondition condition)
 {
     grr_game_t *game = data;
+
+    grr_game_read_notices (game);
+}
+
+static void
+grr_game_read_notices (grr_game_t *game)
+{
     rr_board_t *board = game->board;
     rr_status_t status;
     char **notice_s;
     rr_notice_t *notice;
+    int i;
 
     while (rr_client_notice_pending (game->client)) {
        status = rr_client_next_notice (game->client, &notice_s);
@@ -112,6 +124,18 @@ game_callback (gpointer data,
            gtk_exit (1);
            return;
        }
+       for (i=0; notice_s[i]; i++) {
+           gtk_text_buffer_insert_at_cursor (game->message_buffer, notice_s[i], -1);
+           gtk_text_buffer_insert_at_cursor (game->message_buffer, " ", -1);
+       }
+       gtk_text_buffer_insert_at_cursor (game->message_buffer, "\n", -1);
+       gtk_text_buffer_get_iter_at_offset (game->message_buffer,
+                                           &game->message_iter,
+                                           -1);
+       gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (game->message_view),
+                                     &game->message_iter,
+                                     0.0, FALSE, 0.0, 0.0);
+
        notice = rr_client_parse_notice (game->client, notice_s);
        switch (notice->type) {
        case RR_NOTICE_USER:
@@ -120,7 +144,6 @@ game_callback (gpointer data,
        case RR_NOTICE_DISPOSE:
        case RR_NOTICE_MESSAGE:
        case RR_NOTICE_GAMESTATE:
-       case RR_NOTICE_TURN:
        case RR_NOTICE_JOIN:
        case RR_NOTICE_WATCH:
        case RR_NOTICE_PART:
@@ -135,29 +158,39 @@ game_callback (gpointer data,
        case RR_NOTICE_RESET:
        case RR_NOTICE_SCORE:
        case RR_NOTICE_ACTIVATE:
-           /* Ignore these requests */
+           /* XXX: Need to actually handle many of these. */
+           fprintf (stderr, "Warning: Ignoring notice of type %d\n", notice->type);
            break;
        case RR_NOTICE_POSITION:
            rr_board_position (board, notice->u.position.robot,
                               notice->u.position.x, notice->u.position.y);
            gtk_widget_queue_draw (GTK_WIDGET (game->window));
            break;
+       case RR_NOTICE_TURN:
+           rr_board_set_goal_target (board, notice->u.target);
+           gtk_widget_queue_draw (GTK_WIDGET (game->window));
+           break;
        }
        free (notice);
     }
 }
 
-void
+static void
 command_callback (GtkWidget *widget,
                  grr_game_t *game)
 {
     const gchar *entry_text;
     char **response;
 
-    entry_text = gtk_entry_get_text (GTK_ENTRY (widget));
-    rr_client_request (game->client, entry_text, &response);
-    gtk_entry_set_text (GTK_ENTRY (widget), "");
+    entry_text = gtk_entry_get_text (GTK_ENTRY (game->command_entry));
     gtk_text_buffer_insert_at_cursor (game->message_buffer, entry_text, -1);
+    gtk_text_buffer_insert_at_cursor (game->message_buffer, "\n", -1);
+
+    rr_client_request (game->client, entry_text, &response);
+
+    gtk_entry_set_text (GTK_ENTRY (game->command_entry), "");
+
+    grr_game_read_notices (game);
 
 /* XXX: Huh? Why is this triggering valgrind?
     free (response);
@@ -166,7 +199,7 @@ command_callback (GtkWidget *widget,
 }
 
 static int
-do_gui (grr_game_t *game)
+grr_game_start_gui (grr_game_t *game)
 {
     GtkWidget *board_frame;
     GtkWidget *vpaned;
@@ -232,6 +265,8 @@ do_gui (grr_game_t *game)
 
     gtk_widget_show (window);
     
+    grr_game_read_notices (game);
+
     gtk_main ();
     
     return 0;