[gnome-robots] introduce Assets abstraction



commit f5d26931b77685255169259d066bbcadc02350d7
Author: Andrey Kutejko <andy128k gmail com>
Date:   Mon Sep 28 02:16:09 2020 +0200

    introduce Assets abstraction

 src/assets.vala    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/bubble.vala    |  6 ------
 src/game-area.vala | 47 ++++++++++++++++++++++-------------------------
 src/meson.build    |  1 +
 src/robots.vala    | 42 +++++++++---------------------------------
 src/themes.vala    | 14 --------------
 6 files changed, 83 insertions(+), 78 deletions(-)
---
diff --git a/src/assets.vala b/src/assets.vala
new file mode 100644
index 0000000..2a63817
--- /dev/null
+++ b/src/assets.vala
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2020 Andrey Kutejko <andy128k gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * For more details see the file COPYING.
+ */
+
+public interface Assets : Object {
+    public abstract Themes themes { get; }
+    public abstract Bubble aieee_bubble { get; }
+    public abstract Bubble yahoo_bubble { get; }
+    public abstract Bubble splat_bubble { get; }
+}
+
+public class DirectoryAssets : Object, Assets {
+    private Themes _themes;
+    public override Themes themes { get { return _themes; } }
+
+    private Bubble _aieee_bubble;
+    public override Bubble aieee_bubble { get { return _aieee_bubble; } }
+
+    private Bubble _yahoo_bubble;
+    public override Bubble yahoo_bubble { get { return _yahoo_bubble; } }
+
+    private Bubble _splat_bubble;
+    public override Bubble splat_bubble { get { return _splat_bubble; } }
+
+    public DirectoryAssets.from_directory (string directory) throws Error {
+        _themes = Themes.from_directory (
+            Path.build_filename (directory, "themes"));
+        _yahoo_bubble = new Bubble.from_file (
+            Path.build_filename (directory, "pixmaps", "yahoo.png"));
+        _aieee_bubble = new Bubble.from_file (
+            Path.build_filename (directory, "pixmaps", "aieee.png"));
+        _splat_bubble = new Bubble.from_file (
+            Path.build_filename (directory, "pixmaps", "splat.png"));
+    }
+}
+
diff --git a/src/bubble.vala b/src/bubble.vala
index 23801be..762f408 100644
--- a/src/bubble.vala
+++ b/src/bubble.vala
@@ -36,12 +36,6 @@ public class Bubble {
         pixbuf = new Pixbuf.from_file (filename);
     }
 
