[gnome-2048/arnaudb/wip/gtk4: 26/36] Make things start to look promising.



commit ed7fb2cfe118f871d4404743f066f797f6586907
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Apr 28 20:35:09 2020 +0200

    Make things start to look promising.

 data/mainwindow.ui   |  2 +-
 src/game-window.vala | 39 +++++++++------------------------------
 src/game.vala        | 39 ++++++++++++++++++++++++++-------------
 3 files changed, 36 insertions(+), 44 deletions(-)
---
diff --git a/data/mainwindow.ui b/data/mainwindow.ui
index b2df2ac..92e4752 100644
--- a/data/mainwindow.ui
+++ b/data/mainwindow.ui
@@ -35,7 +35,7 @@
             <child>
               <object class="GtkAspectFrame">
                 <child>
-                  <object class="Board" id="_board"/>
+                  <object class="Game" id="_game"/>
                 </child>
               </object>
             </child>
diff --git a/src/game-window.vala b/src/game-window.vala
index 1f9261c..05708ba 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -21,35 +21,16 @@
 using Games;
 using Gtk;
 
-private class Board : Widget
-{
-    internal int width  { internal get; private set; }
-    internal int height { internal get; private set; }
-
-    construct
-    {
-        size_allocate.connect (on_size_allocate);
-    }
-
-    private static inline void on_size_allocate (Widget widget, int _width, int _height, int _baseline)
-    {
-        ((Board) widget).width  = _width;
-        ((Board) widget).height = _height;
-    }
-}
-
 [GtkTemplate (ui = "/org/gnome/TwentyFortyEight/ui/game-window.ui")]
 private class GameWindow : ApplicationWindow
 {
     private GLib.Settings _settings;
 
     [GtkChild] private GameHeaderBar    _header_bar;
-    [GtkChild] private Board            _board;
+    [GtkChild] private Game             _game;
 
     [GtkChild] private Button           _unfullscreen_button;
 
-    private Game _game;
-
     public uint8 cli_cols { private get; protected construct; default = 0; }
     public uint8 cli_rows { private get; protected construct; default = 0; }
 
@@ -64,7 +45,7 @@ private class GameWindow : ApplicationWindow
         _init_window ();
         _create_scores_dialog ();   // the library forbids to delay the dialog creation
 
-        notify ["has-toplevel-focus"].connect (() => _board.grab_focus ());
+        notify ["has-toplevel-focus"].connect (() => _game.grab_focus ());
     }
 
     internal GameWindow (TwentyFortyEight application, uint8 cols, uint8 rows)
@@ -124,7 +105,7 @@ private class GameWindow : ApplicationWindow
         _init_window_state ();
         _load_window_state (this, ref _settings);
 
-        _header_bar.popover_closed.connect (() => _board.grab_focus ());
+        _header_bar.popover_closed.connect (() => _game.grab_focus ());
         _settings.changed.connect ((settings, key_name) => {
                 switch (key_name)
                 {
@@ -147,8 +128,6 @@ private class GameWindow : ApplicationWindow
                                            (uint8) _settings.get_int ("cols")); // and cols from 1 to 9
         _header_bar._update_hamburger_menu (_settings.get_boolean ("allow-undo"));
         _game.load_settings (ref _settings);
-
-        _game.view = _board;
     }
 
     /*\
@@ -271,14 +250,14 @@ private class GameWindow : ApplicationWindow
 
         _header_bar.clear_subtitle ();
         _game.undo ();
-        _board.grab_focus ();
+        _game.grab_focus ();
     }
 
     private void new_game_cb (/* SimpleAction action, Variant? variant */)
     {
         _header_bar.clear_subtitle ();
         _game.new_game (ref _settings);
-        _board.grab_focus ();
+        _game.grab_focus ();
     }
 
     private void new_game_sized_cb (SimpleAction action, Variant? variant)
