private void Resize_Button_Click(object sender, RoutedEventArgs e)
        {
            int w;
            int h;

            if (!Int32.TryParse(Width_TextBox.Text, out w))
            {
                return;
            }
            if (!Int32.TryParse(Height_TextBox.Text, out h))
            {
                return;
            }
            if (w < 0 || h < 0)
            {
                return;
            }

            // Create a thread
            Thread newWindowThread = Gl.CreateProgressDetWindow("Resizing...");

            Scena scn = Gl.curr_scena;

            Gl.ProgressDetProgressChange(0);
            Gl.ProgressDetStatusChange("Delete excess units..");

            for (int x = 0; x < scn.size_map_x; x++)
            {
                for (int y = 0; y < scn.size_map_y; y++)
                {
                    if (x >= w || y >= h)
                    {
                        while (scn.map_units[x, y] != null && scn.map_units[x, y].Count > 0)
                        {
                            scn.unit_delete(scn.map_units[x, y][0], false);
                        }
                    }
                }
            }

            Gl.ProgressDetProgressInc(2);
            Gl.ProgressDetStatusChange("Resizing arrays..");

            scn.size_map_x        = w;
            scn.size_map_y        = h;
            scn.size_map_x_pixels = w * 32;
            scn.size_map_y_pixels = h * 32;

            scn.map       = Utils.ResizeArray <Tile>(scn.map, w, h, scn.tileSet[0]);
            scn.map_res   = Utils.ResizeArray <int>(scn.map_res, w, h, 0);
            scn.map_units = Utils.ResizeArray <List <Unit> >(scn.map_units, w, h, null);

            Gl.ProgressDetProgressInc(3);
            Gl.ProgressDetStatusChange("Full update..");

            scn.Close_Scene_without_spr();

            scn.FullUpdate();

            Gl.ProgressDetProgressChange(100);
            Gl.ProgressDetStatusChange("All done!");

            Gl.CloseProgressDetWindow(newWindowThread);
        }
        private void FillMap_tile_Button_Click(object sender, RoutedEventArgs e)
        {
            if (Gl.palette.selected_sprite < 0)
            {
                return;
            }


            Scena scn = Gl.curr_scena;

            //int step = Gl.palette.curr_ground_brush_size;
            int step = 1;

            //if (step == 0)
            //    return;

            Thread newWindowThread = Gl.CreateProgressDetWindow("Filling...");

            Gl.ProgressDetProgressChange(0);
            Gl.ProgressDetStatusChange("Draw map..");

            #region get rect
            int sx = 0;
            int sy = 0;
            int ex = scn.size_map_x + step;
            int ey = scn.size_map_y + step;

            double dp = (double)100 / (double)scn.size_map_x * (double)4;

            //if (!(bool)AllMap_toggleButton.IsChecked)
            if (Gl.main.select_land_mode)
            {
                sx = (int)Canvas.GetLeft(Gl.main.rectangle_select_land); //Gl.main.rectangle_select_land. ;
                sy = (int)Canvas.GetTop(Gl.main.rectangle_select_land);;
                ex = (int)(Gl.main.rectangle_select_land.Width - 1);
                ey = (int)(Gl.main.rectangle_select_land.Height - 1);

                sx /= 32;
                sy /= 32;
                ex /= 32;
                ey /= 32;

                sx += step / 2;
                sy += step / 2;
                //ex += sx;
                //ey += sy;

                dp = (double)100 / (double)(ex - sx) * (double)4;
            }
            #endregion get rect


            for (int i = sx; i < ex; i += step)
            {
                for (int j = sy; j < ey; j += step)
                {
                    scn.set_Ground_region_tile(i, j, step, Gl.palette.selected_sprite, true);
                }

                if (i % (4 * step) == 0)
                {
                    Gl.ProgressDetProgressInc(dp);
                    if (Gl.main.select_land_mode)
                    {
                        Gl.ProgressDetStatusChange("Draw.. (" + (i - sx) + "/" + (ex - sx) + ")");
                    }
                    else
                    {
                        Gl.ProgressDetStatusChange("Draw.. (" + i + "/" + scn.size_map_x + ")");
                    }
                }
            }

            Gl.main.HistoryWrite();

            Gl.CloseProgressDetWindow(newWindowThread);
        }
        private void FillMap_block_Button_Click(object sender, RoutedEventArgs e)
        {
            if (Gl.palette.selected_block < 0 || Gl.palette.selected_block >= Gl.curr_scena.blocks.Count)
            {
                return;
            }

            Scena scn = Gl.curr_scena;

            int step_x = Gl.palette.block_width;
            int step_y = Gl.palette.block_height;

            if (step_x == 0 || step_y == 0)
            {
                return;
            }

            Thread newWindowThread = Gl.CreateProgressDetWindow("Filling...");

            Gl.ProgressDetProgressChange(0);
            Gl.ProgressDetStatusChange("Draw map..");

            #region get rect
            int sx = step_x / 2;
            int sy = step_y / 2;
            int ex = scn.size_map_x + step_x;
            int ey = scn.size_map_y + step_y;

            //if (!(bool)AllMap_toggleButton.IsChecked)
            if (Gl.main.select_land_mode)
            {
                sx = (int)Canvas.GetLeft(Gl.main.rectangle_select_land); //Gl.main.rectangle_select_land. ;
                sy = (int)Canvas.GetTop(Gl.main.rectangle_select_land);;
                ex = (int)(Gl.main.rectangle_select_land.Width - 1);
                ey = (int)(Gl.main.rectangle_select_land.Height - 1);

                sx /= 32;
                sy /= 32;
                ex /= 32;
                ey /= 32;

                sx += step_x / 2;
                sy += step_y / 2;
                ex += sx;
                ey += sy;
            }
            #endregion get rect

            double dp = (double)100 / (double)(ex - sx) * (double)4;

            for (int i = sx; i < ex; i += step_x)
            {
                for (int j = sy; j < ey; j += step_y)
                {
                    Gl.curr_scena.set_Ground_block(i, j, step_x, step_y, Gl.palette.selected_block, true);
                }

                if (i % (4 * step_x) == 0)
                {
                    Gl.ProgressDetProgressInc(dp);
                    if (Gl.main.select_land_mode)
                    {
                        Gl.ProgressDetStatusChange("Draw.. (" + (i - sx) + "/" + (ex - sx) + ")");
                    }
                    else
                    {
                        Gl.ProgressDetStatusChange("Draw.. (" + i + "/" + scn.size_map_x + ")");
                    }
                }
            }

            Gl.main.HistoryWrite();

            Gl.CloseProgressDetWindow(newWindowThread);
        }
        private void FillMap_autotile_Button_Click(object sender, RoutedEventArgs e)
        {
            if (Gl.palette.autotile_args.type1 == TileType.Unknown)
            {
                return;
            }

            Scena scn = Gl.curr_scena;

            int step = Gl.palette.curr_ground_brush_size;
            int size = Gl.palette.curr_ground_brush_size;

            bool IsOne = step == 1;

            if (IsOne)
            {
                step = 3;
            }

            if (step == 0)
            {
                step = Gl.palette.last_ground_brush_size;
                if (step == 0)
                {
                    step = 1;
                }
            }

            Thread newWindowThread = Gl.CreateProgressDetWindow("Filling...");

            Gl.ProgressDetProgressChange(0);
            Gl.ProgressDetStatusChange("Draw map..");

            #region get rect
            int sx = 0;
            int sy = 0;
            int ex = scn.size_map_x + step;
            int ey = scn.size_map_y + step;

            //if (!(bool)AllMap_toggleButton.IsChecked)
            if (Gl.main.select_land_mode)
            {
                sx = (int)Canvas.GetLeft(Gl.main.rectangle_select_land); //Gl.main.rectangle_select_land. ;
                sy = (int)Canvas.GetTop(Gl.main.rectangle_select_land);;
                ex = (int)(Gl.main.rectangle_select_land.Width - 1);
                ey = (int)(Gl.main.rectangle_select_land.Height - 1);

                sx /= 32;
                sy /= 32;
                ex /= 32;
                ey /= 32;

                sx += step / 2;
                sy += step / 2;
                ex += sx - step / 2 - step % 2;
                ey += sy - step / 2 - step % 2;
            }
            #endregion get rect

            double dp = (double)100 / (double)(ex - sx) * (double)4;

            for (int i = sx; i < ex; i += step)
            {
                for (int j = sy; j < ey; j += step)
                {
                    scn.set_Land_region_autotile(i, j, size, Gl.palette.autotile_args);

                    if (!IsOne && !Gl.main.select_land_mode)
                    {
                        var    timeDiff  = DateTime.UtcNow - new DateTime(1970, 1, 1);
                        long   totaltime = timeDiff.Ticks;
                        Random rnd1      = new Random(unchecked ((int)(i * 1000 + j + totaltime)));
                        timeDiff  = DateTime.UtcNow - new DateTime(1970, 1, 1);
                        totaltime = timeDiff.Ticks;
                        Random rnd2 = new Random(unchecked ((int)(j * 1000 + i + totaltime)));

                        scn.set_Land_region_autotile(i + rnd1.Next(-5, 5), j + rnd2.Next(-5, 5), size, Gl.palette.autotile_args);
                    }
                }

                if (i % (4 * step) == 0)
                {
                    Gl.ProgressDetProgressInc(dp);
                    if (Gl.main.select_land_mode)
                    {
                        Gl.ProgressDetStatusChange("Draw.. (" + (i - sx) + "/" + (ex - sx) + ")");
                    }
                    else
                    {
                        Gl.ProgressDetStatusChange("Draw.. (" + i + "/" + scn.size_map_x + ")");
                    }
                }
            }

            Gl.main.HistoryWrite();

            Gl.CloseProgressDetWindow(newWindowThread);
        }