private void ChangeFrame(int newFrame) { if (curPak == -1 || curSprite == -1 || Paks[curPak].sprites[curSprite].frames.Count == 0) { SetFrameControls(false); curFrame = -1; return; } SetFrameControls(true); Pak.FrameInfo frameInf = Paks[curPak].sprites[curSprite].frames[newFrame]; int width = Paks[curPak].sprites[curSprite].image.Width; int height = Paks[curPak].sprites[curSprite].image.Height; TxtTBarLink.SetValue(tbarX, frameInf.X, 0, width); TxtTBarLink.SetValue(tbarY, frameInf.Y, 0, height); TxtTBarLink.SetValue(tbarWidth, frameInf.Width, 0, width); TxtTBarLink.SetValue(tbarHeight, frameInf.Height, 0, height); TxtTBarLink.SetValue(tbarFixX, frameInf.fixX, minFixX, maxFixX); TxtTBarLink.SetValue(tbarFixY, frameInf.fixY, minFixY, maxFixY); curFrame = newFrame; }
private void startAnimation_Click(object sender, EventArgs e) { if (aniForm != null) { aniForm.Close(); } if (curPak == -1 || Paks[curPak].sprites.Count == 0) { return; } if (curSprite == -1 || Paks[curPak].sprites[curSprite].frames.Count == 0) { return; } int invalidFrame = GetInvalidFrame(Paks[curPak].sprites[curSprite]); if (invalidFrame != -1) { SetGeneralStatus("Warning: Frame " + invalidFrame + " in Sprite " + curPak + " is outside the image boundaries or 0 width/height. Cannot animate!"); return; } aniForm = new Form(); aniForm.Text = "Animation - Pak: " + fileList.Items[curPak] + " Sprite: " + curSprite; aniForm.TopMost = true; PictureBox newPicBox = new PictureBox(); newPicBox.Name = "animation"; newPicBox.SizeMode = PictureBoxSizeMode.AutoSize; aniForm.Controls.Add(newPicBox); List <Bitmap> aniFrames = CutToFrames(Paks[curPak].sprites[curSprite].image, Paks[curPak].sprites[curSprite].frames); List <Pak.FrameInfo> frames = new List <Pak.FrameInfo>(); int lowestFixX = int.MaxValue; int lowestFixY = int.MaxValue; int topWidth = 0; int topHeight = 0; for (int i = 0; i < Paks[curPak].sprites[curSprite].frames.Count; i++) { Pak.FrameInfo frame = Paks[curPak].sprites[curSprite].frames[i]; if (frame.Width > topWidth) { topWidth = frame.Width; } if (frame.Height > topHeight) { topHeight = frame.Height; } if (frame.fixX < lowestFixX) { lowestFixX = frame.fixX; } if (frame.fixY < lowestFixY) { lowestFixY = frame.fixY; } } int maxDimension = topHeight > topWidth ? topHeight : topWidth; int scaleFactor = 1; if (scaleAnimation.Checked) { scaleFactor = (int)Math.Round((float)350 / maxDimension); if (scaleFactor < 1) { scaleFactor = 1; } topWidth *= scaleFactor; topHeight *= scaleFactor; lowestFixX *= scaleFactor; lowestFixY *= scaleFactor; } for (int i = 0; i < Paks[curPak].sprites[curSprite].frames.Count; i++) { Pak.FrameInfo frame = Paks[curPak].sprites[curSprite].frames[i]; Pak.FrameInfo newFrame = new Pak.FrameInfo(); newFrame.Y = frame.Y * scaleFactor; newFrame.X = frame.X * scaleFactor; newFrame.Width = frame.Width * scaleFactor; newFrame.Height = frame.Height * scaleFactor; newFrame.fixX = (Int16)(frame.fixX * scaleFactor); newFrame.fixY = (Int16)(frame.fixY * scaleFactor); frames.Add(newFrame); aniFrames[i] = ResizeImage(aniFrames[i], scaleFactor); frames[i].fixX -= (Int16)lowestFixX; frames[i].fixY -= (Int16)lowestFixY; if (aniForm.Width < (frames[i].Width + frames[i].fixX + 50)) { aniForm.Width = (frames[i].Width + frames[i].fixX + 50); } if (aniForm.Height < (frames[i].Height + frames[i].fixY + 50)) { aniForm.Height = (frames[i].Height + frames[i].fixY + 50); } } aniForm.BackColor = Paks[curPak].sprites[curSprite].image.GetPixel(0, 0); aniForm.FormClosed += delegate(Object sender2, FormClosedEventArgs e2) { this.aniTimer.Stop(); this.aniTimer.Dispose(); }; aniForm.Show(); aniForm.Tag = 0; if (aniTimer != null) { aniTimer.Dispose(); } aniTimer = new Timer(); if (fpsSelection.SelectedIndex == -1) { fpsSelection.SelectedIndex = 11; } newPicBox.Left = newPicBox.Top = 10; aniTimer.Interval = (int)Math.Round(1000 / (float)(fpsSelection.SelectedIndex + 1)); aniTimer.Tick += delegate(object send, EventArgs ee) { newPicBox.Image = aniFrames[(int)aniForm.Tag]; newPicBox.Left = (aniForm.Width - topWidth) / 2 + frames[(int)aniForm.Tag].fixX; newPicBox.Top = (aniForm.Height - topHeight) / 2 + frames[(int)aniForm.Tag].fixY; aniForm.Tag = ((int)aniForm.Tag) + 1; if ((int)aniForm.Tag >= aniFrames.Count) { aniForm.Tag = 0; } }; aniTimer.Start(); }
private void generateSpriteSheet_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"; fileDialog.Multiselect = true; if (fileDialog.ShowDialog() == DialogResult.OK) { List <Image> loadedImages = new List <Image>(); List <Rectangle> frameBounds = new List <Rectangle>(); String [] splitFile = fileDialog.FileName.Split('.'); int start; if (int.TryParse(splitFile[splitFile.Length - 2], out start) == false) { MessageBox.Show("Unable to parse file name: " + Path.GetFileName(fileDialog.FileName), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } splitFile[splitFile.Length - 2] = "{0:D2}"; String pathFormat = String.Join(".", splitFile); for (int i = start; i < 100; i++) { String fileName = String.Format(pathFormat, i); try { var stream = File.OpenRead(fileName); loadedImages.Add((Bitmap)Bitmap.FromStream(stream)); stream.Close(); } catch (FileNotFoundException) { break; } catch (Exception ex) { MessageBox.Show(String.Format("Error occoured while loading image ({0}) : {1}", Path.GetFileName(fileName), ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)); if (Paks[curPak].sprites.Count == 0) { ClosePak(curPak); } return; } } int totalWidth = 0, maxHeight = 0; foreach (Image frame in loadedImages) { Rectangle bounds = getFrameBounds((Bitmap)frame); if (bounds.Width < 1 || bounds.Height < 1) { MessageBox.Show("Could not find frame within image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } frameBounds.Add(bounds); totalWidth += bounds.Width; if (bounds.Height > maxHeight) { maxHeight = bounds.Height; } } Bitmap generatedImage = new Bitmap(totalWidth, maxHeight, loadedImages[0].PixelFormat); generatedImage.SetResolution(loadedImages[0].HorizontalResolution, loadedImages[0].VerticalResolution); Pak.Sprite newSprite = new Pak.Sprite(); using (Graphics g = Graphics.FromImage((Image)generatedImage)) { GraphicsUnit gUnit = GraphicsUnit.Pixel; Brush keyBrush = new SolidBrush(((Bitmap)loadedImages[0]).GetPixel(0, 0)); g.FillRectangle(keyBrush, generatedImage.GetBounds(ref gUnit)); for (int i = 0, x = 0; i < loadedImages.Count; i++) { g.DrawImage(loadedImages[i], x, 0, frameBounds[i], GraphicsUnit.Pixel); Pak.FrameInfo frmInfo = new Pak.FrameInfo(); Rectangle frameRect = frameBounds[i]; frameRect.Y = 0; frameRect.X = x; frmInfo.rect = frameRect; frmInfo.fixX = (short)(loadedImages[i].Width / 2 + frameBounds[i].Left); frmInfo.fixY = (short)(loadedImages[i].Height / 2 + frameBounds[i].Top); newSprite.frames.Add(frmInfo); x += frameBounds[i].Width; } } newSprite.image = (Bitmap)generatedImage; Paks[curPak].sprites.Add(newSprite); ChangeSprite(Paks[curPak].sprites.Count - 1); RefreshSpriteTree(); UpdateControls(); } }