[gnome-2048/arnaudb/wip/gtk4: 48/57] Make things start to work.




commit b44949316f980a9d85c903e03c34695207736ab1
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Jul 16 17:42:56 2020 +0200

    Make things start to work.

 src/game-window.vala |   3 ++
 src/game.vala        | 101 ++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 78 insertions(+), 26 deletions(-)
---
diff --git a/src/game-window.vala b/src/game-window.vala
index ae55199..307afb0 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -309,11 +309,14 @@ private class GameWindow : ApplicationWindow
     {
         key_controller = new EventControllerKey ();
         key_controller.key_pressed.connect (on_key_pressed);
+        key_controller.set_propagation_phase (PropagationPhase.CAPTURE);
         ((Widget) this).add_controller (key_controller);
     }
 
     private static inline bool on_key_pressed (EventControllerKey _key_controller, uint keyval, uint 
keycode, Gdk.ModifierType state)
     {
+        warning ("key pressed");
+
         GameWindow _this = (GameWindow) _key_controller.get_widget ();
         if (_this._header_bar.has_popover () || (_this.focus_visible && !_this._game.is_focus ()))
             return false;
diff --git a/src/game.vala b/src/game.vala
index 2ad533e..279d58d 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -77,6 +77,8 @@ private class Game : Gtk.Widget
         Gtk.BinLayout layout = new Gtk.BinLayout ();
         set_layout_manager (layout);
 
+        focusable = true;
+
 //        map.connect (_init_state_watcher);
 
         add_css_class ("background-grid");
@@ -166,6 +168,8 @@ private class Game : Gtk.Widget
         if (_state != GameState.IDLE && _state != GameState.STOPPED)
             return;
 
+        warning ("new game");
+
         _clean_finish_move_animation ();
         _grid.clear ();
         _clear_history ();
@@ -185,7 +189,10 @@ private class Game : Gtk.Widget
         else if (_background_init_done)
             _clear_foreground ();
         else // new_game could be called without an existing game
+        {
+            warning ("glou");
             _init_background ();
+        }
 
         score = 0;
         _state = GameState.SHOWING_FIRST_TILE;
@@ -245,6 +252,8 @@ private class Game : Gtk.Widget
 
     private void _init_background ()
     {
+        warning ("init background");
+
         uint8 rows = _grid.rows;
         uint8 cols = _grid.cols;
 
@@ -261,10 +270,12 @@ private class Game : Gtk.Widget
         float tile_width  = canvas_width  / cols;
         float tile_height = canvas_height / rows;
 
+        warning (@"_init_background size: $cols, $rows");
         for (uint8 i = 0; i < rows; i++)
         {
             for (uint8 j = 0; j < cols; j++)
             {
+                warning (@"_init_background: ($i, $j)");
                 float x = j * tile_width  + (j + 1) * BLANK_COL_WIDTH;
                 float y = i * tile_height + (i + 1) * BLANK_ROW_HEIGHT;
 
@@ -277,6 +288,23 @@ private class Game : Gtk.Widget
                 _foreground_nxt [i, j] = null;
             }
         }
+        for (uint8 i = 0; i < rows; i++)
+        {
+            for (uint8 j = 0; j < cols; j++)
+            {
+                warning (@"_init_foreground: ($i, $j)");
+                float x = j * tile_width  + (j + 1) * BLANK_COL_WIDTH;
+                float y = i * tile_height + (i + 1) * BLANK_ROW_HEIGHT;
+
+                RoundedRectangle rect = new RoundedRectangle (x, y, tile_width, tile_height);
+
+                _foreground_grid.attach (rect, /* x and y */ j, i, /* width and height */ 1, 1);
+
+//                _background     [i, j] = rect;
+//                _foreground_cur [i, j] = null;
+//                _foreground_nxt [i, j] = null;
+            }
+        }
         _background_init_done = true;
     }
 
@@ -297,6 +325,7 @@ private class Game : Gtk.Widget
         {
             for (uint8 j = 0; j < cols; j++)
             {
+                warning (@"_resize_view: ($i, $j)");
                 float x = j * tile_width  + (j + 1) * BLANK_COL_WIDTH;
                 float y = i * tile_height + (i + 1) * BLANK_ROW_HEIGHT;
 
@@ -322,6 +351,7 @@ private class Game : Gtk.Widget
         {
             for (uint8 j = 0; j < cols; j++)
             {
+                warning (@"_idle_resize_view: ($i, $j)");
                 _background [i, j].idle_resize ();
 
                 if (_foreground_cur [i, j] != null)
@@ -358,6 +388,8 @@ private class Game : Gtk.Widget
 
     private void _create_tile (Tile tile)
     {
+        warning ("create tile");
+
         GridPosition pos = tile.pos;
         assert (_foreground_nxt [pos.row, pos.col] == null);
 
@@ -465,7 +497,9 @@ private class Game : Gtk.Widget
 
     private void _clear_background ()
     {
-//        _background_grid.@foreach ((widget) => widget.destroy ());
+        Gtk.Widget? child;
+        while ((child = _background_grid.get_last_child ()) != null)
+            ((!) child).unparent ();
     }
 
     private void _clear_foreground ()
@@ -481,12 +515,15 @@ private class Game : Gtk.Widget
         float tile_width  = canvas_width  / cols;
         float tile_height = canvas_height / rows;
 
-//        _foreground_grid.@foreach ((widget) => widget.destroy ());
+        Gtk.Widget? child;
+        while ((child = _foreground_grid.get_last_child ()) != null)
+            ((!) child).unparent ();
 
         for (uint8 i = 0; i < rows; i++)
         {
             for (uint8 j = 0; j < cols; j++)
             {
+                warning (@"clearing foreground: ($i, $j)");
                 if (_foreground_cur [i, j] != null)
                     _foreground_cur [i, j] = null;
                 if (_foreground_nxt [i, j] != null)
@@ -520,6 +557,7 @@ private class Game : Gtk.Widget
         {
             for (uint8 j = 0; j < cols; j++)
             {
+                warning (@"restoring foreground: ($i, $j)");
                 uint8 val = _grid [i, j];
                 if (val != 0)
                 {
@@ -553,6 +591,8 @@ private class Game : Gtk.Widget
 
     internal void move (MoveRequest request)
     {
+        warning ("move");
+
         if (_state == GameState.SHOWING_NEW_TILE)
             _apply_move ();
         else if (_state != GameState.IDLE)
@@ -585,6 +625,7 @@ private class Game : Gtk.Widget
         {
             _state = GameState.MOVING;
 //            _move_trans.start ();
+            Timeout.add (_animations_duration, _on_move_trans_stopped);
             _store_movement (clone);
         }
 
@@ -592,31 +633,34 @@ private class Game : Gtk.Widget
     }
 
 //    private void _on_move_trans_stopped (Clutter.Timeline trans, bool is_finished)
-//    {
-//        debug (@"move animation stopped\n$_grid");
+    private bool _on_move_trans_stopped ()
+    {
+        debug (@"move animation stopped\n$_grid");
 
 //        ((Clutter.TransitionGroup) trans).remove_all ();
 
-//        foreach (TileMovement? e in _to_hide)
-//        {
-//            if (e == null)
-//                assert_not_reached ();
-//            _dim_tile (((!) e).from);
-//        }
+        foreach (TileMovement? e in _to_hide)
+        {
+            if (e == null)
+                assert_not_reached ();
+            _dim_tile (((!) e).from);
+        }
 
-//        long delta_score = 0;   // do not notify["score"] multiple times
-//        foreach (Tile? e in _to_show)
-//        {
-//            if (e == null)
-//                assert_not_reached ();
-//            _create_tile ((!) e);
-//            _show_tile (((!) e).pos);
-//            delta_score += (long) Math.pow (2, ((!) e).val);
-//        }
-//        score += delta_score;
+        long delta_score = 0;   // do not notify["score"] multiple times
+        foreach (Tile? e in _to_show)
+        {
+            if (e == null)
+                assert_not_reached ();
+            _create_tile ((!) e);
+            _show_tile (((!) e).pos);
+            delta_score += (long) Math.pow (2, ((!) e).val);
+        }
+        score += delta_score;
+
+        _create_random_tile ();
 
-//        _create_random_tile ();
-//    }
+        return Source.REMOVE;
+    }
 
     /*\
     * * new tile animation
@@ -635,11 +679,14 @@ private class Game : Gtk.Widget
 //        /* _show_hide_trans should be finished two times (forward and backward) before
 //           one _move_trans is done, so at least animation time should be strictly half */
 //        _show_hide_trans.set_duration (animate ? _animations_duration / 3 : 10);
+
+        Timeout.add (animate ? _animations_duration / 3 : 10, _on_show_hide_trans_stopped);
     }
 
 //    private void _on_show_hide_trans_stopped (Clutter.Timeline trans, bool is_finished)
-//    {
-//        debug ("show/hide animation stopped");
+    private bool _on_show_hide_trans_stopped ()
+    {
+        debug ("show/hide animation stopped");
 
 //        if (trans.direction == Clutter.TimelineDirection.FORWARD)
 //        {
@@ -649,8 +696,10 @@ private class Game : Gtk.Widget
 //        }
 
 //        ((Clutter.TransitionGroup) trans).remove_all ();
-//        _apply_move ();
-//    }
+        _apply_move ();
+
+        return Source.REMOVE;
+    }
 
     private void _apply_move ()
     {


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