public void Refresh(TileSheet tileSheet) { //try //{ System.Drawing.Bitmap tileSheetBitmap = null; // copy bitmap in memory, otherwise Clone() method forces // a reload of the image on every call! using (var tileSheetBitmapDisk = new System.Drawing.Bitmap(tileSheet.ImageSource)) { tileSheetBitmap = new System.Drawing.Bitmap(tileSheetBitmapDisk); } int tileCount = tileSheet.TileCount; System.Drawing.Bitmap[] tileBitmaps = new System.Drawing.Bitmap[tileCount]; Size tileSize = tileSheet.TileSize; System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(0, 0, tileSize.Width, tileSize.Height); System.Drawing.Rectangle srcRect = new System.Drawing.Rectangle(destRect.Location, destRect.Size); for (int tileIndex = 0; tileIndex < tileCount; tileIndex++) { Rectangle tileRectangle = tileSheet.GetTileImageBounds(tileIndex); srcRect.X = tileRectangle.Location.X; srcRect.Y = tileRectangle.Location.Y; srcRect.Width = tileRectangle.Size.Width; srcRect.Height = tileRectangle.Size.Height; System.Drawing.Bitmap tileBitmap = tileSheetBitmap.Clone( srcRect, tileSheetBitmap.PixelFormat); tileBitmaps[tileIndex] = tileBitmap; } m_bitmapCache[tileSheet] = tileBitmaps; //} //catch (Exception innerException) //{ // Exception exception = new Exception( // "Unable to load tile sheet '" + tileSheet.Id + "' with image source '" + tileSheet.ImageSource + "'", // innerException); // throw exception; //} }
private void OnPreviewPaint(object sender, PaintEventArgs paintEventArgs) { Graphics graphics = paintEventArgs.Graphics; if (m_bitmapImageSource == null && m_imageSourceErrorMessge == null) { graphics.DrawString("No image source selected", this.Font, SystemBrushes.ControlText, 0.0f, 0.0f); return; } else if (m_bitmapImageSource == null) { graphics.DrawString("Error loading image source:" + m_imageSourceErrorMessge, this.Font, SystemBrushes.ControlText, 0.0f, 0.0f); } else { graphics.InterpolationMode = InterpolationMode.NearestNeighbor; graphics.PixelOffsetMode = PixelOffsetMode.Half; graphics.ScaleTransform(m_trackBarZoom.Value, m_trackBarZoom.Value); graphics.TranslateTransform(-m_previewOffset.X, -m_previewOffset.Y); int imageWidth = m_bitmapImageSource.Width; int imageHeight = m_bitmapImageSource.Height; graphics.DrawImage(m_bitmapImageSource, 0, 0, imageWidth, imageHeight); int marginLeft = (int)m_textBoxLeftMargin.Value; int marginTop = (int)m_textBoxTopMargin.Value; int tileWidth = (int)m_textBoxTileWidth.Value; int tileHeight = (int)m_textBoxTileHeight.Value; int tilePaddingX = (int)m_textBoxSpacingX.Value; int tilePaddingY = (int)m_textBoxSpacingY.Value; Pen alignmentPen = new Pen(SystemColors.ActiveCaption); float fZoomInverse = 1.0f / m_trackBarZoom.Value; alignmentPen.Width = fZoomInverse; for (int posY = marginTop; posY + tileHeight <= imageHeight; posY += tileHeight + tilePaddingY) { for (int posX = marginLeft; posX + tileWidth <= imageWidth; posX += tileWidth + tilePaddingX) { graphics.DrawRectangle(alignmentPen, posX, posY, tileWidth, tileHeight); } } Pen selectionPen = Pens.Red; Brush selectionBrush = new SolidBrush(Color.FromArgb(128, Color.Red)); if (m_previewMode != PreviewMode.Preview && m_tileHoverIndex != -1) { xTile.Dimensions.Rectangle rectangle = m_tileSheet.GetTileImageBounds(m_tileHoverIndex); graphics.FillRectangle(selectionBrush, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); graphics.DrawRectangle(selectionPen, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); } if (m_previewMode == PreviewMode.PickSecond) { xTile.Dimensions.Rectangle rectangle = m_tileSheet.GetTileImageBounds(m_swapTileIndex1); graphics.FillRectangle(selectionBrush, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); graphics.DrawRectangle(selectionPen, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); } } }
public override void Do() { Bitmap imageSourceBitmap = LoadUnlockedBitmap(m_tileSheet.ImageSource); xTile.Dimensions.Rectangle rectangle1 = m_tileSheet.GetTileImageBounds(m_tileIndex1); xTile.Dimensions.Rectangle rectangle2 = m_tileSheet.GetTileImageBounds(m_tileIndex2); System.Drawing.Rectangle source1 = new System.Drawing.Rectangle(rectangle1.X, rectangle1.Y, rectangle1.Width, rectangle1.Height); System.Drawing.Rectangle source2 = new System.Drawing.Rectangle(rectangle2.X, rectangle2.Y, rectangle2.Width, rectangle2.Height); Bitmap tileBitmap1 = imageSourceBitmap.Clone(source1, imageSourceBitmap.PixelFormat); Bitmap tileBitmap2 = imageSourceBitmap.Clone(source2, imageSourceBitmap.PixelFormat); Graphics graphics = Graphics.FromImage(imageSourceBitmap); graphics.InterpolationMode = InterpolationMode.NearestNeighbor; graphics.SetClip(source1); graphics.Clear(Color.FromArgb(0, 0, 0, 0)); graphics.DrawImageUnscaled(tileBitmap2, source1.Location); graphics.SetClip(source2); graphics.Clear(Color.FromArgb(0, 0, 0, 0)); graphics.DrawImageUnscaled(tileBitmap1, source2.Location); imageSourceBitmap.Save(m_tileSheet.ImageSource); TileImageCache.Instance.Refresh(m_tileSheet); Map map = m_tileSheet.Map; foreach (Layer layer in map.Layers) { Location tileLocation = new Location(); for (tileLocation.Y = 0; tileLocation.Y < layer.LayerHeight; tileLocation.Y++) { for (tileLocation.X = 0; tileLocation.X < layer.LayerWidth; tileLocation.X++) { Tile tile = layer.Tiles[tileLocation]; if (tile == null) { continue; } if (tile.TileSheet != m_tileSheet) { continue; } if (tile is StaticTile) { if (tile.TileIndex == m_tileIndex1) { tile.TileIndex = m_tileIndex2; } else if (tile.TileIndex == m_tileIndex2) { tile.TileIndex = m_tileIndex1; } } else if (tile is AnimatedTile) { AnimatedTile animatedTile = (AnimatedTile)tile; foreach (StaticTile tileFrame in animatedTile.TileFrames) { if (tileFrame.TileIndex == m_tileIndex1) { tileFrame.TileIndex = m_tileIndex2; } else if (tileFrame.TileIndex == m_tileIndex2) { tileFrame.TileIndex = m_tileIndex1; } } } } } } }