-    public Bubble.from_data_file (string filename) throws Error {
-        this.from_file (
-            GLib.Path.build_filename (DATA_DIRECTORY, "pixmaps", filename)
-        );
-    }
-
     public void draw (Context cr, int x, int y) {
         int clip_x = x < width ? x : x - width;
         int clip_y = y < height ? y : y - height;
diff --git a/src/game-area.vala b/src/game-area.vala
index b672694..3490399 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -34,12 +34,10 @@ public class GameArea : DrawingArea {
     private EventControllerMotion motion_controller;
     private Game game;
     private GameConfigs game_configs;
+    private Assets assets;
     private Theme _theme;
     private RGBA light_background;
     private RGBA dark_background;
-    private Bubble aieee_bubble;
-    private Bubble yahoo_bubble;
-    private Bubble splat_bubble;
 
     private uint timer_id;
     private Animated player_animation;
@@ -86,23 +84,20 @@ public class GameArea : DrawingArea {
 
     public GameArea (Game game,
                      GameConfigs game_configs,
-                     Theme theme,
-                     Bubble aieee_bubble,
-                     Bubble yahoo_bubble,
-                     Bubble splat_bubble,
+                     Assets assets,
                      SoundPlayer sound_player,
                      Properties properties
-    ) {
+    ) throws Error {
         this.game = game;
         this.game_configs = game_configs;
-        this.theme = theme;
-        this.background_color_name = "#7590AE";
-        this.aieee_bubble = aieee_bubble;
-        this.yahoo_bubble = yahoo_bubble;
-        this.splat_bubble = splat_bubble;
+        this.assets = assets;
+        this.background_color = properties.bgcolour;
+        this._theme = assets.themes.find_best_match (properties.theme);
         this.sound_player = sound_player;
         this.properties = properties;
 
+        game.config = game_configs.find_by_name (properties.selected_config);
+
         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));
@@ -145,6 +140,8 @@ public class GameArea : DrawingArea {
         game.game_event.connect (on_game_event);
 
         properties.changed.connect (properties_changed_cb);
+
+        start_new_game ();
     }
 
     ~GameArea () {
@@ -171,21 +168,21 @@ public class GameArea : DrawingArea {
         }
 
         if (game.splat != null) {
-            splat_bubble.draw (cr,
-                               game.splat.x * tile_width + 8,
-                               game.splat.y * tile_height + 8);
+            assets.splat_bubble.draw (cr,
+                                      game.splat.x * tile_width + 8,
+                                      game.splat.y * tile_height + 8);
         }
 
         switch (game.state) {
         case Game.State.DEAD:
-            aieee_bubble.draw (cr,
-                               game.player.x * tile_width + 8,
-                               game.player.y * tile_height + 4);
+            assets.aieee_bubble.draw (cr,
+                                      game.player.x * tile_width + 8,
+                                      game.player.y * tile_height + 4);
             break;
         case Game.State.COMPLETE:
-            yahoo_bubble.draw (cr,
-                               game.player.x * tile_width + 8,
-                               game.player.y * tile_height + 4);
+            assets.yahoo_bubble.draw (cr,
+                                      game.player.x * tile_width + 8,
+                                      game.player.y * tile_height + 4);
             break;
         default:
             break;
@@ -318,11 +315,11 @@ public class GameArea : DrawingArea {
 
         if (theme.name != properties.theme) {
             try {
-                theme = themes.find_best_match (properties.theme);
+                theme = assets.themes.find_best_match (properties.theme);
             } catch (Error e) {
                 warning ("Cannot change theme to %s: %s",
-                    properties.theme,
-                    e.message);
+                         properties.theme,
+                         e.message);
             }
         }
 
diff --git a/src/meson.build b/src/meson.build
index 771de4c..929e551 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -25,6 +25,7 @@ sources = files(
     'graphics.vala',
     'arena.vala',
     'game.vala',
+    'assets.vala',
     'game-area.vala',
     'bubble.vala',
     'window-size.vala',
diff --git a/src/robots.vala b/src/robots.vala
index 87d9a33..81da7a6 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -34,10 +34,7 @@ public class RobotsWindow : ApplicationWindow {
     public RobotsWindow (Gtk.Application app,
                          Properties properties,
                          GameConfigs game_configs,
-                         Themes themes,
-                         Bubble yahoo_bubble,
-                         Bubble aieee_bubble,
-                         Bubble splat_bubble,
+                         Assets assets,
                          SoundPlayer sound_player
     ) throws Error {
         Object (application: app);
@@ -63,16 +60,10 @@ public class RobotsWindow : ApplicationWindow {
         };
         add_action_entries (win_entries, this);
 
-        Theme theme = themes.find_best_match (properties.theme);
-        properties.theme = theme.name;
-
         var game = new Game ();
         game_area = new GameArea (game,
                                   game_configs,
-                                  theme,
-                                  aieee_bubble,
-                                  yahoo_bubble,
-                                  splat_bubble,
+                                  assets,
                                   sound_player,
                                   properties);
         game_area.updated.connect (game => update_game_status (game));
@@ -103,12 +94,7 @@ public class RobotsWindow : ApplicationWindow {
             });
         });
 
-        game_area.background_color = properties.bgcolour;
-
         keyboard_set (properties.keys);
-
-        var game_config = game_configs.find_by_name (properties.selected_config);
-        game_area.set_game_config (game_config);
     }
 
     private Box button_box () {
@@ -270,10 +256,7 @@ class RobotsApplication : Gtk.Application {
     private Properties properties;
 
     private GameConfigs game_configs;
-    private Themes themes;
-    private Bubble yahoo_bubble;
-    private Bubble aieee_bubble;
-    private Bubble splat_bubble;
+    private Assets assets;
     private SoundPlayer sound_player;
 
     public RobotsApplication () {
@@ -286,10 +269,6 @@ class RobotsApplication : Gtk.Application {
         base.startup ();
 
         Environment.set_application_name (_("Robots"));
-
-        var settings = new GLib.Settings ("org.gnome.Robots");
-        properties = new Properties (settings);
-
         Window.set_default_icon_name ("org.gnome.Robots");
 
         GLib.ActionEntry[] app_entries = {
@@ -309,11 +288,8 @@ class RobotsApplication : Gtk.Application {
         make_cursors ();
 
         try {
+            assets = new DirectoryAssets.from_directory (DATA_DIRECTORY);
             game_configs = new GameConfigs.load ();
-            themes = get_themes ();
-            yahoo_bubble = new Bubble.from_data_file ("yahoo.png");
-            aieee_bubble = new Bubble.from_data_file ("aieee.png");
-            splat_bubble = new Bubble.from_data_file ("splat.png");
             sound_player = new SoundPlayer ();
         } catch (Error e) {
             critical ("%s", e.message);
@@ -330,6 +306,9 @@ class RobotsApplication : Gtk.Application {
 
             quit ();
         }
+
+        var settings = new GLib.Settings ("org.gnome.Robots");
+        properties = new Properties (settings);
     }
 
     protected override void activate () {
@@ -343,10 +322,7 @@ class RobotsApplication : Gtk.Application {
             window = new RobotsWindow (this,
                                        properties,
                                        game_configs,
-                                       themes,
-                                       yahoo_bubble,
-                                       aieee_bubble,
-                                       splat_bubble,
+                                       assets,
                                        sound_player);
         } catch (Error e) {
             critical ("%s", e.message);
@@ -393,7 +369,7 @@ class RobotsApplication : Gtk.Application {
     private void preferences_cb () {
         PropertiesDialog.show_dialog (get_active_window (),
                                       game_configs,
-                                      themes,
+                                      assets.themes,
                                       properties);
     }
 
diff --git a/src/themes.vala b/src/themes.vala
index 07ce5d4..39aa4a6 100644
--- a/src/themes.vala
+++ b/src/themes.vala
@@ -17,15 +17,6 @@
  * For more details see the file COPYING.
  */
 
-public Themes themes = null;
-
-public Themes get_themes () {
-    if (themes == null) {
-        themes = Themes.from_data_directory ();
-    }
-    return themes;
-}
-
 public class Themes : Gtk.ListStore {
 
     public enum Column {
@@ -69,11 +60,6 @@ public class Themes : Gtk.ListStore {
         return themes;
     }
 
-    public static Themes from_data_directory () {
-        var directory = GLib.Path.build_filename (DATA_DIRECTORY, "themes");
-        return from_directory (directory);
-    }
-
     public Gtk.TreeIter? find_iter_by_name (string name) {
         Gtk.TreeIter iter;
         if (get_iter_first (out iter)) {


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