[gnome-2048/arnaudb/wip/gtk4: 35/57] Test.




commit eff7cefdf8d8216dd6f1e47b3840ae906a173896
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Apr 29 14:56:45 2020 +0200

    Test.

 data/gnome-2048.css                          |   3 +
 data/mainwindow.ui                           |   6 +-
 src/game-window.vala                         |  11 ++-
 src/game.vala                                | 120 +++++++++++++++++----------
 src/org.gnome.TwentyFortyEight.gresource.xml |   2 +-
 src/view.vala                                |   6 +-
 6 files changed, 96 insertions(+), 52 deletions(-)
---
diff --git a/data/gnome-2048.css b/data/gnome-2048.css
new file mode 100644
index 0000000..d84cd77
--- /dev/null
+++ b/data/gnome-2048.css
@@ -0,0 +1,3 @@
+.background-grid {
+  background-color:#babdb6;
+}
diff --git a/data/mainwindow.ui b/data/mainwindow.ui
index e63bbfb..e1155ee 100644
--- a/data/mainwindow.ui
+++ b/data/mainwindow.ui
@@ -30,11 +30,7 @@
     <child>
       <object class="GtkOverlay">
         <child>
-          <object class="GtkAspectFrame">
-            <child>
-              <object class="Game" id="_game"/>
-            </child>
-          </object>
+          <object class="GtkAspectFrame" id="_frame"/>
         </child>
         <child type="overlay">
           <object class="GtkButton" id="_unfullscreen_button">
diff --git a/src/game-window.vala b/src/game-window.vala
index 8d19c86..58c8031 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -27,7 +27,8 @@ private class GameWindow : ApplicationWindow
     private GLib.Settings _settings;
 
     [GtkChild] private GameHeaderBar    _header_bar;
-    [GtkChild] private Game             _game;
+    [GtkChild] private AspectFrame      _frame;
+               private Game             _game;
 
     [GtkChild] private Button           _unfullscreen_button;
 
@@ -36,6 +37,12 @@ private class GameWindow : ApplicationWindow
 
     construct
     {
+        CssProvider css_provider = new CssProvider ();
+        css_provider.load_from_resource ("/org/gnome/TwentyFortyEight/ui/gnome-2048.css");
+        Gdk.Display? gdk_display = Gdk.Display.get_default ();
+        if (gdk_display != null) // else..?
+            StyleContext.add_provider_for_display ((!) gdk_display, css_provider, 
STYLE_PROVIDER_PRIORITY_APPLICATION);
+
         _settings = new GLib.Settings ("org.gnome.TwentyFortyEight");
 
         _install_ui_action_entries ();
@@ -98,6 +105,8 @@ private class GameWindow : ApplicationWindow
         _game.target_value_reached.connect (target_value_reached_cb);
         _game.undo_enabled.connect (() => { undo_action.set_enabled (true); });
         _game.undo_disabled.connect (() => { undo_action.set_enabled (false); });
+
+        _frame.add (_game);
     }
 
     private void _init_window ()
diff --git a/src/game.vala b/src/game.vala
index 3096740..2164841 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -69,8 +69,45 @@ private class Game : Gtk.Widget
 
     private uint _resize_view_id;
 
+    private Gtk.Grid _background_grid;
+    private Gtk.Grid _foreground_grid;
+
     construct
     {
+        Gtk.BinLayout layout = new Gtk.BinLayout ();
+        set_layout_manager (layout);
+
+        add_css_class ("background-grid");
+
+        _background_grid = new Gtk.Grid ();
+        _background_grid.hexpand = true;
+        _background_grid.vexpand = true;
+        _background_grid.column_homogeneous = true;
+        _background_grid.row_homogeneous = true;
+        _background_grid.column_spacing = 8;
+        _background_grid.row_spacing = 8;
+        _background_grid.margin_start = 8;
+        _background_grid.margin_end = 8;
+        _background_grid.margin_top = 8;
+        _background_grid.margin_bottom = 8;
+
+        _foreground_grid = new Gtk.Grid ();
+        _foreground_grid.hexpand = true;
+        _foreground_grid.vexpand = true;
+        _foreground_grid.column_homogeneous = true;
+        _foreground_grid.row_homogeneous = true;
+        _foreground_grid.column_spacing = 8;
+        _foreground_grid.row_spacing = 8;
+        _foreground_grid.margin_start = 8;
+        _foreground_grid.margin_end = 8;
+        _foreground_grid.margin_top = 8;
+        _foreground_grid.margin_bottom = 8;
+
+        Gtk.Overlay overlay = new Gtk.Overlay ();
+        overlay.add (_background_grid);
+        overlay.add_overlay (_foreground_grid);
+        overlay.insert_after (this, /* insert first */ null);
+
         hexpand = true;
         vexpand = true;
     }
@@ -94,39 +131,6 @@ private class Game : Gtk.Widget
     * * 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_background = new Clutter.Actor ();
