/// <summary>
    /// This method is called by MapperOptimalEfficiency_AdditionalStats for each iteration
    /// </summary>
    /// <param name="images"></param>
    /// <param name="maxWidth"></param>
    /// <param name="maxHeight"></param>
    /// <returns></returns>
    protected override S MappingRestrictedBox(
        IOrderedEnumerable <IImageInfo> images, int maxWidth, int maxHeight, ICanvasStats canvasStats,
        out int lowestFreeHeightDeficitTallestRightFlushedImage)
    {
        S intermediateSpriteInfo =
            base.MappingRestrictedBox(images, maxWidth, maxHeight, canvasStats, out lowestFreeHeightDeficitTallestRightFlushedImage);

        IterationStats          stats          = null;
        CanvasWritingStepImages canvasBLFStats = _canvas as CanvasWritingStepImages;

        List <ImagePlacementDetails> imageDetails = null;

        if (canvasBLFStats != null)
        {
            imageDetails = new List <ImagePlacementDetails>(canvasBLFStats.ImageDetails);
        }

        if (intermediateSpriteInfo != null)
        {
            stats =
                new IterationStats
            {
                Result                   = IterationResult.Success,
                MaxCanvasWidth           = maxWidth,
                MaxCanvasHeight          = maxHeight,
                IntermediateSpriteWidth  = intermediateSpriteInfo.Width,
                IntermediateSpriteHeight = intermediateSpriteInfo.Height,
                ImageDetails             = imageDetails
            };
        }
        else
        {
            stats =
                new IterationStats
            {
                Result          = IterationResult.Failure,
                MaxCanvasWidth  = maxWidth,
                MaxCanvasHeight = maxHeight,
                ImageDetails    = imageDetails
            };
        }

        _currentMappingIterationStats.Add(stats);

        // Clear out the current image details as kept by the canvas, otherwise we get the same details again next time.
        if (canvasBLFStats != null)
        {
            canvasBLFStats.ImageDetails.Clear();
        }

        return(intermediateSpriteInfo);
    }
        /// <summary>
        /// Produces a mapping to a sprite that has given maximum dimensions.
        /// If the mapping can not be done inside those dimensions, returns null.
        /// </summary>
        /// <param name="images">
        /// List of image infos.
        ///
        /// This method will not sort this list.
        /// All images in this collection will be used, regardless of size.
        /// </param>
        /// <param name="maxWidth">
        /// The sprite won't be wider than this.
        /// </param>
        /// <param name="maxHeight">
        /// The generated sprite won't be higher than this.
        /// </param>
        /// <param name="canvasStats">
        /// The statistics produced by the canvas. These numbers are since the last call to its SetCanvasDimensions method.
        /// </param>
        /// <param name="lowestFreeHeightDeficitTallestRightFlushedImage">
        /// The lowest free height deficit for the images up to and including the tallest rectangle whose right hand border sits furthest to the right
        /// of all images.
        ///
        /// This is the minimum amount by which the height of the canvas needs to be increased to accommodate that rectangle.
        /// if the width of the canvas is decreased to one less than the width now taken by images.
        ///
        /// Note that providing the additional height might get some other (not right flushed) image to be placed higher, thereby
        /// making room for the flushed right image.
        ///
        /// This will be set to Int32.MaxValue if there was never any free height deficit.
        /// </param>
        /// <returns>
        /// The generated sprite.
        ///
        /// null if not all the images could be placed within the size limitations.
        /// </returns>
        protected virtual S MappingRestrictedBox(
            IOrderedEnumerable <IImageInfo> images,
            int maxWidth, int maxHeight, ICanvasStats canvasStats,
            out int lowestFreeHeightDeficitTallestRightFlushedImage)
        {
            lowestFreeHeightDeficitTallestRightFlushedImage = 0;
            _canvas.SetCanvasDimensionsbis(maxWidth, maxHeight);

            S   spriteInfo = new S();
            int heightHighestRightFlushedImage = 0;
            int furthestRightEdge = 0;

            foreach (IImageInfo image in images)
            {
                int xOffset;
                int yOffset;
                int lowestFreeHeightDeficit;
                if (!_canvas.AddRectangle(
                        image.Width, image.Height,
                        out xOffset, out yOffset,
                        out lowestFreeHeightDeficit))
                {
                    // Not enough room on the canvas to place the rectangle
                    spriteInfo = null;
                    break;
                }

                MappedImageInfo imageLocation = new MappedImageInfo(xOffset, yOffset, image);
                spriteInfo.AddMappedImage(imageLocation);

                // Update the lowestFreeHeightDeficitTallestRightFlushedImage
                int rightEdge = image.Width + xOffset;
                if ((rightEdge > furthestRightEdge) ||
                    ((rightEdge == furthestRightEdge) && (image.Height > heightHighestRightFlushedImage)))
                {
                    // The image is flushed the furthest right of all images, or it is flushed equally far to the right
                    // as the furthest flushed image but it is taller.

                    lowestFreeHeightDeficitTallestRightFlushedImage = lowestFreeHeightDeficit;
                    heightHighestRightFlushedImage = image.Height;
                    furthestRightEdge = rightEdge;
                }
            }

            _canvas.GetStatistics(canvasStats);

            return(spriteInfo);
        }
Exemplo n.º 3
0
 /// <summary>
 /// See ICanvas
 /// </summary>
 /// <param name="canvasStats"></param>
 public void GetStatistics(ICanvasStats canvasStats)
 {
     canvasStats.NbrCellsGenerated = _nbrCellsGenerated;
     canvasStats.RectangleAddAttempts = _nbrRectangleAddAttempts;
     canvasStats.LowestFreeHeightDeficit = _lowestFreeHeightDeficitSinceLastRedim;
 }
Exemplo n.º 4
0
        /// <summary>
        /// See ICanvas
        /// </summary>
        /// <param name="canvasStats"></param>

        public void GetStatistics(ICanvasStats canvasStats)
        {
            canvasStats.NbrCellsGenerated       = _nbrCellsGenerated;
            canvasStats.RectangleAddAttempts    = _nbrRectangleAddAttempts;
            canvasStats.LowestFreeHeightDeficit = _lowestFreeHeightDeficitSinceLastRedim;
        }