[gnome-nibbles/arnaudb/wip/gtk4: 52/62] Make worms mostly work.



commit 903c515ee1b6f83c386ce056878423eff3cdc6b6
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Jun 16 16:55:09 2020 +0200

    Make worms mostly work.

 src/nibbles-view.vala   | 48 +++++++++++++++++++++++-------------------------
 src/nibbles-window.vala |  1 +
 2 files changed, 24 insertions(+), 25 deletions(-)
---
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index e32da50..f1d29d8 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -20,7 +20,7 @@ using Gtk;
 
 private class WormView : Object
 {
-    private List<Widget> widgets = new List<Widget> ();
+    internal List<Widget> widgets = new List<Widget> ();
 
     internal void set_opacity (uint8 new_opacity)
     {
@@ -49,15 +49,18 @@ private class WormView : Object
 //        restore_easing_state ();
 //    }
 
-//    protected override void hide ()
-//    {
+    internal void hide ()
+    {
+        foreach (Widget widget in widgets)
+            widget.hide ();
+
 //        save_easing_state ();
 //        set_easing_mode (Clutter.AnimationMode.EASE_IN_QUAD);
 //        set_easing_duration (NibblesGame.GAMEDELAY * 15);
 //        set_scale (0.4f, 0.4f);
 //        set_opacity (0);
 //        restore_easing_state ();
-//    }
+    }
 }
 
 private class NibblesView : Widget
@@ -458,8 +461,8 @@ private class NibblesView : Widget
 
     private void animate_end_game_cb ()
     {
-//        foreach (var worm in game.worms)
-//            worm_actors.@get (worm).hide ();
+        foreach (var worm in game.worms)
+            worm_actors.@get (worm).hide ();
 
         foreach (var actor in warp_actors)
             actor.hide ();
@@ -509,24 +512,16 @@ private class NibblesView : Widget
 
     private void worm_added_cb (Worm worm)
     {
-//        var actor = new GtkClutter.Texture ();
-//        try
-//        {
-//            actor.set_from_pixbuf (worm_pixmaps[game.worm_props.@get (worm).color]);
-//        }
-//        catch (Clutter.TextureError e)
-//        {
-//            error ("Nibbles failed to set texture: %s", e.message);
-//        }
-//        catch (Error e)
-//        {
-//            error ("Nibbles failed to set texture: %s", e.message);
-//        }
-//        actor.set_size (tile_size, tile_size);
-//        actor.set_position (worm.list.first ().x * tile_size, worm.list.first ().y * tile_size);
+        var actor = new Image.from_pixbuf (worm_pixmaps[game.worm_props.@get (worm).color]);
+        actor.pixel_size = tile_size;
+        actor.insert_after (this, /* insert first */ null);
 
-//        var actors = worm_actors.@get (worm);
-//        actors.add_child (actor);
+        GridLayoutChild child_layout = (GridLayoutChild) layout.get_layout_child (actor);
+        child_layout.set_left_attach (worm.list.first ().x);
+        child_layout.set_top_attach (worm.list.first ().y);
+
+        var actors = worm_actors.@get (worm);
+        actors.widgets.append (actor);
     }
 
     private void worm_finish_added_cb (Worm worm)
@@ -556,8 +551,11 @@ private class NibblesView : Widget
     {
         var actors = worm_actors.@get (worm);
 
-//        var tail_actor = actors.first_child;
-//        actors.remove_child (tail_actor);
+        var tail_actor = actors.widgets.first ().data;
+        actors.widgets.remove (tail_actor);
+        tail_actor.hide ();
+        tail_actor.unparent ();
+        tail_actor.destroy ();
         worm_added_cb (worm);
     }
 
diff --git a/src/nibbles-window.vala b/src/nibbles-window.vala
index b4d6c2a..c4a150f 100644
--- a/src/nibbles-window.vala
+++ b/src/nibbles-window.vala
@@ -113,6 +113,7 @@ private class NibblesWindow : ApplicationWindow
             maximize ();
 
         key_controller = new EventControllerKey ();
+        key_controller.set_propagation_phase (PropagationPhase.CAPTURE);
         key_controller.key_pressed.connect (key_press_event_cb);
         ((Widget) this).add_controller (key_controller);
 


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