using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using C1.C1Flash;

namespace Movie_FadeEffect
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private C1.C1Flash.C1FlashMovie c1FlashMovie1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.button1 = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.c1FlashMovie1 = new C1.C1Flash.C1FlashMovie();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(136, 56);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(133, 38);
			this.button1.TabIndex = 0;
			this.button1.Text = "Create and View";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(19, 10);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(64, 52);
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.Visible = false;
			// 
			// c1FlashMovie1
			// 
			this.c1FlashMovie1.BackgroundColor = System.Drawing.Color.Empty;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(393, 156);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pictureBox1,
																		  this.button1});
			this.Name = "Form1";
			this.Text = "C1Flash Sample - Fade Effect";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private string tempdir = System.IO.Path.GetDirectoryName(Application.ExecutablePath);

		private void LaunchViewer(string filename)
		{
			try
			{
				System.Diagnostics.Process.Start(filename);
			}
			catch (Exception e)
			{
				System.Diagnostics.Process.Start("IEXPLORE.EXE", filename);
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			Image img = this.pictureBox1.Image;
			Rectangle imgRect = new Rectangle(20 * Constants.TWIPS, 40 * Constants.TWIPS, 
				img.Width * Constants.TWIPS, img.Height * Constants.TWIPS);

			FImage image = new FImage(imgRect, img);

			// rotate and scale the image, fade out using Frame's Opacity property
			for (int i = 1 ; i <= 40; i++)
			{
				image.Depth = i;
				image.Rotate(i * 9);
				image.Scale((float)(i * 0.025), (float)(i * 0.025));
				this.c1FlashMovie1.Frames(i).AddObject(image);
				this.c1FlashMovie1.Frames(i).Opacity = i * 0.025;
				this.c1FlashMovie1.Frames(i + 1).RemoveObject(image);
			}

			// add welcome text
			Rectangle rectText = imgRect;
			rectText.Width *= 3;
			rectText.Offset(0, rectText.Height);
			FEditText text = new FEditText(rectText, "Thanks Using ComponentOne C1Flash", "Tahoma", false, true);
			text.ForeColor = Color.Red;
			text.Height = 20 * Constants.TWIPS;
			text.Depth = 41;
			this.c1FlashMovie1.Frames(41).AddObject(text);

			// resets the transform
            image.ResetTransform();

			// fade in using the Image's Opacity property
			for (int i = 1; i <= 100; i++)
			{
				image.Depth = i + 41;
				this.c1FlashMovie1.Frames(i + 40).AddObject(image);
				image.Opacity = 1 - i * 0.01;
				this.c1FlashMovie1.Frames(i + 40 + 1).RemoveObject(image);
			}

			this.c1FlashMovie1.Frames(200);
			this.c1FlashMovie1.FrameRate = 40;

			this.c1FlashMovie1.RenderToFile(tempdir + @"\c1flash_movie_fade.swf");
			LaunchViewer(tempdir + @"\c1flash_movie_fade.swf");
		}
	}
}
