private List <RoomTemplateConfiguration> GetRoomTemplateConfigurations(List <RoomTemplateGrid2D> roomTemplates, double expectedRatio, DungeonDrawerOptions options)
        {
            var orderedRoomTemplates = roomTemplates.OrderByDescending(x => x.Outline.BoundingRectangle.Width).ToList();
            var minDistance          = 3;

            var bestRatio = double.MaxValue;
            var bestScale = double.MinValue;
            List <RoomTemplateConfiguration>  bestConfigurations = null;
            List <List <RoomTemplateGrid2D> > lastRows           = null;

            while (true)
            {
                int rowWidth;

                if (lastRows == null)
                {
                    rowWidth = orderedRoomTemplates[0].Outline.BoundingRectangle.Width;
                }
                else
                {
                    rowWidth = GetNextBreakPoint(lastRows);
                }

                var rows            = GetRows(roomTemplates, rowWidth);
                var configurations  = GetConfigurations(rows, rowWidth, minDistance);
                var ratio           = GetWidthHeightRatio(configurations);
                var emptySpaceRatio = GetEmptySpaceRatio(rows, rowWidth);

                var outlines    = configurations.Select(x => x.RoomTemplate.Outline + x.Position).ToList();
                var boundingBox = DrawingUtils.GetBoundingBox(outlines);
                var(width, height, scale) = DrawingUtils.GetSize(boundingBox, options.Width, options.Height, options.Scale, options.PaddingAbsolute, options.PaddingPercentage);

                if (scale > bestScale)
                {
                    bestConfigurations = configurations;
                    bestScale          = scale;
                }

                //if (Math.Abs(expectedRatio - ratio) < Math.Abs(expectedRatio - bestRatio))
                //{
                //    bestConfigurations = configurations;
                //    bestRatio = ratio;
                //}

                lastRows = rows;

                if (rows.Count == 1)
                {
                    if (bestConfigurations == null)
                    {
                        bestConfigurations = configurations;
                    }

                    break;
                }
            }

            return(bestConfigurations);
        }
