[gnome-2048] Do not store settings.



commit f1d229e7bdd39bf181c271451bdeed9f0db71d6f
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Feb 1 14:38:01 2019 +0100

    Do not store settings.
    
    There is something wrong in how
    settings are shared everywhere.
    More work would be needed here.

 src/application.vala | 73 +++++++++++++++++++++++++++++++---------------------
 src/game.vala        | 67 +++++++++++++++++++----------------------------
 2 files changed, 70 insertions(+), 70 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index 2651d20..54d3486 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -169,7 +169,7 @@ private class Application : Gtk.Application
 
     private void _init_game ()
     {
-        _game = new Game (_settings);
+        _game = new Game (ref _settings);
         _game.notify["score"].connect ((s, p) => {
                 _score.label = _game.score.to_string ();
             });
@@ -203,8 +203,9 @@ private class Application : Gtk.Application
 
         add_window (_window);
 
-        _create_header_bar (builder);
-        _create_game_view (builder);
+        _create_header_bar (builder, out _header_bar, out _score, out _new_game_button, out 
_hamburger_button);
+        _connect_and_update ();
+        _create_game_view (builder, out _embed, ref _game);
 
         _window.set_events (_window.get_events () | Gdk.EventMask.STRUCTURE_MASK | 
Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.KEY_RELEASE_MASK);
         _window.key_press_event.connect (key_press_event_cb);
@@ -217,36 +218,56 @@ private class Application : Gtk.Application
         _window.set_geometry_hints (_window, geom, Gdk.WindowHints.MIN_SIZE);
     }
 
-    private void _create_header_bar (Builder builder)
+    private static inline void _create_header_bar (Builder     builder,
+                                               out HeaderBar   header_bar,
+                                               out Label       score,
+                                               out MenuButton  new_game_button,
+                                               out MenuButton  hamburger_button)
     {
-        _header_bar = (HeaderBar) builder.get_object ("headerbar");
-
-        _score = (Label) builder.get_object ("score");
+        header_bar          = (HeaderBar)   builder.get_object ("headerbar");
+        score               = (Label)       builder.get_object ("score");
+        new_game_button     = (MenuButton)  builder.get_object ("new-game-button");
+        hamburger_button    = (MenuButton)  builder.get_object ("hamburger-button");
+    }
 
+    private inline void _connect_and_update ()
+    {
         ((SimpleAction) lookup_action ("undo")).set_enabled (false);
 
-        _new_game_button = (MenuButton) builder.get_object ("new-game-button");
-        _settings.changed.connect ((settings, key_name) => {
-                if (key_name == "cols" || key_name == "rows")
-                    _update_new_game_menu ();
-            });
-        _update_new_game_menu ();
-
-        _hamburger_button = (MenuButton) builder.get_object ("hamburger-button");
         _hamburger_button.notify ["active"].connect (() => {
                 if (!_hamburger_button.active)
                     _embed.grab_focus ();
             });
-        _settings.changed ["allow-undo"].connect (_update_hamburger_menu);
+        _settings.changed.connect ((settings, key_name) => {
+                switch (key_name)
+                {
+                    case "cols":
+                    case "rows":
+                        _update_new_game_menu ();
+                        return;
+                    case "allow-undo":
+                        _update_hamburger_menu ();
+                        _game.load_settings (ref _settings);
+                        return;
+                    case "allow-undo-max":
+                    case "animations-speed":
+                        _game.load_settings (ref _settings);
+                        return;
+                }
+            });
+        _update_new_game_menu ();
         _update_hamburger_menu ();
+        _game.load_settings (ref _settings);
     }
 
