Updated AnimatedSprite to support offsets and subsprites simultaneously

This commit is contained in:
quinnvoker
2018-12-09 15:52:41 -08:00
parent 0a02cde928
commit 06486e72fc
3 changed files with 88 additions and 47 deletions
+20 -7
View File
@@ -80,8 +80,9 @@
this.animationPanel = new System.Windows.Forms.Panel(); this.animationPanel = new System.Windows.Forms.Panel();
this.animationNameLabel = new System.Windows.Forms.Label(); this.animationNameLabel = new System.Windows.Forms.Label();
this.animationLabel = 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.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.spritePreview)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.frameTrackBar)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.frameTrackBar)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.delayInputBox)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.delayInputBox)).BeginInit();
@@ -216,7 +217,7 @@
// moveFrameUpButton // 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.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.Name = "moveFrameUpButton";
this.moveFrameUpButton.Size = new System.Drawing.Size(27, 23); this.moveFrameUpButton.Size = new System.Drawing.Size(27, 23);
this.moveFrameUpButton.TabIndex = 13; this.moveFrameUpButton.TabIndex = 13;
@@ -226,7 +227,7 @@
// //
// moveFrameDownButton // 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.Name = "moveFrameDownButton";
this.moveFrameDownButton.Size = new System.Drawing.Size(27, 23); this.moveFrameDownButton.Size = new System.Drawing.Size(27, 23);
this.moveFrameDownButton.TabIndex = 14; this.moveFrameDownButton.TabIndex = 14;
@@ -404,6 +405,7 @@
this.frameEditorPanel.Controls.Add(this.moveFrameDownButton); this.frameEditorPanel.Controls.Add(this.moveFrameDownButton);
this.frameEditorPanel.Controls.Add(this.delayInputBox); this.frameEditorPanel.Controls.Add(this.delayInputBox);
this.frameEditorPanel.Controls.Add(this.moveFrameUpButton); this.frameEditorPanel.Controls.Add(this.moveFrameUpButton);
this.frameEditorPanel.Controls.Add(this.addEmptyFrameButton);
this.frameEditorPanel.Controls.Add(this.removeFrameButton); this.frameEditorPanel.Controls.Add(this.removeFrameButton);
this.frameEditorPanel.Enabled = false; this.frameEditorPanel.Enabled = false;
this.frameEditorPanel.Location = new System.Drawing.Point(0, 27); this.frameEditorPanel.Location = new System.Drawing.Point(0, 27);
@@ -638,6 +640,12 @@
this.animationLabel.TabIndex = 22; this.animationLabel.TabIndex = 22;
this.animationLabel.Text = "Animation"; this.animationLabel.Text = "Animation";
// //
// loadAnimationSetDialog
//
this.loadAnimationSetDialog.DefaultExt = "animset";
this.loadAnimationSetDialog.FileName = "openFileDialog1";
this.loadAnimationSetDialog.Filter = "QUROGames AnimationSet|*.animSet";
//
// animationPreview // animationPreview
// //
this.animationPreview.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 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.ZoomLevel = 0F;
this.animationPreview.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.animationPreview_MouseWheel); this.animationPreview.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.animationPreview_MouseWheel);
// //
// loadAnimationSetDialog // addEmptyFrameButton
// //
this.loadAnimationSetDialog.DefaultExt = "animset"; this.addEmptyFrameButton.Location = new System.Drawing.Point(163, 19);
this.loadAnimationSetDialog.FileName = "openFileDialog1"; this.addEmptyFrameButton.Name = "addEmptyFrameButton";
this.loadAnimationSetDialog.Filter = "QUROGames AnimationSet|*.animSet"; 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 // Form1
// //
@@ -747,6 +759,7 @@
private System.Windows.Forms.NumericUpDown subSpriteXPosBox; private System.Windows.Forms.NumericUpDown subSpriteXPosBox;
private System.Windows.Forms.Panel subSpritePanel; private System.Windows.Forms.Panel subSpritePanel;
private System.Windows.Forms.Label subSpritePositionLabel; private System.Windows.Forms.Label subSpritePositionLabel;
private System.Windows.Forms.Button addEmptyFrameButton;
} }
} }
+43 -31
View File
@@ -31,17 +31,7 @@ namespace AnimationEditor
private Animation currentAnimation; private Animation currentAnimation;
private Frame currentFrame; private Frame currentFrame;
private bool r; private bool reading;
private bool reading
{
get { return r; }
set
{
r = value;
Console.WriteLine("Reading: " + r);
}
}
private float importDelay = 0; private float importDelay = 0;
public Form1() public Form1()
@@ -92,29 +82,34 @@ namespace AnimationEditor
{ {
if (spriteListBox.SelectedItem != null) if (spriteListBox.SelectedItem != null)
{ {
int newSelectedIndex; AddFrame(new Frame((SpriteMapRegion)spriteListBox.SelectedItem, importDelay));
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);
} }
} }
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() private void UpdateAnimation()
{ {
if(frames?.Count > 0) if(frames?.Count > 0)
@@ -510,6 +505,18 @@ namespace AnimationEditor
frame.Sprite = loadedSpriteMap[frame.Name]; frame.Sprite = loadedSpriteMap[frame.Name];
updateCounter++; 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); currentFrame.SubSprites.Add(spriteToAdd);
subSprites.Add(spriteToAdd); subSprites.Add(spriteToAdd);
} }
private void addEmptyFrameButton_Click(object sender, EventArgs e)
{
AddFrame(new Frame());
}
} }
} }
+25 -9
View File
@@ -76,21 +76,37 @@ namespace QURO.AnimationLibrary
{ {
spriteBatch.Draw(SpriteSheet, position - CurrentFrame.Origin, CurrentFrame.Bounds, tint); spriteBatch.Draw(SpriteSheet, position - CurrentFrame.Origin, CurrentFrame.Bounds, tint);
if (CurrentFrame.SubSprites != null) if (CurrentFrame.SubSprites != null)
{ DrawSubSprites(spriteBatch, position, tint);
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);
}
}
} }
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; Rectangle sourceRect = CurrentFrame.Bounds;
sourceRect.Offset(spriteOffset); sourceRect.Offset(spriteMapOffset);
spriteBatch.Draw(SpriteSheet, position, sourceRect, tint); 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);
}
} }
} }
} }