[gnome-robots] Make current theme a property of GameArea



commit acc6674b97dcbdce00f67b0407fab9e71f483f01
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sat Sep 12 22:37:21 2020 +0200

    Make current theme a property of GameArea

 src/game-area.vala  | 14 +++++++++++++-
 src/graphics.vala   | 23 +----------------------
 src/properties.vala | 11 ++++-------
 src/robots.vala     | 21 +++++++++++++++++----
 4 files changed, 35 insertions(+), 34 deletions(-)
---
diff --git a/src/game-area.vala b/src/game-area.vala
index 8bfef4c..77f67a8 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -29,14 +29,26 @@ public class GameArea : DrawingArea {
     private GestureMultiPress click_controller;
     private EventControllerMotion motion_controller;
     private Game game;
+    private Theme _theme;
 
     private Animated player_animation;
     private Animated player_dead_animation;
     private Animated robot1_animation;
     private Animated robot2_animation;
 
-    public GameArea (Game game) {
+    public Theme theme {
+        get {
+            return _theme;
+        }
+        set {
+            _theme = value;
+            queue_draw ();
+        }
+    }
+
+    public GameArea (Game game, Theme theme) {
         this.game = game;
+        this.theme = theme;
 
         add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | 
Gdk.EventMask.POINTER_MOTION_MASK);
         configure_event.connect (event => resize_cb (event));
diff --git a/src/graphics.vala b/src/graphics.vala
index 2aff851..1007fe5 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -42,8 +42,6 @@ public const int BUBBLE_YOFFSET = 4;
 public int tile_width = 0;
 public int tile_height = 0;
 
-Theme theme = null;
-
 RGBA light_background;
 RGBA dark_background;
 
@@ -57,10 +55,7 @@ int bubble_xo = 0;
 int bubble_yo = 0;
 BubbleType bubble_type = BubbleType.NONE;
 
-/**
- * Loads all of the 'speech bubble' graphics
- **/
-void load_bubble_graphics () throws Error {
+public void load_game_graphics () throws Error {
     yahoo_pixbuf = new Pixbuf.from_file (
         GLib.Path.build_filename (DATA_DIRECTORY, "pixmaps", "yahoo.png"));
     aieee_pixbuf = new Pixbuf.from_file (
@@ -69,22 +64,6 @@ void load_bubble_graphics () throws Error {
         GLib.Path.build_filename (DATA_DIRECTORY, "pixmaps", "splat.png"));
 }
 
-
-/**
- * load_game_graphics
- *
- * Description:
- * Loads all of the game graphics
- *
- * Returns:
- * TRUE on success FALSE otherwise
- **/
-public void load_game_graphics (string theme_path) throws Error {
-    theme = new Theme.from_file (theme_path);
-
-    load_bubble_graphics ();
-}
-
 public void set_background_color (RGBA color) {
     if (game_area == null)
         return;
diff --git a/src/properties.vala b/src/properties.vala
index 3e93a18..6ec4c50 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -76,7 +76,8 @@ void pmap_selection (ComboBox combo) {
         conf_set_theme (properties.themename);
 
         try {
-            load_game_graphics (theme_path);
+            game_area.theme = new Theme.from_file (theme_path);
+            load_game_graphics ();
         } catch (Error e) {
             // TODO
         }
@@ -316,18 +317,14 @@ public void load_properties () {
     properties.show_toolbar     = settings.get_boolean (KEY_SHOW_TOOLBAR);
 }
 
-public void apply_properties () throws Error {
-    set_background_color (properties.bgcolour);
-
+public Theme get_theme_from_properties () throws Error {
     var themes = get_themes ();
     var iter = themes.find_best_match (properties.themename);
 
     string theme_path;
     themes.get_values (iter, out properties.themename, out theme_path);
 
-    load_game_graphics (theme_path);
-
-    keyboard_set (properties.keys);
+    return new Theme.from_file (theme_path);
 }
 
 RGBA string_to_rgba (string color) {
diff --git a/src/robots.vala b/src/robots.vala
index 8ce3837..4c74798 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -270,6 +270,8 @@ bool keyboard_cb (EventControllerKey controller, uint keyval, uint keycode, Gdk.
 }
 
 void activate (Gtk.Application app) {
+    load_properties ();
+
     if (window != null) {
         window.present_with_time (get_current_event_time ());
         return;
@@ -300,7 +302,16 @@ void activate (Gtk.Application app) {
 
     window.add_action_entries (win_entries, app);
 
-    game_area = new GameArea (game);
+    Theme theme = null;
+    try {
+        theme = get_theme_from_properties ();
+    } catch (Error e) {
+        // error ("%s", e.message);
+        // TODO message box
+        app.quit ();
+    }
+
+    game_area = new GameArea (game, theme);
     game_area.destroy.connect (() => game_area = null);
 
     var gridframe = new Games.GridFrame (GAME_WIDTH, GAME_HEIGHT);
@@ -380,10 +391,12 @@ void activate (Gtk.Application app) {
         app.quit ();
     }
 
-    load_properties ();
-
     try {
-        apply_properties ();
+        set_background_color (properties.bgcolour);
+
+        load_game_graphics ();
+
+        keyboard_set (properties.keys);
     } catch (Error e) {
         // error ("%s", e.message);
         // TODO message box


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