shithub: puzzles

Download patch

ref: 180802b362c715dfb22893021f3434834447e5b5
parent: a1c88470a30cf57226aa091f546da18d0dff07e1
author: Simon Tatham <anakin@pobox.com>
date: Tue May 11 13:44:30 EDT 2004

Framework alteration: we now support a `game_ui' structure in
addition to the `game_state'. The new structure is intended to
contain ephemeral data pertaining to the game's user interface
rather than the actual game: things stored in the UI structure are
not restored in an Undo, for example.
make_move() is passed the UI to modify as it wishes; it is now
allowed to return the _same_ game_state it was passed, to indicate
that although no move has been made there has been a UI operation
requiring a redraw.

[originally from svn r4207]

--- a/cube.c
+++ b/cube.c
@@ -950,8 +950,17 @@
     sfree(state);
 }
 
-game_state *make_move(game_state *from, int x, int y, int button)
+game_ui *new_ui(game_state *state)
 {
+    return NULL;
+}
+
+void free_ui(game_ui *ui)
+{
+}
+
+game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
+{
     int direction;
     int pkey[2], skey[2], dkey[2];
     float points[4];
@@ -1309,7 +1318,8 @@
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, float animtime, float flashtime)
+                 game_state *state, game_ui *ui,
+                 float animtime, float flashtime)
 {
     int i, j;
     struct bbox bb = find_bbox(&state->params);
--- a/fifteen.c
+++ b/fifteen.c
@@ -349,8 +349,17 @@
     sfree(state);
 }
 
-game_state *make_move(game_state *from, int x, int y, int button)
+game_ui *new_ui(game_state *state)
 {
+    return NULL;
+}
+
+void free_ui(game_ui *ui)
+{
+}
+
+game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
+{
     int gx, gy, dx, dy, ux, uy, up, p;
     game_state *ret;
 
@@ -519,7 +528,8 @@
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, float animtime, float flashtime)
+                 game_state *state, game_ui *ui,
+                 float animtime, float flashtime)
 {
     int i, pass, bgcolour;
 
--- a/gtk.c
+++ b/gtk.c
@@ -283,7 +283,7 @@
     if (!fe->pixmap)
         return TRUE;
 
-    if (event->type != GDK_BUTTON_PRESS)
+    if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
         return TRUE;
 
     if (event->button == 2 || (event->state & GDK_SHIFT_MASK))
@@ -295,6 +295,9 @@
     else
 	return FALSE;		       /* don't even know what button! */
 
+    if (event->type == GDK_BUTTON_RELEASE)
+        button += LEFT_RELEASE - LEFT_BUTTON;
+
     if (!midend_process_key(fe->me, event->x, event->y, button))
 	gtk_widget_destroy(fe->window);
 
@@ -301,6 +304,33 @@
     return TRUE;
 }
 
+static gint motion_event(GtkWidget *widget, GdkEventMotion *event,
+                         gpointer data)
+{
+    frontend *fe = (frontend *)data;
+    int button;
+
+    if (!fe->pixmap)
+        return TRUE;
+
+    if (event->type != GDK_BUTTON_PRESS && event->type != GDK_BUTTON_RELEASE)
+        return TRUE;
+
+    if (event->state & (GDK_BUTTON2_MASK | GDK_SHIFT_MASK))
+	button = MIDDLE_DRAG;
+    else if (event->state & GDK_BUTTON1_MASK)
+	button = LEFT_DRAG;
+    else if (event->state & GDK_BUTTON3_MASK)
+	button = RIGHT_DRAG;
+    else
+	return FALSE;		       /* don't even know what button! */
+
+    if (!midend_process_key(fe->me, event->x, event->y, button))
+	gtk_widget_destroy(fe->window);
+
+    return TRUE;
+}
+
 static gint expose_area(GtkWidget *widget, GdkEventExpose *event,
                         gpointer data)
 {
@@ -893,6 +923,10 @@
 		       GTK_SIGNAL_FUNC(key_event), fe);
     gtk_signal_connect(GTK_OBJECT(fe->area), "button_press_event",
 		       GTK_SIGNAL_FUNC(button_event), fe);
