AnimationEditor - fixed menu bar item being disabled

This commit is contained in:
miku-666
2023-05-11 22:16:18 +02:00
parent 82d9ac511d
commit 3977fe4e6d
2 changed files with 48 additions and 35 deletions

View File

@@ -58,10 +58,13 @@ namespace PckStudio.Forms.Editor
string[] animData = anim.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
int lastFrameTime = MinimumFrameTime;
if (animData.Length <= 0)
{
for (int i = 0; i < TextureCount; i++)
{
AddFrame(i);
}
return;
}
foreach (string frameInfo in animData)
{
@@ -80,14 +83,19 @@ namespace PckStudio.Forms.Editor
}
}
public Frame AddFrame(int frameTextureIndex) => AddFrame(frameTextureIndex, MinimumFrameTime);
public Frame AddFrame(int frameTextureIndex, int frameTime)
private void CheckTextureIndex(int index)
{
if (frameTextureIndex < 0 || frameTextureIndex >= textures.Count)
throw new ArgumentOutOfRangeException(nameof(frameTextureIndex));
Frame f = new Frame(textures[frameTextureIndex], frameTime);
frames.Add(f);
return f;
if ((index < 0 || index >= textures.Count))
throw new ArgumentOutOfRangeException(nameof(index));
}
public Frame AddFrame(int textureIndex) => AddFrame(textureIndex, MinimumFrameTime);
public Frame AddFrame(int textureIndex, int frameTime)
{
CheckTextureIndex(textureIndex);
Frame frame = new Frame(textures[textureIndex], frameTime);
frames.Add(frame);
return frame;
}
public bool RemoveFrame(int frameIndex)
@@ -103,7 +111,7 @@ namespace PckStudio.Forms.Editor
return frames;
}
public List<Image> GetFrameTextures()
public List<Image> GetTextures()
{
return textures;
}
@@ -114,11 +122,15 @@ namespace PckStudio.Forms.Editor
return textures.IndexOf(frameTexture);
}
public void SetFrame(Frame frame, int frameTextureIndex, int frameTime = MinimumFrameTime)
=> SetFrame(frames.IndexOf(frame), frameTextureIndex, frameTime);
public void SetFrame(int frameIndex, int frameTextureIndex, int frameTime = MinimumFrameTime)
public void SetFrame(int frameIndex, Frame frame)
{
frames[frameIndex] = new Frame(textures[frameTextureIndex], frameTime);
frames[frameIndex] = frame;
}
public void SetFrame(int frameIndex, int textureIndex, int frameTime = MinimumFrameTime)
{
CheckTextureIndex(textureIndex);
SetFrame(frameIndex, new Frame(textures[textureIndex], frameTime));
}
public string BuildAnim()
@@ -129,10 +141,8 @@ namespace PckStudio.Forms.Editor
return stringBuilder.ToString(0, stringBuilder.Length - 1);
}
public Image BuildTexture(bool isClockOrCompass, List<Image> linearImages = default!)
{
var textures = isClockOrCompass ? linearImages : this.textures;
public Image BuildTexture()
{
if (textures[0].Width != textures[0].Height)
throw new Exception("Invalid size");

View File

@@ -39,10 +39,9 @@ namespace PckStudio.Forms.Editor
TileName = Path.GetFileNameWithoutExtension(file.Filename);
InterpolationCheckbox.Visible = !IsEditingSpecial;
InterpolationCheckbox.Checked = InterpolationCheckbox.Visible;
bulkAnimationSpeedToolStripMenuItem.Enabled = InterpolationCheckbox.Visible;
importJavaAnimationToolStripMenuItem.Enabled = InterpolationCheckbox.Visible;
exportJavaAnimationToolStripMenuItem.Enabled = InterpolationCheckbox.Visible;
bulkAnimationSpeedToolStripMenuItem.Enabled = !IsEditingSpecial;
importJavaAnimationToolStripMenuItem.Enabled = !IsEditingSpecial;
exportJavaAnimationToolStripMenuItem.Enabled = !IsEditingSpecial;
animationFile = file;
@@ -72,7 +71,7 @@ namespace PckStudio.Forms.Editor
frameTreeView.Nodes.Clear();
// $"Frame: {i}, Frame Time: {Animation.MinimumFrameTime}"
TextureIcons.Images.Clear();
TextureIcons.Images.AddRange(currentAnimation.GetFrameTextures().ToArray());
TextureIcons.Images.AddRange(currentAnimation.GetTextures().ToArray());
foreach (var frame in currentAnimation.GetFrames())
{
var imageIndex = currentAnimation.GetTextureIndex(frame.Texture);
@@ -125,17 +124,19 @@ namespace PckStudio.Forms.Editor
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
string anim = currentAnimation.BuildAnim();
animationFile.Properties.SetProperty("ANIM", IsEditingSpecial ? "" : anim);
using (var stream = new MemoryStream())
if (!IsEditingSpecial)
{
var texture = currentAnimation.BuildTexture(IsEditingSpecial);
texture.Save(stream, ImageFormat.Png);
animationFile.SetData(stream.ToArray());
string anim = currentAnimation.BuildAnim();
animationFile.Properties.SetProperty("ANIM", anim);
using (var stream = new MemoryStream())
{
var texture = currentAnimation.BuildTexture();
texture.Save(stream, ImageFormat.Png);
animationFile.SetData(stream.ToArray());
}
//Reusing this for the tile path
TileName = "res/textures/" + (isItem ? "items/" : "blocks/") + TileName + ".png" ;
}
//Reusing this for the tile path
TileName = "res/textures/" + (isItem ? "items/" : "blocks/") + TileName + ".png" ;
DialogResult = DialogResult.OK;
}
@@ -222,8 +223,11 @@ namespace PckStudio.Forms.Editor
using FrameEditor diag = new FrameEditor(frame.Ticks, currentAnimation.GetTextureIndex(frame.Texture), TextureIcons);
if (diag.ShowDialog(this) == DialogResult.OK)
{
/* Found a bug here. When passing the frame variable, it would replace the first instance of that frame and time
* rather than the actual frame that was clicked. I've just switched to passing the index to fix this for now. -Matt
/* Found a bug here. When passing the frame variable,
* it would replace the first instance of that frame and time
* rather than the actual frame that was clicked.
* I've just switched to passing the index to fix this for now.
* - Matt
*/
currentAnimation.SetFrame(frameTreeView.SelectedNode.Index, diag.FrameTextureIndex, diag.FrameTime);
@@ -283,8 +287,7 @@ namespace PckStudio.Forms.Editor
MessageBox.Show(textureFile + " was not found", "Texture not found");
return;
}
using MemoryStream textureMem = new MemoryStream(File.ReadAllBytes(textureFile));
var textures = Image.FromStream(textureMem).CreateImageList(ImageLayoutDirection.Horizontal);
var textures = Image.FromFile(textureFile).CreateImageList(ImageLayoutDirection.Horizontal);
var new_animation = new Animation(textures);
try
{
@@ -371,7 +374,7 @@ namespace PckStudio.Forms.Editor
string jsondata = JsonConvert.SerializeObject(mcmeta, Formatting.Indented);
string filename = fileDialog.FileName;
File.WriteAllText(filename, jsondata);
var finalTexture = currentAnimation.BuildTexture(isClockOrCompass: false);
var finalTexture = currentAnimation.BuildTexture();
finalTexture.Save(Path.GetFileNameWithoutExtension(filename)); // removes ".mcmeta" from filename!
MessageBox.Show("Animation was successfully exported to " + filename, "Export successful!");
}