#region Using directives

using System;
using System.IO;
using PunkMan.Game;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

#endregion

namespace PunkMan.Editor
{
    /// <summary>
    /// The PunkMan editor main form
    /// </summary>
    public class EditorMain : Form
    {
        private MenuStrip mainMenu;
        private RaftingContainer leftRaftingContainer;
        private RaftingContainer leftRaftingContainer1;
        private RaftingContainer topRaftingContainer;
        private RaftingContainer bottomRaftingContainer;
        private ToolStripMenuItem fileToolStripMenuItem;
        private ToolStripMenuItem newToolStripMenuItem;
        private ToolStripMenuItem openToolStripMenuItem;
        private ToolStripMenuItem saveToolStripMenuItem;
        private ToolStripMenuItem saveAsToolStripMenuItem;
        private ToolStripMenuItem exitToolStripMenuItem;
        private SplitContainer splitContainer1;
        private Panel drawArea;
        private ListView listView1;
        private ImageList textureList;
        private System.ComponentModel.IContainer components;
        private OpenFileDialog ofDlg;
        private SaveFileDialog sfDlg;
        private PictureBox pictureBox1;

        /// <summary>
        /// Current map object
        /// </summary>
        private Map map;
        /// <summary>
        /// Grid width constant
        /// </summary>
        private readonly int gridWidth = 32;
        /// <summary>
        /// Grid height constant
        /// </summary>
        private readonly int gridHeight = 32;

        /// <summary>
        /// Index of texture
        /// </summary>
        private int texIdx = 0;

        /// <summary>
        /// Name of map file
        /// </summary>
        private string fileName = "";

        /// <summary>
        /// Bitmap for map layout drawing
        /// </summary>
        private Bitmap mapBitmap;

        /// <summary>
        /// List of texture thumbnails
        /// </summary>
        private Bitmap[] smallTexture;
        private ToolStripMenuItem viewToolStripMenuItem;
        private ToolStripMenuItem showwalkablesToolStripMenuItem;
        private SplitContainer splitContainer2;
        private CheckBox cbWalkable;
        private ToolStrip toolStrip1;
        private ToolStripButton cellModeToolStripButton;
        private ToolStripButton entityModeToolStripButton;
        private GroupBox groupBox1;
        private ListBox lbEntity;
        private ToolStripMenuItem showentitiesToolStripMenuItem;
        /// <summary>
        /// Show spaces (walkables)?
        /// </summary>
        private bool visSpace = true;
        private ToolStripMenuItem toolsToolStripMenuItem;
        private ToolStripMenuItem fillRectToolStripMenuItem;
        /// <summary>
        /// Show entities
        /// </summary>
        private bool visEntities = true;
        /// <summary>
        /// Fill Rect Tool first point
        /// </summary>
        private Point fillRectStart;
        

        /// <summary>
        /// The Constructor
        /// </summary>
        public EditorMain() 
        {
            Text = "PunkMan X1 editor";
            InitializeComponent();
            map = new Map(10, 10);
            Size mapSize = new Size(10 * gridWidth, 10 * gridHeight);
            pictureBox1.Size = mapSize;
            
        }

        /// <summary>
        /// Draw map layout
        /// </summary>
        private void RedrawMapBitmap()
        {
            for (int x = 0; x < map.DataWidth; x++)
                for (int y = 0; y < map.DataHeight; y++)
                    RedrawMapBitmap(x, y);

        }

