[lightsoff/arnaudb/rework-ui] Fix bug.



commit 798888d3f66b941690b1df8523675d607280b848
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sun Nov 22 19:29:41 2020 +0100

    Fix bug.
    
    The "playable" variable didn't avoid the
    clicked togglebutton to activate itself.

 src/board-view-gtk.vala | 12 ++++--------
 src/board-view.vala     | 12 ++++++++----
 src/game-view-gtk.vala  | 16 +++++++++-------
 3 files changed, 21 insertions(+), 19 deletions(-)
---
diff --git a/src/board-view-gtk.vala b/src/board-view-gtk.vala
index 6f314d3..c7a640a 100644
--- a/src/board-view-gtk.vala
+++ b/src/board-view-gtk.vala
@@ -15,12 +15,11 @@ private class BoardViewGtk : Grid, BoardView
     private PuzzleGenerator puzzle_generator;
     private ToggleButton[,] lights;
 
-    internal bool playable { private get; internal set; default = true; }
     private const int MIN_TOGGLE_SIZE = 48;
     private int _moves = 0;
     internal int get_moves () { return _moves; }
 
-    internal BoardViewGtk ()
+    construct
     {
         get_style_context ().add_class ("grid");
         row_homogeneous = true;
@@ -45,7 +44,8 @@ private class BoardViewGtk : Grid, BoardView
             }
         set_focus_chain (focus_list);
         _moves = 0;
-        show_all ();
+        completed.connect (() => set_sensitive (false));
+        show ();
     }
 
     // Pseudorandomly generates and sets the state of each light based on
@@ -53,12 +53,9 @@ private class BoardViewGtk : Grid, BoardView
     // depends on GLib's PRNG stability. Also, provides some semblance of
     // symmetry for some levels.
 
-     // Toggle a light and those in each cardinal direction around it.
+    // Toggle a light and those in each cardinal direction around it.
     internal void toggle_light (int x, int y, bool clicked = true)
     {
-        if (!playable)
-            return;
-
         @foreach((light) => ((ToggleButton)light).toggled.disconnect (handle_toggle));
 
         if (x>= size || y >= size || x < 0 || y < 0 )
@@ -80,7 +77,6 @@ private class BoardViewGtk : Grid, BoardView
 
     internal void clear_level ()
     {
-        /* Clear level */
         for (var x = 0; x < size; x++)
             for (var y = 0; y < size; y++)
                 lights[x, y].active = false;
diff --git a/src/board-view.vala b/src/board-view.vala
index 3893586..daac345 100644
--- a/src/board-view.vala
+++ b/src/board-view.vala
@@ -21,10 +21,11 @@ private interface BoardView: GLib.Object {
 
     internal abstract GLib.Object get_light_at (int x, int y);
 
+    protected signal void completed ();
     internal signal void game_won ();
     internal signal void light_toggled ();
 
-        // Pseudorandomly generates and sets the state of each light based on
+    // Pseudorandomly generates and sets the state of each light based on
     // a level number; hopefully this is stable between machines, but that
     // depends on GLib's PRNG stability. Also, provides some semblance of
     // symmetry for some levels.
@@ -70,12 +71,15 @@ private interface BoardView: GLib.Object {
         toggle_light (x, y);
         increase_moves ();
         light_toggled ();
-        if (is_completed ()) {
-            Gdk.threads_add_timeout(300, game_won_timeout);
+        if (is_completed ())
+        {
+            completed ();
+            Gdk.threads_add_timeout (300, game_won_timeout);
         }
     }
 
-    private bool game_won_timeout () {
+    private bool game_won_timeout ()
+    {
         game_won ();
         return GLib.Source.REMOVE;
     }
diff --git a/src/game-view-gtk.vala b/src/game-view-gtk.vala
index bf8ec8d..18d5c10 100644
--- a/src/game-view-gtk.vala
+++ b/src/game-view-gtk.vala
@@ -16,6 +16,8 @@ private class GtkGameView : Stack, GameView
 
     internal void replace_board (BoardView old_board, BoardView new_board, GameView.ReplaceStyle style, bool 
fast = true)
     {
+        ((BoardViewGtk)old_board).sensitive = false;
+
         transition_duration = fast ? 500 : 1000;
         switch (style)
         {
@@ -38,7 +40,6 @@ private class GtkGameView : Stack, GameView
 
         add ((Widget)new_board);
         set_visible_child ((Widget)new_board);
-        ((BoardViewGtk)old_board).playable = false;
         if (Gtk.Settings.get_for_screen (((Widget)new_board).get_screen ()).gtk_enable_animations)
             handlers.push_tail(notify["transition-running"].connect(() => board_replaced 
((BoardViewGtk)old_board, (BoardViewGtk)new_board)));
         else
@@ -49,7 +50,7 @@ private class GtkGameView : Stack, GameView
     internal void board_replaced (BoardViewGtk old_board, BoardViewGtk new_board)
     {
         @foreach((board) => { if (board != get_visible_child ()) remove(board);});
-        new_board.playable = true;
+        new_board.sensitive = true;
         board_view = new_board;
         if (!handlers.is_empty ())
             disconnect(handlers.pop_head());
@@ -80,7 +81,7 @@ private class GtkGameView : Stack, GameView
     internal GtkGameView (int level)
     {
         board_view = (BoardViewGtk)create_board_view (level);
-        board_view.playable = true;
+        board_view.sensitive = true;
         add (board_view);
     }
 
@@ -90,18 +91,19 @@ private class GtkGameView : Stack, GameView
 
         var view = new BoardViewGtk ();
         view.load_level (level);
-        view.game_won.connect (() => game_won_cb());
+        view.game_won.connect (game_won_cb);
         view.light_toggled.connect (light_toggled_cb);
-        view.playable = false;
+        view.sensitive = false;
         return (BoardView)view;
     }
 
-   internal BoardView get_board_view ()
+    internal BoardView get_board_view ()
     {
         return (BoardView)board_view;
     }
 
-    internal int next_level (int direction) {
+    internal int next_level (int direction)
+    {
         current_level += direction;
         return current_level;
     }


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