[iagno/wip/arnaudb/add-texture] Add a texture.



commit d61c2811418eb02be19c82faf670a575b1008cc9
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Feb 23 16:09:44 2019 +0100

    Add a texture.

 data/iagno.gresource.xml                   |   1 +
 data/noise.png                             | Bin 0 -> 11471 bytes
 data/noise.xcf                             | Bin 0 -> 22452 bytes
 data/themes/adwaita.theme.desktop.in       |   1 +
 data/themes/classic.theme.desktop.in       |   1 +
 data/themes/high_contrast.theme.desktop.in |   1 +
 data/themes/sun_and_star.theme.desktop.in  |   1 +
 src/game-view.vala                         |  55 +++++++++++++++++++++++++++--
 8 files changed, 57 insertions(+), 3 deletions(-)
---
diff --git a/data/iagno.gresource.xml b/data/iagno.gresource.xml
index 3ac073e..eb96659 100644
--- a/data/iagno.gresource.xml
+++ b/data/iagno.gresource.xml
@@ -5,5 +5,6 @@
     <file preprocess="xml-stripblanks" compressed="true" alias="themes.ui">ui/iagno-themes.ui</file>
     <file preprocess="xml-stripblanks" compressed="true" alias="iagno-screens.ui">ui/iagno-screens.ui</file>
     <file compressed="true">iagno.css</file>
+    <file>noise.png</file>
   </gresource>
 </gresources>
diff --git a/data/noise.png b/data/noise.png
new file mode 100644
index 0000000..94ebae6
Binary files /dev/null and b/data/noise.png differ
diff --git a/data/noise.xcf b/data/noise.xcf
new file mode 100644
index 0000000..43efcdb
Binary files /dev/null and b/data/noise.xcf differ
diff --git a/data/themes/adwaita.theme.desktop.in b/data/themes/adwaita.theme.desktop.in
index 2422f26..abc5814 100644
--- a/data/themes/adwaita.theme.desktop.in
+++ b/data/themes/adwaita.theme.desktop.in
@@ -14,6 +14,7 @@ Green=0.4
 Blue=0.6
 # Radius=9
 Radius=2
+TextureAlpha=0.25
 
 [Mark]
 Red=0.2
diff --git a/data/themes/classic.theme.desktop.in b/data/themes/classic.theme.desktop.in
index 4b93ed1..caf0e22 100644
--- a/data/themes/classic.theme.desktop.in
+++ b/data/themes/classic.theme.desktop.in
@@ -13,6 +13,7 @@ Red=0.2
 Green=0.6
 Blue=0.4
 Radius=2
+TextureAlpha=0.25
 
 [Mark]
 Red=0.5
diff --git a/data/themes/high_contrast.theme.desktop.in b/data/themes/high_contrast.theme.desktop.in
index 413a8f4..8cc83e7 100644
--- a/data/themes/high_contrast.theme.desktop.in
+++ b/data/themes/high_contrast.theme.desktop.in
@@ -14,6 +14,7 @@ Green=1.0
 Blue=1.0
 # Radius=9
 Radius=0
+TextureAlpha=0.0
 
 [Mark]
 Red=0.4
diff --git a/data/themes/sun_and_star.theme.desktop.in b/data/themes/sun_and_star.theme.desktop.in
index 31402d4..22b8e5b 100644
--- a/data/themes/sun_and_star.theme.desktop.in
+++ b/data/themes/sun_and_star.theme.desktop.in
@@ -13,6 +13,7 @@ Red=0.2
 Green=0.6
 Blue=0.4
 Radius=2
+TextureAlpha=0.25
 
 [Mark]
 Red=0.2
diff --git a/src/game-view.vala b/src/game-view.vala
index c0473b1..cbeaddc 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -38,6 +38,9 @@ private class GameView : Gtk.DrawingArea
     private double background_blue = 0.4;
     private int background_radius = 0;
 
