[gnome-nibbles/wip/vala] Fix name labels alignment



commit 1178a264fc7023620138f14c9fcb95e987802326
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Aug 10 18:33:18 2015 +0300

    Fix name labels alignment

 src/gnome-nibbles.vala |   10 ++++------
 src/nibbles-game.vala  |    7 +++++++
 src/nibbles-view.vala  |   47 ++++++++++++++++++++++-------------------------
 src/worm.vala          |    2 +-
 4 files changed, 34 insertions(+), 32 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 5eec7e7..a7151b5 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -96,7 +96,6 @@ public class Nibbles : Gtk.Application
 
     protected override void startup ()
     {
-        stderr.printf("[Debug] Startup\n");
         base.startup ();
 
         var css_provider = new Gtk.CssProvider ();
@@ -260,10 +259,8 @@ public class Nibbles : Gtk.Application
         game.current_level = game.start_level;
 
         view.new_level (game.current_level);
-        view.create_name_labels ();
         view.connect_worm_signals ();
 
-        stderr.printf("[Debug] Here4\n");
         foreach (var worm in game.worms)
         {
             var color = game.worm_props.get (worm).color;
@@ -278,6 +275,9 @@ public class Nibbles : Gtk.Application
             actors.show ();
         }
         game.add_worms ();
+
+        view.create_name_labels ();
+
         show_game_view ();
 
         start_game_with_countdown ();
@@ -351,8 +351,6 @@ public class Nibbles : Gtk.Application
         game.create_worms ();
         game.load_worm_properties (worm_settings);
 
-        stderr.printf("[Debug] worms size%d\n", game.worms.size);
-
         foreach (var grid in grids_box.get_children ())
             grid.destroy ();
 
@@ -364,7 +362,7 @@ public class Nibbles : Gtk.Application
                 grids_box.add (grid);
             }
         }
-        stderr.printf("[Debug] Here3\n");
+
         main_stack.set_visible_child_name ("controls");
     }
 
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 0065701..15f580a 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -193,7 +193,14 @@ public class NibblesGame : Object
     public void add_worms ()
     {
         foreach (var worm in worms)
+        {
+            /* Required for the first element of the worm added before signals were connected
+             * TODO: Try to connect signals before adding the starting position to the worm
+             */
+            worm.added ();
+
             worm.spawn (walls);
+        }
     }
 
     public void move_worms ()
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index 6e0921a..f3f34dc 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -370,32 +370,20 @@ public class NibblesView : GtkClutter.Embed
         {
             var color = game.worm_props.get (worm).color;
 
-            var label = new Clutter.Text.with_text ("Source Pro 10", _(@"<b>PLAYER $(worm.id + 1)</b>"));
+            var label = new Clutter.Text.with_text ("Monospace 10", _(@"<b>Player $(worm.id + 1)</b>"));
             label.set_use_markup (true);
-            label.set_pivot_point (0.5f, 0.5f);
             label.set_color (Clutter.Color.from_string (colorval_name (color)));
 
-            // TODO: Better aligb these
-            switch (worm.direction)
+            var middle = worm.length / 2;
+            if (worm.direction == WormDirection.UP || worm.direction == WormDirection.DOWN)
             {
-                case WormDirection.UP:
-                    label.set_x (worm.head ().x * game.tile_size);
-                    label.set_y (worm.head ().y * game.tile_size);
-                    break;
-                case WormDirection.DOWN:
-                    label.set_x (worm.head ().x * game.tile_size);
-                    label.set_y (worm.head ().y * game.tile_size);
-                    break;
-                case WormDirection.LEFT:
-                    label.set_x (worm.head ().x * game.tile_size);
-                    label.set_y (worm.head ().y * game.tile_size);
-                    break;
-                case WormDirection.RIGHT:
-                    label.set_x (worm.head ().x * game.tile_size);
-                    label.set_y (worm.head ().y * game.tile_size);
-                    break;
-                default:
-                    break;
+                label.set_x (worm.list[middle].x * game.tile_size - label.width / 2 + game.tile_size / 2);
+                label.set_y (worm.list[middle].y * game.tile_size - 5 * game.tile_size);
+            }
+            else if (worm.direction == WormDirection.LEFT || worm.direction == WormDirection.RIGHT)
+            {
+                label.set_x (worm.list[middle].x * game.tile_size - label.width / 2 + game.tile_size / 2);
+                label.set_y (worm.head ().y * game.tile_size - 3 * game.tile_size);
             }
             name_labels.add (label);
         }
@@ -440,9 +428,18 @@ public class NibblesView : GtkClutter.Embed
         foreach (var worm in game.worms)
         {
             var actor = name_labels.get_child_at_index (worm.id);
-            actor.get_position (out x_pos, out y_pos);
-            actor.x = ((x_pos / game.tile_size) * tile_size);
-            actor.y = ((y_pos / game.tile_size) * tile_size);
+
+            var middle = worm.length / 2;
+            if (worm.direction == WormDirection.UP || worm.direction == WormDirection.DOWN)
+            {
+                actor.set_x (worm.list[middle].x * tile_size - actor.width / 2 + tile_size / 2);
+                actor.set_y (worm.list[middle].y * tile_size - 5 * tile_size);
+            }
+            else if (worm.direction == WormDirection.LEFT || worm.direction == WormDirection.RIGHT)
+            {
+                actor.set_x (worm.list[middle].x * tile_size - actor.width / 2 + tile_size / 2);
+                actor.set_y (worm.head ().y * tile_size - 3 * tile_size);
+            }
         }
     }
 
diff --git a/src/worm.vala b/src/worm.vala
index 8bee155..0fba877 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -210,7 +210,7 @@ public class Worm : Object
 
     public void spawn (int[,] walls)
     {
-        change = STARTING_LENGTH;
+        change = STARTING_LENGTH - 1;
         for (int i = 0; i < STARTING_LENGTH; i++)
             move (walls);
     }


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