+    gtk_signal_connect(GTK_OBJECT(fe->area), "button_release_event",
+		       GTK_SIGNAL_FUNC(button_event), fe);
+    gtk_signal_connect(GTK_OBJECT(fe->area), "motion_notify_event",
+		       GTK_SIGNAL_FUNC(motion_event), fe);
     gtk_signal_connect(GTK_OBJECT(fe->area), "expose_event",
 		       GTK_SIGNAL_FUNC(expose_area), fe);
     gtk_signal_connect(GTK_OBJECT(fe->window), "map_event",
@@ -900,7 +934,10 @@
     gtk_signal_connect(GTK_OBJECT(fe->area), "configure_event",
 		       GTK_SIGNAL_FUNC(configure_area), fe);
 
-    gtk_widget_add_events(GTK_WIDGET(fe->area), GDK_BUTTON_PRESS_MASK);
+    gtk_widget_add_events(GTK_WIDGET(fe->area),
+                          GDK_BUTTON_PRESS_MASK |
+                          GDK_BUTTON_RELEASE_MASK |
+			  GDK_BUTTON_MOTION_MASK);
 
     gtk_widget_show(fe->area);
     gtk_widget_show(fe->window);
--- a/midend.c
+++ b/midend.c
@@ -27,6 +27,7 @@
     game_state **states;
     game_drawstate *drawstate;
     game_state *oldstate;
+    game_ui *ui;
     float anim_time, anim_pos;
     float flash_time, flash_pos;
 };
@@ -56,6 +57,7 @@
     me->npresets = me->presetsize = 0;
     me->anim_time = me->anim_pos = 0.0F;
     me->flash_time = me->flash_pos = 0.0F;
+    me->ui = NULL;
 
     return me;
 }
@@ -99,6 +101,9 @@
     me->states[me->nstates++] = new_game(me->params, me->seed);
     me->statepos = 1;
     me->drawstate = game_new_drawstate(me->states[0]);
+    if (me->ui)
+        free_ui(me->ui);
+    me->ui = new_ui(me->states[0]);
 }
 
 void midend_restart_game(midend_data *me)
@@ -106,6 +111,8 @@
     while (me->nstates > 1)
 	free_game(me->states[--me->nstates]);
     me->statepos = me->nstates;
+    free_ui(me->ui);
+    me->ui = new_ui(me->states[0]);
 }
 
 static int midend_undo(midend_data *me)
@@ -180,9 +187,18 @@
 	free_game(oldstate);
         return 0;
     } else {
-        game_state *s = make_move(me->states[me->statepos-1], x, y, button);
+        game_state *s = make_move(me->states[me->statepos-1], me->ui,
+                                  x, y, button);
 
-        if (s) {
+        if (s == me->states[me->statepos-1]) {
+            /*
+             * make_move() is allowed to return its input state to
+             * indicate that although no move has been made, the UI
+             * state has been updated and a redraw is called for.
+             */
+            midend_redraw(me);
+            return 1;
+        } else if (s) {
             while (me->nstates > me->statepos)
                 free_game(me->states[--me->nstates]);
             ensure(me);
@@ -222,11 +238,12 @@
         if (me->oldstate && me->anim_time > 0 &&
             me->anim_pos < me->anim_time) {
             game_redraw(me->frontend, me->drawstate, me->oldstate,
-                        me->states[me->statepos-1], me->anim_pos,
+                        me->states[me->statepos-1], me->ui, me->anim_pos,
 			me->flash_pos);
         } else {
             game_redraw(me->frontend, me->drawstate, NULL,
-                        me->states[me->statepos-1], 0.0, me->flash_pos);
+                        me->states[me->statepos-1], me->ui, 0.0,
+                        me->flash_pos);
         }
         end_draw(me->frontend);
     }
--- a/net.c
+++ b/net.c
@@ -724,10 +724,19 @@
     return active;
 }
 
+game_ui *new_ui(game_state *state)
+{
+    return NULL;
+}
+
+void free_ui(game_ui *ui)
+{
+}
+
 /* ----------------------------------------------------------------------
  * Process a move.
  */
-game_state *make_move(game_state *state, int x, int y, int button)
+game_state *make_move(game_state *state, game_ui *ui, int x, int y, int button)
 {
     game_state *ret;
     int tx, ty, orig;
@@ -1141,7 +1150,7 @@
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, float t, float ft)
+                 game_state *state, game_ui *ui, float t, float ft)
 {
     int x, y, tx, ty, frame;
     unsigned char *active;
--- a/nullgame.c
+++ b/nullgame.c
@@ -109,11 +109,20 @@
     sfree(state);
 }
 
-game_state *make_move(game_state *from, int x, int y, int button)
+game_ui *new_ui(game_state *state)
 {
     return NULL;
 }
 
+void free_ui(game_ui *ui)
+{
+}
+
+game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
+{
+    return NULL;
+}
+
 /* ----------------------------------------------------------------------
  * Drawing routines.
  */
@@ -152,7 +161,8 @@
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, float animtime, float flashtime)
+                 game_state *state, game_ui *ui,
+                 float animtime, float flashtime)
 {
 }
 
--- a/puzzles.h
+++ b/puzzles.h
@@ -18,6 +18,12 @@
     LEFT_BUTTON = 0x1000,
     MIDDLE_BUTTON,
     RIGHT_BUTTON,
