[gnome-robots] Make a class to represent a bubble



commit f13e7ad19b418c7931bf767a055afc420deb8b6d
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Sep 13 13:14:15 2020 +0200

    Make a class to represent a bubble

 src/bubble.vala   | 54 +++++++++++++++++++++++++++++++++++++++
 src/graphics.vala | 75 +++++++++++++++++++------------------------------------
 src/meson.build   |  1 +
 3 files changed, 81 insertions(+), 49 deletions(-)
---
diff --git a/src/bubble.vala b/src/bubble.vala
new file mode 100644
index 0000000..23801be
--- /dev/null
+++ b/src/bubble.vala
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+using Gtk;
+using Gdk;
+using Cairo;
+
+public class Bubble {
+    private Pixbuf pixbuf;
+
+    public int width {
+        get { return pixbuf.width / 2; }
+    }
+
+    public int height {
+        get { return pixbuf.height / 2; }
+    }
+
+    public Bubble.from_file (string filename) throws Error {
+        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;
+
+        cairo_set_source_pixbuf (cr, pixbuf, x - width, y - height);
+        cr.rectangle (clip_x, clip_y, width, height);
+        cr.fill ();
+    }
+}
+
diff --git a/src/graphics.vala b/src/graphics.vala
index b058bd8..072a15e 100644
--- a/src/graphics.vala
+++ b/src/graphics.vala
@@ -42,23 +42,18 @@ public const int BUBBLE_YOFFSET = 4;
 public int tile_width = 0;
 public int tile_height = 0;
 
-Pixbuf aieee_pixbuf = null;
-Pixbuf yahoo_pixbuf = null;
-Pixbuf splat_pixbuf = null;
+Bubble aieee_bubble = null;
+Bubble yahoo_bubble = null;
+Bubble splat_bubble = null;
 
 int bubble_xpos = 0;
 int bubble_ypos = 0;
-int bubble_xo = 0;
-int bubble_yo = 0;
 BubbleType bubble_type = BubbleType.NONE;
 
 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 (
-        GLib.Path.build_filename (DATA_DIRECTORY, "pixmaps", "aieee.png"));
-    splat_pixbuf = new Pixbuf.from_file (
-        GLib.Path.build_filename (DATA_DIRECTORY, "pixmaps", "splat.png"));
+    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");
 }
 
 public RGBA calculate_light_color (RGBA color) {
@@ -104,45 +99,16 @@ public void draw_bubble (Context cr) {
     if (bubble_type == BubbleType.NONE)
         return;
 
-    Pixbuf pmap;
+    Bubble bubble;
     if (bubble_type == BubbleType.YAHOO) {
-        pmap = yahoo_pixbuf;
+        bubble = yahoo_bubble;
     } else if (bubble_type == BubbleType.AIEEE) {
-        pmap = aieee_pixbuf;
+        bubble = aieee_bubble;
     } else {
-        pmap = splat_pixbuf;
+        bubble = splat_bubble;
     }
 
-    cairo_set_source_pixbuf (cr, pmap, bubble_xpos - bubble_xo, bubble_ypos - bubble_yo);
-    cr.rectangle (bubble_xpos, bubble_ypos, BUBBLE_WIDTH, BUBBLE_HEIGHT);
-    cr.fill ();
-}
-
-/**
- * add_bubble
- * @x: x position
- * @y: y position
- *
- * Description:
- * adds a bubble at @x,@y
- **/
-void add_bubble (BubbleType type, int x, int y) {
-    bubble_type = type;
-    bubble_xpos = x * tile_width - BUBBLE_WIDTH + BUBBLE_XOFFSET;
-    bubble_ypos = y * tile_height - BUBBLE_HEIGHT + BUBBLE_YOFFSET;
-
-    bubble_xo = 0;
-    bubble_yo = 0;
-
-    if (bubble_ypos < 0) {
-        bubble_yo = BUBBLE_HEIGHT;
-        bubble_ypos += BUBBLE_HEIGHT;
-    }
-    if (bubble_xpos < 0) {
-        bubble_xo = BUBBLE_WIDTH;
-        bubble_xpos += BUBBLE_WIDTH;
-    }
-    game_area.queue_draw ();
+    bubble.draw (cr, bubble_xpos, bubble_ypos);
 }
 
 /**
@@ -180,7 +146,11 @@ public void remove_splat_bubble () {
  * adds and "Yahoo" bubble at @x,@y
  **/
 public void add_yahoo_bubble (int x, int y) {
-    add_bubble (BubbleType.YAHOO, x, y);
+    bubble_type = BubbleType.YAHOO;
+    bubble_xpos = x * tile_width + BUBBLE_XOFFSET;
+    bubble_ypos = y * tile_height + BUBBLE_YOFFSET;
+
+    game_area.queue_draw ();
 }
 
 
@@ -193,7 +163,11 @@ public void add_yahoo_bubble (int x, int y) {
  * adds and "Aieee" bubble at @x,@y
  **/
 public void add_aieee_bubble (int x, int y) {
-    add_bubble (BubbleType.AIEEE, x, y);
+    bubble_type = BubbleType.AIEEE;
+    bubble_xpos = x * tile_width + BUBBLE_XOFFSET;
+    bubble_ypos = y * tile_height + BUBBLE_YOFFSET;
+
+    game_area.queue_draw ();
 }
 
 /**
@@ -205,7 +179,10 @@ public void add_aieee_bubble (int x, int y) {
  * adds a "Splat" speech bubble at @x,@y
  **/
 public void add_splat_bubble (int x, int y) {
-    add_bubble (BubbleType.SPLAT, x, y);
-    bubble_ypos += BUBBLE_YOFFSET;
+    bubble_type = BubbleType.SPLAT;
+    bubble_xpos = x * tile_width + BUBBLE_XOFFSET;
+    bubble_ypos = y * tile_height + 2 * BUBBLE_YOFFSET;
+
+    game_area.queue_draw ();
 }
 
diff --git a/src/meson.build b/src/meson.build
index 3cf36ee..730bfa8 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -25,6 +25,7 @@ sources = files(
     'arena.vala',
     'game.vala',
     'game-area.vala',
+    'bubble.vala',
     'robots.vala',
 )
 


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