        /// <summary>
        /// Draw map cell
        /// </summary>
        /// <param name="x">x position of cell in map </param>
        /// <param name="y">x position of cell in map </param>
        private void RedrawMapBitmap(int x, int y)
        {
            
            System.Drawing.Font fnt = new System.Drawing.Font(FontFamily.GenericMonospace, 14f);
            //if (x < 0 || x >= map.DataWidth || y < 0 || y >= map.DataHeight) 
            //    return;
            int rx = x * gridWidth;
            int ry = y * gridHeight;
            Graphics g = Graphics.FromImage(mapBitmap);
            g.DrawImage(smallTexture[map.Data[x, y].Index], rx, ry);
            g.DrawLine(Pens.Black, rx + gridWidth - 1, ry, rx + gridWidth - 1, ry + gridHeight - 1);
            g.DrawLine(Pens.Black, rx, ry + gridHeight - 1, rx + gridWidth, ry + gridHeight - 1);
            if (visSpace && map.Data[x, y].Walkable)
            {
                g.DrawLine(Pens.OrangeRed, rx, ry, rx + gridWidth, ry + gridHeight);
                g.DrawLine(Pens.OrangeRed, rx + gridWidth, ry, rx, ry + gridHeight);

            }
            if (visEntities && map.Data[x, y].Entity != MapCell.EntityType.None)
            {
                switch (map.Data[x, y].Entity)
                {
                    case MapCell.EntityType.Start:
                        g.DrawString("St", fnt, Brushes.Pink, rx, ry);
                        break;

                    case MapCell.EntityType.End:
                        g.DrawString("Ed", fnt, Brushes.Pink, rx, ry);
                        break;

                    case MapCell.EntityType.Monster:
                        g.DrawString("Mt", fnt, Brushes.Pink, rx, ry);
                        break;

                    case MapCell.EntityType.Rum:
                        g.DrawString("R", fnt, Brushes.Pink, rx, ry);
                        break;

                    case MapCell.EntityType.Absinth:
                        g.DrawString("A", fnt, Brushes.Red, rx, ry);
                        break;

                }
            }
            g.Dispose();
        }

        /// <summary>
        /// Load all possible texture
        /// </summary>
        /// <param name="path">path to directory of textures</param>
        private void LoadTextures(string path)
        {
            //textureList = new ImageList();
            textureList.Images.Clear();
            StreamReader reader = new StreamReader(new FileStream(path + @"\texture.list", FileMode.Open));
            


            int i = 0;
            Size size = new Size(gridWidth, gridHeight);
            /*foreach (FileInfo file in new DirectoryInfo(path).GetFiles())
            {*/
            while (!reader.EndOfStream){
                string fullName = path +@"\"+ reader.ReadLine();
                
                if (!File.Exists(fullName)) continue;

                Bitmap tex = new Bitmap(fullName, false);
                


                textureList.Images.Add(tex);

                string si = i.ToString();
                ListViewItem item = new ListViewItem(si, i);
                //item.Bounds = new Rectangle(0, 0, 256, 256);
                listView1.Items.Add(item);
                i++;

            }
            smallTexture = new Bitmap[textureList.Images.Count];
            for (i = 0; i < textureList.Images.Count; i++)
            {

                smallTexture[i] = new Bitmap(textureList.Images[i], gridWidth, gridHeight);
            }

            mapBitmap = new Bitmap(map.DataWidth * gridWidth, map.DataHeight * gridHeight);
            pictureBox1.Image = mapBitmap;
            RedrawMapBitmap();
            pictureBox1.Invalidate();

        }

