[gnome-robots] Make cursors part of Asset interface



commit f3ce01e43e9e5e22b2466f1a809cccfaf60053ea
Author: Andrey Kutejko <andy128k gmail com>
Date:   Sun Oct 4 15:28:30 2020 +0200

    Make cursors part of Asset interface

 src/application.vala |   2 -
 src/assets.vala      |   8 ++
 src/cursors.vala     | 254 ++++++++++++++++++++++++---------------------------
 src/game-area.vala   |   7 +-
 4 files changed, 133 insertions(+), 138 deletions(-)
---
diff --git a/src/application.vala b/src/application.vala
index d056b64..1824b50 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -53,8 +53,6 @@ class RobotsApplication : Gtk.Application {
         set_accels_for_action ("app.help", { "F1" });
         set_accels_for_action ("app.quit", { "<Primary>q" });
 
-        make_cursors ();
-
         try {
             assets = new DirectoryAssets.from_directory (DATA_DIRECTORY);
             game_configs = new GameConfigs.load ();
diff --git a/src/assets.vala b/src/assets.vala
index 2a63817..b3ecf03 100644
--- a/src/assets.vala
+++ b/src/assets.vala
@@ -22,6 +22,7 @@ public interface Assets : Object {
     public abstract Bubble aieee_bubble { get; }
     public abstract Bubble yahoo_bubble { get; }
     public abstract Bubble splat_bubble { get; }
+    public abstract Array<Gdk.Cursor> cursors { get; }
 }
 
 public class DirectoryAssets : Object, Assets {
@@ -37,15 +38,22 @@ public class DirectoryAssets : Object, Assets {
     private Bubble _splat_bubble;
     public override Bubble splat_bubble { get { return _splat_bubble; } }
 
+    private Array<Gdk.Cursor> _cursors;
+    public override Array<Gdk.Cursor> cursors { get { return _cursors; } }
+
     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"));
+
+        var display = Gdk.Display.get_default ();
+        _cursors = make_cursors_for_display (display);
     }
 }
 
diff --git a/src/cursors.vala b/src/cursors.vala
index b4b94ca..f90d112 100644
--- a/src/cursors.vala
+++ b/src/cursors.vala
@@ -19,38 +19,54 @@
 
 using Gdk;
 
-Cursor make_cursor (Display display, string[] xpm, int hsx, int hsy) {
-    var pixbuf = new Pixbuf.from_xpm_data (xpm);
-    return new Cursor.from_pixbuf (display, pixbuf, hsx, hsy);
-}
-
-Cursor default_cursor;
-Cursor cursors[9];
-
-public void make_cursors () {
-    var display = Display.get_default ();
-
-    default_cursor = new Cursor.for_display (display, CursorType.LEFT_PTR);
-
-    cursors[0] = make_cursor (display, cursor_up_left, 3, 3);
-    cursors[1] = make_cursor (display, cursor_up, 10, 3);
-    cursors[2] = make_cursor (display, cursor_up_right, 17, 3);
-    cursors[3] = make_cursor (display, cursor_left, 3, 10);
-    cursors[4] = make_cursor (display, cursor_hold, 10, 10);
-    cursors[5] = make_cursor (display, cursor_right, 17, 10);
-    cursors[6] = make_cursor (display, cursor_down_left, 3, 17);
-    cursors[7] = make_cursor (display, cursor_down, 10, 17);
-    cursors[8] = make_cursor (display, cursor_down_right, 17, 17);
+public Array<Cursor> make_cursors_for_display (Display display) {
+    Array<Cursor> cursors = new Array<Cursor> ();
+    cursors.append_val (make_cursor (display, cursor_up_left, 3, 3));
+    cursors.append_val (make_cursor (display, cursor_up, 10, 3));
+    cursors.append_val (make_cursor (display, cursor_up_right, 17, 3));
+    cursors.append_val (make_cursor (display, cursor_left, 3, 10));
+    cursors.append_val (make_cursor (display, cursor_hold, 10, 10));
+    cursors.append_val (make_cursor (display, cursor_right, 17, 10));
+    cursors.append_val (make_cursor (display, cursor_down_left, 3, 17));
+    cursors.append_val (make_cursor (display, cursor_down, 10, 17));
+    cursors.append_val (make_cursor (display, cursor_down_right, 17, 17));
+    return cursors;
 }
 
-public void set_cursor_default (Window window) {
-    window.set_cursor (default_cursor);
+private Cursor make_cursor (Display display, string[] xpm, int hsx, int hsy) {
+    var pixbuf = new Pixbuf.from_xpm_data (xpm);
+    return new Cursor.from_pixbuf (display, pixbuf, hsx, hsy);
 }
 
-public void set_cursor_by_direction (Window window, int dx, int dy) {
-    int index = 3 * dy + dx + 4;
-    window.set_cursor (cursors[index]);
-}
+const string cursor_up_left[] = {
+  /* width height num_colors chars_per_pixel */
+  " 20 20 3 2",
+  /* colors */
+  ".. c none",
+  "## c #ffffff",
+  "** c #000000",
+  /* pixels */
+  "........................................",
+  "..######################................",
+  "..##****************##..................",
+  "..##**************##....................",
+  "..##************##......................",
+  "..##**************##....................",
+  "..##****************##..................",
+  "..##******************##................",
+  "..##****##**************##..............",
+  "..##**##..##**************##............",
+  "..####......##**********##..............",
+  "..##..........##******##................",
+  "................##**##..................",
+  "..................##....................",
+  "........................................",
+  "........................................",
+  "........................................",
+  "........................................",
+  "........................................",
+  "........................................"
+};
 
 const string cursor_up[] = {
   /* width height num_colors chars_per_pixel */
@@ -112,7 +128,7 @@ const string cursor_up_right[] = {
   "........................................"
 };
 
-const string cursor_right[] = {
+const string cursor_left[] = {
   /* width height num_colors chars_per_pixel */
   " 20 20 3 2",
   /* colors */
@@ -122,27 +138,27 @@ const string cursor_right[] = {
   /* pixels */
   "........................................",
   "........................................",
-  "......................##................",
-  "......................####..............",
-  "......................##**##............",
-  "......................##****##..........",
-  "......################********##........",
+  "................##......................",
+  "..............####......................",
+  "............##**##......................",
+  "..........##****##......................",
+  "........##********################......",
   "......##************************##......",
-  "......##**************************##....",
-  "......##****************************##..",
-  "......##**************************##....",
+  "....##**************************##......",
+  "..##****************************##......",
+  "....##**************************##......",
   "......##************************##......",
-  "......################********##........",
-  "......................##****##..........",
-  "......................##**##............",
-  "......................####..............",
-  "......................##................",
+  "........##********################......",
+  "..........##****##......................",
+  "............##**##......................",
+  "..............####......................",
+  "................##......................",
   "........................................",
   "........................................",
   "........................................"
 };
 
-const string cursor_down_right[] = {
+const string cursor_hold[] = {
   /* width height num_colors chars_per_pixel */
   " 20 20 3 2",
   /* colors */
@@ -154,25 +170,25 @@ const string cursor_down_right[] = {
   "........................................",
   "........................................",
   "........................................",
+  "..............##########................",
+  "..........####**********####............",
+  "..........##**************##............",
+  "........##******************##..........",
+  "........##******************##..........",
+  "........##******************##..........",
+  "........##******************##..........",
+  "........##******************##..........",
+  "..........##**************##............",
+  "..........####**********####............",
+  "..............##########................",
+  "........................................",
+  "........................................",
   "........................................",
   "........................................",
-  "....................##..................",
-  "..................##**##................",
-  "................##******##..........##..",
-  "..............##**********##......####..",
-  "............##**************##..##**##..",
-  "..............##**************##****##..",
-  "................##******************##..",
-  "..................##****************##..",
-  "....................##**************##..",
-  "......................##************##..",
-  "....................##**************##..",
-  "..................##****************##..",
-  "................######################..",
   "........................................"
 };
 
-const string cursor_down[] = {
+const string cursor_right[] = {
   /* width height num_colors chars_per_pixel */
   " 20 20 3 2",
   /* colors */
@@ -182,23 +198,23 @@ const string cursor_down[] = {
   /* pixels */
   "........................................",
   "........................................",
+  "......................##................",
+  "......................####..............",
+  "......................##**##............",
+  "......................##****##..........",
+  "......################********##........",
+  "......##************************##......",
+  "......##**************************##....",
+  "......##****************************##..",
+  "......##**************************##....",
+  "......##************************##......",
+  "......################********##........",
+  "......................##****##..........",
+  "......................##**##............",
+  "......................####..............",
+  "......................##................",
+  "........................................",
   "........................................",
-  "..............##############............",
-  "..............##**********##............",
-  "..............##**********##............",
-  "..............##**********##............",
-  "..............##**********##............",
-  "..............##**********##............",
-  "..............##**********##............",
-  "..............##**********##............",
-  "......########**************########....",
-  "........##**********************##......",
-  "..........##******************##........",
-  "............##**************##..........",
-  "..............##**********##............",
-  "................##******##..............",
-  "..................##**##................",
-  "....................##..................",
   "........................................"
 };
 
@@ -232,37 +248,7 @@ const string cursor_down_left[] = {
   "........................................"
 };
 
-const string cursor_left[] = {
-  /* width height num_colors chars_per_pixel */
-  " 20 20 3 2",
-  /* colors */
-  ".. c none",
-  "## c #ffffff",
-  "** c #000000",
-  /* pixels */
-  "........................................",
-  "........................................",
-  "................##......................",
-  "..............####......................",
-  "............##**##......................",
-  "..........##****##......................",
-  "........##********################......",
-  "......##************************##......",
-  "....##**************************##......",
-  "..##****************************##......",
-  "....##**************************##......",
-  "......##************************##......",
-  "........##********################......",
-  "..........##****##......................",
-  "............##**##......................",
-  "..............####......................",
-  "................##......................",
-  "........................................",
-  "........................................",
-  "........................................"
-};
-
-const string cursor_up_left[] = {
+const string cursor_down[] = {
   /* width height num_colors chars_per_pixel */
   " 20 20 3 2",
   /* colors */
@@ -271,28 +257,28 @@ const string cursor_up_left[] = {
   "** c #000000",
   /* pixels */
   "........................................",
-  "..######################................",
-  "..##****************##..................",
-  "..##**************##....................",
-  "..##************##......................",
-  "..##**************##....................",
-  "..##****************##..................",
-  "..##******************##................",
-  "..##****##**************##..............",
-  "..##**##..##**************##............",
-  "..####......##**********##..............",
-  "..##..........##******##................",
-  "................##**##..................",
-  "..................##....................",
-  "........................................",
-  "........................................",
-  "........................................",
   "........................................",
   "........................................",
+  "..............##############............",
+  "..............##**********##............",
+  "..............##**********##............",
+  "..............##**********##............",
+  "..............##**********##............",
+  "..............##**********##............",
+  "..............##**********##............",
+  "..............##**********##............",
+  "......########**************########....",
+  "........##**********************##......",
+  "..........##******************##........",
+  "............##**************##..........",
+  "..............##**********##............",
+  "................##******##..............",
+  "..................##**##................",
+  "....................##..................",
   "........................................"
 };
 
-const string cursor_hold[] = {
+const string cursor_down_right[] = {
   /* width height num_colors chars_per_pixel */
   " 20 20 3 2",
   /* colors */
@@ -304,21 +290,21 @@ const string cursor_hold[] = {
   "........................................",
   "........................................",
   "........................................",
-  "..............##########................",
-  "..........####**********####............",
-  "..........##**************##............",
-  "........##******************##..........",
-  "........##******************##..........",
-  "........##******************##..........",
-  "........##******************##..........",
-  "........##******************##..........",
-  "..........##**************##............",
-  "..........####**********####............",
-  "..............##########................",
-  "........................................",
-  "........................................",
   "........................................",
   "........................................",
+  "....................##..................",
+  "..................##**##................",
+  "................##******##..........##..",
+  "..............##**********##......####..",
+  "............##**************##..##**##..",
+  "..............##**************##****##..",
+  "................##******************##..",
+  "..................##****************##..",
+  "....................##**************##..",
+  "......................##************##..",
+  "....................##**************##..",
+  "..................##****************##..",
+  "................######################..",
   "........................................"
 };
 
diff --git a/src/game-area.vala b/src/game-area.vala
index 3490399..2d0116b 100644
--- a/src/game-area.vala
+++ b/src/game-area.vala
@@ -261,11 +261,14 @@ public class GameArea : DrawingArea {
     private void move_cb (double x, double y) {
         var window = get_window ();
         if (game.state != Game.State.PLAYING) {
-            set_cursor_default (window);
+            window.set_cursor (null);
         } else {
             int dx, dy;
             get_dir (x, y, out dx, out dy);
-            set_cursor_by_direction (window, dx, dy);
+
+            var cursor_index = 3 * dy + dx + 4;
+            var cursor = assets.cursors.index(cursor_index);
+            window.set_cursor (cursor);
         }
     }
 


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