@@ -325,7 +304,7 @@ private class GameWindow : ApplicationWindow
     private static inline bool on_key_pressed (EventControllerKey _key_controller, uint keyval, uint 
keycode, Gdk.ModifierType state)
     {
         GameWindow _this = (GameWindow) _key_controller.get_widget ();
-        if (_this._header_bar.has_popover () || (_this.focus_visible && !_this._board.is_focus ()))
+        if (_this._header_bar.has_popover () || (_this.focus_visible && !_this._game.is_focus ()))
             return false;
         if (_this._game.cannot_move ())
             return false;
@@ -360,14 +339,14 @@ private class GameWindow : ApplicationWindow
 
     private inline void _init_gestures ()
     {
-        gesture_swipe = new GestureSwipe ();  // _window works, but problems with headerbar; the main grid 
or the aspectframe do as _board
+        gesture_swipe = new GestureSwipe ();  // _window works, but problems with headerbar; the main grid 
or the aspectframe do as _game
         gesture_swipe.set_propagation_phase (PropagationPhase.CAPTURE);
         gesture_swipe.set_button (/* all buttons */ 0);
         gesture_swipe.swipe.connect (_on_swipe);
-        _board.add_controller (gesture_swipe);
+        _game.add_controller (gesture_swipe);
     }
 
-    private inline void _on_swipe (GestureSwipe _gesture_swipe, double velocity_x, double velocity_y)   // 
do not make static, _gesture_swipe.get_wigdet () is _board, not the window
+    private inline void _on_swipe (GestureSwipe _gesture_swipe, double velocity_x, double velocity_y)   // 
do not make static, _gesture_swipe.get_wigdet () is _game, not the window
     {
         uint button = _gesture_swipe.get_current_button ();
         if (button != Gdk.BUTTON_PRIMARY && button != Gdk.BUTTON_SECONDARY)
diff --git a/src/game.vala b/src/game.vala
index e405256..9889452 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -18,8 +18,11 @@
    along with GNOME 2048.  If not, see <https://www.gnu.org/licenses/>.
 */
 
-private class Game : Object
+private class Game : Gtk.Widget
 {
+    internal int width  { internal get; private set; }
+    internal int height { internal get; private set; }
+
     private enum GameState {
         STOPPED,
         IDLE,
@@ -66,11 +69,19 @@ private class Game : Object
 
     private uint _resize_view_id;
 
+    construct
+    {
+        hexpand = true;
+        vexpand = true;
+    }
+
     internal Game (ref GLib.Settings settings)
     {
         uint8 cols = (uint8) settings.get_int ("cols");  // schema ranges cols
         uint8 rows = (uint8) settings.get_int ("rows"); // and rows from 1 to 9
         _init_grid (rows, cols, out _grid, ref settings);
+
+        size_allocate.connect (_on_size_allocate);
     }
 
     private static void _init_grid (uint8 rows, uint8 cols, out Grid grid, ref GLib.Settings settings)
@@ -83,16 +94,15 @@ private class Game : Object
     * * view
     \*/
 
-    private Board _view;
+//    private Board _view;
 //    private Clutter.Actor _view;
 //    private Clutter.Actor _view_background;
 //    private Clutter.Actor _view_foreground;
 
-    [CCode (notify = false)] internal Board view {
-        internal get { return _view; }
-        internal set {
-            _view = value;
-            _view.size_allocate.connect (_on_size_allocate);
+//    [CCode (notify = false)] internal Board view {
+//        internal get { return _view; }
+//        internal set {
+//            _view = value;
 
 //            _view_background = new Clutter.Actor ();
 //            _view_foreground = new Clutter.Actor ();
@@ -100,8 +110,8 @@ private class Game : Object
 //            _view_foreground.show ();
 //            _view.add_child (_view_background);
 //            _view.add_child (_view_foreground);
-        }
-    }
+//        }
+//    }
 //    [CCode (notify = false)] internal Clutter.Actor view {
 //        internal get { return _view; }
 //        internal set {
@@ -119,6 +129,9 @@ private class Game : Object
 
     private void _on_size_allocate (Gtk.Widget widget, int width, int height, int baseline)
     {
+        width  = _width;
+        height = _height;
+
         if (_background_init_done)
             _resize_view ();
         else
@@ -226,8 +239,8 @@ private class Game : Object
         _foreground_cur = new TileView? [rows, cols];
         _foreground_nxt = new TileView? [rows, cols];
 
-        float canvas_width  = (float) _view.width;
-        float canvas_height = (float) _view.height;
+        float canvas_width  = (float) width;
+        float canvas_height = (float) height;
 
         canvas_width  -= (cols + 1) * BLANK_COL_WIDTH;
         canvas_height -= (rows + 1) * BLANK_ROW_HEIGHT;
@@ -260,8 +273,8 @@ private class Game : Object
     {
         uint8 rows = _grid.rows;
         uint8 cols = _grid.cols;
-        float canvas_width  = (float) _view.width;
-        float canvas_height = (float) _view.height;
+        float canvas_width  = (float) width;
+        float canvas_height = (float) height;
 
         canvas_width  -= (cols + 1) * BLANK_COL_WIDTH;
         canvas_height -= (rows + 1) * BLANK_ROW_HEIGHT;


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