[four-in-a-row: 54/72] made scorebox not a singleton



commit 6135ba6dd92bdf561a18efe19254f8a67feccbed
Author: Jacob Humphrey <jacob ryan humphrey gmail com>
Date:   Sat Dec 15 03:50:07 2018 -0600

    made scorebox not a singleton

 src/four-in-a-row.vala | 12 ++++++++----
 src/prefs-box.vala     |  1 -
 src/scorebox.vala      | 27 ++++++++++++++-------------
 3 files changed, 22 insertions(+), 18 deletions(-)
---
diff --git a/src/four-in-a-row.vala b/src/four-in-a-row.vala
index 3abba0b..a2994d0 100644
--- a/src/four-in-a-row.vala
+++ b/src/four-in-a-row.vala
@@ -31,6 +31,7 @@ class FourInARow : Gtk.Application {
     PlayerID winner;
     public PlayerID who_starts;
     PrefsBox? prefsbox = null;
+    Scorebox scorebox;
     /**
      * socre:
      *
@@ -64,6 +65,8 @@ class FourInARow : Gtk.Application {
     public void game_reset() {
         stop_anim();
 
+        scorebox.reset();
+
         undo_action.set_enabled(false);
         hint_action.set_enabled(false);
 
@@ -204,7 +207,7 @@ class FourInARow : Gtk.Application {
             window.show_all();
             GameBoardView.instance.refresh_pixmaps();
             GameBoardView.instance.draw_all();
-            Scorebox.instance.update(score);       /* update visible player descriptions */
+            scorebox.update(score);       /* update visible player descriptions */
             prompt_player();
             game_reset();
         }
@@ -338,7 +341,7 @@ class FourInARow : Gtk.Application {
 
         if (gameover) {
             score[winner]++;
-            Scorebox.instance.update(score);
+            scorebox.update(score);
             prompt_player();
         } else {
             swap_player();
@@ -526,7 +529,7 @@ class FourInARow : Gtk.Application {
     }
 
     void on_game_scores(SimpleAction action, Variant? parameter) {
-            Scorebox.instance.present();
+            scorebox.present();
             return;
     }
 
@@ -607,7 +610,7 @@ class FourInARow : Gtk.Application {
 
         if (gameover) {
             score[winner]--;
-            Scorebox.instance.update(score);
+            scorebox.update(score);
             gameover = false;
             prompt_player();
         } else {
@@ -701,6 +704,7 @@ class FourInARow : Gtk.Application {
 
     protected override void startup() {
         base.startup();
+        scorebox = new Scorebox(this);
         Gtk.AspectFrame frame;
         GLib.Menu app_menu, section;
 
diff --git a/src/prefs-box.vala b/src/prefs-box.vala
index 8f01f3a..0b0ab51 100644
--- a/src/prefs-box.vala
+++ b/src/prefs-box.vala
@@ -140,7 +140,6 @@ class PrefsBox : Gtk.Dialog {
 
         Prefs.instance.level[PlayerID.PLAYER2] = (Level)value;
         Prefs.instance.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/scorebox.vala b/src/scorebox.vala
index e1f8827..702b55e 100644
--- a/src/scorebox.vala
+++ b/src/scorebox.vala
@@ -24,19 +24,19 @@ class Scorebox : Gtk.Dialog {
     Gtk.Label label_score[3];
     public new FourInARow application;
 
-    static Once<Scorebox> _instance;
-    public static Scorebox instance {
-        get {
-            return _instance.once(() => {
-                var scorebox = new Scorebox();
+    // static Once<Scorebox> _instance;
+    // public static Scorebox insstance {
+    //     get {
+    //         return _instance.once(() => {
+    //             var scorebox = new Scorebox();
                 //scorebox.show_all();
                 //scorebox.update();
-                return scorebox;
-            });
-        }
-    }
+    //             return scorebox;
+    //         });
+    //     }
+    // }
 
-    Scorebox() {
+    public Scorebox(FourInARow application) {
         Object(title: _("Scores"),
                //parent: window,
                use_header_bar: 1,
@@ -94,7 +94,7 @@ class Scorebox : Gtk.Dialog {
         label_score[PlayerID.NOBODY].set_yalign(0.5f);
         grid.show_all();
 
-        application = global::application;
+        this.application = application;
     }
 
     /**
@@ -102,10 +102,11 @@ class Scorebox : Gtk.Dialog {
      *
      * updates the scorebox with the latest scores
      */
-    public void update(int[] scores) {        if (Prefs.instance.get_n_human_players() == 1) {
+    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:");
+                label_name[PlayerID.PLAYER2].label = _("Me:");
             } else {
                 label_name[PlayerID.PLAYER2].set_text(_("You:"));
                 label_name[PlayerID.PLAYER1].label = _("Me:");


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