File: TimedAnimation.cs

package info (click to toggle)
monodevelop 4.0.12%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 219,596 kB
  • ctags: 253,200
  • sloc: cs: 1,486,058; xml: 952,347; java: 60,981; makefile: 4,213; sh: 1,727; ansic: 867; objc: 302; sql: 111
file content (109 lines) | stat: -rw-r--r-- 3,614 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;
using MonoMac.CoreAnimation;
using MonoMac.CoreGraphics;

namespace TimedAnimation {

	public partial class TimedAnimation : MonoMac.AppKit.NSView {
		NSImageView photo1;
		NSImageView photo2;
		
		public TimedAnimation (IntPtr handle) : base(handle) {}

		[Export("initWithCoder:")]
		public TimedAnimation (NSCoder coder) : base(coder) {}
		
		[Export("initWithFrame:")]
		public TimedAnimation (RectangleF frame) : base(frame)
		{
			float xInset = frame.Width / 3;
			float yInset = frame.Height / 3;
			
			RectangleF aniFrame = frame.Inset (xInset, yInset);
			
			// photo1 starts and the left edge
			PointF origin = frame.Location;
			origin.X = 0.0f;
			origin.Y = Bounds.GetMidY () - aniFrame.Height / 2;
			aniFrame.Location = origin;
			
			photo1 = new NSImageView (aniFrame);
			photo1.ImageScaling = NSImageScale.AxesIndependently;
			photo1.Image = NSImage.ImageNamed ("photo1.jpg");
			AddSubview (photo1);
			
			// photo2 starts in the center
			origin.X = Bounds.GetMidX () - aniFrame.Width / 2;
			aniFrame.Location = origin;
			photo2 = new NSImageView (aniFrame);
			photo2.ImageScaling = NSImageScale.AxesIndependently;
			photo2.Image = NSImage.ImageNamed ("photo2.jpg");
			AddSubview (photo2);
		}
		
		public override bool AcceptsFirstResponder ()
		{
			return true;
		}
		
		public override void KeyDown (NSEvent theEvent)
		{
			if ((NSKey)(theEvent.Characters [0]) == NSKey.RightArrow)
				right ();
			else if (theEvent.Characters [0] == 'r')
				reset ();
			else
				base.KeyDown (theEvent);
		}
		
		CABasicAnimation basicAnimationNamed (string name, float duration)
		{
			CABasicAnimation animation = new CABasicAnimation ();
			animation.Duration = duration;
			animation.SetValueForKey ((NSString)name, (NSString)"name");
			return animation;
		}
		
		void right () 
		{
			// photo1 is going to move to where photo2 is
			PointF newOrigin = photo2.Frame.Location;
			CABasicAnimation animation = basicAnimationNamed ("photo1",1.0f);
			animation.AnimationStopped += HandleAnimationStopped;
			photo1.Animations = NSDictionary.FromObjectAndKey (animation, (NSString)"frameOrigin");
			((NSView)photo1.Animator).SetFrameOrigin (newOrigin);
		}
		
		void reset ()
		{
			// NOTE - fix the animations to allow null values
			photo1.Animations = new NSDictionary ();
			photo2.Animations = new NSDictionary ();
			
			PointF newPhoto1Origin = new PointF (0, Frame.GetMidY () - photo1.Bounds.Height / 2);
			PointF newPhoto2Origin = new PointF (Frame.GetMidX () - photo2.Bounds.Width / 2,
			                                     Frame.GetMidY () - photo1.Bounds.Height / 2);
			((NSView)photo1.Animator).SetFrameOrigin (newPhoto1Origin);
			((NSView)photo2.Animator).SetFrameOrigin (newPhoto2Origin);
		}
		
		void HandleAnimationStopped (object sender, CAAnimationStateEventArgs e)
		{
			//Console.WriteLine("stopped" + ((CAAnimation)sender).ValueForKey((NSString)"name"));
			var animation = (CAAnimation)sender;
			string animationValue = ((CAAnimation)sender).ValueForKey((NSString)"name").ToString();
			if (e.Finished && animationValue == "photo1"){
				CABasicAnimation photo2Animation = basicAnimationNamed ("photo2", (float)animation.Duration);
				photo2.Animations = NSDictionary.FromObjectAndKey (photo2Animation, (NSString)"frameOrigin");
				PointF newPhoto2Origin = new PointF(Frame.GetMaxX () - photo2.Frame.Size.Width, photo2.Frame.Location.Y);
				((NSView)photo2.Animator).SetFrameOrigin(newPhoto2Origin);
			}
		}
	}
}