        /// <summary>
        /// Might main method
        /// </summary>
        /// <param name="args">command line args</param>
        public static void Main(string[] args)
        {
            using (EditorMain form = new EditorMain())
            {
                form.LoadTextures(Game.Game.GetDataFilePath("textures"));
                form.Show();

                while (form.Created)
                {
                    Application.DoEvents();
                }

            }
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.mainMenu = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.newToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.showwalkablesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.leftRaftingContainer = new System.Windows.Forms.RaftingContainer();
            this.leftRaftingContainer1 = new System.Windows.Forms.RaftingContainer();
            this.topRaftingContainer = new System.Windows.Forms.RaftingContainer();
            this.bottomRaftingContainer = new System.Windows.Forms.RaftingContainer();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.drawArea = new System.Windows.Forms.Panel();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.splitContainer2 = new System.Windows.Forms.SplitContainer();
            this.listView1 = new System.Windows.Forms.ListView();
            this.textureList = new System.Windows.Forms.ImageList(this.components);
            this.cbWalkable = new System.Windows.Forms.CheckBox();
            this.ofDlg = new System.Windows.Forms.OpenFileDialog();
            this.sfDlg = new System.Windows.Forms.SaveFileDialog();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.cellModeToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.entityModeToolStripButton = new System.Windows.Forms.ToolStripButton();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.lbEntity = new System.Windows.Forms.ListBox();
            this.showentitiesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.fillRectToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.mainMenu.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.topRaftingContainer)).BeginInit();
            this.topRaftingContainer.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.bottomRaftingContainer)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            this.drawArea.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.splitContainer2.Panel1.SuspendLayout();
            this.splitContainer2.Panel2.SuspendLayout();
            this.splitContainer2.SuspendLayout();
            this.toolStrip1.SuspendLayout();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
// 
// mainMenu
// 
            this.mainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.viewToolStripMenuItem,
            this.toolsToolStripMenuItem});
            this.mainMenu.Location = new System.Drawing.Point(0, 0);
            this.mainMenu.Name = "mainMenu";
            this.mainMenu.Padding = new System.Windows.Forms.Padding(6, 2, 0, 2);
            this.mainMenu.Raft = System.Windows.Forms.RaftingSides.Top;
            this.mainMenu.TabIndex = 0;
            this.mainMenu.Text = "menuStrip1";
// 
// fileToolStripMenuItem
// 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.newToolStripMenuItem,
            this.openToolStripMenuItem,
            this.saveToolStripMenuItem,
            this.saveAsToolStripMenuItem,
            this.exitToolStripMenuItem});
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.SettingsKey = "EditorMain.fileToolStripMenuItem";
            this.fileToolStripMenuItem.Text = "&File";
// 
// newToolStripMenuItem
// 
            this.newToolStripMenuItem.Name = "newToolStripMenuItem";
            this.newToolStripMenuItem.SettingsKey = "EditorMain.newToolStripMenuItem";
            this.newToolStripMenuItem.Text = "&New";
            this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click);
// 
// openToolStripMenuItem
// 
            this.openToolStripMenuItem.Name = "openToolStripMenuItem";
            this.openToolStripMenuItem.SettingsKey = "EditorMain.openToolStripMenuItem";
            this.openToolStripMenuItem.Text = "&Open";
            this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click);
// 
// saveToolStripMenuItem
// 
            this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
            this.saveToolStripMenuItem.SettingsKey = "EditorMain.saveToolStripMenuItem";
            this.saveToolStripMenuItem.Text = "&Save";
            this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
// 
// saveAsToolStripMenuItem
// 
            this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem";
            this.saveAsToolStripMenuItem.SettingsKey = "EditorMain.saveAsToolStripMenuItem";
            this.saveAsToolStripMenuItem.Text = "Save &As";
            this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click);
// 
// exitToolStripMenuItem
// 
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.SettingsKey = "EditorMain.exitToolStripMenuItem";
            this.exitToolStripMenuItem.Text = "&Exit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
// 
// viewToolStripMenuItem
// 
            this.viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.showwalkablesToolStripMenuItem,
            this.showentitiesToolStripMenuItem});
            this.viewToolStripMenuItem.Name = "viewToolStripMenuItem";
            this.viewToolStripMenuItem.SettingsKey = "EditorMain.viewToolStripMenuItem";
            this.viewToolStripMenuItem.Text = "&View";
