[gnome-robots] Make background color a property of GameArea



commit 3454660bfc5252bafc9fb9c4dad265dcdb504ea4
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sat Sep 12 22:54:56 2020 +0200

    Make background color a property of GameArea

 src/game-area.vala  | 24 ++++++++++++++++++++++++
 src/graphics.vala   | 41 +++++++++++++----------------------------
 src/properties.vala |  4 ++--
 src/robots.vala     |  2 +-
 4 files changed, 40 insertions(+), 31 deletions(-)
---
diff --git a/src/game-area.vala b/src/game-area.vala
index 77f67a8..c0eb71a 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -30,6 +30,8 @@ public class GameArea : DrawingArea {
     private EventControllerMotion motion_controller;
     private Game game;
     private Theme _theme;
+    private RGBA light_background;
+    private RGBA dark_background;
 
     private Animated player_animation;
     private Animated player_dead_animation;
@@ -46,9 +48,31 @@ public class GameArea : DrawingArea {
         }
     }
 
+    public RGBA background_color {
+        get { return dark_background; }
+        set {
+            dark_background = value;
+            light_background = calculate_light_color (value);
+            queue_draw ();
+        }
+    }
+
+    public string background_color_name {
+        owned get {
+            return rgba_to_string (dark_background);
+        }
+        set {
+            RGBA color = RGBA ();
+            if (color.parse (name)) {
+                background_color = color;
+            }
+        }
+    }
+
     public GameArea (Game game, Theme theme) {
         this.game = game;
         this.theme = theme;
+        this.background_color_name = "#7590AE";
 
         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 1007fe5..b058bd8 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -42,9 +42,6 @@ public const int BUBBLE_YOFFSET = 4;
 public int tile_width = 0;
 public int tile_height = 0;
 
-RGBA light_background;
-RGBA dark_background;
-
 Pixbuf aieee_pixbuf = null;
 Pixbuf yahoo_pixbuf = null;
 Pixbuf splat_pixbuf = null;
@@ -64,42 +61,30 @@ public void load_game_graphics () throws Error {
         GLib.Path.build_filename (DATA_DIRECTORY, "pixmaps", "splat.png"));
 }
 
-public void set_background_color (RGBA color) {
-    if (game_area == null)
-        return;
-
+public RGBA calculate_light_color (RGBA color) {
     /* While the two colours are labelled "light" and "dark" which one is
      * which actually depends on how light or dark the base colour is. */
 
+    RGBA light = RGBA ();
     double brightness = color.red + color.green + color.blue;
     if (brightness > (1.0 / 1.1)) {
         /* Darken light colours. */
-        light_background.red = 0.9 * color.red;
-        light_background.green = 0.9 * color.green;
-        light_background.blue = 0.9 * color.blue;
+        light.red = 0.9 * color.red;
+        light.green = 0.9 * color.green;
+        light.blue = 0.9 * color.blue;
     } else if (brightness > 0.04) {
         /* Lighten darker colours. */
-        light_background.red = 1.1 * color.red;
-        light_background.green = 1.1 * color.green;
-        light_background.blue = 1.1 * color.blue;
+        light.red = 1.1 * color.red;
+        light.green = 1.1 * color.green;
+        light.blue = 1.1 * color.blue;
     } else {
         /* Very dark colours, add rather than multiply. */
-        light_background.red += 0.04;
-        light_background.green += 0.04;
-        light_background.blue += 0.04;
-    }
-    light_background.alpha = 1.0;
-    dark_background = color;
-
-    clear_game_area ();
-}
-
-public void set_background_color_from_name (string name) {
-    RGBA color = RGBA ();
-    if (!color.parse (name)) {
-        color.parse ("#7590AE");
+        light.red = 0.04 + color.red;
+        light.green = 0.04 + color.green;
+        light.blue = 0.04 + color.blue;
     }
-    set_background_color (color);
+    light.alpha = 1.0;
+    return light;
 }
 
 /**
diff --git a/src/properties.vala b/src/properties.vala
index 6ec4c50..7c3546f 100644
--- a/src/properties.vala
+++ b/src/properties.vala
@@ -145,7 +145,7 @@ ComboBox create_theme_picker (Themes themes, string current_theme) {
 
 void bg_color_callback (ColorChooser color_chooser) {
     properties.bgcolour = color_chooser.get_rgba ();
-    set_background_color (properties.bgcolour);
+    game_area.background_color = properties.bgcolour;
     clear_game_area ();
     conf_set_background_color (properties.bgcolour);
 }
@@ -333,7 +333,7 @@ RGBA string_to_rgba (string color) {
     return rgba;
 }
 
-string rgba_to_string (RGBA color) {
+public string rgba_to_string (RGBA color) {
     return "#%04x%04x%04x".printf (
         (int) (color.red * 65535 + 0.5),
         (int) (color.green * 65535 + 0.5),
diff --git a/src/robots.vala b/src/robots.vala
index 4c74798..b1594a1 100644
--- a/src/robots.vala
+++ b/src/robots.vala
@@ -392,7 +392,7 @@ void activate (Gtk.Application app) {
     }
 
     try {
-        set_background_color (properties.bgcolour);
+        game_area.background_color = properties.bgcolour;
 
         load_game_graphics ();
 


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