public TileMap(Tileset tileset, MapLayer layer, int tileWidth, int tileHeight)
            : base(tileWidth, tileHeight)
        {
            mapWidth = layer.Width;
            mapHeight = layer.Height;

            tilesets = new List<Tileset>();
            tilesets.Add(tileset);
            mapLayers = new List<MapLayer>();
            mapLayers.Add(layer);
        }
        /// <summary>
        /// Metoda slouzici pro nahrani hry
        /// </summary>
        /// <param name="gamename"></param>
        public static void LoadGame(string gamename)
        {
            GameSerialize ob = new GameSerialize();
            try
            {
                Serializer ser = new Serializer();
                ob = ser.DeSerializeGame(gamename);
                LoadMapAndTextures(ob.mapName);
                LoadHeroSprite(ob.hero.Gender);

                //Overrides Units loaded from MapSerialize
                Units = new List<Unit>();
                foreach (Unit item in ob.units)
                {
                    Unit klon = baseUnits[item.Name].Clone(item.Notoriety, item.Cell.X, item.Cell.Y);
                    Session.FrontMap.GetTile(item.Cell).obj = klon;
                    Units.Add(klon);
                }

                //Overrides Keys loaded from MapSerialize
                Keys = new List<GameKey>();
                foreach (GameKey item in ob.keys)
                {
                    GameKey klon = baseKeys[item.index].Clone(item.Cell.X, item.Cell.Y);
                    Session.FrontMap.GetTile(item.Cell).obj = klon;
                    Keys.Add(klon);
                }

                MapLayer frontPath = new MapLayer(Session.MapProps.FrontSizeX, Session.MapProps.FrontSizeY);
                for (int y = 0; y < frontPath.Height; y++)
                {
                    for (int x = 0; x < frontPath.Width; x++)
                    {
                        Tile tile = new Tile(-1, -1, y, x);
                        frontPath.SetTile(x, y, tile);
                    }
                }
                Session.FrontMap.mapLayers.Add(frontPath);

                Session.CurrentHero = new Hero(ob.hero, playerSpriteHero);
                Session.CurrentHero.units = new List<Unit>();
                foreach (Unit item in ob.hero.units)
                {
                    Session.CurrentHero.units.Add(baseUnits[item.Name].Clone(item.Notoriety, item.Cell.X, item.Cell.Y, item.Attack, item.Defense, item.HitPoints, item.isInHero));
                }
                Session.Camera = ob.cam;
                Session.FrontMap.mapLayers[1] = ob.frontSplatter;
            }
            catch (Exception ex)
            {
                string message = ex.Message;

            }
        }
        /// <summary>
        /// Metoda pro stlaceni tlacitka Load.
        /// Nacte mapu a na chvili oznami ze je mapa nactena
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void linkLabel2_Selected(object sender, EventArgs e)
        {
            Session.LoadMapAndTextures("map.xml");

            switch (LayerMenu.clickedIndex)
            {
                case 0:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[0];
                    break;
                case 1:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[1];
                    break;
                case 2:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[0];
                    break;
                case 3:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[1];
                    break;
            }

            lvisible = true;
            label1.Text = "Loaded";
            label1.Visible = true;
        }
        protected override void LoadContent()
        {
            ram = new Rectangle(770, 250, 220, 220);

            //Nacteni fontu, ramecku pro tileset a pozadi
            font = Content.Load<SpriteFont>(@"Fonts\editorFont");
            ramTexture = Content.Load<Texture2D>(@"Pictures\ram");
            background = new BackgroundComponent(GameRef, Content.Load<Texture2D>(@"Backgrounds\editorscreen"), DrawMode.Center);

            #region ovladaci prvky

            spriteFont = Content.Load<SpriteFont>(@"Fonts\menuFont");
            string[] LayermenuItems = { "Back Map", "Back Splatter", "Front Map", "Front Splatter" };
            LayerMenu = new MenuComponent(font, LayermenuItems, false);
            LayerMenu.SetPostion(new Vector2(770, 30));
            LayerMenu.SelectedColor = Color.Yellow;
            LayerMenu.HiliteColor = Color.White;

            spriteFont = Content.Load<SpriteFont>(@"Fonts\menuFont");
            string[] ActionmenuItems = { "Add", "Move", "Pick", "Delete" };
            ActionMenu = new MenuComponent(font, ActionmenuItems, false);
            ActionMenu.SetPostion(new Vector2(900, 30));
            ActionMenu.SelectedColor = Color.Yellow;
            ActionMenu.HiliteColor = Color.White;

            spriteFont = Content.Load<SpriteFont>(@"Fonts\menuFont");

            LinkLabel linkLabel1 = new LinkLabel(spriteFont);
            linkLabel1.Text = "Save";
            linkLabel1.Size = linkLabel1.SpriteFont.MeasureString(linkLabel1.Text);
            linkLabel1.Position = new Vector2(770,720);
            linkLabel1.Selected += new EventHandler(linkLabel1_Selected);
            Controls.Add(linkLabel1);

            LinkLabel linkLabel2 = new LinkLabel(spriteFont);
            linkLabel2.Text = "Load";
            linkLabel2.Size = linkLabel2.SpriteFont.MeasureString(linkLabel2.Text);
            linkLabel2.Position = new Vector2(940,720);
            linkLabel2.Selected += new EventHandler(linkLabel2_Selected);
            Controls.Add(linkLabel2);
            Controls.editor = true;

            label1 = new Label(spriteFont);
            label1.Position = new Vector2(845, 720);
            label1.Visible = false;
            Controls.Add(label1);

            Session.LoadMapAndTextures("map.xml");

            string[] CharacterStrings = new string[Session.baseUnits.Keys.Count];
            Session.baseUnits.Keys.CopyTo(CharacterStrings, 0);

            CharacterMenu = new MenuComponent(font, CharacterStrings, false);
            CharacterMenu.SetPostion(new Vector2(770, 200));
            CharacterMenu.SelectedColor = Color.Yellow;
            CharacterMenu.HiliteColor = Color.White;

            int[] KeyInts = new int[Session.baseKeys.Keys.Count];
            Session.baseKeys.Keys.CopyTo(KeyInts, 0);
            string[] KeyStrings = new string[Session.baseKeys.Keys.Count];
            for (int i = 0; i < KeyInts.Length; i++)
            {
                KeyStrings[i] = KeyInts[i].ToString();
            }

            KeyMenu = new MenuComponent(font, KeyStrings, false);
            KeyMenu.SetPostion(new Vector2(770, 200));
            KeyMenu.SelectedColor = Color.Yellow;
            KeyMenu.HiliteColor = Color.White;

            string[] TileSetmenuItems = new string[Session.FrontMap.tilesets.Count + 2];
            for (int i = 0; i < Session.FrontMap.tilesets.Count; i++)
            {
                TileSetmenuItems[i] = Session.FrontMap.tilesets[i].Texture.Name;  //files[i].Name.Substring(0, files[i].Name.Length - 4);
            }
            TileSetmenuItems[TileSetmenuItems.Length - 2] = "Characters";
            TileSetmenuItems[TileSetmenuItems.Length - 1] = "Keys";

            TileSetMenu = new MenuComponent(font, TileSetmenuItems, false);
            TileSetMenu.SetPostion(new Vector2(770, 100));
            TileSetMenu.SelectedColor = Color.Yellow;
            TileSetMenu.HiliteColor = Color.White;

            #endregion

            //Nastaveni prvniho itemu u LayerMenu
            switch (LayerMenu.clickedIndex)
            {
                case 0:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[0];
                    break;
                case 1:
                    CurrentMap = Session.BackMap;
                    CurrentLayer = Session.BackMap.mapLayers[1];
                    break;
                case 2:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[0];
                    break;
                case 3:
                    CurrentMap = Session.FrontMap;
                    CurrentLayer = Session.FrontMap.mapLayers[1];
                    break;
            }

            //Nastaveni bazoveho policka pro akce
            CurrentTile = new Tile(0, 0, -1, -1);
            CurrentTileSet = Session.FrontMap.tilesets[CurrentTile.Tileset];

            //Nacteni vstupnich ramecku pro obraz TileSetu
            int tiles = CurrentTileSet.TilesWide * CurrentTileSet.TilesHigh;
            sourceRectangles = new Rectangle[tiles];
            float Scale = (float)ram.Width / (float)Session.FrontMap.tilesets[CurrentTile.Tileset].Texture.Width;
            int tile = 0;
            for (int y = 0; y < CurrentTileSet.TilesHigh; y++)
                for (int x = 0; x < CurrentTileSet.TilesWide; x++)
                {
                    sourceRectangles[tile] = new Rectangle(
                    (int)(x * CurrentTileSet.TileWidth * Scale),
                    (int)(y * CurrentTileSet.TileWidth * Scale),
                    (int)(CurrentTileSet.TileWidth * Scale),
                    (int)(CurrentTileSet.TileHeight * Scale));
                    tile++;
                }

            base.LoadContent();
        }
        public override void Update(GameTime gameTime)
        {
            if (KeyboardInput.KeyReleased(Keys.Escape))
                manager.PopScreen();

            //Oznamovaci label
            #region Time label

            if (lvisible)
            {
                lvisible = false;
                end = gameTime.TotalGameTime + TimeSpan.FromSeconds(1);
            }
            if (end.TotalMilliseconds < gameTime.TotalGameTime.TotalMilliseconds)
            {
                label1.Visible = false;
            }

            #endregion

            //Osetreni pohybu po mape klavesnici
            #region Posun + camera

            Vector2 motion = new Vector2();

            if (KeyboardInput.KeyDown(Keys.Up))
                motion.Y = -1;
            if (KeyboardInput.KeyDown(Keys.Down))
                motion.Y = 1;
            if (KeyboardInput.KeyDown(Keys.Left))
                motion.X = -1;
            if (KeyboardInput.KeyDown(Keys.Right))
                motion.X = 1;

            Session.Camera.Position += motion * Session.Camera.Speed;
            Session.Camera.LockCamera();

            #endregion

            //Osetreni vsech menicek jejich aktualni hodnoty a nasledne vykresleni tilesetu
            #region Tlacitka + menicka

            if (LayerMenu.isMouseOver() && MouseInput.LeftButtonReleased())
            {
                switch (LayerMenu.SelectedIndex)
                {
                    case 0:
                        LayerMenu.clickedIndex = 0;
                        CurrentMap = Session.BackMap;
                        CurrentLayer = Session.BackMap.mapLayers[0];
                        break;
                    case 1:
                        LayerMenu.clickedIndex = 1;
                        CurrentMap = Session.BackMap;
                        CurrentLayer = Session.BackMap.mapLayers[1];
                        break;
                    case 2:
                        LayerMenu.clickedIndex = 2;
                        CurrentMap = Session.FrontMap;
                        CurrentLayer = Session.FrontMap.mapLayers[0];
                        break;
                    case 3:
                        LayerMenu.clickedIndex = 3;
                        CurrentMap = Session.FrontMap;
                        CurrentLayer = Session.FrontMap.mapLayers[1];
                        break;
                }
            }
            LayerMenu.Update();

            if (ActionMenu.isMouseOver() && MouseInput.LeftButtonReleased() && toMove == null)
            {
                switch (ActionMenu.SelectedIndex)
                {
                    case 0:
                        ActionMenu.clickedIndex = 0;
                        action = Action.Add;
                        break;
                    case 1:
                        ActionMenu.clickedIndex = 1;
                        action = Action.Move;
                        break;
                    case 2:
                        ActionMenu.clickedIndex = 2;
                        action = Action.Pick;
                        break;
                    case 3:
                        ActionMenu.clickedIndex = 3;
                        action = Action.Delete;
                        break;
                }
            }
            ActionMenu.Update();

            if(TileSetMenu.clickedIndex == 3)
            {
                if (CharacterMenu.isMouseOver() && MouseInput.LeftButtonReleased())
                {
                    CharacterMenu.clickedIndex = CharacterMenu.SelectedIndex;
                }
                CharacterMenu.Update();
            }
            if (TileSetMenu.clickedIndex == 4)
            {
                if (KeyMenu.isMouseOver() && MouseInput.LeftButtonReleased())
                {
                    KeyMenu.clickedIndex = KeyMenu.SelectedIndex;
                }
                KeyMenu.Update();
            }

            if (TileSetMenu.isMouseOver() && MouseInput.LeftButtonReleased())
            {
                TileSetMenu.clickedIndex = TileSetMenu.SelectedIndex;
                if (TileSetMenu.clickedIndex < 3)
                {
                    CurrentTile.TileIndex = 0;
                    CurrentTile.Tileset = TileSetMenu.clickedIndex;
                    CurrentTileSet = Session.FrontMap.tilesets[CurrentTile.Tileset];

                    int tiles = CurrentTileSet.TilesWide * CurrentTileSet.TilesHigh;
                    sourceRectangles = new Rectangle[tiles];
                    float Scale = (float)ram.Width / (float)Session.FrontMap.tilesets[CurrentTile.Tileset].Texture.Width;
                    int tile = 0;
                    for (int y = 0; y < CurrentTileSet.TilesHigh; y++)
                        for (int x = 0; x < CurrentTileSet.TilesWide; x++)
                        {
                            sourceRectangles[tile] = new Rectangle(
                            (int)(x * CurrentTileSet.TileWidth * Scale),
                            (int)(y * CurrentTileSet.TileWidth * Scale),
                            (int)(CurrentTileSet.TileWidth * Scale),
                            (int)(CurrentTileSet.TileHeight * Scale));
                            tile++;
                        }
                }
            }
            TileSetMenu.Update();
            Controls.Update(gameTime);

            #endregion

            //Osetreni akci provedenych do mapy
            #region Actions

            switch (action)
            {
                case Action.Delete:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
                        {
                            if (TileSetMenu.clickedIndex < 3)
                            {
                                Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                                CurrentLayer.SetTile(tmp.X, tmp.Y, -1, -1, true);
                            }
                            else if (TileSetMenu.clickedIndex == 3 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                Unit u = (Unit)tmp.obj;
                                Session.Units.Remove(u);
                                tmp.obj = null;
                            }
                            else if (TileSetMenu.clickedIndex == 4 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                GameKey u = (GameKey)tmp.obj;
                                Session.Keys.Remove(u);
                                tmp.obj = null;
                            }
                        }

                        break;
                    }
                case Action.Add:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
                        {
                            if (TileSetMenu.clickedIndex < 3)
                            {
                                Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                                if (CurrentTile.Tileset == 1)
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, CurrentTile.TileIndex, CurrentTile.Tileset, false);
                                else
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, CurrentTile.TileIndex, CurrentTile.Tileset, true);
                            }
                            else if (TileSetMenu.clickedIndex == 3 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                Session.Units.Add(Session.baseUnits[CharacterMenu.menuItems[CharacterMenu.clickedIndex]].Clone(Notoriety.Enemy, tmp.X, tmp.Y));
                            }
                            else if (TileSetMenu.clickedIndex == 4 && MouseInput.LeftButtonPressed())
                            {
                                Tile tmp = Session.FrontMap.GetTile(Session.FrontMap.MouseToPoint());
                                Session.Keys.Add(Session.baseKeys[KeyMenu.clickedIndex + 1].Clone(tmp.X, tmp.Y));
                            }
                        }
                        break;
                    }
                case Action.Pick:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
                        {
                            Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                            CurrentTile = new Tile(tmp.TileIndex, tmp.Tileset, -1, -1);
                            TileSetMenu.clickedIndex = tmp.Tileset;
                        }
                        break;
                    }
                case Action.Move:
                    {
                        if (MouseInput.MouseOver(Session.ViewportRect.X, Session.ViewportRect.Y, Session.ViewportRect.Width, Session.ViewportRect.Height) && MouseInput.LeftButtonPressed())
                        {
                            if (toMove == null)
                            {
                                toMove = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                            }
                            else
                            {
                                Tile tmp = CurrentLayer.GetTile(CurrentMap.MouseToPoint());
                                if (CurrentTile.Tileset == 1)
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, toMove.TileIndex, toMove.Tileset, false);
                                else
                                    CurrentLayer.SetTile(tmp.X, tmp.Y, CurrentTile.TileIndex, CurrentTile.Tileset, true);
                                CurrentLayer.SetTile(toMove.X, toMove.Y, -1, -1, true);
                                toMove = null;
                            }
                        }
                        break;
                    }
            }

            #endregion

            //Osetreni kliku do tilesetu a nastaveni bazoveho policka pro upravu
            #region Get CurrentTile
            if (MouseInput.MouseOver(ram.X, ram.Y, ram.Width, ram.Height))
            {
                if (MouseInput.LeftButtonReleased())
                {
                    Point p = new Point(MouseInput.MouseState.X, MouseInput.MouseState.Y);
                    p.X -= ram.X;
                    p.Y -= ram.Y;
                    int tiles = CurrentTileSet.TilesWide * CurrentTileSet.TilesHigh;
                    for (int i = 0; i < tiles; i++)
                    {
                        if (sourceRectangles[i].Contains(p))
                        {
                            CurrentTile.TileIndex = i;
                            break;
                        }
                    }
                }
            }

            #endregion

            //Update vsech jednotek a klicu
            Session.UpdateMap(gameTime);
            base.Update(gameTime);
        }
 /// <summary>
 /// Pridani vrstvy
 /// </summary>
 /// <param name="layer"></param>
 internal void AddLayer(MapLayer layer)
 {
     if (layer.Width != mapWidth || layer.Height != mapHeight)
         throw new Exception("Error in layer size.");
     mapLayers.Add(layer);
 }
        /// <summary>
        /// Obsluha tlacitka potvrzujici vstup do hry
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void linkLabel1_Selected(object sender, EventArgs e)
        {
            //Nacteni vsech veci okolo mapy, hrdiny a inicializace map
            Session.LoadMapAndTextures("map.xml");
            Session.LoadHeroSprite(genderSelector.SelectedItem);
            Session.Camera = new Camera(new Vector2(0,0));

            //Nastaveni jednotek do tilemapy
            foreach (Unit item in Session.Units)
            {
                Session.FrontMap.GetTile(item.Cell).obj = item;
            }

            //Nastaveni klicu do tilemapy
            foreach (GameKey item in Session.Keys)
            {
                Session.FrontMap.GetTile(item.Cell).obj = item;
            }

            //Nastaveni vrstvy mapy vyuzivajici pathengine pro vykresleni oznamovaci cesty
            MapLayer frontPath = new MapLayer(Session.MapProps.FrontSizeX, Session.MapProps.FrontSizeY);
            for (int y = 0; y < frontPath.Height; y++)
            {
                for (int x = 0; x < frontPath.Width; x++)
                {
                    Tile tile = new Tile(-1, -1, y, x);
                    frontPath.SetTile(x, y, tile);
                }
            }
            Session.FrontMap.mapLayers.Add(frontPath);

            //Vytvoreni noveho hrdiny
            CreateNewHero();

            //Pridani dat pro ulozeni
            Session.CurrentHero.Gender = genderSelector.SelectedItem;
            if(Session.CurrentHero.Gender == "Female")
                Session.CurrentHero.Sprite.texture = Content.Load<Texture2D>(@"Sprites\femalefighter");

            //Autosave
            Session.SaveGame("AutoSave.xml");

            //Vraceni na hlavni menu a okamzity prehozeni na herni okno
            manager.PopScreen();
            manager.PushScreen(GameRef.GamePlayScreen);
        }