// 
// showwalkablesToolStripMenuItem
// 
            this.showwalkablesToolStripMenuItem.Checked = true;
            this.showwalkablesToolStripMenuItem.CheckOnClick = true;
            this.showwalkablesToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
            this.showwalkablesToolStripMenuItem.Name = "showwalkablesToolStripMenuItem";
            this.showwalkablesToolStripMenuItem.SettingsKey = "EditorMain.showwalkablesToolStripMenuItem";
            this.showwalkablesToolStripMenuItem.Text = "Show walkables";
            this.showwalkablesToolStripMenuItem.CheckedChanged += new System.EventHandler(this.showwalkablesToolStripMenuItem_CheckedChanged);
// 
// leftRaftingContainer
// 
            this.leftRaftingContainer.Dock = System.Windows.Forms.DockStyle.Left;
            this.leftRaftingContainer.Name = "leftRaftingContainer";
// 
// leftRaftingContainer1
// 
            this.leftRaftingContainer1.Dock = System.Windows.Forms.DockStyle.Left;
            this.leftRaftingContainer1.Name = "leftRaftingContainer1";
// 
// topRaftingContainer
// 
            this.topRaftingContainer.Controls.Add(this.mainMenu);
            this.topRaftingContainer.Controls.Add(this.toolStrip1);
            this.topRaftingContainer.Dock = System.Windows.Forms.DockStyle.Top;
            this.topRaftingContainer.Name = "topRaftingContainer";
// 
// bottomRaftingContainer
// 
            this.bottomRaftingContainer.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.bottomRaftingContainer.Name = "bottomRaftingContainer";
// 
// splitContainer1
// 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 46);
            this.splitContainer1.Name = "splitContainer1";
// 
// Panel1
// 
            this.splitContainer1.Panel1.Controls.Add(this.drawArea);
// 
// Panel2
// 
            this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
            this.splitContainer1.Size = new System.Drawing.Size(644, 484);
            this.splitContainer1.SplitterDistance = 461;
            this.splitContainer1.TabIndex = 4;
            this.splitContainer1.Text = "splitContainer1";
// 
// drawArea
// 
            this.drawArea.AutoScroll = true;
            this.drawArea.AutoScrollMargin = new System.Drawing.Size(8, 8);
            this.drawArea.AutoScrollMinSize = new System.Drawing.Size(8, 8);
            this.drawArea.BackColor = System.Drawing.Color.Black;
            this.drawArea.Controls.Add(this.pictureBox1);
            this.drawArea.Dock = System.Windows.Forms.DockStyle.Fill;
            this.drawArea.Location = new System.Drawing.Point(0, 0);
            this.drawArea.Name = "drawArea";
            this.drawArea.Size = new System.Drawing.Size(461, 484);
            this.drawArea.TabIndex = 0;
// 
// pictureBox1
// 
            this.pictureBox1.AutoSize = true;
            this.pictureBox1.BackColor = System.Drawing.Color.Black;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(398, 473);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
            this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
// 
// splitContainer2
// 
            this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer2.Location = new System.Drawing.Point(0, 0);
            this.splitContainer2.Name = "splitContainer2";
            this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
// 
// Panel1
// 
            this.splitContainer2.Panel1.Controls.Add(this.listView1);
// 
// Panel2
// 
            this.splitContainer2.Panel2.Controls.Add(this.groupBox1);
            this.splitContainer2.Panel2.Controls.Add(this.cbWalkable);
            this.splitContainer2.Size = new System.Drawing.Size(179, 484);
            this.splitContainer2.SplitterDistance = 328;
            this.splitContainer2.TabIndex = 1;
            this.splitContainer2.Text = "splitContainer2";
// 
// listView1
// 
            this.listView1.BackColor = System.Drawing.SystemColors.AppWorkspace;
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView1.GridLines = true;
            this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
            this.listView1.LargeImageList = this.textureList;
            this.listView1.Location = new System.Drawing.Point(0, 0);
            this.listView1.MultiSelect = false;
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(179, 328);
            this.listView1.TabIndex = 0;
            this.listView1.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listView1_ItemSelectionChanged);
// 
// textureList
// 
            this.textureList.ImageSize = new System.Drawing.Size(128, 128);
            this.textureList.TransparentColor = System.Drawing.Color.Transparent;
