private void worldPanel_MouseMove(object sender, MouseEventArgs e)
        {
            Point offset = worldPanel.AutoScrollPosition;

            int x = (e.X - offset.X) / tileSize.Width;
            int y = (e.Y - offset.Y) / tileSize.Height;

            if (isPressed == true)
            {
                switch (CurrTool)
                {
                    case 0: // Tile
                        {
                            if (x < mapSize.Width && y < mapSize.Height)
                            {
                                try
                                {
                                    TileMap[x, y].sRect.X = selectedTile.X * tileSize.Width;
                                    TileMap[x, y].sRect.Y = selectedTile.Y * tileSize.Height;
                                    TileMap[x, y].sRect.Width = tileSize.Width;
                                    TileMap[x, y].sRect.Height = tileSize.Height;
                                    TileMap[x, y].X = x * tileSize.Width;
                                    TileMap[x, y].Y = y * tileSize.Height;

                                }
                                catch { }
                            }
                        }
                        break;
                    case 1: // Objects
                        if (x < mapSize.Width && y < mapSize.Height)
                        {
                            Point test = new Point(x, y);
                            bool taken = false;
                            for (int i = 0; i < ListofObjects.Count; i++)
                            {
                                if (ListofObjects[i].Position == test)
                                    taken = true; // or break??? maybe return?? then we wouldnt need the "taken" bool or the following if check
                            }
                            if (!taken)
                            {
                                Object obj = new Object(objectID);
                                obj.Position = new Point(x, y);
                                obj.sRect.X = selectedObject.X * tileSize.Width;
                                obj.sRect.Y = selectedObject.Y * tileSize.Height;
                                obj.sRect.Width = tileSize.Width;
                                obj.sRect.Height = tileSize.Height;
                                for (int currObj = 0; currObj < ListofObjects.Count; currObj++)
                                {
                                    if (ListofObjects[currObj].Type == objectID)
                                        obj.Name = ListofObjects[currObj].Name;
                                }
                                ListofObjects.Add(obj);
                                Objects.Items.Add(names[objectID] + objectID);
                                Objects.SelectedIndex = Objects.Items.Count - 1;
                            }
                        }
                        break;
                    case 2: // collision
                        {
                            if (collisionID != 2)
                            {
                                bool taken = false;
                                for (int i = 0; i < collisionObjects.Count; i++)
                                {
                                    if (collisionObjects[i].PtPostion == new Point(x, y))
                                        taken = true;
                                }
                                if (!taken)
                                {
                                    Collidable temp = new Collidable();
                                    temp.PtPostion = new Point(x, y);
                                    temp.Id = collisionID;
                                    collisionObjects.Add(temp);
                                    temp.Index = collisionObjects.Count;
                                    if (temp.Id == 0)
                                        listBoxCollisionObjects.Items.Add("Collider" + temp.Index);
                                    else
                                        listBoxCollisionObjects.Items.Add("Trigger" + temp.Index);
                                }
                            }
                            else
                            {
                                for (int i = 0; i < collisionObjects.Count; i++)
                                {
                                    if (collisionObjects[i].PtPostion == new Point(x, y))
                                    {
                                        collisionObjects.RemoveAt(i);
                                        listBoxCollisionObjects.Items.RemoveAt(i);

                                    }
                                }
                            }
                        }
                        break;
                    case 3:
                        {
                            Point test = new Point(x, y);
                            bool taken = false;
                            for (int i = 0; i < navGraph.Count; i++)
                            {
                                if (navGraph[i].Position == test)
                                    taken = true; // or break??? maybe return?? then we wouldnt need the "taken" bool or the following if check
                            }
                            if (!taken)
                            {
                                Node temp = new Node();
                                temp.Position = new Point(x, y);
                                temp.Index = navGraph.Count();
                                navGraph.Add(temp);
                                PathNodes.Items.Add("Node " + temp.Index);
                                PathNodes.SelectedIndex = navGraph.Count - 1;
                            }
                        }
                        break;
                }
            }
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory();

            if (DialogResult.OK == dlg.ShowDialog())
            {

                ListofObjects = new List<Object>();
                navGraph = new List<Node>();
                collisionObjects = new List<Collidable>();

                PathNodes.Items.Clear();
                Objects.Items.Clear();
                listBoxCollisionObjects.Items.Clear();

                numericMapCol.Value = 0;
                numericMapCol.Value = 0;
                numericMapRows.Value = 0;
                numericSheetWidth.Value = 0;

                XElement xRoot = XElement.Load(dlg.FileName);
                //XElement xLevel = xRoot.Element("Level");

                XElement xWorldInfo = xRoot.Element("world_info");
                XAttribute xTWidth = xWorldInfo.Attribute("width");
                tileSize.Width = int.Parse(xTWidth.Value);
                XAttribute xTHeight = xWorldInfo.Attribute("height");
                tileSize.Height = int.Parse(xTHeight.Value);
                XAttribute xWWidth = xWorldInfo.Attribute("worldWidth");
                mapSize.Width = int.Parse(xWWidth.Value);
                XAttribute xWHeight = xWorldInfo.Attribute("worldHeight");
                mapSize.Height = int.Parse(xWHeight.Value);
                XAttribute xTSWidth = xWorldInfo.Attribute("tileSheetWidth");
                tSetSize.Width = int.Parse(xTSWidth.Value);
                XAttribute xTSHeight = xWorldInfo.Attribute("tileSheetHeight");
                tSetSize.Height = int.Parse(xTSHeight.Value);
                filePath = xWorldInfo.Value;

                numericMapCol.Value = mapSize.Width;
                numericMapRows.Value = mapSize.Height;

                TileMap = new Tile[mapSize.Width, mapSize.Height];
                for (int i = 0; i < mapSize.Width; ++i)
                {
                    for (int j = 0; j < mapSize.Height; ++j)
                    {
                        TileMap[i, j] = new Tile();
                        TileMap[i, j].X = 0;
                        TileMap[i, j].Y = 0;
                        TileMap[i, j].sRect = new Rectangle(0, 0, tileSize.Width, tileSize.Height);
                    }
                }

                if (D3DTileId != -1)
                {
                    m_TM.UnloadTexture(D3DTileId);
                    D3DTileId = -1;

                }
                D3DTileId = m_TM.LoadTexture(filePath);

                XElement xPlayer = xRoot.Element("player_info");

                if (xPlayer != null)
                {
                    XAttribute xId = xPlayer.Attribute("id");
                    XAttribute xPosX = xPlayer.Attribute("posX");
                    XAttribute xPosY = xPlayer.Attribute("posY");
                    XAttribute xNumEvents = xPlayer.Attribute("numEvents");
                    XAttribute xNumWaypoints = xPlayer.Attribute("numWaypoints");
                    XAttribute xNumEnemies = xPlayer.Attribute("numEnemies");
                    XAttribute xState = xPlayer.Attribute("startState");

                    XAttribute xSRectLeft = xPlayer.Attribute("sRectleft");
                    XAttribute xSRectTop = xPlayer.Attribute("sRecttop");
                    XAttribute xSRectWidth = xPlayer.Attribute("sRectwidth");
                    XAttribute xSRectHeight = xPlayer.Attribute("sRectheight");

                    Object tObject = new Object(int.Parse(xId.Value));
                    tObject.Position = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value));
                    tObject.NumEnemies = int.Parse(xNumEnemies.Value);
                    tObject.sRect = new Rectangle(int.Parse(xSRectLeft.Value), int.Parse(xSRectTop.Value), int.Parse(xSRectWidth.Value), int.Parse(xSRectHeight.Value));
                    if (xState != null)
                    {
                        tObject.State = int.Parse(xState.Value);

                    }

                    string NameandEvents = xPlayer.Value;
                    string[] info = NameandEvents.Split('/');
                    tObject.Name = info[0];
                    for (int i = 1; i < info.Length; i++)
                    {

                        tObject.events.Add(info[i]);
                    }
                    int numWaypoints = int.Parse(xNumWaypoints.Value);

                    for (int currWP = 0; currWP < numWaypoints; currWP++)
                    {
                        string waypoint = "waypoint";
                        waypoint += currWP;
                        XAttribute xWaypoint = xPlayer.Attribute(waypoint);
                        tObject.waypoints.Add(int.Parse(xWaypoint.Value));
                    }

                    ListofObjects.Add(tObject);
                    Objects.Items.Add(tObject.Name + " " + tObject.Type);
                    names[tObject.Type] = tObject.Name;
                }

              // D3DTileId = m_TM.LoadTexture(filePath);
                XElement xTileList = xRoot.Element("tile_list");
                IEnumerable<XElement> xTiles = xTileList.Elements();

                int count = 0;
                //int rowcount = 0;

                foreach (XElement xTile in xTiles)
                {
                    Tile tTile = new Tile();
                    Rectangle sourceRect = new Rectangle();

                    XAttribute xSourceRectLeft = xTile.Attribute("rposx");
                    sourceRect.X = int.Parse(xSourceRectLeft.Value);

                    XAttribute xSourceRectTop = xTile.Attribute("rposy");
                    sourceRect.Y = int.Parse(xSourceRectTop.Value);

                    XAttribute xPositionX = xTile.Attribute("posX");
                    tTile.X = int.Parse(xPositionX.Value);
                    XAttribute xPositionY = xTile.Attribute("posY");
                    tTile.Y = int.Parse(xPositionY.Value);

                    sourceRect.Width = tileSize.Width;
                    sourceRect.Height = tileSize.Height;

                    tTile.sRect = sourceRect;
                    //tTile.Y = (count / mapSize.Width) * tileSize.Width;
                    //tTile.X = count % mapSize.Height * tileSize.Height;

                    TileMap[tTile.X / tileSize.Width, tTile.Y / tileSize.Width] = tTile;
                    //TileMap[(count / mapSize.Width) * tileSize.Width, (count % mapSize.Width) * tileSize.Height] = tTile;
                    count++;
                }

                XElement xCollsionList = xRoot.Element("collsion_list");
                if (xCollsionList != null)
                {
                    IEnumerable<XElement> xColliders = xCollsionList.Elements();

                    foreach (XElement xCollider in xColliders)
                    {
                        Collidable tCollider = new Collidable();

                        XAttribute xPosX = xCollider.Attribute("posX");
                        XAttribute xPosY = xCollider.Attribute("posY");
                        XAttribute xType = xCollider.Attribute("type");
                        XAttribute xIndex = xCollider.Attribute("index");

                        string eventTrigger = xCollider.Value;

                        string[] info = eventTrigger.Split('/');

                        for (int i = 1; i < info.Length; i++)
                        {

                            tCollider.eventTrigger.Add(info[i]);
                        }
                        if (eventTrigger.Length > 0)
                            tCollider.eventTrigger.Add(eventTrigger.Remove(eventTrigger.Length - 1));

                        tCollider.Id = int.Parse(xType.Value);
                        tCollider.PtPostion = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value));
                        tCollider.Index = int.Parse(xIndex.Value);

                        collisionObjects.Add(tCollider);
                        if (tCollider.Id == 0)
                            listBoxCollisionObjects.Items.Add("Collider" + tCollider.Index);
                        else
                            listBoxCollisionObjects.Items.Add("Trigger" + tCollider.Index);

                    }

                }

                XElement xNavLayer = xRoot.Element("graph_list");
                if (xNavLayer != null)
                {
                    IEnumerable<XElement> xNodes = xNavLayer.Elements();

                    foreach (XElement xNode in xNodes)
                    {
                        Node tNode = new Node();

                        XAttribute xPosX = xNode.Attribute("posX");
                        XAttribute xPosY = xNode.Attribute("posY");
                        XAttribute xIndex = xNode.Attribute("index");
                        XAttribute xNumEdges = xNode.Attribute("numEdges");

                        tNode.Position = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value));

                        tNode.Index = int.Parse(xIndex.Value);

                        int numEdges = int.Parse(xNumEdges.Value);
                        tNode.Tag = xNode.Value;
                        for (int currEdge = 0; currEdge < numEdges; currEdge++)
                        {
                            string name = "edge";
                            name += currEdge;
                            XName xname = name;

                            XAttribute xEdge = xNode.Attribute(xname);
                            tNode.edges.Add(int.Parse(xEdge.Value));

                        }
                        navGraph.Add(tNode);
                        PathNodes.Items.Add("Node" + tNode.Index.ToString());
                }

                    XElement xObjectLayer = xRoot.Element("objects_list");

                    if (xObjectLayer != null)
                    {
                         IEnumerable<XElement> xObjects = xObjectLayer.Elements();

                        foreach (XElement xObject in xObjects)
                        {

                            XAttribute xId = xObject.Attribute("id");
                            XAttribute xPosX = xObject.Attribute("posX");
                            XAttribute xPosY = xObject.Attribute("posY");
                            XAttribute xNumEvents = xObject.Attribute("numEvents");
                            XAttribute xNumWaypoints = xObject.Attribute("numWaypoints");
                            XAttribute xNumEnemies = xObject.Attribute("numEnemies");

                            XAttribute xSRectLeft = xObject.Attribute("sRectleft");
                            XAttribute xSRectTop = xObject.Attribute("sRecttop");
                            XAttribute xSRectWidth = xObject.Attribute("sRectwidth");
                            XAttribute xSRectHeight = xObject.Attribute("sRectheight");

                            Object tObject = new Object(int.Parse(xId.Value));
                            tObject.Position = new Point(int.Parse(xPosX.Value), int.Parse(xPosY.Value));
                            tObject.NumEnemies = int.Parse(xNumEnemies.Value);
                            tObject.sRect = new Rectangle(int.Parse(xSRectLeft.Value), int.Parse(xSRectTop.Value), int.Parse(xSRectWidth.Value), int.Parse(xSRectHeight.Value));

                            string NameandEvents = xObject.Value;
                           string[] info = NameandEvents.Split('/');
                           tObject.Name = info[0];
                           for (int i = 1; i < info.Length; i++)
                           {

                               tObject.events.Add(info[i]);
                           }
                            int numWaypoints = int.Parse(xNumWaypoints.Value);

                            for (int currWP = 0; currWP < numWaypoints; currWP++)
                            {
                                string waypoint = "waypoint";
                                waypoint += currWP;
                                XAttribute xWaypoint = xObject.Attribute(waypoint);
                                tObject.waypoints.Add(int.Parse(xWaypoint.Value));
                            }

                            ListofObjects.Add(tObject);
                            Objects.Items.Add(tObject.Name +" "+ tObject.Type);
                            names[tObject.Type] = tObject.Name;
                        }
                    }

                }

                worldPanel.AutoScrollMinSize = new Size(tileSize.Width * mapSize.Width, tileSize.Height * mapSize.Height);

            }
        }
        private void worldPanel_MouseDown(object sender, MouseEventArgs e)
        {
            isPressed = true;

            Point offset = worldPanel.AutoScrollPosition;

            int x = (e.X - offset.X) / tileSize.Width;
            int y = (e.Y - offset.Y) / tileSize.Height;

            if (x < mapSize.Width && y < mapSize.Height)
            {
                switch (CurrTool)
                {
                    case 0: // Tile
                        {
                            if (D3DTileId != -1)
                            {
                                TileMap[x, y].sRect.X = selectedTile.X * tileSize.Width;
                                TileMap[x, y].sRect.Y = selectedTile.Y * tileSize.Height;
                                TileMap[x, y].sRect.Width = tileSize.Width;
                                TileMap[x, y].sRect.Height = tileSize.Height;

                                TileMap[x, y].X = x * tileSize.Width;
                                TileMap[x, y].Y = y * tileSize.Height;
                            }

                        }
                        break;
                    case 1: // Objects
                        {
                            Point test = new Point(x, y);
                            bool taken = false;
                            for (int i = 0; i < ListofObjects.Count; i++)
                            {
                                if (ListofObjects[i].Position == test)
                                    taken = true; // or break??? maybe return?? then we wouldnt need the "taken" bool or the following if check
                            }
                            if (!taken)
                            {
                                Object temp = new Object(objectID);
                                temp.Position = new Point(x, y);
                                temp.sRect.X = selectedObject.X * tileSize.Width;
                                temp.sRect.Y = selectedObject.Y * tileSize.Height;
                                temp.sRect.Width = tileSize.Width;
                                temp.sRect.Height = tileSize.Height;
                                temp.Name = names[objectID];

                                for (int currObj = 0; currObj < ListofObjects.Count; currObj++)
                                {
                                    if (ListofObjects[currObj].Type == objectID)
                                        temp.Name = ListofObjects[currObj].Name;
                                }
                                ListofObjects.Add(temp);
                                Objects.Items.Add(names[objectID] + objectID);
                                Objects.SelectedIndex = ListofObjects.Count - 1;
                            }
                        }
                        break;
                    //case 2: // Line
                    //    {
                    //        //Line temp = new Line();
                    //        //temp.Point1 = new Point(x, y);
                    //        //pathfinderList.Add(temp);
                    //        //listBoxLines.Items.Add("Line " + listBoxLines.Items.Count);
                    //        //listBoxLines.SelectedIndex = listBoxLines.Items.Count - 1;
                    //    }
                    //    break;
                    //case 3: // Entry
                    //    {
                    //        //Entry temp = new Entry();
                    //        //temp.PositionInRoom = new Point(x, y);
                    //        //pathfinderList[listBoxLines.SelectedIndex].Entries.Add(temp);
                    //        //listBoxEntries.Items.Add("Entry " + listBoxEntries.Items.Count);
                    //        //listBoxEntries.SelectedIndex = listBoxEntries.Items.Count - 1;
                    //    }
                    //    break;
                    case 2: // collision
                        {
                            if (collisionID != 2)
                            {
                                bool taken = false;
                                for (int i = 0; i < collisionObjects.Count; i++)
                                {
                                    if (collisionObjects[i].PtPostion == new Point(x, y))
                                        taken = true;
                                }
                                if (!taken)
                                {
                                    Collidable temp = new Collidable();
                                    temp.PtPostion = new Point(x, y);
                                    temp.Id = collisionID;
                                    collisionObjects.Add(temp);
                                    temp.Index = collisionObjects.Count;
                                    if (temp.Id == 0)
                                        listBoxCollisionObjects.Items.Add("Collider" + temp.Index);
                                    else
                                        listBoxCollisionObjects.Items.Add("Trigger" + temp.Index);

                                }
                            }
                            else
                            {
                                for (int i = 0; i < collisionObjects.Count; i++)
                                {
                                    if (collisionObjects[i].PtPostion == new Point(x, y))
                                    {
                                        collisionObjects.RemoveAt(i);
                                        listBoxCollisionObjects.Items.RemoveAt(i);

                                    }
                                }
                            }
                        }
                        break;
                    case 3:
                        {
                            Point test = new Point(x, y);
                            bool taken = false;
                            for (int i = 0; i < navGraph.Count; i++)
                            {
                                if (navGraph[i].Position == test)
                                    taken = true; // or break??? maybe return?? then we wouldnt need the "taken" bool or the following if check
                            }
                            if (!taken)
                            {
                                Node temp = new Node();
                                temp.Position = new Point(x, y);
                                temp.Index = navGraph.Count();
                                navGraph.Add(temp);
                                PathNodes.Items.Add("Node " + temp.Index);
                                PathNodes.SelectedIndex = navGraph.Count - 1;
                            }

                        }
                        break;

                }

            }
        }