Example #1
0
 private MapImageProcessor(MapImageTemplateProcessor mapImageTemplateProcessor, Bitmap gameMapMapImage, TextDrawingHandler textDrawingHandler,
                           ColoringHandler coloringHandler, SelectRegionHandler selectRegionHandler, bool isFogOfWar)
 {
     MapImage                 = gameMapMapImage;
     templateProcessor        = mapImageTemplateProcessor;
     this.isFogOfWar          = isFogOfWar;
     this.textDrawingHandler  = textDrawingHandler;
     this.coloringHandler     = coloringHandler;
     this.selectRegionHandler = selectRegionHandler;
 }
        public TextDrawingHandler(Bitmap mapImage, MapImageTemplateProcessor templateProcessor, ColoringHandler coloringHandler)
        {
            if (templateProcessor == null || coloringHandler == null || mapImage == null)
            {
                throw new ArgumentException();
            }

            this.templateProcessor = templateProcessor;
            this.coloringHandler   = coloringHandler;
            this.mapImage          = mapImage;
        }
Example #3
0
        /// <summary>
        ///     Initializes an instance of MapImageProcessor.
        /// </summary>
        /// <param name="map">Map of the future game.</param>
        /// <param name="regionHighlightedImagePath">
        ///     Path of image whose role is to map region to certain color to recognize what
        ///     image has been clicked on by the user.
        /// </param>
        /// <param name="regionColorMappingPath">Path of file mapping color to certain existing map region.</param>
        /// <param name="mapImagePath">Map of image that will be used as map and displayed to the user.</param>
        /// <param name="isFogOfWar">True, if the given map should be processed as fog of war type.</param>
        /// <returns>Initialized instance.</returns>
        public static MapImageProcessor Create(Map map, string regionHighlightedImagePath,
                                               string regionColorMappingPath, string mapImagePath, bool isFogOfWar)
        {
            Bitmap regionHighlightedImage = new Bitmap(regionHighlightedImagePath);
            Bitmap image = new Bitmap(mapImagePath);

            // images are not equally sized
            if (image.Size != regionHighlightedImage.Size)
            {
                throw new ArgumentException();
            }

            // read the file
            XmlReaderSettings settings = new XmlReaderSettings
            {
                ValidationType = ValidationType.Schema
            };

            settings.ValidationFlags        |= XmlSchemaValidationFlags.ProcessInlineSchema;
            settings.ValidationFlags        |= XmlSchemaValidationFlags.ProcessSchemaLocation;
            settings.ValidationFlags        |= XmlSchemaValidationFlags.ReportValidationWarnings;
            settings.ValidationEventHandler += (sender, args) =>
            {
                switch (args.Severity)
                {
                case XmlSeverityType.Error:
                    throw new XmlSchemaValidationException();

                case XmlSeverityType.Warning:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            };

            var dictionary = new Dictionary <Color, Region>();

            using (XmlReader reader = XmlReader.Create(regionColorMappingPath, settings))
            {
                Color  color  = default(Color);
                Region region = default(Region);

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        switch (reader.Name)
                        {
                        case "Entry":
                            break;

                        case nameof(Color):
                            // TODO : maybe better way is possible
                            byte red   = Convert.ToByte(reader.GetAttribute("Red"));
                            byte green = Convert.ToByte(reader.GetAttribute("Green"));
                            byte blue  = Convert.ToByte(reader.GetAttribute("Blue"));
                            byte alpha = Convert.ToByte(reader.GetAttribute("Alpha"));
                            color = Color.FromArgb(alpha, red, green, blue);
                            break;

                        case nameof(Region):
                            region = (from item in map.Regions
                                      where item.Name == reader.GetAttribute("Name")
                                      select item).First();
                            break;
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if (reader.Name == "Entry")
                        {
                            dictionary.Add(color, region);
                        }
                        break;
                    }
                }
            }

            MapImageTemplateProcessor mapImageTemplateProcessor =
                new MapImageTemplateProcessor(map, regionHighlightedImage, dictionary);

            ColoringHandler coloringHandler = new ColoringHandler(image, mapImageTemplateProcessor);

            TextDrawingHandler textDrawingHandler = new TextDrawingHandler(image, mapImageTemplateProcessor, coloringHandler);

            HighlightHandler highlightHandler = new HighlightHandler(image, mapImageTemplateProcessor, textDrawingHandler, coloringHandler);

            SelectRegionHandler selectRegionHandler = new SelectRegionHandler(mapImageTemplateProcessor, highlightHandler, isFogOfWar);

            return(new MapImageProcessor(mapImageTemplateProcessor, image, textDrawingHandler, coloringHandler, selectRegionHandler, isFogOfWar));
        }