[f-spot] Add some bling



commit 52db28d0bd3639da08de41100d5f6070f32f85d5
Author: Stephane Delcroix <stephane delcroix org>
Date:   Sun Apr 26 21:55:19 2009 +0200

    Add some bling
    
    the F-Spot.Bling namespace now contains various animotrs modelling reallife object movement. As those animators uses GLib.Idle, it's 100% safe to use them to animate Gtk widgets or properties. You can define your own EasedAnimation by subclassing EasedAnimation.
---
 src/Bling/BackAnimation.cs          |   42 +++++++++
 src/Bling/CubicAnimation.cs         |   24 +++++
 src/Bling/DoubleBackAnimation.cs    |   29 ++++++
 src/Bling/DoubleCubicAnimation.cs   |   24 +++++
 src/Bling/DoubleLinearAnimation.cs  |   25 +++++
 src/Bling/DoubleQuinticAnimation.cs |   24 +++++
 src/Bling/EasedAnimation.cs         |  175 +++++++++++++++++++++++++++++++++++
 src/Bling/EasingMode.cs             |   18 ++++
 src/Bling/LinearAnimation.cs        |   25 +++++
 src/Bling/QuinticAnimation.cs       |   24 +++++
 src/Makefile.am                     |   10 ++
 11 files changed, 420 insertions(+), 0 deletions(-)