Beispiel #2
0
        public Bitmap DrawGraph(LevelDescriptionGrid2D <TRoom> levelDescription, Dictionary <TRoom, Vector2Int> positions, DungeonDrawerOptions options)
        {
            var configurations = GetConfigurations(positions);

            var outlines    = configurations.Select(x => x.Outline + x.Position).ToList();
            var boundingBox = DrawingUtils.GetBoundingBox(outlines);

            var(width, height, scale) = DrawingUtils.GetSize(boundingBox, options.Width, options.Height, options.Scale, options.PaddingAbsolute, options.PaddingPercentage);
            var offset = DrawingUtils.GetOffset(boundingBox, width, height, scale);

            bitmap   = new Bitmap(width, height);
            graphics = Graphics.FromImage(bitmap);
            graphics.SmoothingMode = SmoothingMode.HighQuality;

            using (SolidBrush brush = new SolidBrush(Color.FromArgb(248, 248, 244)))
            {
                graphics.FillRectangle(brush, 0, 0, width, height);
            }

            var outlinePen = new Pen(Color.FromArgb(50, 50, 50), 0.25f)
            {
                EndCap   = LineCap.Round,
                StartCap = LineCap.Round
            };

            var shadePen = new Pen(Color.FromArgb(204, 206, 206), 1.3f)
            {
                EndCap   = LineCap.Round,
                StartCap = LineCap.Round
            };

            graphics.TranslateTransform(offset.X, offset.Y);
            graphics.ScaleTransform(scale, scale);


            foreach (var configuration in configurations)
            {
                DrawShading(GetOutline(configuration.Outline, null, configuration.Position), shadePen);
            }

            //var hatchingUsedPoints = new List<Vector2>();
            //foreach (var configuration in configurations)
            //{
            //    DrawHatching(configuration.Outline + configuration.Position, hatchingUsedPoints, options.HatchingClusterOffset, options.HatchingLength);
            //}

            DrawEdges(levelDescription.GetGraphWithoutCorridors(), configurations, outlinePen);

            foreach (var configuration in configurations)
            {
                DrawRoomBackground(configuration.Outline + configuration.Position, options.RoomBackgroundColor);
                DrawGrid(configuration.Outline + configuration.Position);
                DrawOutline(configuration.Outline + configuration.Position, GetOutline(configuration.Outline, null, configuration.Position), outlinePen);
            }

            foreach (var configuration in configurations)
            {
                if (options.ShowRoomNames)
                {
                    DrawTextOntoPolygon(configuration.Outline + configuration.Position, configuration.Room.ToString(), options.FontSize);
                }
            }

            shadePen.Dispose();
            outlinePen.Dispose();

            return(bitmap);
        }
        public Bitmap DrawRoomTemplates(List <RoomTemplateGrid2D> roomTemplates, DungeonDrawerOptions options)
        {
            var configurations = GetRoomTemplateConfigurations(roomTemplates, options.Width.Value / (double)options.Height.Value, options);

            var outlines    = configurations.Select(x => x.RoomTemplate.Outline + x.Position).ToList();
            var boundingBox = DrawingUtils.GetBoundingBox(outlines);

            var(width, height, scale) = DrawingUtils.GetSize(boundingBox, options.Width, options.Height, options.Scale, options.PaddingAbsolute, options.PaddingPercentage);
            var offset = DrawingUtils.GetOffset(boundingBox, width, height, scale);

            bitmap   = new Bitmap(width, height);
            graphics = Graphics.FromImage(bitmap);
            graphics.SmoothingMode = SmoothingMode.HighQuality;

            using (SolidBrush brush = new SolidBrush(Color.FromArgb(248, 248, 244)))
            {
                graphics.FillRectangle(brush, 0, 0, width, height);
            }

            var outlinePen = new Pen(Color.FromArgb(50, 50, 50), 0.2f)
            {
                EndCap   = LineCap.Round,
                StartCap = LineCap.Round
            };

            var shadePen = new Pen(Color.FromArgb(204, 206, 206), 1.3f)
            {
                EndCap   = LineCap.Round,
                StartCap = LineCap.Round
            };

            graphics.TranslateTransform(offset.X, offset.Y);
            graphics.ScaleTransform(scale, scale);

            if (options.EnableShading)
            {
                foreach (var roomTemplate in configurations)
                {
                    DrawShading(GetOutline(roomTemplate.RoomTemplate.Outline, null, roomTemplate.Position), shadePen);
                }
            }

            if (options.EnableHatching)
            {
                var hatchingUsedPoints = new List <Tuple <RectangleGrid2D, List <Vector2> > >();
                foreach (var roomTemplate in configurations)
                {
                    DrawHatching(roomTemplate.RoomTemplate.Outline + roomTemplate.Position, hatchingUsedPoints, options.HatchingClusterOffset, options.HatchingLength);
                }
            }

            foreach (var roomTemplate in configurations)
            {
                DrawRoomBackground(roomTemplate.RoomTemplate.Outline + roomTemplate.Position, options.RoomBackgroundColor);
                DrawGrid(roomTemplate.RoomTemplate.Outline + roomTemplate.Position);
                DrawOutline(roomTemplate.RoomTemplate.Outline + roomTemplate.Position, GetOutline(roomTemplate.RoomTemplate.Outline, null, roomTemplate.Position), outlinePen);
                DrawDoors(roomTemplate);
            }

            foreach (var roomTemplate in configurations)
            {
                if (options.ShowRoomNames)
                {
                    DrawTextOntoPolygon(roomTemplate.RoomTemplate.Outline + roomTemplate.Position, roomTemplate.RoomTemplate.Name, options.FontSize);
                }
            }

            shadePen.Dispose();
            outlinePen.Dispose();

            return(bitmap);
        }
        /// <summary>
        /// Draws a given layout and returns a bitmap with the drawing.
        /// </summary>
        /// <param name="layout">Layout to be drawn.</param>
        /// <param name="options">Options. See <see cref="DungeonDrawerOptions"/>.</param>
        public Bitmap DrawLayout(LayoutGrid2D <TRoom> layout, DungeonDrawerOptions options)
        {
            var roomOutlines = layout.Rooms.Select(x => x.Outline + x.Position).ToList();
            var boundingBox  = DrawingUtils.GetBoundingBox(roomOutlines);

            var(width, height, scale) = DrawingUtils.GetSize(boundingBox, options.Width, options.Height, options.Scale, options.PaddingAbsolute, options.PaddingPercentage);
            var offset = DrawingUtils.GetOffset(boundingBox, width, height, scale);

            bitmap   = new Bitmap(width, height);
            graphics = Graphics.FromImage(bitmap);
            graphics.SmoothingMode = SmoothingMode.HighQuality;

            using (SolidBrush brush = new SolidBrush(options.BackgroundColor))
            {
                graphics.FillRectangle(brush, 0, 0, width, height);
            }

            outlinePen = new Pen(Color.FromArgb(50, 50, 50), 0.2f)
            {
                EndCap   = LineCap.Round,
                StartCap = LineCap.Round
            };

            shadePen = new Pen(Color.FromArgb(204, 206, 206), 1.3f)
            {
                EndCap   = LineCap.Round,
                StartCap = LineCap.Round
            };

            var rooms = layout.Rooms.ToList();

            graphics.TranslateTransform(offset.X, offset.Y);
            graphics.ScaleTransform(scale, scale);

            if (options.EnableShading)
            {
                foreach (var room in rooms)
                {
                    DrawShading(GetOutline(room.Outline, room.Doors.Select(x => x.DoorLine).ToList(), room.Position), shadePen);
                }
            }

            if (options.EnableHatching)
            {
                var hatchingUsedPoints = new List <Tuple <RectangleGrid2D, List <Vector2> > >();
                foreach (var room in rooms)
                {
                    DrawHatching(room.Outline + room.Position, hatchingUsedPoints, options.HatchingClusterOffset, options.HatchingLength);
                }
            }

            foreach (var room in rooms)
            {
                DrawRoomBackground(room.Outline + room.Position, options.RoomBackgroundColor);

                if (options.EnableGridLines)
                {
                    DrawGrid(room.Outline + room.Position);
                }

                DrawOutline(room.Outline + room.Position, GetOutline(room.Outline, room.Doors.Select(x => x.DoorLine).ToList(), room.Position), outlinePen);
            }

            foreach (var room in rooms)
            {
                if (options.ShowRoomNames && !room.IsCorridor)
                {
                    DrawTextOntoPolygon(room.Outline + room.Position, room.Room.ToString(), options.FontSize);
                }
            }

            outlinePen.Dispose();
            shadePen.Dispose();

            return(bitmap);
        }