// 
// cbWalkable
// 
            this.cbWalkable.AutoSize = true;
            this.cbWalkable.Checked = true;
            this.cbWalkable.CheckState = System.Windows.Forms.CheckState.Checked;
            this.cbWalkable.Dock = System.Windows.Forms.DockStyle.Fill;
            this.cbWalkable.Location = new System.Drawing.Point(0, 0);
            this.cbWalkable.MaximumSize = new System.Drawing.Size(0, 50);
            this.cbWalkable.Name = "cbWalkable";
            this.cbWalkable.Size = new System.Drawing.Size(179, 50);
            this.cbWalkable.TabIndex = 0;
            this.cbWalkable.Text = "Walkable";
// 
// ofDlg
// 
            this.ofDlg.Filter = "Punk Man Map (*.pmm)|*.pmm|All files (*.*)|*.*";
// 
// sfDlg
// 
            this.sfDlg.DefaultExt = "pmm";
            this.sfDlg.Filter = "Punk Man Map (*.pmm)|*.pmm|All files (*.*)|*.*";
// 
// toolStrip1
// 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.cellModeToolStripButton,
            this.entityModeToolStripButton});
            this.toolStrip1.Location = new System.Drawing.Point(0, 21);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Raft = System.Windows.Forms.RaftingSides.Top;
            this.toolStrip1.TabIndex = 1;
            this.toolStrip1.Text = "toolStrip1";
// 
// cellModeToolStripButton
// 
            this.cellModeToolStripButton.Checked = true;
            this.cellModeToolStripButton.CheckOnClick = true;
            this.cellModeToolStripButton.CheckState = System.Windows.Forms.CheckState.Checked;
            this.cellModeToolStripButton.Name = "cellModeToolStripButton";
            this.cellModeToolStripButton.SettingsKey = "EditorMain.cellModeToolStripButton";
            this.cellModeToolStripButton.Text = "Cell Mode";
            this.cellModeToolStripButton.CheckedChanged += new System.EventHandler(this.cellModeToolStripButton_CheckedChanged);
// 
// entityModeToolStripButton
// 
            this.entityModeToolStripButton.CheckOnClick = true;
            this.entityModeToolStripButton.Name = "entityModeToolStripButton";
            this.entityModeToolStripButton.SettingsKey = "EditorMain.entityModeToolStripButton";
            this.entityModeToolStripButton.Text = "Entity Mode";
            this.entityModeToolStripButton.CheckedChanged += new System.EventHandler(this.entityModeToolStripButton_CheckedChanged);
// 
// groupBox1
// 
            this.groupBox1.Controls.Add(this.lbEntity);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.groupBox1.Location = new System.Drawing.Point(0, 34);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(179, 118);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Entity Type";
// 
// lbEntity
// 
            this.lbEntity.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbEntity.FormattingEnabled = true;
            this.lbEntity.Items.AddRange(new object[] {
            "[ none ]",
            "Player Start",
            "Player End",
            "Monster",
            "Rum",
            "Absinth"});
            this.lbEntity.Location = new System.Drawing.Point(3, 16);
            this.lbEntity.Name = "lbEntity";
            this.lbEntity.Size = new System.Drawing.Size(173, 95);
            this.lbEntity.TabIndex = 0;
// 
// showentitiesToolStripMenuItem
// 
            this.showentitiesToolStripMenuItem.Checked = true;
            this.showentitiesToolStripMenuItem.CheckOnClick = true;
            this.showentitiesToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
            this.showentitiesToolStripMenuItem.Name = "showentitiesToolStripMenuItem";
            this.showentitiesToolStripMenuItem.SettingsKey = "EditorMain.showentitiesToolStripMenuItem";
            this.showentitiesToolStripMenuItem.Text = "Show entities";
