From 06486e72fcddde9dc4ca49401079391396fdea78 Mon Sep 17 00:00:00 2001 From: quinnvoker Date: Sun, 9 Dec 2018 15:52:41 -0800 Subject: [PATCH] Updated AnimatedSprite to support offsets and subsprites simultaneously --- AnimationEditor/Form1.Designer.cs | 27 ++++++--- AnimationEditor/Form1.cs | 74 ++++++++++++++----------- QURO.AnimationLibrary/AnimatedSprite.cs | 34 +++++++++--- 3 files changed, 88 insertions(+), 47 deletions(-) diff --git a/AnimationEditor/Form1.Designer.cs b/AnimationEditor/Form1.Designer.cs index 2388916..9456db5 100644 --- a/AnimationEditor/Form1.Designer.cs +++ b/AnimationEditor/Form1.Designer.cs @@ -80,8 +80,9 @@ this.animationPanel = new System.Windows.Forms.Panel(); this.animationNameLabel = new System.Windows.Forms.Label(); this.animationLabel = new System.Windows.Forms.Label(); - this.animationPreview = new AnimationEditor.AnimationPreview(); this.loadAnimationSetDialog = new System.Windows.Forms.OpenFileDialog(); + this.animationPreview = new AnimationEditor.AnimationPreview(); + this.addEmptyFrameButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.spritePreview)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.frameTrackBar)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.delayInputBox)).BeginInit(); @@ -216,7 +217,7 @@ // moveFrameUpButton // this.moveFrameUpButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.moveFrameUpButton.Location = new System.Drawing.Point(223, 19); + this.moveFrameUpButton.Location = new System.Drawing.Point(225, 19); this.moveFrameUpButton.Name = "moveFrameUpButton"; this.moveFrameUpButton.Size = new System.Drawing.Size(27, 23); this.moveFrameUpButton.TabIndex = 13; @@ -226,7 +227,7 @@ // // moveFrameDownButton // - this.moveFrameDownButton.Location = new System.Drawing.Point(190, 19); + this.moveFrameDownButton.Location = new System.Drawing.Point(194, 19); this.moveFrameDownButton.Name = "moveFrameDownButton"; this.moveFrameDownButton.Size = new System.Drawing.Size(27, 23); this.moveFrameDownButton.TabIndex = 14; @@ -404,6 +405,7 @@ this.frameEditorPanel.Controls.Add(this.moveFrameDownButton); this.frameEditorPanel.Controls.Add(this.delayInputBox); this.frameEditorPanel.Controls.Add(this.moveFrameUpButton); + this.frameEditorPanel.Controls.Add(this.addEmptyFrameButton); this.frameEditorPanel.Controls.Add(this.removeFrameButton); this.frameEditorPanel.Enabled = false; this.frameEditorPanel.Location = new System.Drawing.Point(0, 27); @@ -638,6 +640,12 @@ this.animationLabel.TabIndex = 22; this.animationLabel.Text = "Animation"; // + // loadAnimationSetDialog + // + this.loadAnimationSetDialog.DefaultExt = "animset"; + this.loadAnimationSetDialog.FileName = "openFileDialog1"; + this.loadAnimationSetDialog.Filter = "QUROGames AnimationSet|*.animSet"; + // // animationPreview // this.animationPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -654,11 +662,15 @@ this.animationPreview.ZoomLevel = 0F; this.animationPreview.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.animationPreview_MouseWheel); // - // loadAnimationSetDialog + // addEmptyFrameButton // - this.loadAnimationSetDialog.DefaultExt = "animset"; - this.loadAnimationSetDialog.FileName = "openFileDialog1"; - this.loadAnimationSetDialog.Filter = "QUROGames AnimationSet|*.animSet"; + this.addEmptyFrameButton.Location = new System.Drawing.Point(163, 19); + this.addEmptyFrameButton.Name = "addEmptyFrameButton"; + this.addEmptyFrameButton.Size = new System.Drawing.Size(27, 23); + this.addEmptyFrameButton.TabIndex = 12; + this.addEmptyFrameButton.Text = "+"; + this.addEmptyFrameButton.UseVisualStyleBackColor = true; + this.addEmptyFrameButton.Click += new System.EventHandler(this.addEmptyFrameButton_Click); // // Form1 // @@ -747,6 +759,7 @@ private System.Windows.Forms.NumericUpDown subSpriteXPosBox; private System.Windows.Forms.Panel subSpritePanel; private System.Windows.Forms.Label subSpritePositionLabel; + private System.Windows.Forms.Button addEmptyFrameButton; } } diff --git a/AnimationEditor/Form1.cs b/AnimationEditor/Form1.cs index ec8717c..f23ea06 100644 --- a/AnimationEditor/Form1.cs +++ b/AnimationEditor/Form1.cs @@ -31,17 +31,7 @@ namespace AnimationEditor private Animation currentAnimation; private Frame currentFrame; - private bool r; - - private bool reading - { - get { return r; } - set - { - r = value; - Console.WriteLine("Reading: " + r); - } - } + private bool reading; private float importDelay = 0; public Form1() @@ -92,29 +82,34 @@ namespace AnimationEditor { if (spriteListBox.SelectedItem != null) { - int newSelectedIndex; - - if (frames.Count == 1 && frames[0].Bounds == Microsoft.Xna.Framework.Rectangle.Empty) - { - frames.Clear(); - } - - if (frameListBox.SelectedItem == null || frameListBox.SelectedIndex == frames.Count - 1) - { - frames.Add(new Frame((SpriteMapRegion)spriteListBox.SelectedItem, importDelay)); - frameTrackBar.Maximum = frames.Count - 1; - newSelectedIndex = frames.Count - 1; - } - else - { - frames.Insert(frameListBox.SelectedIndex + 1, new Frame((SpriteMapRegion)spriteListBox.SelectedItem, 0.25f)); - newSelectedIndex = frameListBox.SelectedIndex + 1; - } - UpdateAnimation(); - frameListBox_SetSingleSelection(newSelectedIndex); + AddFrame(new Frame((SpriteMapRegion)spriteListBox.SelectedItem, importDelay)); } } + private void AddFrame(Frame frameToAdd) + { + int newSelectedIndex; + + if (frames.Count == 1 && frames[0].Bounds == Microsoft.Xna.Framework.Rectangle.Empty) + { + frames.Clear(); + } + + if (frameListBox.SelectedItem == null || frameListBox.SelectedIndex == frames.Count - 1) + { + frames.Add(frameToAdd); + frameTrackBar.Maximum = frames.Count - 1; + newSelectedIndex = frames.Count - 1; + } + else + { + frames.Insert(frameListBox.SelectedIndex + 1, frameToAdd); + newSelectedIndex = frameListBox.SelectedIndex + 1; + } + UpdateAnimation(); + frameListBox_SetSingleSelection(newSelectedIndex); + } + private void UpdateAnimation() { if(frames?.Count > 0) @@ -510,6 +505,18 @@ namespace AnimationEditor frame.Sprite = loadedSpriteMap[frame.Name]; updateCounter++; } + if (frame.SubSprites != null) + { + for (int index = 0; index < frame.SubSprites.Count; index++) + { + var subSprite = frame.SubSprites[index]; + if (loadedSpriteMap.ContainsKey(subSprite.Name)) + { + frame.SubSprites[index] = loadedSpriteMap[subSprite.Name]; + updateCounter++; + } + } + } } } @@ -560,5 +567,10 @@ namespace AnimationEditor currentFrame.SubSprites.Add(spriteToAdd); subSprites.Add(spriteToAdd); } + + private void addEmptyFrameButton_Click(object sender, EventArgs e) + { + AddFrame(new Frame()); + } } } diff --git a/QURO.AnimationLibrary/AnimatedSprite.cs b/QURO.AnimationLibrary/AnimatedSprite.cs index 6d01ac7..ecf142a 100644 --- a/QURO.AnimationLibrary/AnimatedSprite.cs +++ b/QURO.AnimationLibrary/AnimatedSprite.cs @@ -76,21 +76,37 @@ namespace QURO.AnimationLibrary { spriteBatch.Draw(SpriteSheet, position - CurrentFrame.Origin, CurrentFrame.Bounds, tint); if (CurrentFrame.SubSprites != null) - { - for (int index = 0; index < CurrentFrame.SubSprites.Count; index++) - { - var subSprite = CurrentFrame.SubSprites[index]; - spriteBatch.Draw(SpriteSheet, position - subSprite.Origin + CurrentFrame.SubSpritePositions[index], subSprite.Bounds, tint); - } - } + DrawSubSprites(spriteBatch, position, tint); } - public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 spriteOffset, Color tint) + public void Draw(SpriteBatch spriteBatch, Vector2 position, Vector2 spriteMapOffset, Color tint, bool offsetSubSprites = false) { Rectangle sourceRect = CurrentFrame.Bounds; - sourceRect.Offset(spriteOffset); + sourceRect.Offset(spriteMapOffset); spriteBatch.Draw(SpriteSheet, position, sourceRect, tint); + if (CurrentFrame.SubSprites != null) + DrawSubSprites(spriteBatch, position, tint, offsetSubSprites? (Vector2?)spriteMapOffset : null); + } + + public void DrawSubSprites(SpriteBatch spriteBatch, Vector2 position, Color tint, Vector2? spriteMapOffset = null) + { + if (CurrentFrame.SubSprites.Count != CurrentFrame.SubSpritePositions.Count) + { + System.Console.WriteLine("SubSpriteCount and SubSpritePositionCount are not equal! Skipping subsprites..."); + return; + } + for (int index = 0; index < CurrentFrame.SubSprites.Count; index++) + { + var subSprite = CurrentFrame.SubSprites[index]; + + Rectangle sourceRect = subSprite.Bounds; + + if (spriteMapOffset != null) + sourceRect.Offset((Vector2)spriteMapOffset); + + spriteBatch.Draw(SpriteSheet, position - subSprite.Origin + CurrentFrame.SubSpritePositions[index], sourceRect, tint); + } } } }