[gnome-robots/prepare-to-gtk4] Prepare for GTK4




commit 0d082197202323d7aa86c1941382bcf5e968b757
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Sep 12 19:07:52 2021 +0200

    Prepare for GTK4

 src/application.vala       |  29 ++++------
 src/controls.vala          |  13 +++--
 src/game-area.vala         |  65 ++++++++++-----------
 src/properties-dialog.vala | 137 ++++++++++++++++++++-------------------------
 4 files changed, 112 insertions(+), 132 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index 1824b50..fa385cb 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -58,18 +58,10 @@ class RobotsApplication : Gtk.Application {
             game_configs = new GameConfigs.load ();
             sound_player = new SoundPlayer ();
         } catch (Error e) {
-            critical ("%s", e.message);
-
-            var errordialog = new MessageDialog.with_markup (get_active_window (), // is it null?
-                                                             DialogFlags.MODAL,
-                                                             MessageType.ERROR,
-                                                             ButtonsType.OK,
-                                                             "<b>%s</b>\n\n%s",
-                                                             _("No game data could be found."),
-                                                             _("The program Robots was unable to find any 
valid game configuration files. Please check that the program is installed correctly."));
-            errordialog.set_resizable (false);
-            errordialog.run ();
-
+            critical ("%s\n%s",
+                _("The program Robots was unable to find any valid game configuration files. Please check 
that the program is installed correctly."),
+                e.message
+            );
             quit ();
         }
 
@@ -111,12 +103,13 @@ class RobotsApplication : Gtk.Application {
             dialog.add_button (_("Keep _Playing"), ResponseType.REJECT);
             dialog.add_button (_("_New Game"), ResponseType.ACCEPT);
 
-            var ret = dialog.run ();
-            dialog.destroy ();
-
-            if (ret == ResponseType.ACCEPT) {
-                window.start_new_game ();
-            }
+            dialog.response.connect ((ret) => {
+                dialog.destroy ();
+                if (ret == ResponseType.ACCEPT) {
+                    window.start_new_game ();
+                }
+            });
+            dialog.present ();
         } else {
             activate ();
         }
diff --git a/src/controls.vala b/src/controls.vala
index b07a2aa..3c0c2b6 100644
--- a/src/controls.vala
+++ b/src/controls.vala
@@ -19,7 +19,7 @@
 
 using Gtk;
 
-public class GamesControlsList : ScrolledWindow {
+public class GamesControlsList : Bin {
 
     private Gtk.ListStore store;
     private TreeView view;
@@ -72,10 +72,13 @@ public class GamesControlsList : ScrolledWindow {
 
         view.append_column (column2);
 
-        hscrollbar_policy = PolicyType.NEVER;
-        vscrollbar_policy = PolicyType.AUTOMATIC;
-        shadow_type = ShadowType.IN;
-        add (view);
+        var sw = new ScrolledWindow (null, null);
+        sw.hscrollbar_policy = PolicyType.NEVER;
+        sw.vscrollbar_policy = PolicyType.AUTOMATIC;
+        sw.shadow_type = ShadowType.IN;
+        sw.add (view);
+
+        add (sw);
 
         this.properties = properties;
         properties.changed.connect (properties_changed_cb);
diff --git a/src/game-area.vala b/src/game-area.vala
index 6083786..7127267 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -27,9 +27,6 @@ public class GameArea : DrawingArea {
     const int MINIMUM_TILE_HEIGHT = 8;
     const int ANIMATION_DELAY = 100;
 
-    private int tile_width = 0;
-    private int tile_height = 0;
-
     private GestureMultiPress click_controller;
     private EventControllerMotion motion_controller;
     private Game game;
@@ -99,7 +96,6 @@ public class GameArea : DrawingArea {
         game.config = game_configs.find_by_name (properties.selected_config) ?? game_configs[0];
 
         add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | 
Gdk.EventMask.POINTER_MOTION_MASK);
-        configure_event.connect (event => resize_cb (event));
         draw.connect ((cr) => draw_cb (cr));
 
         click_controller = new GestureMultiPress (this);
@@ -148,41 +144,45 @@ public class GameArea : DrawingArea {
         Source.remove (timer_id);
     }
 
-    private bool resize_cb (Gdk.EventConfigure e) {
-        int trial_width = e.width / game.arena.width;
-        int trial_height = e.height / game.arena.height;
-
-        if (trial_width != tile_width || trial_height != tile_height) {
-            tile_width = trial_width;
-            tile_height = trial_height;
-        }
+    private struct Size {
+        int width;
+        int height;
+    }
 
-        return false;
+    private Size tile_size () {
+        Allocation allocation;
+        get_allocation (out allocation);
+        return Size () {
+            width = allocation.width / game.arena.width,
+            height = allocation.height / game.arena.height
+        };
     }
 
     private bool draw_cb (Context cr) {
+        Size tile_size = tile_size ();
+
         for (int j = 0; j < game.arena.height; j++) {
             for (int i = 0; i < game.arena.width; i++) {
-                draw_object (i, j, game.arena[i, j], cr);
+                draw_object (i, j, game.arena[i, j], tile_size, cr);
             }
         }
 
         if (game.splat != null) {
             assets.splat_bubble.draw (cr,
-                                      game.splat.x * tile_width + 8,
-                                      game.splat.y * tile_height + 8);
+                                      game.splat.x * tile_size.width + 8,
+                                      game.splat.y * tile_size.height + 8);
         }
 
         switch (game.state) {
         case Game.State.DEAD:
             assets.aieee_bubble.draw (cr,
-                                      game.player.x * tile_width + 8,
-                                      game.player.y * tile_height + 4);
+                                      game.player.x * tile_size.width + 8,
+                                      game.player.y * tile_size.height + 4);
             break;
         case Game.State.COMPLETE:
             assets.yahoo_bubble.draw (cr,
-                                      game.player.x * tile_width + 8,
-                                      game.player.y * tile_height + 4);
+                                      game.player.x * tile_size.width + 8,
+                                      game.player.y * tile_size.height + 4);
             break;
         default:
             break;
@@ -191,17 +191,17 @@ public class GameArea : DrawingArea {
         return true;
     }
 
-    private void draw_object (int x, int y, ObjectType type, Context cr) {
+    private void draw_object (int x, int y, ObjectType type, Size tile_size, Context cr) {
         if ((x + y) % 2 != 0) {
             cairo_set_source_rgba (cr, dark_background);
         } else {
             cairo_set_source_rgba (cr, light_background);
         }
 
-        x *= tile_width;
-        y *= tile_height;
+        x *= tile_size.width;
+        y *= tile_size.height;
 
-        cr.rectangle (x, y, tile_width, tile_height);
+        cr.rectangle (x, y, tile_size.width, tile_size.height);
         cr.fill ();
 
         int animation = 0;
@@ -228,7 +228,7 @@ public class GameArea : DrawingArea {
 
         cr.save ();
         cr.translate (x, y);
-        theme.draw_object (type, animation, cr, tile_width, tile_height);
+        theme.draw_object (type, animation, cr, tile_size.width, tile_size.height);
         cr.restore ();
     }
 
@@ -277,8 +277,9 @@ public class GameArea : DrawingArea {
             {-1, 0}, {-1, -1}, {0, -1}, {1, -1},
             {1, 0}, {1, 1}, {0, 1}, {-1, 1}
         };
-        int x = ((int) (ix / tile_width)).clamp (0, game.arena.width);
-        int y = ((int) (iy / tile_height)).clamp (0, game.arena.height);
+        Size tile_size = tile_size ();
+        int x = ((int) (ix / tile_size.width)).clamp (0, game.arena.width);
+        int y = ((int) (iy / tile_size.height)).clamp (0, game.arena.height);
 
         /* If we click on our man then we assume we hold. */
         if ((x == game.player.x) && (y == game.player.y)) {
@@ -297,8 +298,8 @@ public class GameArea : DrawingArea {
         }
 
         /* Otherwise go in the general direction of the mouse click. */
-        double dx = ix - (game.player.x + 0.5) * tile_width;
-        double dy = iy - (game.player.y + 0.5) * tile_height;
+        double dx = ix - (game.player.x + 0.5) * tile_size.width;
+        double dy = iy - (game.player.y + 0.5) * tile_size.height;
 
         double angle = Math.atan2 (dy, dx);
 
@@ -416,13 +417,13 @@ public class GameArea : DrawingArea {
     private void message_box (string msg) {
         var window = get_toplevel () as Gtk.Window;
         if (window != null) {
-            var box = new Gtk.MessageDialog (window,
+            var dlg = new Gtk.MessageDialog (window,
                                              Gtk.DialogFlags.MODAL,
                                              Gtk.MessageType.INFO,
                                              Gtk.ButtonsType.OK,
                                              "%s", msg);
-            box.run ();
-            box.destroy ();
+            dlg.response.connect (() => dlg.destroy ());
+            dlg.present ();
         } else {
             warning ("There is no top level window.");
             info ("%s", msg);
diff --git a/src/properties-dialog.vala b/src/properties-dialog.vala
index e754efa..501aea9 100644
--- a/src/properties-dialog.vala
+++ b/src/properties-dialog.vala
@@ -20,37 +20,6 @@
 using Gtk;
 using Gdk;
 
-class GameConfigPicker : ComboBoxText {
-
-    private GameConfigs game_configs;
-
-    public signal void game_config_changed (GameConfig game_config);
-
-    public GameConfigPicker (GameConfigs game_configs, string current_config) {
-        Object ();
-        this.game_configs = game_configs;
-
-        int active_index = 0;
-        for (int i = 0; i < game_configs.count (); ++i) {
-            var config = game_configs[(uint)i];
-
-            var config_name = config.name ();
-            append_text (config_name);
-
-            if (config_name == current_config) {
-                active_index = i;
-            }
-        }
-        set_active (active_index);
-
-        changed.connect (() => {
-            var config_name = get_active_text ();
-            var game_config = game_configs.find_by_name (config_name);
-            game_config_changed(game_config);
-        });
-    }
-}
-
 class ThemePicker : ComboBox {
 
     private Themes themes;
@@ -98,43 +67,41 @@ public class PropertiesDialog : Dialog {
         Object (use_header_bar: 1,
                 title: _("Preferences"),
                 transient_for: parent,
-                modal: true,
-                border_width: 5);
+                modal: true);
         this.properties = properties;
 
-        get_content_area ().set_spacing (2);
-
         /* Set up notebook and add it to hbox of the gtk_dialog */
         var notebook = new Notebook ();
-        notebook.border_width = 5;
+        notebook.margin_top = 10;
+        notebook.margin_bottom = 10;
+        notebook.margin_start = 10;
+        notebook.margin_end = 10;
         get_content_area ().pack_start (notebook, true, true, 0);
 
         /* The configuration page */
-        var cpage = new Box (Orientation.VERTICAL, 18);
-        cpage.border_width = 12;
-
-        var grid = new Grid ();
-        grid.set_row_spacing (6);
-        grid.set_column_spacing (12);
-        cpage.pack_start (grid, false, false, 0);
+        var cpage = form_grid ();
 
         var label = new Label (_("Game Type"));
-        grid.attach (label, 0, 0, 1, 1);
+        cpage.attach (label, 0, 0, 1, 1);
+
+        var typemenu = create_game_config_picker (game_configs,
+                                                  properties.selected_config);
+        typemenu.changed.connect (() => {
+            properties.selected_config = typemenu.get_active_text ();
+        });
 
-        var typemenu = new GameConfigPicker (game_configs, properties.selected_config);
-        typemenu.game_config_changed.connect (game_config_changed);
-        grid.attach (typemenu, 1, 0, 1, 1);
+        cpage.attach (typemenu, 1, 0, 1, 1);
 
         var safe_chkbox = new CheckButton.with_mnemonic (_("_Use safe moves"));
         safe_chkbox.set_active (properties.safe_moves);
         safe_chkbox.set_tooltip_text (_("Prevent accidental moves that result in getting killed."));
-        grid.attach (safe_chkbox, 0, 1, 2, 1);
+        cpage.attach (safe_chkbox, 0, 1, 2, 1);
 
         var super_safe_chkbox = new CheckButton.with_mnemonic (_("U_se super safe moves"));
         super_safe_chkbox.set_active (properties.super_safe_moves);
         super_safe_chkbox.set_tooltip_text (_("Prevents all moves that result in getting killed."));
         super_safe_chkbox.set_sensitive (properties.safe_moves);
-        grid.attach (super_safe_chkbox, 0, 2, 2, 1);
+        cpage.attach (super_safe_chkbox, 0, 2, 2, 1);
 
         safe_chkbox.toggled.connect ((toggle) => {
             properties.safe_moves = toggle.active;
@@ -150,57 +117,47 @@ public class PropertiesDialog : Dialog {
             properties.sound = toggle.active;
         });
         sound_chkbox.set_tooltip_text (_("Play sounds for events like winning a level and dying."));
-        grid.attach (sound_chkbox, 0, 3, 2, 1);
+        cpage.attach (sound_chkbox, 0, 3, 2, 1);
 
         label = new Label.with_mnemonic (_("Game"));
         notebook.append_page (cpage, label);
 
         /* The graphics page */
-        var gpage = new Box (Orientation.VERTICAL, 18);
-        gpage.set_border_width (12);
-
-        grid = new Grid ();
-        grid.set_row_spacing (6);
-        grid.set_column_spacing (12);
-        gpage.pack_start (grid, false, false, 0);
+        var gpage = form_grid ();
 
         label = new Label.with_mnemonic (_("_Image theme:"));
         label.set_hexpand (true);
         label.set_halign (Align.START);
-        grid.attach (label, 0, 0, 1, 1);
+        gpage.attach (label, 0, 0, 1, 1);
 
         var theme_picker = new ThemePicker (themes, properties.theme);
         theme_picker.theme_changed.connect (theme_changed);
         label.set_mnemonic_widget (theme_picker);
-        grid.attach (theme_picker, 1, 0, 1, 1);
+        gpage.attach (theme_picker, 1, 0, 1, 1);
 
         label = new Label.with_mnemonic (_("_Background color:"));
         label.set_halign (Align.START);
-        grid.attach (label, 0, 1, 1, 1);
+        gpage.attach (label, 0, 1, 1, 1);
 
         var w = new ColorButton ();
         w.set_rgba (properties.bgcolour);
         w.color_set.connect((color) => bg_color_changed(color));
         label.set_mnemonic_widget (w);
-        grid.attach (w, 1, 1, 1, 1);
+        gpage.attach (w, 1, 1, 1, 1);
 
         label = new Label.with_mnemonic (_("Appearance"));
         notebook.append_page (gpage, label);
 
         /* The keyboard page */
-        var kpage = new Box (Orientation.VERTICAL, 18);
-        kpage.set_border_width (12);
-
-        var vbox = new Box (Orientation.VERTICAL, 6);
-        kpage.pack_start (vbox, true, true, 0);
+        var kpage = form_grid ();
 
         var controls_list = new GamesControlsList (properties);
+        controls_list.hexpand = true;
+        controls_list.vexpand = true;
+        kpage.attach (controls_list, 0, 0, 1, 1);
 
-        vbox.pack_start (controls_list, true, true, 0);
-
-        var hbox = new ButtonBox (Orientation.HORIZONTAL);
-        hbox.set_layout (ButtonBoxStyle.START);
-        vbox.pack_start (hbox, false, false, 0);
+        var hbox = new Box (Orientation.HORIZONTAL, 12);
+        kpage.attach (hbox, 0, 1, 1, 1);
 
         var dbut = new Button.with_mnemonic (_("_Restore Defaults"));
         dbut.clicked.connect (reset_keys);
@@ -210,10 +167,6 @@ public class PropertiesDialog : Dialog {
         notebook.append_page (kpage, label);
     }
 
-    private void game_config_changed (GameConfig game_config) {
-        properties.selected_config = game_config.name ();
-    }
-
     private void theme_changed (string theme_name) {
         /* FIXME: Should be de-suffixed. */
         properties.theme = theme_name;
@@ -236,9 +189,39 @@ public class PropertiesDialog : Dialog {
                                         game_configs,
                                         themes,
                                         properties);
+        dlg.response.connect (() => dlg.destroy ());
         dlg.show_all ();
-        dlg.run ();
-        dlg.destroy ();
     }
 }
 
+private Grid form_grid () {
+    var grid = new Grid ();
+    grid.row_spacing = 6;
+    grid.column_spacing = 12;
+    grid.margin_top = 12;
+    grid.margin_bottom = 12;
+    grid.margin_start = 12;
+    grid.margin_end = 12;
+    return grid;
+}
+
+private ComboBoxText create_game_config_picker (GameConfigs game_configs,
+                                                string current_config
+) {
+    var cb = new ComboBoxText ();
+
+    int active_index = 0;
+    for (int i = 0; i < game_configs.count (); ++i) {
+        var config = game_configs[(uint)i];
+
+        var config_name = config.name ();
+        cb.append_text (config_name);
+
+        if (config_name == current_config) {
+            active_index = i;
+        }
+    }
+    cb.set_active (active_index);
+
+    return cb;
+}


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