shithub: puzzles

Download patch

ref: 4227ac1fd5dc25c247e7526526079b85e1890766
parent: 5acce15ed907d29a5575668a09e7d94cf7a36b3f
author: Ben Harris <bjh21@bjh21.me.uk>
date: Tue May 30 15:57:15 EDT 2023

Add preferences for existing UI style controls

Some puzzles have keys that make changes to the display style in ways
that would probably have been user preferences if they had existed.
I've added a user preference for each of these.  The keys still work,
and unlike the preferences can be changed without saving any state.

The affected settings are:
 * Labelling colours with numbers in Guess ("L" key)
 * Labelling regions with numbers in Map ("L" key)
 * Whether monsters are shown as letters or pictures in Undead ("A" key)

--- a/guess.c
+++ b/guess.c
@@ -411,20 +411,45 @@
 {
     game_ui *ui = snew(game_ui);
     memset(ui, 0, sizeof(game_ui));
-    ui->params = state->params;        /* structure copy */
-    ui->curr_pegs = new_pegrow(state->params.npegs);
-    ui->holds = snewn(state->params.npegs, bool);
+    if (state != NULL) {
+        ui->params = state->params;        /* structure copy */
+        ui->curr_pegs = new_pegrow(state->params.npegs);
+        ui->holds = snewn(state->params.npegs, bool);
+        memset(ui->holds, 0, sizeof(bool)*state->params.npegs);
+    }
     ui->display_cur = getenv_bool("PUZZLES_SHOW_CURSOR", false);
-    memset(ui->holds, 0, sizeof(bool)*state->params.npegs);
     ui->drag_opeg = -1;
     return ui;
 }
 
+static config_item *get_prefs(game_ui *ui)
+{
+    config_item *ret;
+
+    ret = snewn(2, config_item);
+
+    ret[0].name = "Label colours with numbers";
+    ret[0].kw = "show-labels";
+    ret[0].type = C_BOOLEAN;
+    ret[0].u.boolean.bval = ui->show_labels;
+
+    ret[1].name = NULL;
+    ret[1].type = C_END;
+
+    return ret;
+}
+
+static void set_prefs(game_ui *ui, const config_item *cfg)
+{
+    ui->show_labels = cfg[0].u.boolean.bval;
+}
+
 static void free_ui(game_ui *ui)
 {
     if (ui->hint)
         free_pegrow(ui->hint);
-    free_pegrow(ui->curr_pegs);
+    if (ui->curr_pegs)
+        free_pegrow(ui->curr_pegs);
     sfree(ui->holds);
     sfree(ui);
 }
@@ -1518,7 +1543,7 @@
     free_game,
     true, solve_game,
     false, NULL, NULL, /* can_format_as_text_now, text_format */
-    NULL, NULL, /* get_prefs, set_prefs */
+    get_prefs, set_prefs,
     new_ui,
     free_ui,
     encode_ui,
--- a/map.c
+++ b/map.c
@@ -2337,7 +2337,7 @@
 {
     config_item *ret;
 
-    ret = snewn(2, config_item);
+    ret = snewn(3, config_item);
 
     ret[0].name = "Victory flash effect";
     ret[0].kw = "flash-type";
@@ -2346,9 +2346,14 @@
     ret[0].u.choices.choicekws = ":cyclic:each-white:all-white";
     ret[0].u.choices.selected = ui->flash_type;
 
-    ret[1].name = NULL;
-    ret[1].type = C_END;
+    ret[1].name = "Number regions";
+    ret[1].kw = "show-numbers";
+    ret[1].type = C_BOOLEAN;
+    ret[1].u.boolean.bval = ui->show_numbers;
 
+    ret[2].name = NULL;
+    ret[2].type = C_END;
+
     return ret;
 }
 
@@ -2355,6 +2360,7 @@
 static void set_prefs(game_ui *ui, const config_item *cfg)
 {
     ui->flash_type = cfg[0].u.choices.selected;
+    ui->show_numbers = cfg[1].u.boolean.bval;
 }
 
 static void free_ui(game_ui *ui)
--- a/puzzles.but
+++ b/puzzles.but
@@ -1443,7 +1443,14 @@
 this increases the search space (making things harder), and is turned on by
 default.
 
+\H{guess-prefs} \I{preferences, for Guess}Guess user preferences
 
+On platforms that support user preferences, the \q{Preferences} option
+on the \q{Game} menu will let you configure whether pegs are labelled
+with their numbers.  Unlike the \q{L} key, this will persist between
+games.
+
+
 \C{pegs} \i{Pegs}
 
 \cfg{winhelp-topic}{games.pegs}
@@ -2004,7 +2011,7 @@
 
 On platforms that support user preferences, the \q{Preferences} option
 on the \q{Game} menu will let you configure the style of the victory
-flash.
+flash and also whether the regions start out labelled with numbers.
 
 
 \C{loopy} \i{Loopy}
@@ -3287,6 +3294,13 @@
 \dt \e{Difficulty}
 
 \dd Controls the difficulty of the generated puzzle.
+
+\H{undead-prefs} \I{preferences, for Undead}Undead user preferences
+
+On platforms that support user preferences, the \q{Preferences} option
+on the \q{Game} menu will let you configure whether Undead uses letters
+or pictures to represent monsters.
+
 
 \C{unruly} \i{Unruly}
 
--- a/undead.c
+++ b/undead.c
@@ -1656,6 +1656,30 @@
     return ui;
 }
 
+static config_item *get_prefs(game_ui *ui)
+{
+    config_item *ret;
+
+    ret = snewn(2, config_item);
+
+    ret[0].name = "Monster representation";
+    ret[0].kw = "monsters";
+    ret[0].type = C_CHOICES;
+    ret[0].u.choices.choicenames = ":Pictures:Letters";
+    ret[0].u.choices.choicekws = ":pictures:letters";
+    ret[0].u.choices.selected = ui->ascii;
+
+    ret[1].name = NULL;
+    ret[1].type = C_END;
+
+    return ret;
+}
+
+static void set_prefs(game_ui *ui, const config_item *cfg)
+{
+    ui->ascii = cfg[0].u.choices.selected;
+}
+
 static void free_ui(game_ui *ui) {
     sfree(ui);
     return;
@@ -2784,7 +2808,7 @@
     free_game,
     true, solve_game,
     true, game_can_format_as_text_now, game_text_format,
-    NULL, NULL, /* get_prefs, set_prefs */
+    get_prefs, set_prefs,
     new_ui,
     free_ui,
     NULL, /* encode_ui */