/* grr_board_view - GTK+ widget for displaying an rr_board
*
- * Copyright © 2003 Carl Worth
+ * Copyright © 2003 Carl Worth
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
#include <stdlib.h>
#include <string.h>
-#include <Xr.h>
-#include <xsvg.h>
+#include <cairo.h>
+#include <cairo-xlib.h>
+#include <librsvg/rsvg.h>
+#include <librsvg/rsvg-cairo.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
GdkEventMotion *event);
static void grr_board_view_update_mouse (grr_board_view_t *view, gint x, gint y);
-static void grr_board_view_update (grr_board_view_t *view);
/* Local data */
view->client = NULL;
view->button = 0;
+
+ view->time = 60.0;
+ view->drift_correct = 0.0;
view->timer = 0;
+ view->zap_time = 0.0;
view->cell_width = 0;
view->cell_height = 0;
- view->cell_icon = grr_icon_create ("cell");
+ view->cell1_icon = grr_icon_create ("cell1");
+ view->cell2_icon = grr_icon_create ("cell2");
view->wall_icon = grr_icon_create ("wall");
for (i=0; i < RR_NUM_ROBOTS; i++) {
}
}
-GtkWidget*
+grr_board_view_t *
grr_board_view_new (rr_board_t *board)
{
grr_board_view_t *view;
+ int x, y;
view = g_object_new (grr_board_view_get_type (), NULL);
view->board = board;
+ rr_board_get_size (board, &view->board_width, &view->board_height);
+
+ view->damaged = malloc (view->board_height * sizeof (int *));
+ for (y = 0; y < view->board_height; y++) {
+ view->damaged[y] = malloc (view->board_width * sizeof (int));
+ for (x = 0; x< view->board_width; x++) {
+ view->damaged[y][x] = 1;
+ }
+ }
- grr_board_view_update (view);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
- return GTK_WIDGET (view);
+ return view;
}
void
view->client = client;
}
+void
+grr_board_view_set_active_robot (grr_board_view_t *view, rr_robot_t robot)
+{
+ g_return_if_fail (view != NULL);
+
+ view->active_robot = robot;
+}
+
+void
+grr_board_view_move_active (grr_board_view_t *view, rr_direction_t dir)
+{
+ if (view->client) {
+ rr_client_move (view->client, view->active_robot, dir);
+ } else {
+ rr_board_move (view->board, view->active_robot, dir);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ }
+}
+
+void
+grr_board_view_undo (grr_board_view_t *view)
+{
+ if (view->client) {
+ rr_client_undo (view->client);
+ } else {
+ rr_board_undo (view->board);
+ gtk_widget_queue_draw (GTK_WIDGET (view));
+ }
+}
+
static void
grr_board_view_destroy (GtkObject *object)
{
static void
grr_board_view_draw_walls (grr_board_view_t *view,
- XrState *xrs,
+ cairo_t *xrs,
rr_wall_t wall)
{
if (wall == RR_WALL_NONE)
grr_icon_draw (view->wall_icon, xrs);
}
if (wall & RR_WALL_LEFT) {
- XrSave (xrs);
- XrRotate (xrs, M_PI_2);
+ cairo_save (xrs);
+ cairo_rotate (xrs, M_PI_2);
grr_icon_draw (view->wall_icon, xrs);
- XrRestore (xrs);
+ cairo_restore (xrs);
}
if (wall & RR_WALL_RIGHT) {
- XrSave (xrs);
- XrTranslate (xrs, GRR_SVG_ASSUMED_WIDTH, 0);
- XrRotate (xrs, M_PI_2);
+ cairo_save (xrs);
+ cairo_translate (xrs, GRR_SVG_ASSUMED_WIDTH, 0);
+ cairo_rotate (xrs, M_PI_2);
grr_icon_draw (view->wall_icon, xrs);
- XrRestore (xrs);
+ cairo_restore (xrs);
}
if (wall & RR_WALL_BELOW) {
- XrSave (xrs);
- XrTranslate (xrs, 0, GRR_SVG_ASSUMED_HEIGHT);
+ cairo_save (xrs);
+ cairo_translate (xrs, 0, GRR_SVG_ASSUMED_HEIGHT);
grr_icon_draw (view->wall_icon, xrs);
- XrRestore (xrs);
+ cairo_restore (xrs);
}
}
-static void
-grr_board_view_draw_cell (grr_board_view_t *view,
- XrState *xrs,
- rr_cell_t cell,
- rr_target_t goal,
- int width,
- int height)
-{
- int xpad, ypad;
- rr_robot_t robot;
- rr_target_t target;
-
- XrSave (xrs);
- XrScale (xrs,
- width / GRR_SVG_ASSUMED_WIDTH,
- height / GRR_SVG_ASSUMED_HEIGHT);
- grr_icon_draw (view->cell_icon, xrs);
- grr_board_view_draw_walls (view, xrs, RR_CELL_GET_WALLS (cell));
- XrRestore (xrs);
-
- xpad = width / 5;
- ypad = width / 5;
-
- XrSave (xrs);
- XrTranslate (xrs, xpad, ypad);
- XrScale (xrs,
- (width - 2*xpad) / GRR_SVG_ASSUMED_WIDTH,
- (height - 2*ypad) / GRR_SVG_ASSUMED_HEIGHT);
-
- target = RR_CELL_GET_TARGET (cell);
- if (target)
- grr_icon_draw (view->target_icon[rr_target_idx (target)], xrs);
-
- XrRestore (xrs);
-
- /* XXX: This is a kludge, (it obscures the cell background in
- addition to the target). The real way to do this is to draw the
- target itself with less opacity. */
- if (target && target != goal) {
- XrSave (xrs);
- XrScale (xrs,
- width / GRR_SVG_ASSUMED_WIDTH,
- height / GRR_SVG_ASSUMED_HEIGHT);
- XrRectangle (xrs, 0, 0,
- GRR_SVG_ASSUMED_WIDTH,
- GRR_SVG_ASSUMED_HEIGHT);
- XrSetRGBColor (xrs, 1, 1, 1);
- XrSetAlpha (xrs, 0.75);
- XrFill (xrs);
- XrRestore (xrs);
- }
-
- XrSave (xrs);
- XrTranslate (xrs, xpad, ypad);
- XrScale (xrs,
- (width - 2*xpad) / GRR_SVG_ASSUMED_WIDTH,
- (height - 2*ypad) / GRR_SVG_ASSUMED_HEIGHT);
-
- robot = RR_CELL_GET_ROBOT (cell);
- if (robot)
- grr_icon_draw (view->robot_icon[rr_robot_idx (robot)], xrs);
-
- XrRestore (xrs);
-
-}
-
static gint
grr_board_view_expose (GtkWidget *widget,
GdkEventExpose *event)
rr_board_t *board;
Display *dpy;
Drawable drawable;
- XrState *xrs;
+ Visual *visual;
+ cairo_surface_t *surface;
+ cairo_t *xrs;
GdkDrawable *real_drawable;
gint x_off, y_off;
int i, j;
g_return_val_if_fail (GRR_IS_BOARD_VIEW (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
- if (event->count > 0)
- return FALSE;
-
view = GRR_BOARD_VIEW (widget);
board = view->board;
- /* Unabstract X from GTK+ */
- gdk_window_get_internal_paint_info (widget->window, &real_drawable, &x_off, &y_off);
- dpy = gdk_x11_drawable_get_xdisplay (real_drawable);
- drawable = gdk_x11_drawable_get_xid (real_drawable);
-
- /* Ignore GTK+ and use Xr for drawing. */
- xrs = XrCreate ();
- XrSetTargetDrawable (xrs, dpy, drawable);
-
- rr_board_get_size (board, &view->board_width, &view->board_height);
+ xrs = gdk_cairo_create (widget->window);
new_cell_width = widget->allocation.width / view->board_width;
if (new_cell_width == 0)
if (new_cell_height == 0)
new_cell_height = 1;
- view->cell_width = new_cell_width;
- view->cell_height = new_cell_height;
-
- /* Pre-render icons if the size has changed */
- /* XXX: This code isn't fully baked yet
if (new_cell_width != view->cell_width
|| new_cell_height != view->cell_height) {
int i;
view->cell_width = new_cell_width;
view->cell_height = new_cell_height;
- XrSave (xrs);
- XrScale (xrs,
+ cairo_save (xrs);
+ cairo_scale (xrs,
view->cell_width / GRR_SVG_ASSUMED_WIDTH,
view->cell_height / GRR_SVG_ASSUMED_HEIGHT);
- grr_icon_predraw (view->cell_icon, xrs,
+ grr_icon_predraw (view->cell1_icon, xrs,
+ view->cell_width, view->cell_height);
+ grr_icon_predraw (view->cell2_icon, xrs,
view->cell_width, view->cell_height);
grr_icon_predraw (view->wall_icon, xrs,
view->cell_width, view->cell_height);
}
- XrRestore (xrs);
+ cairo_restore (xrs);
}
- */
- XrTranslate (xrs, -x_off, -y_off);
+ view->cell_width = new_cell_width;
+ view->cell_height = new_cell_height;
+
+ if (event->area.width) {
+ int x_start, y_start;
+ int x, y;
+ int x_stop, y_stop;
+ grr_board_view_transform_pixel_to_cell (view,
+ event->area.x, event->area.y,
+ &x_start, &y_start);
+ grr_board_view_transform_pixel_to_cell (view,
+ event->area.x + event->area.width - 1,
+ event->area.y + event->area.height - 1,
+ &x_stop, &y_stop);
+ for (y = y_start; y <= y_stop; y++)
+ for (x = x_start; x <= x_stop; x++)
+ view->damaged[y][x] = 1;
+ }
+
+ if (event->count > 0)
+ return FALSE;
+
+ cairo_translate (xrs, -x_off, -y_off);
view->board_pad_x = (widget->allocation.width - view->board_width * view->cell_width) / 2;
view->board_pad_y = (widget->allocation.height - view->board_height * view->cell_height) / 2;
- XrTranslate (xrs, view->board_pad_x, view->board_pad_y);
+ cairo_translate (xrs, view->board_pad_x, view->board_pad_y);
goal_target = rr_board_get_goal_target (board);
- /* Draw cell targets */
+ /* Draw cell contents */
for (j=0; j < view->board_height; j++) {
for (i=0; i < view->board_width; i++) {
- XrSave (xrs);
- XrTranslate (xrs, i * view->cell_width, j * view->cell_height);
- grr_board_view_draw_cell (view, xrs,
- rr_board_get_cell (board, i, j),
- goal_target,
- view->cell_width, view->cell_height);
- XrRestore (xrs);
+ if (! view->damaged[j][i])
+ continue;
+ rr_cell_t cell = rr_board_get_cell (board, i, j);
+ rr_target_t target = RR_CELL_GET_TARGET (cell);
+ rr_robot_t robot = RR_CELL_GET_ROBOT (cell);
+
+ cairo_save (xrs);
+ cairo_translate (xrs, i * view->cell_width, j * view->cell_height);
+
+ if ((i+j) % 2 == 0)
+ grr_icon_draw_predrawn (view->cell1_icon, xrs, 1.0);
+ else
+ grr_icon_draw_predrawn (view->cell2_icon, xrs, 1.0);
+
+ if (target)
+ grr_icon_draw_predrawn (view->target_icon[rr_target_idx (target)],
+ xrs,
+ target == goal_target ? 1.0 : 0.25);
+
+ if (robot)
+ grr_icon_draw_predrawn (view->robot_icon[rr_robot_idx (robot)],
+ xrs, 1.0);
+
+ cairo_restore (xrs);
}
}
- /* Draw grid. */
- XrSave (xrs);
- {
- XrSetRGBColor (xrs, .75, .75, 1);
- XrSetLineWidth (xrs, 1);
- for (j=0; j < view->board_height; j++) {
- XrMoveTo (xrs, 0, j * view->cell_height + 0.5);
- XrRelLineTo (xrs, view->board_width * view->cell_width, 0);
- }
- for (i=0; i < view->board_width; i++) {
- XrMoveTo (xrs, i * view->cell_width + 0.5, 0);
- XrRelLineTo (xrs, 0, view->board_height * view->cell_height);
- }
- XrStroke (xrs);
- }
- XrRestore (xrs);
-
/* Draw goal target in center of board */
- XrSave (xrs);
- {
- XrTranslate (xrs,
+ /* XXX: Not a perfect heuristic. Should check for the vacant box too. */
+ if (view->board_width == 16 && view->board_height == 16) {
+ cairo_save (xrs);
+ cairo_translate (xrs,
(view->board_width / 2 - 1) * view->cell_width,
(view->board_height / 2 - 1) * view->cell_height);
- grr_board_view_draw_cell (view, xrs, goal_target, goal_target,
- view->cell_width * 2, view->cell_height * 2);
+ cairo_scale (xrs,
+ 2 * view->cell_width / GRR_SVG_ASSUMED_WIDTH,
+ 2 * view->cell_height / GRR_SVG_ASSUMED_HEIGHT);
+ cairo_rectangle (xrs, 0, 0, GRR_SVG_ASSUMED_WIDTH, GRR_SVG_ASSUMED_HEIGHT);
+ cairo_set_source_rgb (xrs, 1, 1, 1);
+ cairo_fill (xrs);
+ grr_icon_draw (view->target_icon[rr_target_idx (goal_target)], xrs);
+ cairo_restore (xrs);
+
+ /* Draw clock */
+ if (view->time < 60.0) {
+ cairo_save (xrs);
+ cairo_new_path (xrs);
+ cairo_move_to (xrs,
+ (double) view->board_width / 2 * view->cell_width,
+ (double) view->board_height / 2 * view->cell_height);
+ cairo_arc (xrs,
+ (double) view->board_width / 2 * view->cell_width,
+ (double) view->board_height / 2 * view->cell_height,
+ .9 * view->cell_width,
+ - M_PI_2,
+ 2 * M_PI * (60.0 - view->time) / 60.0 - M_PI_2);
+ cairo_close_path (xrs);
+
+ cairo_set_source_rgba (xrs, 0.0, 0.0, 0.0, 0.5);
+ cairo_fill (xrs);
+
+ cairo_restore (xrs);
+ }
}
- XrRestore (xrs);
/* Draw walls */
for (j=0; j < view->board_height; j++) {
for (i=0; i < view->board_width; i++) {
- XrSave (xrs);
- XrTranslate (xrs, i * view->cell_width, j * view->cell_height);
- XrScale (xrs,
+ if (! view->damaged[j][i])
+ continue;
+ view->damaged[j][i] = 0;
+ cairo_save (xrs);
+ cairo_translate (xrs, i * view->cell_width, j * view->cell_height);
+ cairo_scale (xrs,
view->cell_width / GRR_SVG_ASSUMED_WIDTH,
view->cell_height / GRR_SVG_ASSUMED_HEIGHT);
grr_board_view_draw_walls (view, xrs, RR_CELL_GET_WALLS (rr_board_get_cell(board, i, j)));
- XrRestore (xrs);
+ cairo_restore (xrs);
}
}
- XrDestroy (xrs);
+ cairo_destroy (xrs);
return FALSE;
}
-static void
-grr_board_view_pointer_coords_to_grid (grr_board_view_t *view,
- int pointer_x, int pointer_y,
- int *grid_x, int *grid_y)
+void
+grr_board_view_transform_pixel_to_cell (grr_board_view_t *view,
+ int pixel_x, int pixel_y,
+ int *cell_x, int *cell_y)
{
- *grid_x = (pointer_x - view->board_pad_x) / view->cell_width;
- *grid_y = (pointer_y - view->board_pad_y) / view->cell_height;
+ int x, y;
+ x = (pixel_x - view->board_pad_x) / view->cell_width;
+ y = (pixel_y - view->board_pad_y) / view->cell_height;
+ if (x < 0)
+ x = 0;
+ if (x >= view->board_width)
+ x = view->board_width - 1;
+ if (y < 0)
+ y = 0;
+ if (y >= view->board_height)
+ y = view->board_height - 1;
+ *cell_x = x;
+ *cell_y = y;
}
+void
+grr_board_view_transform_cell_to_pixel (grr_board_view_t *view,
+ int cell_x, int cell_y,
+ int *pixel_x, int *pixel_y)
+{
+ *pixel_x = view->cell_width * cell_x + view->board_pad_x;
+ *pixel_y = view->cell_height * cell_y + view->board_pad_y;
+}
static gint
grr_board_view_button_press (GtkWidget *widget,
- GdkEventButton *event)
+ GdkEventButton *event)
{
grr_board_view_t *view;
rr_cell_t cell;
view = GRR_BOARD_VIEW (widget);
- grr_board_view_pointer_coords_to_grid (view,
- event->x, event->y,
- &x, &y);
+ grr_board_view_transform_pixel_to_cell (view,
+ event->x, event->y,
+ &x, &y);
cell = rr_board_get_cell (view->board, x, y);
if (!view->button && RR_CELL_GET_ROBOT (cell)) {
gtk_grab_add (widget);
view->button = event->button;
- view->drag_robot = RR_CELL_GET_ROBOT (cell);
+ grr_board_view_set_active_robot (view, RR_CELL_GET_ROBOT (cell));
grr_board_view_update_mouse (view, event->x, event->y);
}
GdkEventButton *event)
{
grr_board_view_t *view;
- const char *dir;
- const char *robot;
+ rr_direction_t dir;
int x, y;
int robot_x, robot_y;
int dx, dy;
view->button = 0;
}
- grr_board_view_pointer_coords_to_grid (view, event->x, event->y, &x, &y);
+ grr_board_view_transform_pixel_to_cell (view, event->x, event->y, &x, &y);
- rr_board_find_robot (view->board, view->drag_robot, &robot_x, &robot_y);
+ rr_board_find_robot (view->board, view->active_robot, &robot_x, &robot_y);
dx = x - robot_x;
dy = y - robot_y;
if (dx == 0 && dy == 0)
if (abs(dx) > abs(dy))
if (x > robot_x)
- dir = "east";
+ dir = RR_DIRECTION_EAST;
else
- dir = "west";
+ dir = RR_DIRECTION_WEST;
else
if (y > robot_y)
- dir = "south";
+ dir = RR_DIRECTION_SOUTH;
else
- dir = "north";
-
- switch (view->drag_robot) {
- case RR_ROBOT_BLUE:
- robot = "blue";
- break;
- case RR_ROBOT_GREEN:
- robot = "green";
- break;
- case RR_ROBOT_RED:
- robot = "RED";
- break;
- case RR_ROBOT_YELLOW:
- robot = "YELLOW";
- break;
- default:
- return FALSE;
- }
-
- if (view->client) {
- char *move_str;
-
- grr_sprintf_alloc (&move_str, "%s %s", robot, dir);
- if (move_str == NULL)
- return FALSE;
- rr_client_move (view->client, move_str);
- free (move_str);
- }
+ dir = RR_DIRECTION_NORTH;
+
+ grr_board_view_move_active (view, dir);
- return FALSE;
+ return FALSE;
}
static gint
g_return_if_fail (view != NULL);
g_return_if_fail (GRR_IS_BOARD_VIEW (view));
- /* XXX: Should draw a robot here */
+ /* XXX: Perhaps should draw a robot here */
}
+void
+grr_board_view_mark_damage (grr_board_view_t *view, int i, int j)
+{
+ int x, y;
+
+ grr_board_view_transform_cell_to_pixel (view, i, j, &x, &y);
+ gtk_widget_queue_draw_area (GTK_WIDGET (view), x, y, view->cell_width, view->cell_height);
+}
+
+#define TIMER_INTERVAL_MS 100.0
+
static void
-grr_board_view_update (grr_board_view_t *view)
+grr_board_view_refresh_timer (grr_board_view_t *view)
{
- g_return_if_fail (view != NULL);
- g_return_if_fail (GRR_IS_BOARD_VIEW (view));
+ int x, y;
- gtk_widget_queue_draw (GTK_WIDGET (view));
+ grr_board_view_transform_cell_to_pixel (view,
+ view->board_width / 2 - 1,
+ view->board_height / 2 -1,
+ &x, &y);
+ gtk_widget_queue_draw_area (GTK_WIDGET (view),
+ x, y,
+ 2 * view->cell_width,
+ 2 * view->cell_height);
+}
+
+static gboolean
+grr_board_view_decrement_timer (void *widget)
+{
+ grr_board_view_t *view = widget;
+
+ view->time -= TIMER_INTERVAL_MS / 1000.0;
+ view->time += view->drift_correct;
+
+ if (view->zap_time) {
+ view->time -= view->zap_time;
+ view->zap_time += 1.0;
+ }
+
+ grr_board_view_refresh_timer (view);
+
+ if (view->time < 0) {
+ grr_board_view_stop_timer (view);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+void
+grr_board_view_set_timer (grr_board_view_t *view, double time)
+{
+ /* XXX: It would be good to adjust the clock for latency somewhere... */
+ if (view->timer) {
+ double correction_time;
+ /* Correct any drift within the next 10 seconds, or half the
+ remaining time --- whichever is less. */
+ if (time >= 20.0)
+ correction_time = 10.0;
+ else
+ correction_time = time / 2.0;
+ view->drift_correct = (time - view->time) / (correction_time * TIMER_INTERVAL_MS);
+ } else {
+ view->time = time;
+ view->drift_correct = 0.0;
+ view->zap_time = 0.0;
+ }
+}
+
+void
+grr_board_view_start_timer (grr_board_view_t *view)
+{
+ if (view->timer)
+ return;
+
+ view->timer = gtk_timeout_add (TIMER_INTERVAL_MS, grr_board_view_decrement_timer, view);
+}
+
+void
+grr_board_view_stop_timer (grr_board_view_t *view)
+{
+ if (view->timer == 0)
+ return;
+
+ gtk_timeout_remove (view->timer);
+ view->timer = 0;
+ view->time = 0.0;
+ view->drift_correct = 0.0;
+ view->zap_time = 0.0;
+
+ grr_board_view_refresh_timer (view);
+}
+
+void
+grr_board_view_zap_timer (grr_board_view_t *view)
+{
+ view->zap_time = 4;
+
+ grr_board_view_refresh_timer (view);
}