-//            _view_foreground = new Clutter.Actor ();
-//            _view_background.show ();
-//            _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 {
-//            _view = value;
-//            _view.allocation_changed.connect (_on_allocation_changed);
-
-//            _view_background = new Clutter.Actor ();
-//            _view_foreground = new Clutter.Actor ();
-//            _view_background.show ();
-//            _view_foreground.show ();
-//            _view.add_child (_view_background);
-//            _view.add_child (_view_foreground);
-//        }
-//    }
-
     private void _on_size_allocate (Gtk.Widget widget, int width, int height, int baseline)
     {
         width  = _width;
@@ -232,8 +236,6 @@ private class Game : Gtk.Widget
     {
         uint8 rows = _grid.rows;
         uint8 cols = _grid.cols;
-//        Clutter.Color background_color = Clutter.Color.from_string ("#babdb6");
-//        _view.set_background_color (background_color);
 
         _background     = new RoundedRectangle [rows, cols];
         _foreground_cur = new TileView? [rows, cols];
@@ -257,9 +259,7 @@ private class Game : Gtk.Widget
 
                 RoundedRectangle rect = new RoundedRectangle (x, y, tile_width, tile_height);
 
-//                _view_background.add_child (rect.actor);
-//                rect.canvas.invalidate ();
-//                rect.actor.show ();
+                _background_grid.attach (rect, /* x and y */ j, i, /* width and height */ 1, 1);
 
                 _background     [i, j] = rect;
                 _foreground_cur [i, j] = null;
@@ -299,8 +299,8 @@ private class Game : Gtk.Widget
             }
         }
 
-//        if (_resize_view_id == 0)
-//            _resize_view_id = Clutter.Threads.Timeout.add (1000, _idle_resize_view);
+        if (_resize_view_id == 0)
+            _resize_view_id = Timeout.add_seconds (1, _idle_resize_view);
     }
 
     private bool _idle_resize_view ()
@@ -373,6 +373,10 @@ private class Game : Gtk.Widget
 //        actor.set_opacity (0);
 //        actor.show ();
 //        _view_foreground.add_child (actor);
+        Gtk.Widget? widget = _foreground_grid.get_child_at (pos.row, pos.col);
+        if (widget != null)
+            ((!) widget).destroy ();
+        _foreground_grid.attach ((!) tile_view, /* x and y */ pos.row, pos.col, /* height and width */ 1, 1);
 
 //        trans = new Clutter.PropertyTransition ("scale-x");
 //        trans.set_from_value (1.0);
@@ -450,14 +454,24 @@ private class Game : Gtk.Widget
 
     private void _clear_background ()
     {
-//        _view_background.remove_all_children ();
+        _background_grid.@foreach ((widget) => widget.destroy ());
     }
 
     private void _clear_foreground ()
     {
         uint8 rows = _grid.rows;
         uint8 cols = _grid.cols;
-//        _view_foreground.remove_all_children ();
+        float canvas_width  = (float) width;
+        float canvas_height = (float) height;
+
+        canvas_width  -= (cols + 1) * BLANK_COL_WIDTH;
+        canvas_height -= (rows + 1) * BLANK_ROW_HEIGHT;
+
+        float tile_width  = canvas_width  / cols;
+        float tile_height = canvas_height / rows;
+
+        _foreground_grid.@foreach ((widget) => widget.destroy ());
+
         for (uint8 i = 0; i < rows; i++)
         {
             for (uint8 j = 0; j < cols; j++)
@@ -466,6 +480,12 @@ private class Game : Gtk.Widget
                     _foreground_cur [i, j] = null;
                 if (_foreground_nxt [i, j] != null)
                     _foreground_nxt [i, j] = null;
+
+                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 */ i, j, /* height and width */ 1, 1);
             }
         }
     }
@@ -474,6 +494,14 @@ private class Game : Gtk.Widget
     {
         uint8 rows = _grid.rows;
         uint8 cols = _grid.cols;
+        float canvas_width  = (float) width;
+        float canvas_height = (float) height;
+
+        canvas_width  -= (cols + 1) * BLANK_COL_WIDTH;
+        canvas_height -= (rows + 1) * BLANK_ROW_HEIGHT;
+
+        float tile_width  = canvas_width  / cols;
+        float tile_height = canvas_height / rows;
 
         _create_show_hide_transition (animate);
 
@@ -490,6 +518,14 @@ private class Game : Gtk.Widget
                     _to_show.add (tile);
                     _show_tile (pos);
                 }
+                else
+                {
+                    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 */ i, j, /* height and width */ 1, 1);
+                }
             }
         }
 
diff --git a/src/org.gnome.TwentyFortyEight.gresource.xml b/src/org.gnome.TwentyFortyEight.gresource.xml
index b2cf713..8e6c2d0 100644
--- a/src/org.gnome.TwentyFortyEight.gresource.xml
+++ b/src/org.gnome.TwentyFortyEight.gresource.xml
@@ -4,7 +4,7 @@
     <file preprocess="xml-stripblanks" alias="congrats.ui">../data/congrats.ui</file>
     <file preprocess="xml-stripblanks" alias="game-headerbar.ui">../data/game-headerbar.ui</file>
     <file preprocess="xml-stripblanks" alias="game-window.ui">../data/mainwindow.ui</file>
-    <!-- file>data/style.css</file -->
+    <file alias="gnome-2048.css">../data/gnome-2048.css</file>
   </gresource>
   <gresource prefix="/org/gnome/TwentyFortyEight/gtk">
     <file preprocess="xml-stripblanks" alias="help-overlay.ui">../data/help-overlay.ui</file>
diff --git a/src/view.vala b/src/view.vala
index 2c1f8ca..bdce5c0 100644
--- a/src/view.vala
+++ b/src/view.vala
@@ -119,9 +119,9 @@ private class RoundedRectangle : Gtk.DrawingArea
         color = (!) nullable_color;
 
         uint8 sbits = (uint8) (Math.pow (2, tile_value) % 7);
-//        color.red   <<= sbits;
-//        color.green <<= sbits;
-//        color.blue  <<= sbits;
+        color.red   = (float) ((uint8) (color.red   * 255.0f) << sbits) / 255.0f;
+        color.green = (float) ((uint8) (color.green * 255.0f) << sbits) / 255.0f;
+        color.blue  = (float) ((uint8) (color.blue  * 255.0f) << sbits) / 255.0f;
 
         colors.insert ((int) tile_value, color);
     }


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