[four-in-a-row: 47/72] moved some global vars to classes



commit 50e96c527d49f0f16cfb8143213f7788f669138b
Author: Jacob Humphrey <jacob ryan humphrey gmail com>
Date:   Sat Dec 15 01:06:58 2018 -0600

    moved some global vars to classes

 src/prefs-box.vala | 31 ++++++++++++++++++++++++++-
 src/prefs.vala     | 62 ++++++++++++++++++++++++------------------------------
 2 files changed, 57 insertions(+), 36 deletions(-)
---
diff --git a/src/prefs-box.vala b/src/prefs-box.vala
index 81bad98..1dbe59c 100644
--- a/src/prefs-box.vala
+++ b/src/prefs-box.vala
@@ -19,7 +19,11 @@
  * along with GNOME Four-in-a-row. If not, see <http://www.gnu.org/licenses/>.
  */
 class PrefsBox : Gtk.Dialog {
-    public Gtk.Notebook notebook;
+    Gtk.Notebook notebook;
+    Gtk.ComboBox combobox;
+    Gtk.ComboBoxText combobox_theme;
+    Gtk.ToggleButton checkbutton_sound;
+
     public PrefsBox(Gtk.Window parent) {
         Gtk.Grid grid;
         GamesControlsList controls_list;
@@ -109,10 +113,35 @@ class PrefsBox : Gtk.Dialog {
         /* connect signals */
         combobox_theme.changed.connect(on_select_theme);
         checkbutton_sound.toggled.connect(p.on_toggle_sound);
+        p.theme_changed.connect((theme_id) => {
+            combobox_theme.set_active(theme_id);
+        });
+        p.sound_changed.connect((sound) => {
+            checkbutton_sound.set_active(sound);
+        });
     }
 
     protected override bool delete_event(Gdk.EventAny event) {
         hide();
         return true;
     }
+
+    void on_select_theme(Gtk.ComboBox combo) {
+        int id = combo.get_active();
+        settings.set_int("theme-id", id);
+    }
+
+    void on_select_opponent(Gtk.ComboBox w) {
+        Gtk.TreeIter iter;
+        int value;
+
+        w.get_active_iter(out iter);
+        w.get_model().get(iter, 1, out value);
+
+        p.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. */
+        global::application.game_reset();
+    }
 }
diff --git a/src/prefs.vala b/src/prefs.vala
index 0c69f4d..9cebc5d 100644
--- a/src/prefs.vala
+++ b/src/prefs.vala
@@ -54,25 +54,40 @@ class Prefs {
             return val;
     }
 
+    /**
+     * theme_changed:
+     *
+     * emmited when the theme is changed
+     *
+     * @theme_id: The new theme_id
+     */
+    public signal void theme_changed(int theme_id);
+
+    /**
+     * sound_changed:
+     *
+     * emmited when the sound fx are enabled/disabled
+     *
+     * @sound: true if sound is enabled
+     */
+    public signal void sound_changed(bool sound);
+
     public void settings_changed_cb(string key) {
         if (key == "sound") {
-            //p.do_sound = settings.get_boolean("sound");
-            ((Gtk.ToggleButton)checkbutton_sound).set_active(p.do_sound);
+            sound_changed(do_sound);
         } else if (key == "key-left") {
-            p.keypress[Move.LEFT] = settings.get_int("key-left");
+            keypress[Move.LEFT] = settings.get_int("key-left");
         } else if (key == "key-right") {
-            p.keypress[Move.RIGHT] = settings.get_int("key-right");
+            keypress[Move.RIGHT] = settings.get_int("key-right");
         } else if (key == "key-drop") {
-            p.keypress[Move.DROP] = settings.get_int("key-drop");
+            keypress[Move.DROP] = settings.get_int("key-drop");
         } else if (key == "theme-id") {
             int val = sane_theme_id(settings.get_int("theme-id"));
-            if (val != p.theme_id) {
-                p.theme_id = val;
+            if (val != theme_id) {
+                theme_id = val;
                 if (!GameBoardView.instance.change_theme())
                     return;
-                if (prefsbox == null)
-                    return;
-                combobox_theme.set_active(p.theme_id);
+                theme_changed(theme_id);
             }
         }
     }
@@ -93,9 +108,8 @@ class Prefs {
 
 Settings settings;
 PrefsBox? prefsbox = null;
-Gtk.ComboBox combobox;
-Gtk.ComboBoxText combobox_theme;
-Gtk.CheckButton checkbutton_sound;
+
+
 /*
  * Needed to force vala to include headers in the correct order.
  * See https://gitlab.gnome.org/GNOME/vala/issues/98
@@ -117,28 +131,6 @@ public Level sane_player_level(Level val) {
     return val;
 }
 
-public void on_select_theme(Gtk.ComboBox combo) {
-    int id = combo.get_active();
-    settings.set_int("theme-id", id);
-}
-
-
-public void on_select_opponent(Gtk.ComboBox w) {
-    Gtk.TreeIter iter;
-    int value;
-
-    w.get_active_iter(out iter);
-    w.get_model().get(iter, 1, out value);
-
-    p.level[PlayerID.PLAYER2] = (Level)value;
-    settings.set_int("opponent", value);
-    Scorebox.instance.reset();
-    application.who_starts = PlayerID.PLAYER2; /* This gets reversed in game_reset. */
-    application.game_reset();
-}
-
-
-
 public void prefsbox_open() {
     Gtk.Grid grid;
     GamesControlsList controls_list;


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