diff --git a/src/Bling/BackAnimation.cs b/src/Bling/BackAnimation.cs
new file mode 100644
index 0000000..7f22c1a
--- /dev/null
+++ b/src/Bling/BackAnimation.cs
@@ -0,0 +1,42 @@
+//
+// FSpot.Bling.BackAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{	public abstract class BackAnimation<T>: EasedAnimation<T>
+	{
+		double amplitude;
+
+		public BackAnimation (T from, T to, TimeSpan duration, Action<T> action) : this (from, to, duration, action, 0)
+		{
+		}
+
+		public BackAnimation (T from, T to, TimeSpan duration, Action<T> action, double amplitude) : base (from, to, duration, action)
+		{
+			if (amplitude < 0)
+				throw new ArgumentOutOfRangeException ("amplitude");
+			this.amplitude = amplitude;
+		}
+
+		public double Amplitude {
+			get { return amplitude; }
+			set {
+				if (value < 0)
+					throw new ArgumentOutOfRangeException ("amplitude");
+				amplitude = value;
+			}
+		}
+
+		protected override double EaseInCore (double normalizedTime)
+		{
+			return normalizedTime * normalizedTime * normalizedTime - normalizedTime * amplitude * Math.Sin (normalizedTime * Math.PI);
+		}
+	}
+}
diff --git a/src/Bling/CubicAnimation.cs b/src/Bling/CubicAnimation.cs
new file mode 100644
index 0000000..28f3032
--- /dev/null
+++ b/src/Bling/CubicAnimation.cs
@@ -0,0 +1,24 @@
+//
+// FSpot.Bling.CubicAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{	public abstract class CubicAnimation<T>: EasedAnimation<T>
+	{
+		public CubicAnimation (T from, T to, TimeSpan duration, Action<T> action) : base (from, to, duration, action)
+		{
+		}
+
+		protected override double EaseInCore (double normalizedTime)
+		{
+			return normalizedTime * normalizedTime * normalizedTime;
+		}
+	}
+}
diff --git a/src/Bling/DoubleBackAnimation.cs b/src/Bling/DoubleBackAnimation.cs
new file mode 100644
index 0000000..892ca23
--- /dev/null
+++ b/src/Bling/DoubleBackAnimation.cs
@@ -0,0 +1,29 @@
+//
+// FSpot.Bling.DoubleBackAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{	public class DoubleBackAnimation : BackAnimation<double>
+	{
+		public DoubleBackAnimation (double from, double to, TimeSpan duration, Action<double> action, double amplitude) : base (from, to, duration, action, amplitude)
+		{
+		}
+
+		public DoubleBackAnimation (double from, double to, TimeSpan duration, Action<double> action) : this (from, to, duration, action, 0)
+		{
+		}
+
+		protected override double Interpolate (double from, double to, double progress)
+		{
+			return from + progress * (to - from);
+		}
+	}
+}
+
diff --git a/src/Bling/DoubleCubicAnimation.cs b/src/Bling/DoubleCubicAnimation.cs
new file mode 100644
index 0000000..876fd37
--- /dev/null
+++ b/src/Bling/DoubleCubicAnimation.cs
@@ -0,0 +1,24 @@
+//
+// FSpot.Bling.DoubleCubicAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{	public class DoubleCubicAnimation : CubicAnimation<double>
+	{
+		public DoubleCubicAnimation (double from, double to, TimeSpan duration, Action<double> action) : base (from, to, duration, action)
+		{
+		}
+
+		protected override double Interpolate (double from, double to, double progress)
+		{
+			return from + progress * (to - from);
+		}
+	}
+}
diff --git a/src/Bling/DoubleLinearAnimation.cs b/src/Bling/DoubleLinearAnimation.cs
new file mode 100644
index 0000000..53ad7fc
--- /dev/null
+++ b/src/Bling/DoubleLinearAnimation.cs
@@ -0,0 +1,25 @@
+//
+// FSpot.Bling.DoubleLinearAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{	public class DoubleLinearAnimation : LinearAnimation<double>
+	{
+		public DoubleLinearAnimation (double from, double to, TimeSpan duration, Action<double> action) : base (from, to, duration, action)
+		{
+		}
+
+		protected override double Interpolate (double from, double to, double progress)
+		{
+			return from + progress * (to - from);
+		}
+	}
+}
+
diff --git a/src/Bling/DoubleQuinticAnimation.cs b/src/Bling/DoubleQuinticAnimation.cs
new file mode 100644
index 0000000..6944df1
--- /dev/null
+++ b/src/Bling/DoubleQuinticAnimation.cs
@@ -0,0 +1,24 @@
+//
+// FSpot.Bling.DoubleQuinticAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{	public class DoubleQuinticAnimation : QuinticAnimation<double>
+	{
+		public DoubleQuinticAnimation (double from, double to, TimeSpan duration, Action<double> action) : base (from, to, duration, action)
+		{
+		}
+
+		protected override double Interpolate (double from, double to, double progress)
+		{
+			return from + progress * (to - from);
+		}
+	}
+}
diff --git a/src/Bling/EasedAnimation.cs b/src/Bling/EasedAnimation.cs
new file mode 100644
index 0000000..7060edf
--- /dev/null
+++ b/src/Bling/EasedAnimation.cs
@@ -0,0 +1,175 @@
+//
+// FSpot.Bling.EasedAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+//TODO: send the progresschanged event
+
+using System;
+using System.ComponentModel;
+
+namespace FSpot.Bling
+{
+	public abstract class EasedAnimation<T>
+	{
+		enum AnimationState {
+			NotRunning = 0,
+			Running,
+			Paused,
+		}
+
+		TimeSpan duration;
+		TimeSpan pausedafter;
+
+		DateTimeOffset starttime;
+		T from;
+		T to;
+		Action<T> action;
+		AnimationState state;
+		EasingMode easingmode;
+
+		public EasedAnimation ()
+		{
+			from = default (T);
+			to = default (T);
+			duration = TimeSpan.Zero;
+			action = null;
+			state = AnimationState.NotRunning;
+			easingmode = EasingMode.In;
+		}
+
+		public EasedAnimation (T from, T to, TimeSpan duration, Action<T> action)
+		{
+			this.from = from;
+			this.to = to;
+			this.duration = duration;
+			this.action = action;
+			state = AnimationState.NotRunning;
+			easingmode = EasingMode.In;
+		}
+
+		public void Pause ()
+		{
+			if (state == AnimationState.Paused)
+				return;
+			if (state != AnimationState.NotRunning)
+				throw new InvalidOperationException ("Can't Pause () a non running animation");
+			GLib.Idle.Remove (Handler);
+			pausedafter = DateTimeOffset.Now - starttime;
+			state = AnimationState.Paused;
+		}
+
+		public void Resume ()
+		{
+			if (state == AnimationState.Running)
+				return;
+			if (state != AnimationState.Paused)
+				throw new InvalidOperationException ("Can't Resume a non running animation");
+			starttime = DateTimeOffset.Now - pausedafter;
+			state = AnimationState.Running;
+			GLib.Idle.Add (Handler);
+		}
+
+		public void Start ()
+		{
+			if (state != AnimationState.NotRunning)
+				throw new InvalidOperationException ("Can't Start() a running or paused animation");
+			starttime = DateTimeOffset.Now;
+			state = AnimationState.Running;
+			GLib.Idle.Add (Handler);
+		}
+
+		public void Stop ()
+		{
+			if (state == AnimationState.NotRunning)
+				return;
+			else
+				GLib.Idle.Remove (Handler);
+			action (to);
+			EventHandler h = Completed;
+			if (h != null)
+				Completed (this, EventArgs.Empty);
+			state = AnimationState.NotRunning;
+		}
+
+		public void Restart ()
+		{
+			if (state == AnimationState.NotRunning) {
+				Start ();
+				return;
+			}
+			if (state == AnimationState.Paused) {
+				Resume ();
+				return;
+			}
+			starttime = DateTimeOffset.Now;
+		}
+
+		public double Ease (double normalizedTime)
+		{
+			switch (easingmode) {
+			case EasingMode.In:
+				return EaseInCore (normalizedTime);
+			case EasingMode.Out:
+				return 1.0 - EaseInCore (1 - normalizedTime);
+			case EasingMode.InOut:
+				return (normalizedTime <= 0.5
+					? EaseInCore (normalizedTime * 2) * 0.5
+					: 1.0 - EaseInCore ((1 - normalizedTime) * 2) * 0.5);
+			}
+			throw new InvalidOperationException ("Unknown value for EasingMode");
+		}
+
+		bool Handler ()
+		{
+			double percentage = (double)(DateTimeOffset.Now - starttime ).Ticks / (double)duration.Ticks;
+			action (Interpolate (from, to, Ease (Math.Min (percentage, 1.0))));
+			if (percentage >= 1.0) {
+				EventHandler h = Completed;
+				if (h != null)
+					Completed (this, EventArgs.Empty);
+				state = AnimationState.NotRunning;
+				return false;
+			}
+			return true;
+		}
+
+		protected abstract double EaseInCore (double percentage);
+		protected abstract T Interpolate (T from, T to, double progress);
+
+		public event EventHandler Completed;
+		public event EventHandler<ProgressChangedEventArgs> ProgressChanged;
+
+		public EasingMode EasingMode {
+			get { return easingmode; }
+			set { easingmode = value; }
+		}
+
+		public bool IsRunning {
+			get { return state == AnimationState.Running; }
+		}
+
+		public bool IsPaused {
+			get { return state == AnimationState.Paused; }
+		}
+
+		public TimeSpan Duration {
+			get { return duration; }
+			set { duration = value; }
+		}
+
+		public T From {
+			get { return from; }
+			set { from = value; }
+		}
+
+		public T To {
+			get { return to; }
+			set { to = value; }
+		}
+	}
+}
diff --git a/src/Bling/EasingMode.cs b/src/Bling/EasingMode.cs
new file mode 100644
index 0000000..b8e3cb2
--- /dev/null
+++ b/src/Bling/EasingMode.cs
@@ -0,0 +1,18 @@
+//
+// FSpot.Bling.EasingMode,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+namespace FSpot.Bling
+{
+	public enum EasingMode {
+		In = 0,
+		Out,
+		InOut,
+	}
+}
+
diff --git a/src/Bling/LinearAnimation.cs b/src/Bling/LinearAnimation.cs
new file mode 100644
index 0000000..4930126
--- /dev/null
+++ b/src/Bling/LinearAnimation.cs
@@ -0,0 +1,25 @@
+//
+// FSpot.Bling.LinearAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{
+	public abstract class LinearAnimation<T> : EasedAnimation<T>
+	{
+		public LinearAnimation (T from, T to, TimeSpan duration, Action<T> action) : base (from, to, duration, action)
+		{
+		}
+
+		protected override double EaseInCore (double normalizedTime)
+		{
+			return normalizedTime;
+		}
+	}
+}
diff --git a/src/Bling/QuinticAnimation.cs b/src/Bling/QuinticAnimation.cs
new file mode 100644
index 0000000..fc758dc
--- /dev/null
+++ b/src/Bling/QuinticAnimation.cs
@@ -0,0 +1,24 @@
+//
+// FSpot.Bling.QuinticAnimation,cs
+//
+// Author(s):
+//	Stephane Delcroix  <stephane delcroix org>
+//
+// This is free software. See COPYING for details
+//
+
+using System;
+
+namespace FSpot.Bling
+{	public abstract class QuinticAnimation<T>: EasedAnimation<T>
+	{
+		public QuinticAnimation (T from, T to, TimeSpan duration, Action<T> action) : base (from, to, duration, action)
+		{
+		}
+
+		protected override double EaseInCore (double normalizedTime)
+		{
+			return normalizedTime * normalizedTime * normalizedTime * normalizedTime * normalizedTime;
+		}
+	}
+}
diff --git a/src/Makefile.am b/src/Makefile.am
index 04b2a96..67efb79 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -68,6 +68,16 @@ JOBSCHEDULER_CSDISTFILES =				\
 	$(srcdir)/JobScheduler/Scheduler.cs
 
 WIDGETS_CSDISTFILES =				\
+	$(srcdir)/Bling/BackAnimation.cs	\
+	$(srcdir)/Bling/CubicAnimation.cs	\
+	$(srcdir)/Bling/DoubleBackAnimation.cs	\
+	$(srcdir)/Bling/DoubleCubicAnimation.cs	\
+	$(srcdir)/Bling/DoubleLinearAnimation.cs\
+	$(srcdir)/Bling/DoubleQuinticAnimation.cs\
+	$(srcdir)/Bling/EasedAnimation.cs	\
+	$(srcdir)/Bling/EasingMode.cs		\
+	$(srcdir)/Bling/LinearAnimation.cs	\
+	$(srcdir)/Bling/QuinticAnimation.cs	\
 	$(srcdir)/Widgets/ComplexMenuItem.cs	\
 	$(srcdir)/Widgets/CustomPrintWidget.cs	\
 	$(srcdir)/Widgets/HighlightedBox.cs	\



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