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

namespace LevelEditor
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{

        #region Designer Generated Declarations
        
        private System.Windows.Forms.Panel panelWorkSpace;
        private System.Windows.Forms.StatusBar statusBar1;
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.StatusBarPanel statusBarPanel1;
        private System.Windows.Forms.StatusBarPanel statusBarPanel2;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
        private System.Windows.Forms.MenuItem menuItem7;
        private System.Windows.Forms.MenuItem menuItemAddStage;
        private System.Windows.Forms.MenuItem menuItem9;
        private System.Windows.Forms.MenuItem menuItemRemoveStage;
        private System.Windows.Forms.MenuItem menuItemOpen;
        private System.Windows.Forms.MenuItem menuItemExit;
        private System.Windows.Forms.MenuItem menuItemSaveAs;
        private System.Windows.Forms.ListBox lbWorldObjects;
        private System.Windows.Forms.ListBox lbCollisionBoxes;
        private System.Windows.Forms.ListBox lbStages;
        private System.Windows.Forms.ListBox lbShapes;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.GroupBox groupBox3;
        private System.Windows.Forms.GroupBox groupBox4;
        private System.Windows.Forms.GroupBox groupBox5;
        private System.Windows.Forms.GroupBox groupBox6;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.GroupBox groupBox7;
        private System.Windows.Forms.Button buttonAddStage;
        private System.Windows.Forms.Button buttonDeleteStage;
        private System.Windows.Forms.Button buttonEditImage;
        private System.Windows.Forms.Button buttonSave;
        private System.Windows.Forms.Button buttonSelectMode;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

        #endregion

        #region Declarations

        private int iGridSize = 10;

        private int iCurrMode = 0;

        private bool bDraggingRect = false;

        private bool bShowGrid = true;

        private enum enumMode {None, Select, NewCollisionBox, NewWorldObject ,Resize, MovingWO, MovingCB};

        private enum enumCollisionBoxStrings {CollisionBox, CB_Floor, CB_Wall, CB_Floor_NegSlope, CB_Floor_PosSlope, 
                                              CB_Ceiling_NegSlope, CB_Ceiling_PosSlope, Transition, Transition100};

        private string[] strCollisionBoxes = {"Collision Box", "CB_Floor", "CB_Wall", "CB_Floor_NegSlope", "CB_Floor_PosSlope", 
                                              "CB_Ceiling_NegSlope", "CB_Ceiling_PosSlope", "Transition", "Transition100"};

        private enum enumShapes {Rectangle, Rect_Floor, Rect_Wall, Tri_Floor_NegSlope, Tri_Floor_PosSlope,
                                 Tri_Ceiling_NegSlope, Tri_Ceiling_PosSlope};

        private string[] strShapes = {"Rectangle", "Rect_Floor", "Rect_Wall", "Tri_Floor_NegSlope", "Tri_Floor_PosSlope",
                                  "Tri_Ceiling_NegSlope", "Tri_Ceiling_PosSlope"};

        private enum enumWorldObjStrings {Merc, MovingPlatform, MovingPlatformHor, MovingPlatformVer, Tux, Geek, GNU_Robot, Zoom};

        private string[] strWorldObjs = {"Merc", "MovingPlatform", "MovingPlatformHor", "MovingPlatformVer", "Tux", "Geek", "GNU Robot", "Zoom"};

        private int iResizingState = 0;

        private Rectangle rectCurr = new Rectangle();
        
        private Rectangle rectTemp = new Rectangle();

        private Icarus.Logic.WorldObject woCurr = null;

        private Icarus.Logic.Level level = null;

        private string strLevelPath = null;

        private Point pntCurr = new Point();
        
        private Point pntPrev = new Point();

        private Icarus.Logic.Physics.CollisionBox cbCurr = null;

        private enum enumDirection{None, Up, Down, Left, Right};

        private int iResizeBoxSize = 10;

        private Rectangle rectResizeTop = new Rectangle();
        private Rectangle rectResizeBottom = new Rectangle();
        private Rectangle rectResizeLeft = new Rectangle();
        private Rectangle rectResizeRight = new Rectangle();

        //Pens

        private Pen penGrid = new Pen(System.Drawing.Color.Gray);
        private Pen penSelected = new Pen(System.Drawing.Color.Red);
        private Pen penUnselected = new Pen(System.Drawing.Color.LightGreen);
        private Pen penWorldObj = new Pen(System.Drawing.Color.Pink);
        private Pen penInternal = new Pen(System.Drawing.Color.White);
        private Pen penWorldObjChild = new Pen(System.Drawing.Color.LightPink);

        private System.Windows.Forms.Button buttonDeleteSelected;
        private System.Windows.Forms.Button buttonToggleGrid;
        private System.Windows.Forms.MenuItem menuItemToggleGrid;
        private System.Windows.Forms.GroupBox groupBoxTransitionEditor;
        private System.Windows.Forms.TextBox textBoxTransitionY;
        private System.Windows.Forms.TextBox textBoxTransitionNext;
        private System.Windows.Forms.TextBox textBoxTransitionX;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label6;
        private System.Windows.Forms.Label label7;
        private System.Windows.Forms.Button buttonTransitionSet;
        private System.Windows.Forms.Label label8;
        private System.Windows.Forms.Label label9;
        private System.Windows.Forms.Label label10;
        private System.Windows.Forms.TextBox textBoxXviewcenter;
        private System.Windows.Forms.GroupBox groupBoxZoomFactor;
        private System.Windows.Forms.Label label11;
        private System.Windows.Forms.TextBox textBoxZoomFactor;
        private System.Windows.Forms.TextBox textBoxYveiwcenter;

        #endregion

        #region Construction

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

            //Set stages string array to size of max stages
            strShapes = new string[Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages];

            //Setup Collision object listbox
            for(int i = 0; i < strCollisionBoxes.Length; i++)
            {
                if(strCollisionBoxes[i] == null) break;

                lbCollisionBoxes.Items.Add(strCollisionBoxes[i]);
            }

            //Setup the world object listbox
            for(int i = 0; i < strWorldObjs.Length; i++)
            {
                if(strWorldObjs[i] == null) break;

                lbWorldObjects.Items.Add(strWorldObjs[i]);
            }

            //Setup the shape listbox
            for(int i = 0; i < strShapes.Length; i++)
            {
                if(strShapes[i] == null) break;

                lbShapes.Items.Add(strShapes[i]);
            }

            Icarus.Graphics.Animation.AnimationFactory.setInstance(new Icarus.Graphics.Animation.GDI.GDIAnimationFactory());

            //Check to see if the config exists
            Icarus.Resources.ConfigParser cp = null;

            //Set level content path
            try 
            {
                cp = new Icarus.Resources.ConfigParser("config.icfg");
            }
            catch (Exception) 
            {
                //Too bad... Crash
            }

			Icarus.Resources.ResourceManager.Instance.BasePath = cp["Load_Asset_Path"];

            level = new Icarus.Logic.Level();
            //level.Stages[0] = new Icarus.Logic.Stage();
            
            rectResizeTop.Width = rectResizeBottom.Width = rectResizeLeft.Width = rectResizeRight.Width = iResizeBoxSize;
            rectResizeTop.Height = rectResizeBottom.Height = rectResizeLeft.Height = rectResizeRight.Height = iResizeBoxSize;

			// Do a silly useless thing so the smart compiler does not discard our reference to tuxhunter:
			bool discard = TuxHunter.TuxHunterSettings.Instance.ShowSnappy;
		}

        #endregion

        #region Disposal

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

        #endregion

		#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.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
            this.panelWorkSpace = new System.Windows.Forms.Panel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
            this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItemOpen = new System.Windows.Forms.MenuItem();
            this.menuItemSaveAs = new System.Windows.Forms.MenuItem();
            this.menuItemExit = new System.Windows.Forms.MenuItem();
            this.menuItem7 = new System.Windows.Forms.MenuItem();
            this.menuItemAddStage = new System.Windows.Forms.MenuItem();
            this.menuItemRemoveStage = new System.Windows.Forms.MenuItem();
            this.menuItem9 = new System.Windows.Forms.MenuItem();
            this.menuItemToggleGrid = new System.Windows.Forms.MenuItem();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.lbWorldObjects = new System.Windows.Forms.ListBox();
            this.lbCollisionBoxes = new System.Windows.Forms.ListBox();
            this.lbStages = new System.Windows.Forms.ListBox();
            this.lbShapes = new System.Windows.Forms.ListBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.groupBox4 = new System.Windows.Forms.GroupBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.groupBox5 = new System.Windows.Forms.GroupBox();
            this.groupBox6 = new System.Windows.Forms.GroupBox();
            this.label3 = new System.Windows.Forms.Label();
            this.groupBox7 = new System.Windows.Forms.GroupBox();
            this.buttonDeleteStage = new System.Windows.Forms.Button();
            this.buttonAddStage = new System.Windows.Forms.Button();
            this.buttonEditImage = new System.Windows.Forms.Button();
            this.buttonSave = new System.Windows.Forms.Button();
            this.buttonSelectMode = new System.Windows.Forms.Button();
            this.buttonDeleteSelected = new System.Windows.Forms.Button();
            this.buttonToggleGrid = new System.Windows.Forms.Button();
            this.groupBoxTransitionEditor = new System.Windows.Forms.GroupBox();
            this.textBoxYveiwcenter = new System.Windows.Forms.TextBox();
            this.textBoxXviewcenter = new System.Windows.Forms.TextBox();
            this.label9 = new System.Windows.Forms.Label();
            this.label10 = new System.Windows.Forms.Label();
            this.label8 = new System.Windows.Forms.Label();
            this.buttonTransitionSet = new System.Windows.Forms.Button();
            this.label7 = new System.Windows.Forms.Label();
            this.label6 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.textBoxTransitionX = new System.Windows.Forms.TextBox();
            this.textBoxTransitionNext = new System.Windows.Forms.TextBox();
            this.textBoxTransitionY = new System.Windows.Forms.TextBox();
            this.groupBoxZoomFactor = new System.Windows.Forms.GroupBox();
            this.label11 = new System.Windows.Forms.Label();
            this.textBoxZoomFactor = new System.Windows.Forms.TextBox();
            this.panelWorkSpace.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
            this.groupBox4.SuspendLayout();
            this.groupBox6.SuspendLayout();
            this.groupBox7.SuspendLayout();
            this.groupBoxTransitionEditor.SuspendLayout();
            this.groupBoxZoomFactor.SuspendLayout();
            this.SuspendLayout();
            // 
            // panelWorkSpace
            // 
            this.panelWorkSpace.AutoScroll = ((bool)(configurationAppSettings.GetValue("panelWorkSpace.AutoScroll", typeof(bool))));
            this.panelWorkSpace.BackColor = System.Drawing.SystemColors.ControlDark;
            this.panelWorkSpace.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panelWorkSpace.Controls.Add(this.pictureBox1);
            this.panelWorkSpace.Location = new System.Drawing.Point(280, 8);
            this.panelWorkSpace.Name = "panelWorkSpace";
            this.panelWorkSpace.Size = new System.Drawing.Size(504, 512);
            this.panelWorkSpace.TabIndex = 0;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(16, 16);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
            this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
            // 
            // statusBar1
            // 
            this.statusBar1.Location = new System.Drawing.Point(0, 763);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
                                                                                          this.statusBarPanel1,
                                                                                          this.statusBarPanel2});
            this.statusBar1.ShowPanels = true;
            this.statusBar1.Size = new System.Drawing.Size(1256, 22);
            this.statusBar1.TabIndex = 1;
            this.statusBar1.Text = "statusBar1";
            // 
            // statusBarPanel1
            // 
            this.statusBarPanel1.Text = "Status";
            this.statusBarPanel1.Width = 400;
            // 
            // statusBarPanel2
            // 
            this.statusBarPanel2.Alignment = System.Windows.Forms.HorizontalAlignment.Right;
            this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
            this.statusBarPanel2.Width = 840;
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                      this.menuItem1,
                                                                                      this.menuItem7,
                                                                                      this.menuItem9});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                      this.menuItemOpen,
                                                                                      this.menuItemSaveAs,
                                                                                      this.menuItemExit});
            this.menuItem1.Text = "File";
            // 
            // menuItemOpen
            // 
            this.menuItemOpen.Index = 0;
            this.menuItemOpen.Text = "Open";
            this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click);
            // 
            // menuItemSaveAs
            // 
            this.menuItemSaveAs.Index = 1;
            this.menuItemSaveAs.Text = "Save as";
            this.menuItemSaveAs.Click += new System.EventHandler(this.menuItemSaveAs_Click);
            // 
            // menuItemExit
            // 
            this.menuItemExit.Index = 2;
            this.menuItemExit.Text = "Exit";
            this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click);
            // 
            // menuItem7
            // 
            this.menuItem7.Index = 1;
            this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                      this.menuItemAddStage,
                                                                                      this.menuItemRemoveStage});
            this.menuItem7.Text = "Edit";
            // 
            // menuItemAddStage
            // 
            this.menuItemAddStage.Index = 0;
            this.menuItemAddStage.Text = "Add Stage";
            this.menuItemAddStage.Click += new System.EventHandler(this.menuItemAddStage_Click);
            // 
            // menuItemRemoveStage
            // 
            this.menuItemRemoveStage.Index = 1;
            this.menuItemRemoveStage.Text = "Remove Stage";
            this.menuItemRemoveStage.Click += new System.EventHandler(this.menuItemRemoveStage_Click);
            // 
            // menuItem9
            // 
            this.menuItem9.Index = 2;
            this.menuItem9.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                      this.menuItemToggleGrid});
            this.menuItem9.Text = "Options";
            // 
            // menuItemToggleGrid
            // 
            this.menuItemToggleGrid.Index = 0;
            this.menuItemToggleGrid.Text = "Toggle Grid";
            this.menuItemToggleGrid.Click += new System.EventHandler(this.menuItemToggleGrid_Click);
            // 
            // lbWorldObjects
            // 
            this.lbWorldObjects.Location = new System.Drawing.Point(8, 32);
            this.lbWorldObjects.Name = "lbWorldObjects";
            this.lbWorldObjects.Size = new System.Drawing.Size(120, 43);
            this.lbWorldObjects.TabIndex = 2;
            this.lbWorldObjects.SelectedIndexChanged += new System.EventHandler(this.lbWorldObjects_SelectedIndexChanged);
            // 
            // lbCollisionBoxes
            // 
            this.lbCollisionBoxes.Location = new System.Drawing.Point(8, 32);
            this.lbCollisionBoxes.Name = "lbCollisionBoxes";
            this.lbCollisionBoxes.Size = new System.Drawing.Size(120, 56);
            this.lbCollisionBoxes.TabIndex = 3;
            this.lbCollisionBoxes.TabIndexChanged += new System.EventHandler(this.lbCollisionBoxes_TabIndexChanged);
            this.lbCollisionBoxes.SelectedIndexChanged += new System.EventHandler(this.lbCollisionBoxes_SelectedIndexChanged);
            // 
            // lbStages
            // 
            this.lbStages.Location = new System.Drawing.Point(8, 16);
            this.lbStages.Name = "lbStages";
            this.lbStages.Size = new System.Drawing.Size(168, 69);
            this.lbStages.TabIndex = 5;
            this.lbStages.SelectedIndexChanged += new System.EventHandler(this.lbStages_SelectedIndexChanged);
            // 
            // lbShapes
            // 
            this.lbShapes.Location = new System.Drawing.Point(136, 32);
            this.lbShapes.Name = "lbShapes";
            this.lbShapes.Size = new System.Drawing.Size(120, 56);
            this.lbShapes.TabIndex = 6;
            // 
            // groupBox1
            // 
            this.groupBox1.Location = new System.Drawing.Point(8, 344);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(264, 40);
            this.groupBox1.TabIndex = 7;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Level Properties";
            // 
            // groupBox2
            // 
            this.groupBox2.Location = new System.Drawing.Point(8, 392);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(264, 40);
            this.groupBox2.TabIndex = 8;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Stage Properties";
            // 
            // groupBox3
            // 
            this.groupBox3.Location = new System.Drawing.Point(8, 440);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(264, 40);
            this.groupBox3.TabIndex = 9;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "Current Box Properties";
            // 
            // groupBox4
            // 
            this.groupBox4.Controls.Add(this.lbCollisionBoxes);
            this.groupBox4.Controls.Add(this.lbShapes);
            this.groupBox4.Controls.Add(this.label1);
            this.groupBox4.Controls.Add(this.label2);
            this.groupBox4.Location = new System.Drawing.Point(8, 144);
            this.groupBox4.Name = "groupBox4";
            this.groupBox4.Size = new System.Drawing.Size(264, 96);
            this.groupBox4.TabIndex = 10;
            this.groupBox4.TabStop = false;
            this.groupBox4.Text = "Collision Boxes";
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(40, 23);
            this.label1.TabIndex = 13;
            this.label1.Text = "Boxes";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(136, 16);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(48, 23);
            this.label2.TabIndex = 13;
            this.label2.Text = "Shapes";
            // 
            // groupBox5
            // 
            this.groupBox5.Location = new System.Drawing.Point(8, 488);
            this.groupBox5.Name = "groupBox5";
            this.groupBox5.Size = new System.Drawing.Size(264, 40);
            this.groupBox5.TabIndex = 11;
            this.groupBox5.TabStop = false;
            this.groupBox5.Text = "Current Shape Properties";
            // 
            // groupBox6
            // 
            this.groupBox6.Controls.Add(this.lbWorldObjects);
            this.groupBox6.Controls.Add(this.label3);
            this.groupBox6.Location = new System.Drawing.Point(8, 248);
            this.groupBox6.Name = "groupBox6";
            this.groupBox6.Size = new System.Drawing.Size(264, 88);
            this.groupBox6.TabIndex = 12;
            this.groupBox6.TabStop = false;
            this.groupBox6.Text = "World Objects";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 16);
            this.label3.Name = "label3";
            this.label3.TabIndex = 13;
            this.label3.Text = "Players/Items/etc.";
            // 
            // groupBox7
            // 
            this.groupBox7.Controls.Add(this.buttonDeleteStage);
            this.groupBox7.Controls.Add(this.buttonAddStage);
            this.groupBox7.Controls.Add(this.lbStages);
            this.groupBox7.Controls.Add(this.buttonEditImage);
            this.groupBox7.Location = new System.Drawing.Point(8, 40);
            this.groupBox7.Name = "groupBox7";
            this.groupBox7.Size = new System.Drawing.Size(264, 96);
            this.groupBox7.TabIndex = 13;
            this.groupBox7.TabStop = false;
            this.groupBox7.Text = "Stages";
            // 
            // buttonDeleteStage
            // 
            this.buttonDeleteStage.Location = new System.Drawing.Point(184, 40);
            this.buttonDeleteStage.Name = "buttonDeleteStage";
            this.buttonDeleteStage.Size = new System.Drawing.Size(72, 23);
            this.buttonDeleteStage.TabIndex = 7;
            this.buttonDeleteStage.Text = "Delete";
            this.buttonDeleteStage.Click += new System.EventHandler(this.buttonDeleteStage_Click);
            // 
            // buttonAddStage
            // 
            this.buttonAddStage.Location = new System.Drawing.Point(184, 16);
            this.buttonAddStage.Name = "buttonAddStage";
            this.buttonAddStage.Size = new System.Drawing.Size(72, 23);
            this.buttonAddStage.TabIndex = 6;
            this.buttonAddStage.Text = "Add";
            this.buttonAddStage.Click += new System.EventHandler(this.buttonAddStage_Click);
            // 
            // buttonEditImage
            // 
            this.buttonEditImage.Location = new System.Drawing.Point(184, 64);
            this.buttonEditImage.Name = "buttonEditImage";
            this.buttonEditImage.Size = new System.Drawing.Size(72, 23);
            this.buttonEditImage.TabIndex = 14;
            this.buttonEditImage.Text = "Edit Stage";
            this.buttonEditImage.Click += new System.EventHandler(this.buttonEditImage_Click);
            // 
            // buttonSave
            // 
            this.buttonSave.Location = new System.Drawing.Point(8, 8);
            this.buttonSave.Name = "buttonSave";
            this.buttonSave.Size = new System.Drawing.Size(32, 32);
            this.buttonSave.TabIndex = 14;
            this.buttonSave.Text = "Save";
            this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
            // 
            // buttonSelectMode
            // 
            this.buttonSelectMode.Location = new System.Drawing.Point(160, 8);
            this.buttonSelectMode.Name = "buttonSelectMode";
            this.buttonSelectMode.Size = new System.Drawing.Size(48, 32);
            this.buttonSelectMode.TabIndex = 15;
            this.buttonSelectMode.Text = "SelectMode";
            this.buttonSelectMode.Click += new System.EventHandler(this.buttonSelectMode_Click);
            // 
            // buttonDeleteSelected
            // 
            this.buttonDeleteSelected.Location = new System.Drawing.Point(216, 8);
            this.buttonDeleteSelected.Name = "buttonDeleteSelected";
            this.buttonDeleteSelected.Size = new System.Drawing.Size(56, 32);
            this.buttonDeleteSelected.TabIndex = 16;
            this.buttonDeleteSelected.Text = "Delete Selected";
            this.buttonDeleteSelected.Click += new System.EventHandler(this.buttonDeleteSelected_Click);
            // 
            // buttonToggleGrid
            // 
            this.buttonToggleGrid.Location = new System.Drawing.Point(80, 8);
            this.buttonToggleGrid.Name = "buttonToggleGrid";
            this.buttonToggleGrid.Size = new System.Drawing.Size(48, 32);
            this.buttonToggleGrid.TabIndex = 17;
            this.buttonToggleGrid.Text = "Toggle Grid";
            this.buttonToggleGrid.Click += new System.EventHandler(this.buttonToggleGrid_Click);
            // 
            // groupBoxTransitionEditor
            // 
            this.groupBoxTransitionEditor.Controls.Add(this.textBoxYveiwcenter);
            this.groupBoxTransitionEditor.Controls.Add(this.textBoxXviewcenter);
            this.groupBoxTransitionEditor.Controls.Add(this.label9);
            this.groupBoxTransitionEditor.Controls.Add(this.label10);
            this.groupBoxTransitionEditor.Controls.Add(this.label8);
            this.groupBoxTransitionEditor.Controls.Add(this.buttonTransitionSet);
            this.groupBoxTransitionEditor.Controls.Add(this.label7);
            this.groupBoxTransitionEditor.Controls.Add(this.label6);
            this.groupBoxTransitionEditor.Controls.Add(this.label5);
            this.groupBoxTransitionEditor.Controls.Add(this.label4);
            this.groupBoxTransitionEditor.Controls.Add(this.textBoxTransitionX);
            this.groupBoxTransitionEditor.Controls.Add(this.textBoxTransitionNext);
            this.groupBoxTransitionEditor.Controls.Add(this.textBoxTransitionY);
            this.groupBoxTransitionEditor.Location = new System.Drawing.Point(8, 600);
            this.groupBoxTransitionEditor.Name = "groupBoxTransitionEditor";
            this.groupBoxTransitionEditor.Size = new System.Drawing.Size(264, 128);
            this.groupBoxTransitionEditor.TabIndex = 18;
            this.groupBoxTransitionEditor.TabStop = false;
            this.groupBoxTransitionEditor.Text = "Transition Editor";
            this.groupBoxTransitionEditor.Visible = false;
            // 
            // textBoxYveiwcenter
            // 
            this.textBoxYveiwcenter.Location = new System.Drawing.Point(80, 96);
            this.textBoxYveiwcenter.Name = "textBoxYveiwcenter";
            this.textBoxYveiwcenter.Size = new System.Drawing.Size(24, 20);
            this.textBoxYveiwcenter.TabIndex = 13;
            this.textBoxYveiwcenter.Text = "";
            // 
            // textBoxXviewcenter
            // 
            this.textBoxXviewcenter.Location = new System.Drawing.Point(24, 96);
            this.textBoxXviewcenter.Name = "textBoxXviewcenter";
            this.textBoxXviewcenter.Size = new System.Drawing.Size(24, 20);
            this.textBoxXviewcenter.TabIndex = 12;
            this.textBoxXviewcenter.Text = "";
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(64, 104);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(16, 16);
            this.label9.TabIndex = 11;
            this.label9.Text = "Y:";
            // 
            // label10
            // 
            this.label10.Location = new System.Drawing.Point(8, 104);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(16, 16);
            this.label10.TabIndex = 10;
            this.label10.Text = "X:";
            // 
            // label8
            // 
            this.label8.Location = new System.Drawing.Point(8, 72);
            this.label8.Name = "label8";
            this.label8.TabIndex = 9;
            this.label8.Text = "New View Center:";
            // 
            // buttonTransitionSet
            // 
            this.buttonTransitionSet.Location = new System.Drawing.Point(184, 96);
            this.buttonTransitionSet.Name = "buttonTransitionSet";
            this.buttonTransitionSet.Size = new System.Drawing.Size(48, 23);
            this.buttonTransitionSet.TabIndex = 8;
            this.buttonTransitionSet.Text = "Set";
            this.buttonTransitionSet.Click += new System.EventHandler(this.buttonTransitionSet_Click);
            // 
            // label7
            // 
            this.label7.Location = new System.Drawing.Point(120, 48);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(96, 16);
            this.label7.TabIndex = 7;
            this.label7.Text = "Next State Index:";
            // 
            // label6
            // 
            this.label6.Location = new System.Drawing.Point(64, 48);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(16, 16);
            this.label6.TabIndex = 6;
            this.label6.Text = "Y:";
            // 
            // label5
            // 
            this.label5.Location = new System.Drawing.Point(8, 48);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(16, 16);
            this.label5.TabIndex = 5;
            this.label5.Text = "X:";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(8, 16);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(112, 23);
            this.label4.TabIndex = 4;
            this.label4.Text = "New Player Position:";
            // 
            // textBoxTransitionX
            // 
            this.textBoxTransitionX.Location = new System.Drawing.Point(24, 40);
            this.textBoxTransitionX.Name = "textBoxTransitionX";
            this.textBoxTransitionX.Size = new System.Drawing.Size(24, 20);
            this.textBoxTransitionX.TabIndex = 3;
            this.textBoxTransitionX.Text = "";
            // 
            // textBoxTransitionNext
            // 
            this.textBoxTransitionNext.Location = new System.Drawing.Point(216, 40);
            this.textBoxTransitionNext.Name = "textBoxTransitionNext";
            this.textBoxTransitionNext.Size = new System.Drawing.Size(24, 20);
            this.textBoxTransitionNext.TabIndex = 2;
            this.textBoxTransitionNext.Text = "";
            // 
            // textBoxTransitionY
            // 
            this.textBoxTransitionY.Location = new System.Drawing.Point(80, 40);
            this.textBoxTransitionY.Name = "textBoxTransitionY";
            this.textBoxTransitionY.Size = new System.Drawing.Size(24, 20);
            this.textBoxTransitionY.TabIndex = 1;
            this.textBoxTransitionY.Text = "";
            // 
            // groupBoxZoomFactor
            // 
            this.groupBoxZoomFactor.Controls.Add(this.textBoxZoomFactor);
            this.groupBoxZoomFactor.Controls.Add(this.label11);
            this.groupBoxZoomFactor.Location = new System.Drawing.Point(8, 536);
            this.groupBoxZoomFactor.Name = "groupBoxZoomFactor";
            this.groupBoxZoomFactor.Size = new System.Drawing.Size(264, 56);
            this.groupBoxZoomFactor.TabIndex = 19;
            this.groupBoxZoomFactor.TabStop = false;
            this.groupBoxZoomFactor.Text = "Zoom Factor";
            // 
            // label11
            // 
            this.label11.Location = new System.Drawing.Point(8, 24);
            this.label11.Name = "label11";
            this.label11.Size = new System.Drawing.Size(56, 23);
            this.label11.TabIndex = 1;
            this.label11.Text = "Zoom to:";
            // 
            // textBoxZoomFactor
            // 
            this.textBoxZoomFactor.Location = new System.Drawing.Point(56, 16);
            this.textBoxZoomFactor.Name = "textBoxZoomFactor";
            this.textBoxZoomFactor.TabIndex = 2;
            this.textBoxZoomFactor.Text = "";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1256, 785);
            this.Controls.Add(this.groupBoxZoomFactor);
            this.Controls.Add(this.groupBoxTransitionEditor);
            this.Controls.Add(this.statusBar1);
            this.Controls.Add(this.buttonToggleGrid);
            this.Controls.Add(this.buttonDeleteSelected);
            this.Controls.Add(this.buttonSelectMode);
            this.Controls.Add(this.buttonSave);
            this.Controls.Add(this.groupBox7);
            this.Controls.Add(this.groupBox6);
            this.Controls.Add(this.groupBox5);
            this.Controls.Add(this.groupBox4);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.panelWorkSpace);
            this.Menu = this.mainMenu1;
            this.Name = "Form1";
            this.Text = "Icarus Level Editor";
            this.Resize += new System.EventHandler(this.Form1_Resize);
            this.Load += new System.EventHandler(this.FormMain_Load);
            this.panelWorkSpace.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
            this.groupBox4.ResumeLayout(false);
            this.groupBox6.ResumeLayout(false);
            this.groupBox7.ResumeLayout(false);
            this.groupBoxTransitionEditor.ResumeLayout(false);
            this.groupBoxZoomFactor.ResumeLayout(false);
            this.ResumeLayout(false);

        }
		#endregion

        #region Main()

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

        #endregion

        #region Event Handlers

        private void FormMain_Load(object sender, System.EventArgs e)
        {
        
        }

        public void Form1_Resize(object sender, System.EventArgs e)
        {
            panelWorkSpace.Size = new Size(this.Size.Width-this.panelWorkSpace.Left-14, this.Size.Height-this.panelWorkSpace.Top-79);
        }

        private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            
            
            if(bShowGrid)
            {
                this.drawGrid(g, e.ClipRectangle, this.iGridSize);
            }
            
            if(bDraggingRect)
            {
                g.DrawRectangle(penSelected, rectCurr);
            }

            drawCollisionBoxes(level, g);
            drawWorldObjects(level, g);
            
        }




        private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //Only use left mouse button
            if(e.Button != System.Windows.Forms.MouseButtons.Left) return;

            iResizingState = isResizing(e.X, e.Y);

            //Check to see if we are trying to move a box or worldObj
            if(woCurr != null)
            {
                rectTemp.X = woCurr.CollisionBox.BoundingBox.X;
                rectTemp.Y = woCurr.CollisionBox.BoundingBox.Y;
                rectTemp.Width = woCurr.CollisionBox.BoundingBoxWidth;
                rectTemp.Height = woCurr.CollisionBox.BoundingBoxHeight;
                if(rectTemp.Contains(e.X, e.Y))
                {
                    iCurrMode = (int)enumMode.MovingWO;
                }
            }
            else if(cbCurr != null)
            {
                rectTemp.X = cbCurr.BoundingBox.X;
                rectTemp.Y = cbCurr.BoundingBox.Y;
                rectTemp.Width = cbCurr.BoundingBox.Width;
                rectTemp.Height = cbCurr.BoundingBox.Height;
                if(rectTemp.Contains(e.X, e.Y))
                {
                    iCurrMode = (int)enumMode.MovingCB;
                }
            }
            
            if(iResizingState != (int)enumDirection.None)
            {
                iCurrMode = (int)enumMode.Resize;
            }

            if(iCurrMode == (int)enumMode.Select)
            {
                selectBox(e.X, e.Y);
            }
                        
            findClosestGridPoint(e.X, e.Y, ref(pntCurr), this.iGridSize);
            this.rectCurr.X = pntCurr.X;
            this.rectCurr.Y = pntCurr.Y;
            bDraggingRect = true;
        }

        private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //Only use left mouse button
            if(e.Button != System.Windows.Forms.MouseButtons.Left) return;

            //Update status bar
            statusBarPanel2.Text = "X:"+e.X.ToString()+",Y:"+e.Y.ToString();
            
            //Save previous point
            pntPrev.X = pntCurr.X;
            pntPrev.Y = pntCurr.Y;

            //Find nearest grid point
            findClosestGridPoint(e.X, e.Y, ref(pntCurr), iGridSize);

            if(bDraggingRect)
            {
                //If the grid point is new update the screen
                if(pntCurr.X != pntPrev.X || pntCurr.Y != pntPrev.Y)
                {
                    this.rectCurr.Width =  this.pntCurr.X - this.rectCurr.X;
                    this.rectCurr.Height = this.pntCurr.Y - this.rectCurr.Y;
                
                    //TODO: Be more specific with invalidation request
                    pictureBox1.Invalidate();
                }
                //pictureBox1.Invalidate();
            }
            
            if(iCurrMode == (int)enumMode.MovingWO)
            {
                //Don't show temp rect while moving WO
                bDraggingRect = false;

                //Movement in Y
                if(pntCurr.Y != pntPrev.Y)
                {
                    woCurr.Location.y += pntCurr.Y - pntPrev.Y;

                    //TODO: Be more specific with invalidation request
                    pictureBox1.Invalidate();
                }
                
                //Movement in X
                if(pntCurr.X != pntPrev.X)
                {
                    woCurr.Location.x += pntCurr.X - pntPrev.X;

                    //TODO: Be more specific with invalidation request
                    pictureBox1.Invalidate();
                }
            }
            else if(iCurrMode == (int)enumMode.MovingCB)
            {
                //don't show temp rect while moving CB
                bDraggingRect = false;

                //Movement in Y
                if(pntCurr.Y != pntPrev.Y)
                {
                    cbCurr.BoundingBox.Y += pntCurr.Y - pntPrev.Y;
                    cbCurr.setSpecial();

                    //TODO: Be more specific with invalidation request
                    pictureBox1.Invalidate();
                }
                
                //Movement in X
                if(pntCurr.X != pntPrev.X)
                {
                    cbCurr.BoundingBox.X += pntCurr.X - pntPrev.X;
                    cbCurr.setSpecial();

                    //TODO: Be more specific with invalidation request
                    pictureBox1.Invalidate();
                }
            }
            else if(iCurrMode == (int)enumMode.Resize)
            {
                //Don't show temp rectangle while resizing
                bDraggingRect = false;

                //Resize
                if(iResizingState == (int)enumDirection.Up)
                {
                    if(pntCurr.Y != pntPrev.Y)
                    {
                        if(cbCurr.BoundingBox.Y > pntCurr.Y)
                        {
                            cbCurr.BoundingBox.Height += (cbCurr.BoundingBox.Y - pntCurr.Y);
                        }
                        else
                        {
                            cbCurr.BoundingBox.Height -= (pntCurr.Y - cbCurr.BoundingBox.Y);
                        }
                        
                        cbCurr.BoundingBox.Y = pntCurr.Y;
                    }
                }
                else if(iResizingState == (int)enumDirection.Down)
                {
                    if(pntCurr.Y != pntPrev.Y)
                    {
                        if(cbCurr.BoundingBox.Y + cbCurr.BoundingBox.Height > pntCurr.Y)
                        {
                            cbCurr.BoundingBox.Height -= (cbCurr.BoundingBox.Y + cbCurr.BoundingBox.Height - pntCurr.Y);
                        }
                        else
                        {
                            cbCurr.BoundingBox.Height += (pntCurr.Y - (cbCurr.BoundingBox.Y + cbCurr.BoundingBox.Height));
                        }
                        
                    }
                }
                else if(iResizingState == (int)enumDirection.Left)
                {
                    if(pntCurr.X != pntPrev.X)
                    {
                        if(cbCurr.BoundingBox.X > pntCurr.X)
                        {
                            cbCurr.BoundingBox.Width += (cbCurr.BoundingBox.X - pntCurr.X);
                        }
                        else
                        {
                            cbCurr.BoundingBox.Width -= (pntCurr.X - cbCurr.BoundingBox.X);
                        }
                        
                        cbCurr.BoundingBox.X = pntCurr.X;
                    }
                }
                else if(iResizingState == (int)enumDirection.Right)
                {
                    if(pntCurr.X != pntPrev.X)
                    {
                        if(cbCurr.BoundingBox.X + cbCurr.BoundingBox.Width > pntCurr.X)
                        {
                            cbCurr.BoundingBox.Width -= (cbCurr.BoundingBox.X + cbCurr.BoundingBox.Width - pntCurr.X);
                        }
                        else
                        {
                            cbCurr.BoundingBox.Width += (pntCurr.X  - (cbCurr.BoundingBox.X + cbCurr.BoundingBox.Width));
                        }                        
                    }
                }
                
                //setSpecial settings if needed
                cbCurr.setSpecial();

                //TODO: Be more specific with invalidation request
                pictureBox1.Invalidate();
            }
            
        }

        private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            int i;
            
            //Only use left mouse button
            if(e.Button != System.Windows.Forms.MouseButtons.Left) return;

            bDraggingRect = false;
            iResizingState = 0;

            if(iCurrMode == (int)enumMode.NewWorldObject)
            {

                //check index to create at(unless it is Merc, but we can find it anyway)
                for(i = 1; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxActors; i++)
                {
                    if(level.CurrStage.actorList[i] == null)
                    {

                        //Check which object to create
                        if(lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.Merc]))
                        {
                            level.CurrStage.actorList[level.CurrStage.PlayerActorIndex] = new Icarus.Logic.Physics.PhysicsMerc();
                            level.CurrStage.actorList[level.CurrStage.PlayerActorIndex].ControllerType = "TuxHunter.Controllers.UserInputController";
                            //TODO: do not hardcode animation.
                            level.CurrStage.actorList[level.CurrStage.PlayerActorIndex].SetAnimation("merc\\stand_left\\frameset\\stand_left.png", Icarus.Graphics.Animation.AnimationType.LoopForever, 1);
                            level.CurrStage.actorList[level.CurrStage.PlayerActorIndex].Location.x = e.X;
                            level.CurrStage.actorList[level.CurrStage.PlayerActorIndex].Location.y = e.Y;
                            
                            //TODO: Be more specific with invalidation
                            pictureBox1.Invalidate();
                            break;
                        }
                        else if(lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.MovingPlatform]))
                        {
                            level.CurrStage.actorList[i] = new Icarus.Logic.Physics.MovingPlatform();
                            level.CurrStage.actorList[i].Location.x = e.X;
                            level.CurrStage.actorList[i].Location.y = e.Y;
                            level.CurrStage.actorList[i].ControllerType = "TuxHunter.Controllers.MovingPlatformController";

                            
                            //TODO: Be more specific with invalidation
                            pictureBox1.Invalidate();
                            break;
                        }
                        else if(lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.MovingPlatformHor]))
                        {
                            level.CurrStage.actorList[i] = new Icarus.Logic.Physics.MovingPlatformHorizontal();
                            level.CurrStage.actorList[i].Location.x = e.X;
                            level.CurrStage.actorList[i].Location.y = e.Y;
                            ((Icarus.Logic.Physics.MovingPlatformHorizontal)level.CurrStage.actorList[i]).Speed = 2.5;
                            ((Icarus.Logic.Physics.MovingPlatformHorizontal)level.CurrStage.actorList[i]).Distance = 505;
                            level.CurrStage.actorList[i].SetAnimation(@"\moving_platform\platform.png", Icarus.Graphics.Animation.AnimationType.LoopForever, 1);
                            level.CurrStage.actorList[i].CollisionBox = new Icarus.Logic.Physics.CollisionBoxMobile();
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).WO = level.CurrStage.actorList[i];
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.X = 0;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.Y = 0;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.Width = 95;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.Height = 22;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBoxWidth = 95;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBoxHeight = 22;
                            level.CurrStage.actorList[i].CBXoffset = 0;
                            level.CurrStage.actorList[i].CBYoffset = 0;

                            pictureBox1.Invalidate();
                            break;

                        }
                        else if(lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.MovingPlatformVer]))
                        {
                            level.CurrStage.actorList[i] = new Icarus.Logic.Physics.MovingPlatformVertical();
                            level.CurrStage.actorList[i].Location.x = e.X;
                            level.CurrStage.actorList[i].Location.y = e.Y;
                            ((Icarus.Logic.Physics.MovingPlatformVertical)level.CurrStage.actorList[i]).Speed = 3.5;
                            ((Icarus.Logic.Physics.MovingPlatformVertical)level.CurrStage.actorList[i]).Distance = 652;
                            level.CurrStage.actorList[i].SetAnimation(@"\moving_platform\platform.png", Icarus.Graphics.Animation.AnimationType.LoopForever, 1);
                            level.CurrStage.actorList[i].CollisionBox = new Icarus.Logic.Physics.CollisionBoxMobile();
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).WO = level.CurrStage.actorList[i];
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.X = 0;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.Y = 0;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.Width = 95;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBox.Height = 22;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBoxWidth = 95;
                            ((Icarus.Logic.Physics.CollisionBoxMobile)level.CurrStage.actorList[i].CollisionBox).BoundingBoxHeight = 22;
                            level.CurrStage.actorList[i].CBXoffset = 0;
                            level.CurrStage.actorList[i].CBYoffset = 0;
                            

                            pictureBox1.Invalidate();
                            break;

                        }
                        else if (lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.Tux])) 
                        {
                            level.CurrStage.actorList[i] = new Icarus.Logic.Physics.PhysicsTuxRobot();
                            level.CurrStage.actorList[i].ControllerType = "TuxHunter.Controllers.TuxController";

                            level.CurrStage.actorList[i].Location.x = e.X;
                            level.CurrStage.actorList[i].Location.y = e.Y;

                            
                            pictureBox1.Invalidate();
                            break;
                        }
                        else if(lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.Geek]))
                        {
                            level.CurrStage.actorList[i] = new Icarus.Logic.Physics.PhysicsGeek();
                            level.CurrStage.actorList[i].ControllerType = "TuxHunter.Controllers.GeekController";

                            level.CurrStage.actorList[i].Location.x = e.X;
                            level.CurrStage.actorList[i].Location.y = e.Y;

                            pictureBox1.Invalidate();
                            break;

                        }
                        else if(lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.GNU_Robot]))
                        {
                            level.CurrStage.actorList[i] = new Icarus.Logic.Physics.PhysicsGNURobot();
                            level.CurrStage.actorList[i].ControllerType = "TuxHunter.Controllers.BotController";

                            level.CurrStage.actorList[i].Location.x = e.X;
                            level.CurrStage.actorList[i].Location.y = e.Y;

                            pictureBox1.Invalidate();
                            break;
                        }
                        else if(lbWorldObjects.Text.Equals(strWorldObjs[(int)enumWorldObjStrings.Zoom]))
                        {
                            level.CurrStage.actorList[i] = new Icarus.Logic.Physics.ZoomTrigger();
                            level.CurrStage.actorList[i].CollisionBox.BoundingBox.X = 0;
                            level.CurrStage.actorList[i].CollisionBox.BoundingBox.Y = 0;
                            level.CurrStage.actorList[i].CollisionBox.BoundingBox.Width = 500;
                            level.CurrStage.actorList[i].CollisionBox.BoundingBox.Height = 500;
                            level.CurrStage.actorList[i].CollisionBox.BoundingBoxWidth = 500;
                            level.CurrStage.actorList[i].CollisionBox.BoundingBoxHeight = 500;
                            level.CurrStage.actorList[i].Location.x = e.X;
                            level.CurrStage.actorList[i].Location.y = e.Y;
                            ((Icarus.Logic.Physics.ZoomTrigger)level.CurrStage.actorList[i]).ZoomTo = (float)Convert.ToDouble(textBoxZoomFactor.Text.Trim());
                            
                            pictureBox1.Invalidate();
                            break;
                        }
                        else
                        {
                            //Display Error
                            

                            //Do Nothing
                            return;
                        }
                    }
                }
                //Make sure the world obj array wasn't full
                if(i >= Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes)
                {
                    //TODO: display error
                    
                    
                    //Do nothing
                    return;
                }

            }

            if(iCurrMode == (int)enumMode.NewCollisionBox)
            {
                //Determine if a new box should be created.
                if(rectCurr.Width <= 0 || rectCurr.Height <= 0)
                {
                    return;
                }

                //check index to create at
                for(i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes; i++)
                {
                    if(level.CurrStage.CollisionBoxes[i] == null)
                    {
                        //Check which collision object to create

                        //Blank CollisionBox
                        if(lbCollisionBoxes.Text.Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.CollisionBox]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBox();
                        }
                            //Floor
                        else if(lbCollisionBoxes.Text.Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.CB_Floor]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxFloor();
                        }
                            //Wall
                        else if(((string)(lbCollisionBoxes.Text)).Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.CB_Wall]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxWall();
                        }
                            //Slope floor neg
                        else if(((string)(lbCollisionBoxes.Text)).Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.CB_Floor_NegSlope]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxSlopeBL();
                        }
                            //Slope floor pos
                        else if(((string)(lbCollisionBoxes.Text)).Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.CB_Floor_PosSlope]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxSlopeBR();
                        }
                            //Slope Ceiling Neg
                        else if(((string)(lbCollisionBoxes.Text)).Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.CB_Ceiling_NegSlope]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxSlopeTR();
                        }
                            //Slope Ceiling Pos
                        else if(((string)(lbCollisionBoxes.Text)).Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.CB_Ceiling_PosSlope]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxSlopeTL();
                        }
                        else if(((string)(lbCollisionBoxes.Text)).Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.Transition]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxTransition();
                        }
                        else if(((string)(lbCollisionBoxes.Text)).Equals(strCollisionBoxes[(int)enumCollisionBoxStrings.Transition100]))
                        {
                            level.CurrStage.CollisionBoxes[i] = new Icarus.Logic.Physics.CollisionBoxTransition();
                            ((Icarus.Logic.Physics.CollisionBoxTransition)level.CurrStage.CollisionBoxes[i]).Health = 100;
                        }
                        else
                        {
                            //Display Error
                            

                            //Do Nothing
                            return;
                        }

                        //Set the collisionbox's bounding area
                        level.CurrStage.CollisionBoxes[i].BoundingBox.X = rectCurr.X;
                        level.CurrStage.CollisionBoxes[i].BoundingBox.Y = rectCurr.Y;
                        level.CurrStage.CollisionBoxes[i].BoundingBox.Width = rectCurr.Width;
                        level.CurrStage.CollisionBoxes[i].BoundingBox.Height = rectCurr.Height;

                        //Call setSpecial incase it was a prefab CollisionBox
                        level.CurrStage.CollisionBoxes[i].setSpecial();
                        break;
                    }
                }
                //Make sure collision box array wasn't full
                if(i >= Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes)
                {
                    //TODO: display error
                    
                    
                    //Do nothing
                    return;
                }

                //Redraw new rectangle
                rectTemp.X = rectCurr.X;
                rectTemp.Y = rectCurr.Y;
                rectTemp.Width = rectCurr.Width+1;
                rectTemp.Height = rectCurr.Height+1;
                pictureBox1.Invalidate(rectTemp);

                
            }
            else if(iCurrMode == (int)enumMode.Resize)
            {
                iResizingState = 0;
                iCurrMode = (int)enumMode.Select;
            }
            else if(iCurrMode == (int)enumMode.MovingCB)
            {
                iCurrMode = (int)enumMode.Select;
            }
            else if(iCurrMode == (int)enumMode.MovingWO)
            {
                iCurrMode = (int)enumMode.Select;
            }
            else if(iCurrMode == (int)enumMode.Select)
            {
                if(cbCurr != null)
                {
                    if(cbCurr.GetType().Equals(typeof(Icarus.Logic.Physics.CollisionBoxTransition)))
                    {
                        groupBoxTransitionEditor.Visible = true;                    
                    }
                    else
                    {
                        groupBoxTransitionEditor.Visible = false;
                    }
                }
            }



            //Reset rectCurr
            rectCurr.X = 0;
            rectCurr.Y = 0;
            rectCurr.Width = 0;
            rectCurr.Height = 0;
        }

        private void menuItemSaveAs_Click(object sender, System.EventArgs e)
        {

            //TODO: Check to make sure each stage has a default merc location

            saveFileDialog1.Filter = "Level files (*.xml)|*.xml" ;
            saveFileDialog1.FilterIndex = 1 ;
            saveFileDialog1.RestoreDirectory = true ;

            if(saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                strLevelPath = saveFileDialog1.FileName;
                //Change current index to 0 for saving
                int i = level.CurrentStageIndex;
                level.CurrentStageIndex = 0;

                Icarus.Logic.GameEngine.LevelLoader.Instance.SaveLevelXML(strLevelPath, level);
                
                //Change current index back
                level.CurrentStageIndex = i;
            }
        }

        private void menuItemAddStage_Click(object sender, System.EventArgs e)
        {
            int i;
            //Make sure there is room for another stage
            for(i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages; i++)
            {
                if(level.Stages[i] == null) break;
            }
            if(i >= Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages)
            {
                //TODO: pop up message with error
                MessageBox.Show("This level has the maximum number of stages :-(", "Limit Reached", MessageBoxButtons.OK, MessageBoxIcon.Stop);

                //Do Nothing. Quota full.
                return;
            }


            //Display StageEdit Form
            FormStageEdit formStageEdit = new FormStageEdit();
            formStageEdit.Stage =  new Icarus.Logic.Stage();            
            formStageEdit.ShowDialog();

            //set information from form
            level.Stages[i] = formStageEdit.Stage;
            level.CurrStage = level.Stages[i];

            //set image.
            Bitmap MyImage = level.CurrStage.GetBackgroundAt(level.CurrStage.GetBackgroundCount() - 1).Animation.CurrentFrame.getFrameImage();
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal ;
            pictureBox1.ClientSize = new Size(MyImage.Size.Width, MyImage.Size.Height);
            pictureBox1.Image = (Image) MyImage ;

          
            populateStageListBox();

            
        }

        private void populateStageListBox()
        {
            lbStages.Items.Clear();
            for(int i = 0 ; i < level.Stages.Length; i++)
            {
                if(level.Stages[i] == null) break;
                lbStages.Items.Add(level.Stages[i].StageName);
            }
        }

        private void menuItemRemoveStage_Click(object sender, System.EventArgs e)
        {
            buttonDeleteStage_Click(sender, e);
        }

        private void menuItemOpen_Click(object sender, System.EventArgs e)
        {
            openFileDialog1.Filter = "Level files (*.xml)|*.xml" ;
            openFileDialog1.FilterIndex = 1 ;
            openFileDialog1.RestoreDirectory = true ;

            if(openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                strLevelPath = openFileDialog1.FileName;
                level = Icarus.Logic.GameEngine.LevelLoader.Instance.LoadLevelXML(strLevelPath);
                level.CurrentStageIndex = 0;
                
                Bitmap MyImage = level.CurrStage.GetBackgroundAt(level.CurrStage.GetBackgroundCount() - 1).Animation.CurrentFrame.getFrameImage();
                pictureBox1.SizeMode = PictureBoxSizeMode.Normal ;
                pictureBox1.ClientSize = new Size(MyImage.Size.Width, MyImage.Size.Height);
                pictureBox1.Image = (Image) MyImage ;

                //Set stages
                System.IO.DirectoryInfo di = null;
                for(int i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages; i++)
                {
                    if(level.Stages[i] == null) break;
                    di = new System.IO.DirectoryInfo(Icarus.Resources.ResourceManager.Instance.ImagePath+level.Stages[i].GetBackgroundAt(level.CurrStage.GetBackgroundCount() - 1).Animation.FramesetPath);
                    lbStages.Items.Add(di.Name);
                }
            }
        }

        
        public void lbCollisionBoxes_TabIndexChanged(object sender, System.EventArgs e)
        {
            int i = lbCollisionBoxes.TabIndex;
        }

        private void buttonAddStage_Click(object sender, System.EventArgs e)
        {
            menuItemAddStage_Click(sender, e);
        }

        private void buttonEditImage_Click(object sender, System.EventArgs e)
        {
            if(level.CurrStage == null) return;

            //Display StageEdit Form
            FormStageEdit formStageEdit = new FormStageEdit();
            formStageEdit.Stage =  level.CurrStage;         
            formStageEdit.ShowDialog();

            //set image.
            Bitmap MyImage = level.CurrStage.GetBackgroundAt(level.CurrStage.GetBackgroundCount() - 1).Animation.CurrentFrame.getFrameImage();
            pictureBox1.SizeMode = PictureBoxSizeMode.Normal ;
            pictureBox1.ClientSize = new Size(MyImage.Size.Width, MyImage.Size.Height);
            pictureBox1.Image = (Image) MyImage ;

          
            populateStageListBox();
        }

        private void buttonDeleteStage_Click(object sender, System.EventArgs e)
        {
            int iNumStages;
            for(iNumStages = 0; iNumStages < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages; iNumStages++)
            {
                if(level.Stages[iNumStages] == null) break;
            }

            //Must have a enough stages to delete.
            if(iNumStages <= 1)
            {
                MessageBox.Show("Must have more than one stage to delete a stage.", "Delete error", MessageBoxButtons.OK);
                return;
            }


            //Make sure they wanted to delete the stage.
            if(DialogResult.No == MessageBox.Show("Are you sure you want to delete this stage?", "Confirm Delete", MessageBoxButtons.YesNo))
            {
                return;
            }

            for(int i = 0; i < lbStages.Items.Count; i++)
            {
                if(((string)(lbStages.Items[i])).Equals(lbStages.Text))
                {
                    //Remove name from list
                    lbStages.Items.Remove(lbStages.Text);

                    //Remove stage from level
                    for(int j = i+1; j < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages - 1; j ++)
                    {
                        level.Stages[j-1] = level.Stages[j];
                    }
                    level.Stages[Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages-1] = null;

                    //Set current stage to first stage in list
                    level.CurrentStageIndex = 0;
                    pictureBox1.Image = level.CurrStage.GetBackgroundAt(level.CurrStage.GetBackgroundCount() - 1).Animation.CurrentFrame.getFrameImage();
                    break;
                }
            }
        }

        private void buttonSelectMode_Click(object sender, System.EventArgs e)
        {
            iCurrMode = (int)enumMode.Select;
        }

        private void lbCollisionBoxes_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            iCurrMode = (int)enumMode.NewCollisionBox;
        }

        private void lbWorldObjects_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            iCurrMode = (int)enumMode.NewWorldObject;
        }

        private void lbStages_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int i;
            for(i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxStages; i++)
            {
                if(i >= lbStages.Items.Count) break;

                if(((string)(lbStages.Items[i])).Equals(lbStages.Text))
                {
                    //Set current stage
                    level.CurrentStageIndex = i;

                    //Change bitmap
                    Bitmap MyImage = level.CurrStage.GetBackgroundAt(level.CurrStage.GetBackgroundCount() - 1).Animation.CurrentFrame.getFrameImage();
                    pictureBox1.SizeMode = PictureBoxSizeMode.Normal ;
                    pictureBox1.ClientSize = new Size(MyImage.Size.Width, MyImage.Size.Height);
                    pictureBox1.Image = (Image) MyImage ;
                    break;
                }
            }
        }

        private void buttonSave_Click(object sender, System.EventArgs e)
        {
            if(strLevelPath != null)
            {
                //Change current index to 0 for saving
                int i = level.CurrentStageIndex;
                level.CurrentStageIndex = 0;

                Icarus.Logic.GameEngine.LevelLoader.Instance.SaveLevelXML(strLevelPath, level);
                
                //Change current index back
                level.CurrentStageIndex = i;              
            }
            else//Show save as
            {
                menuItemSaveAs_Click(sender, e);
            }
        }

        private void buttonDeleteSelected_Click(object sender, System.EventArgs e)
        {
            if(woCurr != null)
            {
                deleteWO(woCurr);
            }
            else if(cbCurr != null)
            {
                deleteCB(cbCurr);
            }
        }

        private void buttonToggleGrid_Click(object sender, System.EventArgs e)
        {
            if(bShowGrid) bShowGrid = false;
            else bShowGrid = true;
            pictureBox1.Invalidate();
        }

        private void menuItemToggleGrid_Click(object sender, System.EventArgs e)
        {
            if(bShowGrid) bShowGrid = false;
            else bShowGrid = true;
            pictureBox1.Invalidate();
        }

        private void buttonTransitionSet_Click(object sender, System.EventArgs e)
        {
            if(cbCurr.GetType().Equals(typeof(Icarus.Logic.Physics.CollisionBoxTransition)))
            {
                ((Icarus.Logic.Physics.CollisionBoxTransition)cbCurr).X = Convert.ToInt32(textBoxTransitionX.Text.Trim());
                ((Icarus.Logic.Physics.CollisionBoxTransition)cbCurr).Y = Convert.ToInt32(textBoxTransitionY.Text.Trim());
                ((Icarus.Logic.Physics.CollisionBoxTransition)cbCurr).XViewCenter = Convert.ToInt32(textBoxXviewcenter.Text.Trim());
                ((Icarus.Logic.Physics.CollisionBoxTransition)cbCurr).YViewCenter = Convert.ToInt32(textBoxYveiwcenter.Text.Trim());

                ((Icarus.Logic.Physics.CollisionBoxTransition)cbCurr).NextStage = Convert.ToInt32(textBoxTransitionNext.Text.Trim());
            }
        }


        private void menuItemExit_Click(object sender, System.EventArgs e)
        {
            if(DialogResult.Yes == MessageBox.Show("Are you sure you want to Exit?", "Confirm Exit", MessageBoxButtons.YesNo))
            {
                Application.Exit();
            }
        }


        #endregion

        #region Other Methods

        private void deleteCB(Icarus.Logic.Physics.CollisionBox cb)
        {
            int i;
            for(i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes; i++)
            {
                if(level.CurrStage.CollisionBoxes[i] == null) break;
                if(level.CurrStage.CollisionBoxes[i].Equals(cb))
                {
                    for(int j = i+1; j < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes; j++)
                    {
                        level.CurrStage.CollisionBoxes[j-1] = level.CurrStage.CollisionBoxes[j];
                    }
                    level.CurrStage.CollisionBoxes[Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes-1] = null;
                    cbCurr = null;
                    
                    //TODO: Be more specific with invalidation
                    pictureBox1.Invalidate();
                }
            }
        }

        private void deleteWO(Icarus.Logic.WorldObject wo)
        {
            int i;
            for(i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxActors; i++)
            {
                if(level.CurrStage.actorList[i] == null) break;
                if(level.CurrStage.actorList[i].Equals(wo))
                {
                    for(int j = i+1; j < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxActors; j++)
                    {
                        level.CurrStage.actorList[j-1] = level.CurrStage.actorList[j];
                    }
                    level.CurrStage.actorList[Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxActors-1] = null;
                    woCurr = null;

                    //TODO: Be more specific with invalidation
                    pictureBox1.Invalidate();
                }
            }
        }

        private void drawCollisionBoxes(Icarus.Logic.Level level, Graphics g)
        {
            //Might not have added a stage yet
            if(level.CurrStage == null) return;

            //Draw boxes
            for(int i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes; i++)
            {
                if(level.CurrStage.CollisionBoxes[i] == null) break;
                
                rectTemp.X = level.CurrStage.CollisionBoxes[i].BoundingBox.X;
                rectTemp.Y = level.CurrStage.CollisionBoxes[i].BoundingBox.Y;
                rectTemp.Width = level.CurrStage.CollisionBoxes[i].BoundingBox.Width;
                rectTemp.Height = level.CurrStage.CollisionBoxes[i].BoundingBox.Height;
                
                if(!level.CurrStage.CollisionBoxes[i].Equals(cbCurr))
                {
                    g.DrawLine(penUnselected, rectTemp.X, rectTemp.Y, rectTemp.X+rectTemp.Width, rectTemp.Y+rectTemp.Height);
                    g.DrawLine(penUnselected, rectTemp.X+rectTemp.Width, rectTemp.Y, rectTemp.X, rectTemp.Y+rectTemp.Height);
                    g.DrawRectangle(penUnselected, rectTemp);
                }
            }
            //Draw the selected box
            if(cbCurr != null)
            {
                
                rectTemp.X = cbCurr.BoundingBox.X;
                rectTemp.Y = cbCurr.BoundingBox.Y;
                rectTemp.Width = cbCurr.BoundingBox.Width;
                rectTemp.Height = cbCurr.BoundingBox.Height;
                
                //Draw bounding box
                g.DrawRectangle(penSelected, rectTemp);
                
                //Draw internal shapes
                drawInternalBoxes(cbCurr, g);

                int iTmp = iResizeBoxSize/2;
                //Draw dragging Boxes
                rectResizeTop.X = ((cbCurr.BoundingBox.X + cbCurr.BoundingBox.X + cbCurr.BoundingBox.Width)/2) - iTmp;
                rectResizeTop.Y = cbCurr.BoundingBox.Y-iTmp;
                rectResizeBottom.X = rectResizeTop.X;
                rectResizeBottom.Y = cbCurr.BoundingBox.Y + cbCurr.BoundingBox.Height - iTmp;
                rectResizeLeft.X = cbCurr.BoundingBox.X - iTmp;
                rectResizeLeft.Y = ((cbCurr.BoundingBox.Y + cbCurr.BoundingBox.Y + cbCurr.BoundingBox.Height)/2) - iTmp;
                rectResizeRight.X = cbCurr.BoundingBox.X + cbCurr.BoundingBox.Width - iTmp;
                rectResizeRight.Y = rectResizeLeft.Y;
                g.DrawRectangle(penSelected, rectResizeTop);
                g.DrawRectangle(penSelected, rectResizeBottom);
                g.DrawRectangle(penSelected, rectResizeLeft);
                g.DrawRectangle(penSelected, rectResizeRight);
                
                //g.DrawLine(penUnselected, rectTemp.X, rectTemp.Y, rectTemp.X+rectTemp.Width, rectTemp.Y+rectTemp.Height);
                //g.DrawLine(penUnselected, rectTemp.X+rectTemp.Width, rectTemp.Y, rectTemp.X, rectTemp.Y+rectTemp.Height);
                
            }
        }

        private void findClosestGridPoint(int iX, int iY, ref Point result, int iSize)
        {
            if(iX % iSize > iSize / 2)
            {
                //Shift right to nearest point
                result.X = iX + (iSize - iX % iSize);
            }
            else
            {
                //Shift left to nearest point
                result.X = iX - (iX % iSize);
            }
            if(iY % iSize > iSize / 2)
            {
                //Shift down to nearest point
                result.Y = iY + (iSize - iY % iSize);
            }
            else
            {
                //Shift up to nearest point
                result.Y = iY - (iY % iSize);
            }
        }


        private int isResizing(int iX, int iY)
        {
            if(rectResizeTop.Contains(iX, iY))
            {
                return (int)enumDirection.Up;
            }
            else if(rectResizeBottom.Contains(iX, iY))
            {
                return (int)enumDirection.Down;
            }
            else if(rectResizeLeft.Contains(iX, iY))
            {
                return (int)enumDirection.Left;
            }
            else if(rectResizeRight.Contains(iX, iY))
            {
                return (int)enumDirection.Right;
            }
        
            return (int)enumDirection.None;
        }

        private void selectBox(int iX, int iY)
        {
            int i;

            //Check to see if we are selecting a world object
            for(i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxActors; i++)
            {
                if(level.CurrStage.actorList[i] == null) 
                {
                    woCurr = null;
                    break;
                }
                
                rectTemp.X = level.CurrStage.actorList[i].CollisionBox.BoundingBox.X;
                rectTemp.Y = level.CurrStage.actorList[i].CollisionBox.BoundingBox.Y;
                rectTemp.Width = level.CurrStage.actorList[i].CollisionBox.BoundingBoxWidth;
                rectTemp.Height = level.CurrStage.actorList[i].CollisionBox.BoundingBoxHeight;
                if(rectTemp.Contains(iX, iY))
                {
                    woCurr = level.CurrStage.actorList[i];
                    cbCurr = null;//Make sure the other is no longer selected
                    pictureBox1.Invalidate();
                    return;
                }
            }
            if(i == Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxActors) 
            {
                woCurr = null;
            }


            //Check to see if we are selecging a collision box
            for(i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxCollisionBoxes; i++)
            {
                if(level.CurrStage.CollisionBoxes[i] == null)
                {
                    //Break if we hit the end of the list
                    cbCurr = null;
                    break;
                }

                rectTemp.X = level.CurrStage.CollisionBoxes[i].BoundingBox.X;
                rectTemp.Y = level.CurrStage.CollisionBoxes[i].BoundingBox.Y;
                rectTemp.Width = level.CurrStage.CollisionBoxes[i].BoundingBox.Width;
                rectTemp.Height = level.CurrStage.CollisionBoxes[i].BoundingBox.Height;
                if(rectTemp.Contains(iX, iY))
                {
                    //Skip the current selected box
                    if(cbCurr != null && cbCurr.Equals(level.CurrStage.CollisionBoxes[i])) continue;

                    //Set new current collision box
                    cbCurr = level.CurrStage.CollisionBoxes[i];
                    woCurr = null;//Make sure the other is no longer selected
                    break;
                }

            }

            //Redraw it
            pictureBox1.Invalidate();
        }

        private void drawInternalBoxes(Icarus.Logic.Physics.CollisionBox cb, Graphics g)
        {
            for(int i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxInternalRects; i++)
            {
                if(cb.RectArray[i] == null) break;
                rectTemp.X = cb.RectArray[i].X;
                rectTemp.Y = cb.RectArray[i].Y;
                rectTemp.Width = cb.RectArray[i].Width;
                rectTemp.Height = cb.RectArray[i].Height;
                g.DrawRectangle(penInternal, rectTemp);                
            }

            for(int i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxInternalTris; i++)
            {
                if(cb.TriArray[i] == null) break;
                drawTriangle(cb.TriArray[i], g);
            }

        }

        private void drawTriangle(Icarus.Logic.Physics.Triangle tri, Graphics g)
        {
            g.DrawLine(penInternal, tri.P1.x, tri.P1.y, tri.P2.x, tri.P2.y);
            g.DrawLine(penInternal, tri.P2.x, tri.P2.y, tri.P3.x, tri.P3.y);
            g.DrawLine(penInternal, tri.P3.x, tri.P3.y, tri.P1.x, tri.P1.y);
        }

        private void drawWorldObjects(Icarus.Logic.Level level, Graphics g)
        {
            //Make sure a stage has been set
            if(level.CurrStage == null) return;


            for(int i = 0; i < Icarus.Logic.GameEngine.GameSettings.Instance.sv_maxActors; i++)
            {
                //Make sure we have something to draw
                if(level.CurrStage.actorList[i] == null) break;

                if(level.CurrStage.actorList[i].Equals(woCurr))
                {
                    continue;
                }

                //For now we just draw an ellipse
                rectTemp.X = level.CurrStage.actorList[i].CollisionBox.BoundingBox.X;
                rectTemp.Y = level.CurrStage.actorList[i].CollisionBox.BoundingBox.Y;
                rectTemp.Width = level.CurrStage.actorList[i].CollisionBox.BoundingBoxWidth;
                rectTemp.Height = level.CurrStage.actorList[i].CollisionBox.BoundingBoxHeight;
                g.DrawEllipse(penWorldObj, rectTemp);

                //Draw child objs
                drawWorldObjectChild(level.CurrStage.actorList[i], g);

            }

            //Draw current world object
            if(woCurr != null)
            {
                rectTemp.X = woCurr.CollisionBox.BoundingBox.X;
                rectTemp.Y = woCurr.CollisionBox.BoundingBox.Y;
                rectTemp.Width = woCurr.CollisionBox.BoundingBoxWidth;
                rectTemp.Height = woCurr.CollisionBox.BoundingBoxHeight;
                g.DrawEllipse(penSelected, rectTemp);

                //Draw child objs
                drawWorldObjectChild(woCurr, g);

            }
        }

        /// <summary>
        /// Recursively draw world object collision boxes
        /// </summary>
        /// <param name="wo"></param>
        private void drawWorldObjectChild(Icarus.Logic.WorldObject wo, Graphics g)
        {
            Icarus.Logic.WorldObject woChild;
            for(int i = 0; i < wo.Children.Length; i++)
            {
                woChild = wo.Children[i];
                rectTemp.X = woChild.CollisionBox.BoundingBox.X;
                rectTemp.Y = woChild.CollisionBox.BoundingBox.Y;
                rectTemp.Width = woChild.CollisionBox.BoundingBoxWidth;
                rectTemp.Height = woChild.CollisionBox.BoundingBoxHeight;
                g.DrawEllipse(penWorldObjChild, rectTemp);
                                                                    
                drawWorldObjectChild(woChild, g);
            }
        }

        private void drawGrid(Graphics g, Rectangle rectClip, int iSize)
        {

            int i, j;

            //Draw vertical lines
            for(i = rectClip.X + (iSize - rectClip.X % iSize); i < pictureBox1.Width; i+=iSize)
            {
                g.DrawLine(penGrid, i, 0, i, this.pictureBox1.Height); 
            }

            //Draw horizontal lines
            for(j = rectClip.Y + (iSize - rectClip.Y % iSize); j < pictureBox1.Height; j+=iSize)
            {
                g.DrawLine(penGrid, 0, j, this.pictureBox1.Width, j);
            }

        }

        #endregion    

        

        
	}
}
