[ease] Use Cairo for creating the focus effect.



commit d80cb408741be6a51d0e92421d4769646feec70c
Author: Stéphane Maniaci <stephane maniaci gmail com>
Date:   Mon Aug 16 13:36:41 2010 -0500

    Use Cairo for creating the focus effect.
    
    Focus is now a nice circle, however it needs a lot of optimization.

 ease/ease-player.vala |   85 ++++++++++++++++++++++---------------------------
 1 files changed, 38 insertions(+), 47 deletions(-)
---
diff --git a/ease/ease-player.vala b/ease/ease-player.vala
index ff4abf8..5388a1a 100644
--- a/ease/ease-player.vala
+++ b/ease/ease-player.vala
@@ -47,11 +47,8 @@ internal class Ease.Player : Gtk.Window
 	private const uint FOCUS_RADIUS = 40;
 
 	// focus actors
-	private Clutter.Group shader;
-	private Clutter.Rectangle shader_top;
-	private Clutter.Rectangle shader_bottom;
-	private Clutter.Rectangle shader_left;
-	private Clutter.Rectangle shader_right;
+	private Clutter.CairoTexture focus_circle;
+	private Cairo.Pattern radial;
 	
 	internal signal void complete();
 	
@@ -110,24 +107,12 @@ internal class Ease.Player : Gtk.Window
 		Clutter.grab_keyboard(stage);
 
 		// focusing
-		shader_top = new Clutter.Rectangle.with_color (Clutter.Color.from_string ("black"));
-		shader_right = new Clutter.Rectangle.with_color (Clutter.Color.from_string ("black"));
-		shader_bottom = new Clutter.Rectangle.with_color (Clutter.Color.from_string ("black"));
-		shader_left = new Clutter.Rectangle.with_color (Clutter.Color.from_string ("black"));
+		focus_circle = new Clutter.CairoTexture (1024, 768);
+		focus_circle.set_anchor_point_from_gravity (Clutter.Gravity.CENTER);
+		focus_circle.opacity = 0;
+		focus_circle.set_position (stage.width/2, stage.height/2);
 
-		shader = new Clutter.Group ();
-		shader.opacity = 0;
-
-		/* The following function is broken at the moment in the Clutter
-		   bindings. Replace the
-		   internal void add (...); by
-		   internal void add (Clutter.Actor first_actor, ...); */
-		shader.add (shader_top, 
-					shader_right,
-					shader_bottom,
-					shader_left);
-
-		stage.add (shader);
+		this.stage.add_actor (focus_circle);
 		stage.set_clip(0, 0, doc.width, doc.height);
 
 		// make the stacking container
@@ -160,17 +145,19 @@ internal class Ease.Player : Gtk.Window
 	internal void on_motion (Clutter.MotionEvent event)
 	{
 		if (dragging) {
-			// FIXME : duplicate code
-			shader_top.set_size (stage.width, event.y - FOCUS_RADIUS);
-			shader_bottom.set_size (stage.width, (stage.height - event.y) - FOCUS_RADIUS);
-			shader_left.set_size (event.x - FOCUS_RADIUS, FOCUS_RADIUS * 2);
-			shader_right.set_size (stage.width - event.x - FOCUS_RADIUS, 2 * FOCUS_RADIUS);
+			focus_circle.clear ();
+			var cr = focus_circle.create ();
 			
-			shader_left.set_position (0, event.y - FOCUS_RADIUS);
-			shader_right.set_position (event.x + FOCUS_RADIUS, event.y - FOCUS_RADIUS);
-			shader_bottom.set_position (0, event.y + FOCUS_RADIUS);
-			shader.show_all ();
-			stage.raise_child (shader, null);
+			cr.translate (event.x, event.y);
+			radial = new Cairo.Pattern.radial (0, 0, FOCUS_RADIUS, 
+											   0, 0, 2*FOCUS_RADIUS);
+			radial.add_color_stop_rgb (0, 1, 1, 1);
+			radial.add_color_stop_rgb (1, 0, 0, 0);
+
+			cr.set_source (radial);
+			cr.paint ();
+
+			stage.raise_child (focus_circle, null);
 		} else {
 			// fade out
 		}
@@ -180,26 +167,30 @@ internal class Ease.Player : Gtk.Window
 	{
 		dragging = false;
 		// FIXME : should the focus fade time be a constant ?
-		shader.animate (Clutter.AnimationMode.LINEAR, 150,
-						"opacity", 0);
+		focus_circle.animate (Clutter.AnimationMode.LINEAR, 150,
+							  "opacity", 0);
 	}
 
 	internal void on_button_press (Clutter.ButtonEvent event)
 	{
 		dragging = true;
 		debug ("Got a mouse click at %f, %f", event.x, event.y);
-		shader_top.set_size (stage.width, event.y - FOCUS_RADIUS);
-		shader_bottom.set_size (stage.width, (stage.height - event.y) - FOCUS_RADIUS);
-		shader_left.set_size (event.x - FOCUS_RADIUS, FOCUS_RADIUS * 2);
-		shader_right.set_size (stage.width - event.x - FOCUS_RADIUS, 2 * FOCUS_RADIUS);
 
-		shader_left.set_position (0, event.y - FOCUS_RADIUS);
-		shader_right.set_position (event.x + FOCUS_RADIUS, event.y - FOCUS_RADIUS);
-		shader_bottom.set_position (0, event.y + FOCUS_RADIUS);
-		shader.show_all ();
-		stage.raise_child (shader, null);
-		shader.animate (Clutter.AnimationMode.LINEAR, 150,
-						"opacity", FOCUS_OPACITY);
+		focus_circle.clear ();
+		var cr = focus_circle.create ();
+
+		cr.translate (event.x, event.y);
+		radial = new Cairo.Pattern.radial (0, 0, FOCUS_RADIUS,
+										   0, 0, 2*FOCUS_RADIUS);
+		radial.add_color_stop_rgb (0, 1, 1, 1);
+		radial.add_color_stop_rgb (1, 0, 0, 0);
+
+		cr.set_source (radial);
+		cr.paint ();
+
+		stage.raise_child (focus_circle, null);
+		focus_circle.animate (Clutter.AnimationMode.LINEAR, 150,
+							  "opacity", FOCUS_OPACITY);
 	}
 
 	internal void on_key_press (Clutter.KeyEvent event)



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