Updated Animation Editor to use AnimationCategory

This commit is contained in:
miku-666
2023-05-25 15:58:27 +02:00
parent 871a6a46d0
commit c59317699b
5 changed files with 116 additions and 65 deletions

View File

@@ -9,13 +9,13 @@ using PckStudio.Forms.Utilities;
namespace PckStudio.Forms.Additional_Popups.Animation
{
public partial class ChangeTile : MetroForm
internal partial class ChangeTile : MetroForm
{
string selectedTile = "";
Editor.Animation.AnimationCategory category = Editor.Animation.AnimationCategory.Blocks;
bool isItem = false;
public string SelectedTile => selectedTile;
public bool IsItem => isItem;
public Editor.Animation.AnimationCategory Category => category;
List<TreeNode> treeViewBlockCache = new List<TreeNode>();
List<TreeNode> treeViewItemCache = new List<TreeNode>();
@@ -43,8 +43,10 @@ namespace PckStudio.Forms.Additional_Popups.Animation
{
selectedTile = tileData;
Console.WriteLine(selectedTile);
isItem = e.Node.TreeView == treeViewItems;
}
category = e.Node.TreeView == treeViewItems
? Editor.Animation.AnimationCategory.Items
: Editor.Animation.AnimationCategory.Blocks;
}
}
private void GetTileDataToView(string key, TreeNodeCollection collection, Action<TreeNode> additinalAction)
@@ -127,14 +129,16 @@ namespace PckStudio.Forms.Additional_Popups.Animation
private void CancelBtn_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void AcceptBtn_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(selectedTile)) CancelBtn_Click(sender, e);
DialogResult = DialogResult.OK;
Close();
if (string.IsNullOrEmpty(selectedTile))
{
DialogResult = DialogResult.Cancel;
return;
}
DialogResult = DialogResult.OK;
}
}
}

View File

@@ -40,11 +40,12 @@
this.saveToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.importToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.animationTextureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.importAnimationTextureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.importJavaAnimationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.importGifToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.exportAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.exportJavaAnimationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.gifToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.editToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.bulkAnimationSpeedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.changeTileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@@ -148,61 +149,69 @@
//
this.saveToolStripMenuItem1.Image = ((System.Drawing.Image)(resources.GetObject("saveToolStripMenuItem1.Image")));
this.saveToolStripMenuItem1.Name = "saveToolStripMenuItem1";
this.saveToolStripMenuItem1.Size = new System.Drawing.Size(122, 22);
this.saveToolStripMenuItem1.Size = new System.Drawing.Size(180, 22);
this.saveToolStripMenuItem1.Text = "Save";
this.saveToolStripMenuItem1.Click += new System.EventHandler(this.saveToolStripMenuItem1_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(119, 6);
this.toolStripSeparator1.Size = new System.Drawing.Size(177, 6);
//
// importToolStripMenuItem
//
this.importToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.animationTextureToolStripMenuItem,
this.importAnimationTextureToolStripMenuItem,
this.importJavaAnimationToolStripMenuItem,
this.importGifToolStripMenuItem});
this.importToolStripMenuItem.Name = "importToolStripMenuItem";
this.importToolStripMenuItem.Size = new System.Drawing.Size(122, 22);
this.importToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.importToolStripMenuItem.Text = "Import";
//
// animationTextureToolStripMenuItem
// importAnimationTextureToolStripMenuItem
//
this.animationTextureToolStripMenuItem.Name = "animationTextureToolStripMenuItem";
this.animationTextureToolStripMenuItem.Size = new System.Drawing.Size(171, 22);
this.animationTextureToolStripMenuItem.Text = "Animation Texture";
this.animationTextureToolStripMenuItem.Click += new System.EventHandler(this.animationTextureToolStripMenuItem_Click);
this.importAnimationTextureToolStripMenuItem.Name = "importAnimationTextureToolStripMenuItem";
this.importAnimationTextureToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.importAnimationTextureToolStripMenuItem.Text = "Animation Texture";
this.importAnimationTextureToolStripMenuItem.Click += new System.EventHandler(this.importAnimationTextureToolStripMenuItem_Click);
//
// importJavaAnimationToolStripMenuItem
//
this.importJavaAnimationToolStripMenuItem.Name = "importJavaAnimationToolStripMenuItem";
this.importJavaAnimationToolStripMenuItem.Size = new System.Drawing.Size(171, 22);
this.importJavaAnimationToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.importJavaAnimationToolStripMenuItem.Text = "Java Animation";
this.importJavaAnimationToolStripMenuItem.Click += new System.EventHandler(this.importJavaAnimationToolStripMenuItem_Click);
//
// importGifToolStripMenuItem
//
this.importGifToolStripMenuItem.Name = "importGifToolStripMenuItem";
this.importGifToolStripMenuItem.Size = new System.Drawing.Size(171, 22);
this.importGifToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.importGifToolStripMenuItem.Text = "Gif";
this.importGifToolStripMenuItem.Click += new System.EventHandler(this.importGifToolStripMenuItem_Click);
//
// exportAsToolStripMenuItem
//
this.exportAsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.exportJavaAnimationToolStripMenuItem});
this.exportJavaAnimationToolStripMenuItem,
this.gifToolStripMenuItem});
this.exportAsToolStripMenuItem.Name = "exportAsToolStripMenuItem";
this.exportAsToolStripMenuItem.Size = new System.Drawing.Size(122, 22);
this.exportAsToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.exportAsToolStripMenuItem.Text = "Export as";
//
// exportJavaAnimationToolStripMenuItem
//
this.exportJavaAnimationToolStripMenuItem.Name = "exportJavaAnimationToolStripMenuItem";
this.exportJavaAnimationToolStripMenuItem.Size = new System.Drawing.Size(155, 22);
this.exportJavaAnimationToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.exportJavaAnimationToolStripMenuItem.Text = "Java Animation";
this.exportJavaAnimationToolStripMenuItem.Click += new System.EventHandler(this.exportJavaAnimationToolStripMenuItem_Click);
//
// gifToolStripMenuItem
//
this.gifToolStripMenuItem.Name = "gifToolStripMenuItem";
this.gifToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.gifToolStripMenuItem.Text = "Gif";
this.gifToolStripMenuItem.Click += new System.EventHandler(this.gifToolStripMenuItem_Click);
//
// editToolStripMenuItem
//
this.editToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
@@ -385,6 +394,7 @@
private System.Windows.Forms.ToolStripMenuItem exportAsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem exportJavaAnimationToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripMenuItem animationTextureToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem importAnimationTextureToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem gifToolStripMenuItem;
}
}

