[ease] [general] Add Clutter iterables



commit a34ba2bf9616f13615e362b1093b862fbe5f0bc1
Author: Nate Stedman <natesm gmail com>
Date:   Sun Jul 25 03:37:28 2010 -0400

    [general] Add Clutter iterables
    
    foreach-iterable ClutterContainer and ClutterGroup
    added.
    
    SlideActor's "contents" is now iterable with foreach.
    
    Ugly loops replaced with foreach in EditorEmbed.

 Makefile.am                     |    1 +
 src/ease-clutter-iterables.vala |   57 +++++++++++++++++++++++++++++++++++++++
 src/ease-editor-embed.vala      |   32 +++++++--------------
 src/ease-slide-actor.vala       |    6 ++--
 4 files changed, 72 insertions(+), 24 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 53b63c3..22683b2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -17,6 +17,7 @@ ease_SOURCES = \
 	src/ease-actor.vala \
 	src/ease-animated-zoom-slider.vala \
 	src/ease-close-confirm-dialog.vala \
+	src/ease-clutter-iterables.vala \
 	src/ease-color.vala \
 	src/ease-document.vala \
 	src/ease-editor-embed.vala \
diff --git a/src/ease-clutter-iterables.vala b/src/ease-clutter-iterables.vala
new file mode 100644
index 0000000..42452bd
--- /dev/null
+++ b/src/ease-clutter-iterables.vala
@@ -0,0 +1,57 @@
+/*  Ease, a GTK presentation application
+    Copyright (C) 2010 Nate Stedman
+
+    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/>.
+*/
+
+/**
+ * Clutter container mixin, iterable with foreach()
+ */
+public interface Ease.ClutterIterableContainer : Clutter.Container
+{
+	public Iterator iterator()
+	{
+		return new Iterator(this);
+	}
+
+	public class Iterator
+	{
+		private unowned List<Clutter.Actor>* itr;
+		
+		public Iterator(ClutterIterableContainer self)
+		{
+			itr = self.get_children();
+		}
+		
+		public bool next()
+		{
+			return itr != null;
+		}
+		
+		public Clutter.Actor get()
+		{
+			var actor = (Clutter.Actor)(itr->data);
+			itr = itr->next;
+			return actor;
+		}
+	}
+}
+
+/**
+ * ClutterGroup with { link ClutterIterableContainer} mixin.
+ */
+public class Ease.ClutterIterableGroup : Clutter.Group, ClutterIterableContainer
+{
+}
+
diff --git a/src/ease-editor-embed.vala b/src/ease-editor-embed.vala
index 3744adc..7d8208c 100644
--- a/src/ease-editor-embed.vala
+++ b/src/ease-editor-embed.vala
@@ -243,14 +243,11 @@ public class Ease.EditorEmbed : ScrollableEmbed
 		if (slide_actor != null)
 		{
 			contents.remove_actor(slide_actor);
-			for (unowned List<Clutter.Actor>* itr =
-			     slide_actor.contents.get_children();
-			     itr != null;
-			     itr = itr->next)
+			foreach (var a in slide_actor.contents)
 			{
-				((Actor*)(itr->data))->button_press_event.disconnect(actor_clicked);
-				((Actor*)(itr->data))->button_release_event.disconnect(actor_released);
-				((Actor*)(itr->data))->reactive = false;
+				a.button_press_event.disconnect(actor_clicked);
+				a.button_release_event.disconnect(actor_released);
+				a.reactive = false;
 			}
 		}
 		
@@ -280,15 +277,11 @@ public class Ease.EditorEmbed : ScrollableEmbed
 		                                        ActorContext.EDITOR);
 		                                        
 		// make the elements clickable
-		for (unowned List<Clutter.Actor>* itr =
-		     slide_actor.contents.get_children();
-		     itr != null;
-		     itr = itr->next)
+		foreach (var a in slide_actor.contents)
 		{
-			
-			((Actor*)(itr->data))->button_press_event.connect(actor_clicked);
-			((Actor*)(itr->data))->button_release_event.connect(actor_released);
-			((Actor*)(itr->data))->reactive = true;
+			a.button_press_event.connect(actor_clicked);
+			a.button_release_event.connect(actor_released);
+			a.reactive = true;
 		}
 		
 		contents.add_actor(slide_actor);
@@ -361,14 +354,11 @@ public class Ease.EditorEmbed : ScrollableEmbed
 	 */
 	public void select_element(Element e)
 	{
-		for (unowned List<Clutter.Actor>* itr =
-		     slide_actor.contents.get_children();
-		     itr != null;
-		     itr = itr->next)
+		foreach (var a in slide_actor.contents)
 		{
-			if (((Actor*)(itr->data))->element == e)
+			if ((a as Actor).element == e)
 			{
-				select_actor(itr->data as Actor);
+				select_actor(a as Actor);
 			}
 		}
 	}
diff --git a/src/ease-slide-actor.vala b/src/ease-slide-actor.vala
index 2afe905..64df96e 100644
--- a/src/ease-slide-actor.vala
+++ b/src/ease-slide-actor.vala
@@ -37,7 +37,7 @@ public class Ease.SlideActor : Clutter.Group
 	/**
 	 * The group for the slide's contents.
 	 */
-	public Clutter.Group contents;
+	public ClutterIterableGroup contents;
 
 	/**
 	 * The context of the actor (presentation, etc.)
@@ -164,7 +164,7 @@ public class Ease.SlideActor : Clutter.Group
 		set_background();
 		add_actor(background);
 
-		contents = new Clutter.Group();
+		contents = new ClutterIterableGroup();
 
 		foreach (var e in slide.elements)
 		{
@@ -190,7 +190,7 @@ public class Ease.SlideActor : Clutter.Group
 		background = new Clutter.CairoTexture(document.width, document.height);
 		
 		// create a blank contents actor
-		contents = new Clutter.Group();
+		contents = new ClutterIterableGroup();
 		
 		// set the background size
 		background.width = width_px;



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