-    private void _create_game_view (Builder builder)
+    private static inline void _create_game_view (Builder          builder,
+                                              out GtkClutter.Embed embed,
+                                              ref Game             game)
     {
-        _embed = new GtkClutter.Embed ();
+        embed = new GtkClutter.Embed ();
         AspectFrame _frame = (AspectFrame) builder.get_object ("aspectframe");
-        _frame.add (_embed);
-        _game.view = _embed.get_stage ();
+        _frame.add (embed);
+        game.view = embed.get_stage ();
     }
 
     /*\
@@ -327,7 +348,7 @@ private class Application : Gtk.Application
         _header_bar.set_has_subtitle (false);
         _game_restored = false;
 
-        _game.new_game ();
+        _game.new_game (ref _settings);
 
         _embed.grab_focus ();
     }
@@ -347,7 +368,6 @@ private class Application : Gtk.Application
         _settings.set_int ("cols", cols);
         _settings.apply ();
 
-        _game.reload_settings ();
         new_game_cb ();
     }
 
@@ -491,13 +511,8 @@ private class Application : Gtk.Application
         _preferences_dialog.set_application (this); // else we cannot use "app." actions in the dialog
         _preferences_dialog.set_transient_for (_window);
 
-        _preferences_dialog.response.connect ((response_id) => {
-                _preferences_dialog.hide_on_delete ();
-            });
-        _preferences_dialog.delete_event.connect ((response_id) => {
-                _game.reload_settings ();
-                return _preferences_dialog.hide_on_delete ();
-            });
+        _preferences_dialog.response.connect ((dialog, response_id) => { dialog.hide_on_delete (); });
+        _preferences_dialog.delete_event.connect ((dialog, event) => { return dialog.hide_on_delete (); });
 
         Object? congratswitch = builder.get_object ("congratswitch");
         Object? undoswitch    = builder.get_object ("undoswitch");
diff --git a/src/game.vala b/src/game.vala
index 0733454..1226563 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -42,34 +42,20 @@ private class Game : Object
     private Gee.LinkedList<TileMovement?> _to_hide = new Gee.LinkedList<TileMovement?> ();
     private Gee.LinkedList<Tile?>         _to_show = new Gee.LinkedList<Tile?> ();
 
-    private GameState _state;
+    private GameState _state = GameState.STOPPED;
     private Clutter.TransitionGroup _show_hide_trans;
     private Clutter.TransitionGroup _move_trans;
     private int _animations_duration;
 
-    private GLib.Settings _settings;
-
-    private string _saved_path;
+    private string _saved_path = Path.build_filename (Environment.get_user_data_dir (), "gnome-2048", 
"saved");
 
     private uint _resize_view_id;
 
-    internal Game (GLib.Settings settings)
+    internal Game (ref GLib.Settings settings)
     {
-        Object ();
-
-        _settings = settings;
-
-        int rows = _settings.get_int ("rows");
-        int cols = _settings.get_int ("cols");
-
-        _init_grid (rows, cols, out _grid, ref _settings);
-
-        _animations_duration = (int)_settings.get_double ("animations-speed");
-        _load_undo_settings ();
-
-        _saved_path = Path.build_filename (Environment.get_user_data_dir (), "gnome-2048", "saved");
-
-        _state = GameState.STOPPED;
+        int rows = settings.get_int ("rows");
+        int cols = settings.get_int ("cols");
+        _init_grid (rows, cols, out _grid, ref settings);
     }
 
     private static void _init_grid (int rows, int cols, out Grid grid, ref GLib.Settings settings)
@@ -115,13 +101,25 @@ private class Game : Object
 
     internal uint score { internal get; private set; default = 0; }
 
-    internal void new_game ()
+    internal void new_game (ref GLib.Settings settings)
     {
         _clean_finish_move_animation ();
         _grid.clear ();
         _clear_history ();
 
-        if (_background_init_done)
+        int rows = settings.get_int ("rows");
+        int cols = settings.get_int ("cols");
+
+        if ((rows != _grid.rows) || (cols != _grid.cols))
+        {
+            _clear_foreground ();
+            _clear_background ();
+
+            _init_grid (rows, cols, out _grid, ref settings);
+
+            _init_background ();
+        }
+        else if (_background_init_done)
             _clear_foreground ();
         else // new_game could be called without an existing game
             _init_background ();
@@ -182,23 +180,10 @@ private class Game : Object
         return _state != GameState.IDLE;
     }
 
-    internal void reload_settings ()
+    internal void load_settings (ref GLib.Settings settings)
     {
-        _animations_duration = (int)_settings.get_double ("animations-speed");
-        _load_undo_settings ();
-
-        int rows = _settings.get_int ("rows");
-        int cols = _settings.get_int ("cols");
-
-        if ((rows != _grid.rows) || (cols != _grid.cols))
-        {
-            _clear_foreground ();
-            _clear_background ();
-
-            _init_grid (rows, cols, out _grid, ref _settings);
-
-            _init_background ();
-        }
+        _animations_duration = (int) settings.get_double ("animations-speed");
+        _load_undo_settings (ref settings);
     }
 
     private void _init_background ()
@@ -667,16 +652,16 @@ private class Game : Object
             undo_disabled ();
     }
 
-    private void _load_undo_settings ()
+    private void _load_undo_settings (ref GLib.Settings settings)
     {
-        bool allow_undo = _settings.get_boolean ("allow-undo");
+        bool allow_undo = settings.get_boolean ("allow-undo");
         if (_allow_undo && !allow_undo)
         {
             _clear_history ();
             undo_disabled ();
         }
         _allow_undo = allow_undo;
-        _undo_stack_max_size = _settings.get_uint ("allow-undo-max");
+        _undo_stack_max_size = settings.get_uint ("allow-undo-max");
     }
 
     private void _clear_history ()


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