shithub: puzzles

Download patch

ref: c469bd285cc4f482f49203c16169151d2d869d4a
parent: d5f7c4f871200f9808e284408e28defaa8afbcd3
author: Jonas Kölker <jonaskoelker@yahoo.com>
date: Mon Sep 21 13:41:10 EDT 2015

Filling: enable keyboard-driven cursor dragging mode.

--- a/filling.c
+++ b/filling.c
@@ -986,7 +986,7 @@
 
 struct game_ui {
     int *sel; /* w*h highlighted squares, or NULL */
-    int cur_x, cur_y, cur_visible;
+    int cur_x, cur_y, cur_visible, keydragging;
 };
 
 static game_ui *new_ui(const game_state *state)
@@ -994,7 +994,7 @@
     game_ui *ui = snew(game_ui);
 
     ui->sel = NULL;
-    ui->cur_x = ui->cur_y = ui->cur_visible = 0;
+    ui->cur_x = ui->cur_y = ui->cur_visible = ui->keydragging = 0;
 
     return ui;
 }
@@ -1023,6 +1023,7 @@
         sfree(ui->sel);
         ui->sel = NULL;
     }
+    ui->keydragging = FALSE;
 }
 
 #define PREFERRED_TILE_SIZE 32
@@ -1079,34 +1080,57 @@
     if (IS_CURSOR_MOVE(button)) {
         ui->cur_visible = 1;
         move_cursor(button, &ui->cur_x, &ui->cur_y, w, h, 0);
+	if (ui->keydragging) goto select_square;
         return "";
     }
-    if (IS_CURSOR_SELECT(button)) {
+    if (button == CURSOR_SELECT) {
         if (!ui->cur_visible) {
             ui->cur_visible = 1;
             return "";
         }
+	ui->keydragging = !ui->keydragging;
+	if (!ui->keydragging) return "";
+
+      select_square:
         if (!ui->sel) {
             ui->sel = snewn(w*h, int);
             memset(ui->sel, 0, w*h*sizeof(int));
         }
-        if (state->shared->clues[w*ui->cur_y + ui->cur_x] == 0)
-            ui->sel[w*ui->cur_y + ui->cur_x] ^= 1;
-        return "";
+	if (!state->shared->clues[w*ui->cur_y + ui->cur_x])
+	    ui->sel[w*ui->cur_y + ui->cur_x] = 1;
+	return "";
     }
+    if (button == CURSOR_SELECT2) {
+	if (!ui->cur_visible) {
+	    ui->cur_visible = 1;
+	    return "";
+	}
+        if (!ui->sel) {
+            ui->sel = snewn(w*h, int);
+            memset(ui->sel, 0, w*h*sizeof(int));
+        }
+	ui->keydragging = FALSE;
+	if (!state->shared->clues[w*ui->cur_y + ui->cur_x])
+	    ui->sel[w*ui->cur_y + ui->cur_x] ^= 1;
+	for (i = 0; i < w*h && !ui->sel[i]; i++);
+	if (i == w*h) {
+	    sfree(ui->sel);
+	    ui->sel = NULL;
+	}
+	return "";
+    }
 
-    switch (button) {
-      case ' ':
-      case '\r':
-      case '\n':
-      case '\b':
-        button = 0;
-        break;
-      default:
-        if (button < '0' || button > '9') return NULL;
-        button -= '0';
-        if (button > (w == 2 && h == 2? 3: max(w, h))) return NULL;
+    if (button == '\b' || button == 27) {
+	sfree(ui->sel);
+	ui->sel = NULL;
+	ui->keydragging = FALSE;
+	return "";
     }
+
+    if (button < '0' || button > '9') return NULL;
+    button -= '0';
+    if (button > (w == 2 && h == 2 ? 3 : max(w, h))) return NULL;
+    ui->keydragging = FALSE;
 
     for (i = 0; i < w*h; i++) {
         char buf[32];
--- a/puzzles.but
+++ b/puzzles.but
@@ -2453,10 +2453,11 @@
 feature).
 
 You can also move around the grid with the cursor keys; typing a digit will
-fill the square containing the cursor with that number, or typing 0, Space,
-or Enter will clear it. You can also select multiple squares for numbering
-or clearing by using the return key, before typing a digit to fill in the
-highlighted squares (as above).
+fill the square containing the cursor with that number; typing 0 will clear
+it.  You can also select multiple squares for numbering or clearing with the
+return and arrow keys, before typing a digit to fill or clear the highlighted
+squares (as above).  The space bar adds and removes single squares to and from
+the selection.  Backspace and escape remove all squares from the selection.
 
 (All the actions described in \k{common-actions} are also available.)