// 
// toolsToolStripMenuItem
// 
            this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.fillRectToolStripMenuItem
            });
            this.toolsToolStripMenuItem.Name = "toolsToolStripMenuItem";
            this.toolsToolStripMenuItem.SettingsKey = "EditorMain.toolsToolStripMenuItem";
            this.toolsToolStripMenuItem.Text = "&Tools";
// 
// fillRectToolStripMenuItem
// 
            this.fillRectToolStripMenuItem.CheckOnClick = true;
            this.fillRectToolStripMenuItem.Name = "fillRectToolStripMenuItem";
            this.fillRectToolStripMenuItem.SettingsKey = "EditorMain.fillRectToolStripMenuItem";
            this.fillRectToolStripMenuItem.Text = "&Fill Rectangle";
            this.fillRectToolStripMenuItem.Click += new System.EventHandler(this.fillRectToolStripMenuItem_Click);
// 
// EditorMain
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(644, 530);
            this.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.leftRaftingContainer);
            this.Controls.Add(this.leftRaftingContainer1);
            this.Controls.Add(this.topRaftingContainer);
            this.Controls.Add(this.bottomRaftingContainer);
            this.Name = "EditorMain";
            this.Text = "PunkMan X1 Editor";
            this.mainMenu.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.leftRaftingContainer1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.topRaftingContainer)).EndInit();
            this.topRaftingContainer.ResumeLayout(false);
            this.topRaftingContainer.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.bottomRaftingContainer)).EndInit();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.drawArea.ResumeLayout(false);
            this.drawArea.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.splitContainer2.Panel1.ResumeLayout(false);
            this.splitContainer2.Panel2.ResumeLayout(false);
            this.splitContainer2.Panel2.PerformLayout();
            this.splitContainer2.ResumeLayout(false);
            this.toolStrip1.ResumeLayout(false);
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ofDlg.ShowDialog() == DialogResult.OK)
            {
                fileName = ofDlg.FileName;
                map.Load(fileName);
                mapBitmap = new Bitmap(map.DataWidth * gridWidth, map.DataHeight * gridHeight);
                RedrawMapBitmap();
                pictureBox1.Image = mapBitmap;
                pictureBox1.Invalidate();
            }

        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewMapForm nmDlg = new NewMapForm();
            if (nmDlg.ShowDialog() == DialogResult.OK)
            {
                map = new Map(nmDlg.MapWidth, nmDlg.MapHeight);
                Size controlSize = new Size(nmDlg.MapWidth * gridWidth, nmDlg.MapHeight * gridHeight);
                pictureBox1.Size = controlSize;
                mapBitmap = new Bitmap(controlSize.Width, controlSize.Height);
                pictureBox1.Image = mapBitmap;
                RedrawMapBitmap();
                pictureBox1.Invalidate();
            }

        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileName == "")
            {
                if (sfDlg.ShowDialog() == DialogResult.OK)
                {

                    map.Save(sfDlg.FileName);
                    fileName = sfDlg.FileName;
                }
            }
            else map.Save(fileName);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (sfDlg.ShowDialog() == DialogResult.OK)
            {
                map.Save(sfDlg.FileName);
                fileName = sfDlg.FileName;
            }
        }

        private MapCell.EntityType getActiveEntityType()
        {
            MapCell.EntityType et = MapCell.EntityType.None;
            switch (lbEntity.SelectedIndex)
            {
                case 0:
                    et = MapCell.EntityType.None;
                    break;
                case 1:
                    et = MapCell.EntityType.Start;
                    break;
                case 2:
                    et = MapCell.EntityType.End;
                    break;
                case 3:
                    et = MapCell.EntityType.Monster;
                    break;
                case 4:
                    et = MapCell.EntityType.Rum;
                    break;
                case 5:
                    et = MapCell.EntityType.Absinth;
                    break;

                default:
                    et = MapCell.EntityType.None;
                    break;
            }
            return et;
        }


        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Point p = drawArea.AutoScrollPosition;
            int x = e.X - pictureBox1.Location.X + p.X;
            int y = e.Y - pictureBox1.Location.Y + p.Y;
            int mx = x / gridWidth;
            int my = y / gridHeight;
            
            if (mx >= map.DataWidth || my >= map.DataHeight || mx < 0 || my < 0)
                return;

            if (fillRectToolStripMenuItem.Checked)
            {
                if (fillRectStart == Point.Empty)
                {
                    fillRectStart = new Point(mx, my);
                }
                else
                {
                    Point start, end;

                    end = new Point(mx, my);
                    start = fillRectStart;

                    if (start.X > end.X)
                    {
                        int tmp = start.X;
                        start.X = end.X;
                        end.X = tmp;
                    }
                    if (start.Y > end.Y)
                    {
                        int tmp = start.Y;
                        start.Y = end.Y;
                        end.Y = tmp;
                    }

                    for (int ix = start.X; ix <= end.X; ix++)
                    {
                        for (int iy = start.Y; iy <= end.Y; iy++)
                        {
                            if (cellModeToolStripButton.Checked)
                            {
                                map.Data[ix, iy].Index = texIdx;
                                map.Data[ix, iy].Walkable = cbWalkable.Checked;
                            }
                            else if (entityModeToolStripButton.Checked 
                                && getActiveEntityType() != MapCell.EntityType.Start
                                && getActiveEntityType() != MapCell.EntityType.End)
                            {
                                map.Data[ix, iy].Entity = getActiveEntityType();
                            }
                            RedrawMapBitmap(ix, iy);
                        }
                    }

                    pictureBox1.Invalidate();
                    fillRectToolStripMenuItem.Checked = false;
                }
            }
            else if (cellModeToolStripButton.Checked)
            {
                map.Data[mx, my].Index = texIdx;
                map.Data[mx, my].Walkable = cbWalkable.Checked;
                RedrawMapBitmap(mx, my);
                pictureBox1.Invalidate();
            }
            else if (entityModeToolStripButton.Checked)
            {
                MapCell.EntityType et = getActiveEntityType();
                if (et == MapCell.EntityType.Start)
                {
                    Point _p = map.FindStartPoint();
                    if (_p != Point.Empty)
                    {
                        map.Data[_p.X, _p.Y].Entity = MapCell.EntityType.None;
                        RedrawMapBitmap(_p.X, _p.Y);
                    }
                }
                else if (et == MapCell.EntityType.End)
                {
                    Point _p = map.FindEndPoint();
                    if (_p != Point.Empty)
                    {
                        map.Data[_p.X, _p.Y].Entity = MapCell.EntityType.None;
                        RedrawMapBitmap(_p.X, _p.Y);
                    }
                }
                map.Data[mx, my].Entity = et;
                RedrawMapBitmap(mx, my);
                pictureBox1.Invalidate();

            }
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            texIdx = e.Item.Index;
            //pictureBox1.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            
            //map.RenderToGraphics(e.Graphics, textureList, gridWidth, gridHeight);
        }

        private void showwalkablesToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            visSpace = showwalkablesToolStripMenuItem.Checked;
            RedrawMapBitmap();
            pictureBox1.Invalidate();
        }

        private void cellModeToolStripButton_CheckedChanged(object sender, EventArgs e)
        {
            if (cellModeToolStripButton.Checked)
                entityModeToolStripButton.Checked = false;
            else
                entityModeToolStripButton.Checked = true;
        }

        private void entityModeToolStripButton_CheckedChanged(object sender, EventArgs e)
        {
            if (entityModeToolStripButton.Checked)
            {
                cellModeToolStripButton.Checked = false;
                if (lbEntity.SelectedIndex < 0) lbEntity.SelectedIndex = 0;
            }
            else
                cellModeToolStripButton.Checked = true;
        }

        private void fillRectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fillRectStart = Point.Empty;
        }


    }
}
