Exemple #1
0
        public void ResizeMap(string fileName, ResizedMap map)
        {
            string mapPath = Path.Combine(DepoDirectory, @"Source Images", fileName);

            // eventually this can create an image and place cropped versions of that image to other map images that have overlapping distance renderings
            // will need to work with tiled maps too
            using (FileStream pngStream = new FileStream(mapPath, FileMode.Open, FileAccess.Read))
                using (var image = new Bitmap(pngStream))
                {
                    int    initWidth = image.Width, initHeight = image.Height;
                    double aspectRatio  = (double)Display.MapSqX / Display.MapSqY;
                    int    priRes       = (initWidth > initHeight) ? initWidth : initHeight;
                    int    secRes       = (initWidth > initHeight) ? initHeight : initWidth;
                    int    XRes         = initWidth / (Display.MapSqX * Display.PixelDensity);
                    int    YRes         = initHeight / (Display.MapSqY * Display.PixelDensity);
                    int    currRes      = (XRes > YRes) ? XRes : YRes;
                    int    displayScale = 1;
                    double squareAmtX   = (double)map.DistanceX / map.Scale;
                    double squareAmtY   = (double)map.DistanceY / map.Scale;
                    double mostSqs      = (squareAmtX > squareAmtY) ? squareAmtX : squareAmtY;
                    double sqPxls       = (squareAmtX > squareAmtY) ?
                                          image.Width / squareAmtX :
                                          image.Height / squareAmtY;

                    // fix resolution scaling first
                    while ((double)priRes / secRes != aspectRatio)
                    {
                        currRes++;
                        priRes = currRes * Display.MapSqX * Display.PixelDensity;
                        secRes = currRes * Display.MapSqY * Display.PixelDensity;
                    }

                    double scaledX = squareAmtX * Display.PixelDensity;
                    double scaledY = squareAmtY * Display.PixelDensity;
                    while (scaledX * squareAmtX > initWidth || scaledY * squareAmtY > initHeight)
                    {
                        displayScale *= 2;
                        scaledX       = (double)Display.PixelDensity / displayScale;
                        scaledY       = (double)Display.PixelDensity / displayScale;
                    }

                    Size   gridSize   = new Size((int)(scaledX * squareAmtX), (int)(scaledY * squareAmtY));
                    Bitmap gridResize = new Bitmap(image, gridSize);
                    SetMapFill(map, gridResize, currRes);
                    string path = Path.Combine(DepoDirectory, @"Resized Maps", map.MapName + ".png");
                    map.FilePath = path;

                    Bitmap canvas = new Bitmap(
                        currRes * Display.MapSqX * Display.PixelDensity,
                        currRes * Display.MapSqY * Display.PixelDensity);
                    gfx = Graphics.FromImage(canvas);
                    gfx.DrawImage(gridResize, map.RenderPoint);
                }
        }
Exemple #2
0
 public TiledMap(ResizedMap map, int displayX, int displayY)
 {
     MapName     = map.MapName;
     Scale       = map.Scale;
     OffsetX     = map.OffsetX;
     OffsetY     = map.OffsetY;
     DistanceX   = map.DistanceX;
     DistanceY   = map.DistanceY;
     RenderPoint = map.RenderPoint;
     TilesX      = (int)Math.Ceiling((double)DistanceX / displayX * Scale);
     TilesY      = (int)Math.Ceiling((double)DistanceY / displayY * Scale);
 }
Exemple #3
0
        public void AddResizedMap()
        {
            Console.WriteLine("Select image name from the \"Source Images\" folder to resize:");
            string filePath = ReadFilesInDirectory(Path.Combine(DepoDirectory, @"Source Images"));

            if (filePath != "" && File.Exists(filePath))
            {
                ResizedMap map = new ResizedMap();
                if (map.PromptMapInfo())
                {
                    ResizeMap(filePath, map);
                    AddMapJSON(map);
                }

                Console.WriteLine("{0} has been resized and saved to \"Resized Maps\" folder.", Path.GetFileName(filePath));
            }
            else
            {
                Console.WriteLine("File {0} not found.", Path.GetFileName(filePath));
            }
        }
Exemple #4
0
        public void TileMap(ResizedMap oldMap)
        {
            TiledMap map = new TiledMap(oldMap, Display.MapSqX, Display.MapSqY);

            Console.WriteLine("Scale for tiled maps: ");

            int    num;
            string input = Console.ReadLine();

            if (map.CheckValidMapInput(input, out num, 1, true) && map.Scale > num)
            {
                map.Scale = num;
            }
            else
            {
                Console.WriteLine("ERROR: Invalid scale.");
                return;
            }

            map.TiledImageDirectory = Path.Combine(DepoDirectory, @"Tiled Maps", map.MapName);
            Directory.CreateDirectory(map.TiledImageDirectory);

            using (FileStream pngStream = new FileStream(oldMap.FilePath, FileMode.Open, FileAccess.Read))
                using (var image = new Bitmap(pngStream))
                {
                    PixelFormat format = image.PixelFormat;

                    for (int i = 0; i < map.TilesX; i++)
                    {
                        for (int j = 0; j < map.TilesY; j++)
                        {
                            Rectangle cropArea = new Rectangle(i * (image.Width / map.TilesX), j * (image.Height / map.TilesY), (image.Width / map.TilesX), (image.Height / map.TilesY));
                            image.Clone(cropArea, format).Save(Path.Combine(map.TiledImageDirectory, map.MapName + "_" + i + "_" + j + ".png"));
                        }
                    }

                    // adds new serialized object to MapArray
                    AddMapJSON(map);
                }
        }
