[four-in-a-row: 51/72] made Prefs a singleton



commit 564161b9c2d8114891170007e9478fd14259f6dc
Author: Jacob Humphrey <jacob ryan humphrey gmail com>
Date:   Sat Dec 15 01:24:51 2018 -0600

    made Prefs a singleton

 src/four-in-a-row.vala   | 32 ++++++++++++++++----------------
 src/game-board-view.vala | 12 ++++++------
 src/main.vala            |  4 ++--
 src/prefs-box.vala       | 20 ++++++++++----------
 src/prefs.vala           |  9 ++++++++-
 src/scorebox.vala        |  4 ++--
 src/theme.vala           | 12 ++++++------
 7 files changed, 50 insertions(+), 43 deletions(-)
---
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index c0199a0..62872ae 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -87,8 +87,8 @@ class FourInARow : Gtk.Application {
         gameover = false;
         prompt_player();
         if (!is_player_human()) {
-            vstr[0] = player == PLAYER1 ? vlevel[p.level[PlayerID.PLAYER1]]
-                : vlevel[p.level[PlayerID.PLAYER2]];
+            vstr[0] = player == PLAYER1 ? vlevel[Prefs.instance.level[PlayerID.PLAYER1]]
+                : vlevel[Prefs.instance.level[PlayerID.PLAYER2]];
             game_process_move(playgame((string)vstr) - 1);
         }
     }
