/// <summary>Returns the original (unscaled) image of the specified type.</summary> private Image getImage(SokobanImage imageType) { switch (imageType) { case SokobanImage.Piece: return(Properties.Resources.Skin_Piece); case SokobanImage.PieceOnTarget: return(Properties.Resources.Skin_PieceTarget); case SokobanImage.PieceSelected: return(Properties.Resources.Skin_PieceSelected); case SokobanImage.Sokoban: return(Properties.Resources.Skin_Sokoban); case SokobanImage.Wall: return(Properties.Resources.Skin_Wall); case SokobanImage.Target: return(Properties.Resources.Skin_Target); case SokobanImage.TargetUnderPiece: return(Properties.Resources.Skin_TargetUnder); default: throw new Exception("Unknown Sokoban image type"); } }
/// <summary>Returns the specified image scaled for the current cell size. Images are cached for efficiency.</summary> private Image getScaledImage(SokobanImage imageType) { // If we have it - return it Size sz = new Size(CellWidth, CellHeight); if (_cachedImage.ContainsKey(sz)) { _cachedImageAge[sz] = DateTime.Now; return(_cachedImage[sz][imageType]); } // Discard the oldest version if too many if (_cachedImage.Count > _cachedMaxSizes) { KeyValuePair <Size, DateTime> last = new KeyValuePair <Size, DateTime>(new Size(), DateTime.Now); foreach (KeyValuePair <Size, DateTime> kvp in _cachedImageAge) { if (kvp.Value < last.Value) { last = kvp; } } _cachedImage.Remove(last.Key); _cachedImageAge.Remove(last.Key); } // Create scaled versions _cachedImage[sz] = new Dictionary <SokobanImage, Bitmap>(); RectangleF dest = new RectangleF(0, 0, CellWidth * 1.5f + 1f, CellHeight * 1.5f + 1f); foreach (SokobanImage si in Enum.GetValues(typeof(SokobanImage))) { _cachedImage[sz][si] = new Bitmap((int)dest.Width + 1, (int)dest.Height + 1); using (var g = Graphics.FromImage(_cachedImage[sz][si])) { if (si == SokobanImage.Wall) { g.InterpolationMode = InterpolationMode.Bicubic; } else { g.InterpolationMode = InterpolationMode.High; } g.DrawImage(getImage(si), dest); } } // Return the requested one _cachedImageAge[sz] = DateTime.Now; return(_cachedImage[sz][imageType]); }
/// <summary> /// Draws a specified image into the specified cell. /// </summary> /// <param name="g">Graphics object to render onto.</param> /// <param name="x">X co-ordinate of the cell to render into.</param> /// <param name="y">Y co-ordinate of the cell to render into.</param> /// <param name="imageType">Identifies the image to be drawn.</param> public void DrawCell(Graphics g, int x, int y, SokobanImage imageType) { Rectangle rect = CellRect(x, y); g.DrawImageUnscaled(getScaledImage(imageType), rect.Left, rect.Top); }
/// <summary>Draws a specified image into the specified cell.</summary> /// <param name="g">Graphics object to render onto.</param> /// <param name="pos">Co-ordinates of the cell to render into.</param> /// <param name="imageType">Identifies the image to be drawn.</param> public void DrawCell(Graphics g, Point pos, SokobanImage imageType) { DrawCell(g, pos.X, pos.Y, imageType); }