View File

@@ -1,19 +1,19 @@
using MetroFramework.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
using MetroFramework.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OMI.Formats.Pck;
using PckStudio.Forms.Additional_Popups.Animation;
using PckStudio.Forms.Utilities;
using PckStudio.Extensions;
using OMI.Formats.Pck;
using System.Collections.Generic;
using PckStudio.Models;
using System.Diagnostics;
using PckStudio.Properties;
namespace PckStudio.Forms.Editor
@@ -24,34 +24,29 @@ namespace PckStudio.Forms.Editor
private Animation currentAnimation;
private bool isItem = false;
private string animationSection => AnimationResources.GetAnimationSection(isItem);
private string TileName = string.Empty;
private bool IsEditingSpecial => IsSpecialTile(TileName);
private bool IsSpecialTile(string tileName)
{
return tileName == "clock" || tileName == "compass";
}
public AnimationEditor(PckFile.FileData file)
public AnimationEditor(PckFile.FileData file)
{
InitializeComponent();
animationFile = file;
isItem = file.Filename.Split('/').Contains("items");
TileName = Path.GetFileNameWithoutExtension(file.Filename);
TileName = Path.GetFileNameWithoutExtension(animationFile.Filename);
bulkAnimationSpeedToolStripMenuItem.Enabled =
importToolStripMenuItem.Enabled =
exportAsToolStripMenuItem.Enabled =
InterpolationCheckbox.Visible = !IsEditingSpecial;
InterpolationCheckbox.Visible = !IsSpecialTile(TileName);
saveToolStripMenuItem1.Visible = !Settings.Default.AutoSaveChanges;
toolStripSeparator1.Visible = saveToolStripMenuItem1.Visible = !Settings.Default.AutoSaveChanges;
animationFile = file;
if (file.Size > 0)
if (animationFile.Size > 0)
{
using MemoryStream textureMem = new MemoryStream(animationFile.Data);
var texture = new Bitmap(textureMem);
@@ -61,16 +56,10 @@ namespace PckStudio.Forms.Editor
? new Animation(frameTextures, animationFile.Properties.GetPropertyValue("ANIM"))
: new Animation(frameTextures);
}
foreach (JObject content in AnimationResources.JsonTileData[animationSection].Children())
{
var prop = content.Properties().FirstOrDefault(prop => prop.Name == TileName);
if (prop is JProperty)
{
tileLabel.Text = (string)prop.Value;
break;
}
}
currentAnimation.Category = animationFile.Filename.Split('/').Contains("items")
? Animation.AnimationCategory.Items
: Animation.AnimationCategory.Blocks;
SetTileLabel();
LoadAnimationTreeView();
}
@@ -138,7 +127,7 @@ namespace PckStudio.Forms.Editor
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
if (!IsEditingSpecial && currentAnimation is not null)
if (!IsSpecialTile(TileName) && currentAnimation is not null)
{
string anim = currentAnimation.BuildAnim();
animationFile.Properties.SetProperty("ANIM", anim);
@@ -148,7 +137,7 @@ namespace PckStudio.Forms.Editor
texture.Save(stream, ImageFormat.Png);
animationFile.SetData(stream.ToArray());
}
animationFile.Filename = "res/textures/" + (isItem ? "items/" : "blocks/") + TileName + ".png" ;
animationFile.Filename = $"res/textures/{AnimationResources.GetAnimationSection(currentAnimation.Category)}/{TileName}.png";
DialogResult = DialogResult.OK;
return;
}
@@ -362,13 +351,13 @@ namespace PckStudio.Forms.Editor
if (diag.ShowDialog(this) == DialogResult.OK)
{
Console.WriteLine(diag.SelectedTile);
if (TileName != diag.SelectedTile) isItem = diag.IsItem;
currentAnimation.Category = diag.Category;
TileName = diag.SelectedTile;
bulkAnimationSpeedToolStripMenuItem.Enabled =
importToolStripMenuItem.Enabled =
exportAsToolStripMenuItem.Enabled =
InterpolationCheckbox.Visible = !IsEditingSpecial;
InterpolationCheckbox.Visible = !IsSpecialTile(TileName);
SetTileLabel();
}
@@ -376,10 +365,14 @@ namespace PckStudio.Forms.Editor
private void SetTileLabel()
{
foreach (JObject content in AnimationResources.JsonTileData[animationSection].Children())
foreach (JObject content in AnimationResources.JsonTileData[AnimationResources.GetAnimationSection(currentAnimation.Category)].Children())
{
var first = content.Properties().FirstOrDefault(p => p.Name == TileName);
if (first is JProperty p) tileLabel.Text = (string)p.Value;
if (first is JProperty p)
{
tileLabel.Text = (string)p.Value;
break;
}
}
}
@@ -424,7 +417,6 @@ namespace PckStudio.Forms.Editor
private void InterpolationCheckbox_CheckedChanged(object sender, EventArgs e)
{
// Interpolation flag wasn't being updated when the check box changed, this fixes the issue
if (currentAnimation is not null)
currentAnimation.Interpolate = InterpolationCheckbox.Checked;
}
@@ -471,7 +463,7 @@ namespace PckStudio.Forms.Editor
LoadAnimationTreeView();
}
private void animationTextureToolStripMenuItem_Click(object sender, EventArgs e)
private void importAnimationTextureToolStripMenuItem_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog()
{
@@ -485,5 +477,38 @@ namespace PckStudio.Forms.Editor
currentAnimation = new Animation(textures);
LoadAnimationTreeView();
}
//[System.Runtime.InteropServices.DllImport("gdi32.dll")]
//public static extern bool DeleteObject(IntPtr hObject);
private void gifToolStripMenuItem_Click(object sender, EventArgs e)
{
var fileDialog = new SaveFileDialog()
{
Filter = "GIF file|*.gif"
};
if (fileDialog.ShowDialog(this) != DialogResult.OK)
return;
// TODO
//GifBitmapEncoder gifBitmapEncoder = new GifBitmapEncoder();
//foreach (Bitmap texture in currentAnimation.GetTextures())
//{
// var bmp = texture.GetHbitmap();
// var src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
// bmp,
// IntPtr.Zero,
// System.Windows.Int32Rect.Empty,
// BitmapSizeOptions.FromWidthAndHeight(texture.Width, texture.Height));
// gifBitmapEncoder.Frames.Add(BitmapFrame.Create(src));
// DeleteObject(bmp); // recommended, handle memory leak
//}
//using (var fs = fileDialog.OpenFile())
//{
// gifBitmapEncoder.Save(fs);
//}
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Drawing;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
@@ -12,7 +13,18 @@ namespace PckStudio.Forms.Utilities
{
public static class AnimationResources
{
public static string GetAnimationSection(bool isItem) => isItem ? "items" : "blocks";
private const string __blocks = "blocks";
private const string __items = "items";
internal static string GetAnimationSection(Animation.AnimationCategory category)
{
return category switch
{
Animation.AnimationCategory.Items => __items,
Animation.AnimationCategory.Blocks => __blocks,
_ => throw new ArgumentOutOfRangeException(category.ToString())
};
}
private static JObject _jsonData = JObject.Parse(Resources.tileData);
public static JObject JsonTileData => _jsonData ??= JObject.Parse(Resources.tileData);

View File

@@ -872,7 +872,7 @@ namespace PckStudio
return;
var file = new PckFile.FileData(
$"res/textures/{AnimationResources.GetAnimationSection(diag.IsItem)}/{diag.SelectedTile}.png",
$"res/textures/{AnimationResources.GetAnimationSection(diag.Category)}/{diag.SelectedTile}.png",
PckFile.FileData.FileType.TextureFile);
using AnimationEditor animationEditor = new AnimationEditor(file);