+    private double texture_alpha = 0.25;
+    private bool   apply_texture = false;
+
     private double mark_red = 0.2;
     private double mark_green = 0.6;
     private double mark_blue = 0.4;
@@ -82,6 +85,10 @@ private class GameView : Gtk.DrawingArea
     private Cairo.Pattern? tiles_pattern = null;
     private Cairo.Pattern? scoreboard_tiles_pattern = null;
 
+    private bool noise_pixbuf_loaded = false;
+    private Gdk.Pixbuf? noise_pixbuf = null;
+    private Cairo.Pattern? noise_pattern = null;
+
     /* The images being showed on each location */
     private int [,] pixmaps;
 
@@ -212,6 +219,9 @@ private class GameView : Gtk.DrawingArea
             background_blue   = key.get_double  ("Background", "Blue");
             background_radius = key.get_integer ("Background", "Radius");
 
+            texture_alpha     = key.get_double  ("Background", "TextureAlpha");
+            apply_texture     = (texture_alpha > 0.0) && (texture_alpha <= 1.0);
+
             mark_red          = key.get_double  ("Mark", "Red");
             mark_green        = key.get_double  ("Mark", "Green");
             mark_blue         = key.get_double  ("Mark", "Blue");
@@ -271,10 +281,40 @@ private class GameView : Gtk.DrawingArea
         if (tiles_pattern == null || render_size != tile_size)
         {
             render_size = tile_size;
-            var surface = new Cairo.Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, tile_size 
* 8, tile_size * 4);
-            var c = new Cairo.Context (surface);
-            load_image (c, tile_size * 8, tile_size * 4);
+
+            Cairo.Surface surface;
+            Cairo.Context context;
+
+            surface = new Cairo.Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, tile_size * 8,
+                                                                                              tile_size * 4);
+            context = new Cairo.Context (surface);
+            load_image (context, tile_size * 8, tile_size * 4);
             tiles_pattern = new Cairo.Pattern.for_surface (surface);
+
+            if (apply_texture)
+            {
+                try
+                {
+                    noise_pixbuf = new Gdk.Pixbuf.from_resource_at_scale ("/org/gnome/Reversi/ui/noise.png",
+                                                                          /* x */ tile_size,
+                                                                          /* y */ tile_size,
+                                                                          /* preserve aspect ratio */ false);
+                }
+                catch (Error e) { warning (e.message); }
+                noise_pixbuf_loaded = noise_pixbuf != null;
+                if (noise_pixbuf_loaded)
+                {
+                    surface = new Cairo.Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, 
tile_size,
+                                                                                                      
tile_size);
+                    context = new Cairo.Context (surface);
+                    Gdk.cairo_set_source_pixbuf (context, (!) noise_pixbuf, 0, 0);
+                    context.paint_with_alpha (texture_alpha);
+                    // or  surface = Gdk.cairo_surface_create_from_pixbuf ((!) noise_pixbuf, 0, null); ?
+
+                    noise_pattern = new Cairo.Pattern.for_surface (surface);
+                    // ((!) noise_pattern).set_extend (Cairo.Extend.REPEAT);
+                }
+            }
         }
 
         cr.translate (board_x, board_y);
@@ -298,6 +338,15 @@ private class GameView : Gtk.DrawingArea
                 /* draw background */
                 cr.set_source_rgba (background_red, background_green, background_blue, 1.0);
                 rounded_square (cr, tile_x, tile_y, tile_size, 0, background_radius);
+                if (apply_texture && noise_pixbuf_loaded)
+                {
+                    cr.fill_preserve ();
+
+                    var matrix = Cairo.Matrix.identity ();
+                    matrix.translate (-tile_x, -tile_y);
+                    ((!) noise_pattern).set_matrix (matrix);
+                    cr.set_source ((!) noise_pattern);
+                }
                 cr.fill ();
 
                 if ((highlight_x == x && highlight_y == y)


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