@@ -215,7 +215,7 @@ class FourInARow : Gtk.Application {
     // }
 
     public void prompt_player() {
-        int players = p.get_n_human_players();
+        int players = Prefs.instance.get_n_human_players();
         bool human = is_player_human();
         string who;
         string str;
@@ -290,7 +290,7 @@ class FourInARow : Gtk.Application {
     public void play_sound(SoundID id) {
         string name;
 
-        if (!p.do_sound)
+        if (!Prefs.instance.do_sound)
             return;
 
         switch (id) {
@@ -343,8 +343,8 @@ class FourInARow : Gtk.Application {
         } else {
             swap_player();
             if (!is_player_human()) {
-                vstr[0] = player == PlayerID.PLAYER1 ? vlevel[p.level[PlayerID.PLAYER1]]
-                    : vlevel[p.level[PlayerID.PLAYER2]];
+                vstr[0] = player == PlayerID.PLAYER1 ? vlevel[Prefs.instance.level[PlayerID.PLAYER1]]
+                    : vlevel[Prefs.instance.level[PlayerID.PLAYER2]];
                 c = playgame((string)vstr) - 1;
                 if (c < 0)
                     gameover = true;
@@ -370,8 +370,8 @@ class FourInARow : Gtk.Application {
     }
 
     public bool is_player_human() {
-        return player == PLAYER1 ? p.level[PlayerID.PLAYER1] == Level.HUMAN
-            : p.level[PlayerID.PLAYER2] == Level.HUMAN;
+        return player == PLAYER1 ? Prefs.instance.level[PlayerID.PLAYER1] == Level.HUMAN
+            : Prefs.instance.level[PlayerID.PLAYER2] == Level.HUMAN;
     }
 
     public void process_move2(int c) {
@@ -618,7 +618,7 @@ class FourInARow : Gtk.Application {
         Board.instance.set(r, c, Tile.CLEAR);
         GameBoardView.instance.draw_tile(r, c);
 
-        if (p.get_n_human_players() == 1 && !is_player_human()) {
+        if (Prefs.instance.get_n_human_players() == 1 && !is_player_human()) {
             if (moves > 0) {
                 c = vstr[moves] - '0' - 1;
                 r = Board.instance.first_empty_row(c) + 1;
@@ -682,7 +682,7 @@ class FourInARow : Gtk.Application {
         if (Board.instance.is_line_at((Tile)player, row, column)) {
             gameover = true;
             winner = player;
-            switch (p.get_n_human_players()) {
+            switch (Prefs.instance.get_n_human_players()) {
             case 1:
                 play_sound(is_player_human() ? SoundID.YOU_WIN : SoundID.I_WIN);
                 break;
@@ -757,9 +757,9 @@ class FourInARow : Gtk.Application {
 
     bool on_key_press(Gdk.EventKey  e) {
         if ((player_active) || timeout != 0 ||
-                (e.keyval != p.keypress[Move.LEFT] &&
-                e.keyval != p.keypress[Move.RIGHT] &&
-                e.keyval != p.keypress[Move.DROP])) {
+                (e.keyval != Prefs.instance.keypress[Move.LEFT] &&
+                e.keyval != Prefs.instance.keypress[Move.RIGHT] &&
+                e.keyval != Prefs.instance.keypress[Move.DROP])) {
             return false;
         }
 
@@ -768,13 +768,13 @@ class FourInARow : Gtk.Application {
             return true;
         }
 
-        if (e.keyval == p.keypress[Move.LEFT] && column != 0) {
+        if (e.keyval == Prefs.instance.keypress[Move.LEFT] && column != 0) {
             column_moveto--;
             move_cursor(column_moveto);
-        } else if (e.keyval == p.keypress[Move.RIGHT] && column < 6) {
+        } else if (e.keyval == Prefs.instance.keypress[Move.RIGHT] && column < 6) {
             column_moveto++;
             move_cursor(column_moveto);
-        } else if (e.keyval == p.keypress[Move.DROP]) {
+        } else if (e.keyval == Prefs.instance.keypress[Move.DROP]) {
             game_process_move(column);
         }
         return true;
diff --git a/src/game-board-view.vala b/src/game-board-view.vala
index 4959e94..e6480d5 100644
--- a/src/game-board-view.vala
+++ b/src/game-board-view.vala
@@ -124,7 +124,7 @@ class GameBoardView : Gtk.DrawingArea {
         int i;
         Gdk.RGBA color = Gdk.RGBA();
 
-        color.parse(theme[p.theme_id].grid_color);
+        color.parse(theme[Prefs.instance.theme_id].grid_color);
         Gdk.cairo_set_source_rgba(cr, color);
         cr.set_operator(Cairo.Operator.SOURCE);
         cr.set_line_width(1);
@@ -212,12 +212,12 @@ class GameBoardView : Gtk.DrawingArea {
 
         /* Try the theme pixmaps, fallback to the default and then give up */
         while (true) {
-            fname = Path.build_filename(Config.DATA_DIRECTORY, theme[p.theme_id].fname_tileset, null);
+            fname = Path.build_filename(Config.DATA_DIRECTORY, theme[Prefs.instance.theme_id].fname_tileset, 
null);
             try {
                 pb_tileset_tmp = new Gdk.Pixbuf.from_file(fname);
             } catch (Error e) {
-                if (p.theme_id != 0) {
-                    p.theme_id = 0;
+                if (Prefs.instance.theme_id != 0) {
+                    Prefs.instance.theme_id = 0;
                     continue;
                 } else {
                     load_error(fname);
@@ -229,8 +229,8 @@ class GameBoardView : Gtk.DrawingArea {
 
         pb_tileset_raw = pb_tileset_tmp;
 
-        if (theme[p.theme_id].fname_bground != null) {
-            fname = Path.build_filename(Config.DATA_DIRECTORY, theme[p.theme_id].fname_bground, null);
+        if (theme[Prefs.instance.theme_id].fname_bground != null) {
+            fname = Path.build_filename(Config.DATA_DIRECTORY, theme[Prefs.instance.theme_id].fname_bground, 
null);
             try {
                 pb_bground_tmp = new Gdk.Pixbuf.from_file(fname);
             } catch (Error e) {
diff --git a/src/main.vala b/src/main.vala
index 8a8ca31..5cb033b 100644
--- a/src/main.vala
+++ b/src/main.vala
@@ -75,7 +75,7 @@ public enum SoundID {
 FourInARow? application;
 Gtk.ApplicationWindow window;
 Settings settings;
-Prefs p;
+//Prefs p;
 
 public int main(string[] argv) {
     Intl.setlocale();
@@ -99,7 +99,7 @@ public int main(string[] argv) {
 
     Environment.set_application_name(_(APPNAME_LONG));
 
-    p = new Prefs();
+
     application.game_init();
 
     if (!GameBoardView.instance.load_pixmaps())
diff --git a/src/prefs-box.vala b/src/prefs-box.vala
index 1dbe59c..6c80ef0 100644
--- a/src/prefs-box.vala
+++ b/src/prefs-box.vala
@@ -63,19 +63,19 @@ class PrefsBox : Gtk.Dialog {
         combobox.set_model(model);
         model.append(out iter);
         model.set(iter, 0, _("Human"), 1, Level.HUMAN);
-        if (p.level[PlayerID.PLAYER2] == Level.HUMAN)
+        if (Prefs.instance.level[PlayerID.PLAYER2] == Level.HUMAN)
             combobox.set_active_iter(iter);
         model.append(out iter);
         model.set(iter, 0, _("Level one"), 1, Level.WEAK);
-        if (p.level[PlayerID.PLAYER2] == Level.WEAK)
+        if (Prefs.instance.level[PlayerID.PLAYER2] == Level.WEAK)
             combobox.set_active_iter(iter);
         model.append(out iter);
         model.set(iter, 0, _("Level two"), 1, Level.MEDIUM);
-        if (p.level[PlayerID.PLAYER2] == Level.MEDIUM)
+        if (Prefs.instance.level[PlayerID.PLAYER2] == Level.MEDIUM)
             combobox.set_active_iter(iter);
         model.append(out iter);
         model.set(iter, 0, _("Level thre"), 1, Level.STRONG);
-        if (p.level[PlayerID.PLAYER2] == Level.STRONG)
+        if (Prefs.instance.level[PlayerID.PLAYER2] == Level.STRONG)
             combobox.set_active_iter(iter);
 
         combobox.changed.connect(on_select_opponent);
@@ -107,16 +107,16 @@ class PrefsBox : Gtk.Dialog {
         notebook.append_page(controls_list, label);
 
         /* fill in initial values */
-        combobox_theme.set_active(p.theme_id);
-        checkbutton_sound.set_active(p.do_sound);
+        combobox_theme.set_active(Prefs.instance.theme_id);
+        checkbutton_sound.set_active(Prefs.instance.do_sound);
 
         /* connect signals */
         combobox_theme.changed.connect(on_select_theme);
-        checkbutton_sound.toggled.connect(p.on_toggle_sound);
-        p.theme_changed.connect((theme_id) => {
+        checkbutton_sound.toggled.connect(Prefs.instance.on_toggle_sound);
+        Prefs.instance.theme_changed.connect((theme_id) => {
             combobox_theme.set_active(theme_id);
         });
-        p.sound_changed.connect((sound) => {
+        Prefs.instance.sound_changed.connect((sound) => {
             checkbutton_sound.set_active(sound);
         });
     }
@@ -138,7 +138,7 @@ class PrefsBox : Gtk.Dialog {
         w.get_active_iter(out iter);
         w.get_model().get(iter, 1, out value);
 
-        p.level[PlayerID.PLAYER2] = (Level)value;
+        Prefs.instance.level[PlayerID.PLAYER2] = (Level)value;
         settings.set_int("opponent", value);
         Scorebox.instance.reset();
         global::application.who_starts = PlayerID.PLAYER2; /* This gets reversed in game_reset. */
diff --git a/src/prefs.vala b/src/prefs.vala
index f2fe654..57f12ea 100644
--- a/src/prefs.vala
+++ b/src/prefs.vala
@@ -33,6 +33,13 @@ class Prefs {
     public Level level[2];
     public int keypress[3];
 
+    static Once<Prefs> _instance;
+    public static Prefs instance { get {
+        return _instance.once(() => {
+            return new Prefs();
+        });
+    }}
+
     public Prefs() {
         level[PlayerID.PLAYER1] = Level.HUMAN; /* Human. Always human. */
         level[PlayerID.PLAYER2] = (Level) settings.get_int("opponent");
@@ -101,7 +108,7 @@ class Prefs {
     }
 
     public void on_toggle_sound(Gtk.ToggleButton t) {
-        p.do_sound = t.get_active();
+        do_sound = t.get_active();
     }
 
 }
diff --git a/src/scorebox.vala b/src/scorebox.vala
index 345849b..e1f8827 100644
--- a/src/scorebox.vala
+++ b/src/scorebox.vala
@@ -102,8 +102,8 @@ class Scorebox : Gtk.Dialog {
      *
      * updates the scorebox with the latest scores
      */
-    public void update(int[] scores) {        if (p.get_n_human_players() == 1) {
-            if (p.level[PlayerID.PLAYER1] == Level.HUMAN) {
+    public void update(int[] scores) {        if (Prefs.instance.get_n_human_players() == 1) {
+            if (Prefs.instance.level[PlayerID.PLAYER1] == Level.HUMAN) {
                 label_name[PlayerID.PLAYER1].set_text(_("You:"));
                                label_name[PlayerID.PLAYER2].label = _("Me:");
             } else {
diff --git a/src/theme.vala b/src/theme.vala
index 0925f27..25bd06a 100644
--- a/src/theme.vala
+++ b/src/theme.vala
@@ -44,20 +44,20 @@ string theme_get_title(int id) {
 
 string theme_get_player_turn(PlayerID who) {
     if (who == PlayerID.PLAYER1)
-        return theme[p.theme_id].player1_turn;
-    return theme[p.theme_id].player2_turn;
+        return theme[Prefs.instance.theme_id].player1_turn;
+    return theme[Prefs.instance.theme_id].player2_turn;
 }
 
 string theme_get_player_win(PlayerID who) {
     if (who == PlayerID.PLAYER1)
-        return theme[p.theme_id].player1_win;
-    return theme[p.theme_id].player2_win;
+        return theme[Prefs.instance.theme_id].player1_win;
+    return theme[Prefs.instance.theme_id].player2_win;
 }
 
 string theme_get_player(PlayerID who) {
     if (who == PlayerID.PLAYER1)
-        return theme[p.theme_id].player1;
-    return theme[p.theme_id].player2;
+        return theme[Prefs.instance.theme_id].player1;
+    return theme[Prefs.instance.theme_id].player2;
 }
 
 const Theme theme[] = {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]