+    LEFT_DRAG,
+    MIDDLE_DRAG,
+    RIGHT_DRAG,
+    LEFT_RELEASE,
+    MIDDLE_RELEASE,
+    RIGHT_RELEASE,
     CURSOR_UP,
     CURSOR_DOWN,
     CURSOR_LEFT,
@@ -36,6 +42,7 @@
 typedef struct random_state random_state;
 typedef struct game_params game_params;
 typedef struct game_state game_state;
+typedef struct game_ui game_ui;
 typedef struct game_drawstate game_drawstate;
 
 #define ALIGN_VNORMAL 0x000
@@ -165,13 +172,16 @@
 game_state *new_game(game_params *params, char *seed);
 game_state *dup_game(game_state *state);
 void free_game(game_state *state);
-game_state *make_move(game_state *from, int x, int y, int button);
+game_ui *new_ui(game_state *state);
+void free_ui(game_ui *ui);
+game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button);
 void game_size(game_params *params, int *x, int *y);
 float *game_colours(frontend *fe, game_state *state, int *ncolours);
 game_drawstate *game_new_drawstate(game_state *state);
 void game_free_drawstate(game_drawstate *ds);
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *newstate, float anim_time, float flash_time);
+                 game_state *newstate, game_ui *ui, float anim_time,
+                 float flash_time);
 float game_anim_length(game_state *oldstate, game_state *newstate);
 float game_flash_length(game_state *oldstate, game_state *newstate);
 int game_wants_statusbar(void);
--- a/rect.c
+++ b/rect.c
@@ -857,8 +857,17 @@
     return ret;
 }
 
-game_state *make_move(game_state *from, int x, int y, int button)
+game_ui *new_ui(game_state *state)
 {
+    return NULL;
+}
+
+void free_ui(game_ui *ui)
+{
+}
+
+game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
+{
     float xf, yf, dx, dy;
     int hxr, hyr, vxr, vyr;
     game_state *ret;
@@ -1005,7 +1014,8 @@
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, float animtime, float flashtime)
+                 game_state *state, game_ui *ui,
+                 float animtime, float flashtime)
 {
     int x, y;
     unsigned char *correct;
--- a/sixteen.c
+++ b/sixteen.c
@@ -358,8 +358,17 @@
     sfree(state);
 }
 
-game_state *make_move(game_state *from, int x, int y, int button)
+game_ui *new_ui(game_state *state)
 {
+    return NULL;
+}
+
+void free_ui(game_ui *ui)
+{
+}
+
+game_state *make_move(game_state *from, game_ui *ui, int x, int y, int button)
+{
     int cx, cy;
     int dx, dy, tx, ty, n;
     game_state *ret;
@@ -534,7 +543,8 @@
 }
 
 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
-                 game_state *state, float animtime, float flashtime)
+                 game_state *state, game_ui *ui,
+                 float animtime, float flashtime)
 {
     int i, bgcolour;
 
--- a/windows.c
+++ b/windows.c
@@ -939,7 +939,50 @@
 		button = LEFT_BUTTON;
 	    else
 		button = RIGHT_BUTTON;
-		
+
+	    if (!midend_process_key(fe->me, LOWORD(lParam),
+				    HIWORD(lParam), button))
+		PostQuitMessage(0);
+
+	    SetCapture(hwnd);
+	}
+	break;
+      case WM_LBUTTONUP:
+      case WM_RBUTTONUP:
+      case WM_MBUTTONUP:
+	{
+	    int button;
+
+	    /*
+	     * Shift-clicks count as middle-clicks, since otherwise
+	     * two-button Windows users won't have any kind of
+	     * middle click to use.
+	     */
+	    if (message == WM_MBUTTONUP || (wParam & MK_SHIFT))
+		button = MIDDLE_RELEASE;
+	    else if (message == WM_LBUTTONUP)
+		button = LEFT_RELEASE;
+	    else
+		button = RIGHT_RELEASE;
+
+	    if (!midend_process_key(fe->me, LOWORD(lParam),
+				    HIWORD(lParam), button))
+		PostQuitMessage(0);
+
+	    ReleaseCapture();
+	}
+	break;
+      case WM_MOUSEMOVE:
+	{
+	    int button;
+
+	    if (wParam & (MK_MBUTTON | MK_SHIFT))
+		button = MIDDLE_DRAG;
+	    else if (wParam & MK_LBUTTON)
+		button = LEFT_DRAG;
+	    else
+		button = RIGHT_DRAG;
+	    
 	    if (!midend_process_key(fe->me, LOWORD(lParam),
 				    HIWORD(lParam), button))
 		PostQuitMessage(0);