Exemple #5
0
        public void AddTiledMap()
        {
            Console.WriteLine("Select image to tile from the \"Resized Maps\" folder.");
            string imageSelect = Console.ReadLine();
            string filePath    = Path.Combine(DepoDirectory, @"Resized Maps", imageSelect + ".png");

            if (File.Exists(filePath))
            {
                List <JObject> mapNames = new List <JObject>();
                JToken         mapName;
                foreach (JObject obj in MapArray)
                {
                    mapName = obj.GetValue("FilePath");
                    if ((string)mapName == filePath)
                    {
                        mapNames.Add(obj);
                    }
                }
                if (mapNames.Count > 1)
                {
                    Console.WriteLine("This image is used by other maps, select which map info to use: ");
                    int i;
                    for (i = 0; i < mapNames.Count; i++)
                    {
                        Console.WriteLine("{0}: {1}", i + 1, mapNames[i]["MapName"]);
                    }
                    Console.WriteLine("{0}: {1}", i + 1, "Create new map info");
                    try
                    {
                        int select = Convert.ToInt32(Console.ReadLine());
                        if (select > 0 && select < mapNames.Count + 1)
                        {
                            ResizedMap resizedMap = mapNames[select - 1].ToObject <ResizedMap>();
                            TileMap(resizedMap);
                        }
                        else if (select == mapNames.Count + 1)
                        {
                            ResizedMap resizedMap = new ResizedMap();
                            if (resizedMap.PromptMapInfo())
                            {
                                TileMap(resizedMap);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Invalid selection.");
                            return;
                        }
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("ERROR: Invalid int.");
                        return;
                    }
                }
                else if (mapNames.Count == 1)
                {
                    ResizedMap resizedMap = mapNames[0].ToObject <ResizedMap>();
                    TileMap(resizedMap);
                }
                else
                {
                    Console.WriteLine("No map info found for this image. Please enter it now.");
                    ResizedMap resizedMap = new ResizedMap();
                    if (resizedMap.PromptMapInfo())
                    {
                        TileMap(resizedMap);
                    }
                }
            }
        }
Exemple #6
0
        void SetMapFill(ResizedMap map, Bitmap image, int resScale)
        {
            int fillPixelsX = (resScale * Display.MapSqX * Display.PixelDensity) - image.Width;
            int fillPixelsY = (resScale * Display.MapSqY * Display.PixelDensity) - image.Height;

            Console.WriteLine("\nImage of " + image.Width + "x" + image.Height + " will be resized to "
                              + image.Width + fillPixelsX + "x" + image.Height + fillPixelsY + ".");
            Console.WriteLine("Scale original image or fill with blank space?");
            Console.Write("\nSelect location on map to render blank space");
            if (fillPixelsX > 0 && fillPixelsY > 0)
            {
                Console.WriteLine("\n{0, -12} {1, 12}", "1: Top Left", "2: Top Right");
                Console.WriteLine("\n{0, -12} {1, 12}", "3: Bottom Left", "4: Bottom Right");
                Console.Write("Other Input: Cancel\n\n Select: ");

                switch (Console.ReadLine())
                {
                case "1":
                    map.SetFill(fillPixelsX, fillPixelsY);
                    break;

                case "2":
                    map.SetFill(0, fillPixelsY);
                    break;

                case "3":
                    map.SetFill(fillPixelsX, 0);
                    break;

                case "4":
                    map.SetFill(0, 0);
                    break;

                default:
                    break;
                }
            }
            else if (fillPixelsX > 0)
            {
                Console.Write("{0, -12} {0, 12}", "1: Left", "2: Right\n\n Select: ");
                switch (Console.ReadLine())
                {
                case "1":
                    map.SetFill(fillPixelsX, 0);
                    break;

                case "2":
                    map.SetFill(0, 0);
                    break;

                default:
                    break;
                }
            }
            else if (fillPixelsY > 0)
            {
                Console.Write("1: Top\n2: Bottom\n\nSelect: ");
                switch (Console.ReadLine())
                {
                case "1":
                    map.SetFill(0, fillPixelsY);
                    break;

                case "2":
                    map.SetFill(0, 0);
                    break;

                default:
                    break;
                }
            }
        }