Improved tileData.json loading

This commit is contained in:
miku-666
2023-07-16 17:25:44 +02:00
parent eb54a4af86
commit 06e4ebfcfc
7 changed files with 3764 additions and 985 deletions

View File

@@ -23,8 +23,8 @@ namespace PckStudio.Forms.Additional_Popups.Animation
public ChangeTile()
{
InitializeComponent();
treeViewBlocks.ImageList = AnimationResources.BlockList;
treeViewItems.ImageList = AnimationResources.ItemList;
treeViewBlocks.ImageList = AnimationResources.BlockImageList;
treeViewItems.ImageList = AnimationResources.ItemImageList;
InitializeTreeviews();
}
@@ -42,7 +42,7 @@ namespace PckStudio.Forms.Additional_Popups.Animation
if (e.Node.Tag is string tileData)
{
selectedTile = tileData;
Console.WriteLine(selectedTile);
Debug.WriteLine(selectedTile);
category = e.Node.TreeView == treeViewItems
? Editor.Animation.AnimationCategory.Items
: Editor.Animation.AnimationCategory.Blocks;
@@ -51,27 +51,31 @@ namespace PckStudio.Forms.Additional_Popups.Animation
private void GetTileDataToView(string key, TreeNodeCollection collection, Action<TreeNode> additinalAction)
{
List<AnimationResources.TileInfo> textureInfos = key switch
{
"blocks" => AnimationResources.BlockTileInfos,
"items" => AnimationResources.ItemTileInfos,
_ => throw new InvalidOperationException(key)
};
Stopwatch stopwatch = Stopwatch.StartNew();
try
{
if (AnimationResources.JsonTileData[key] is not null)
if (textureInfos is not null)
{
foreach ( (int i, JToken content) in AnimationResources.JsonTileData[key].Children().enumerate())
{
foreach (JProperty prop in ((JObject)content).Properties())
{
if (!string.IsNullOrEmpty((string)prop.Value))
{
TreeNode tileNode = new TreeNode((string)prop.Value)
{
Tag = prop.Name,
ImageIndex = i,
SelectedImageIndex = i,
};
collection.Add(tileNode);
additinalAction(tileNode);
}
}
}
foreach ((int i, var content) in textureInfos.enumerate())
{
if (!string.IsNullOrEmpty(content.DisplayName))
{
TreeNode tileNode = new TreeNode(content.DisplayName)
{
Tag = content.DisplayName,
ImageIndex = i,
SelectedImageIndex = i,
};
collection.Add(tileNode);
additinalAction(tileNode);
}
}
}
}
catch (Newtonsoft.Json.JsonException j_ex)
@@ -79,6 +83,8 @@ namespace PckStudio.Forms.Additional_Popups.Animation
MessageBox.Show(j_ex.Message, "Error");
return;
}
stopwatch.Stop();
Debug.WriteLine($"{nameof(GetTileDataToView)} took {stopwatch.ElapsedMilliseconds}ms", category: nameof(ChangeTile));
}
void filter_TextChanged(object sender, EventArgs e)

View File

@@ -43,6 +43,18 @@ namespace PckStudio.Forms.Editor
public AnimationCategory Category { get; set; }
public string CategoryString => GetCategoryName(Category);
public static string GetCategoryName(AnimationCategory category)
{
return category switch
{
AnimationCategory.Items => "items",
AnimationCategory.Blocks => "blocks",
_ => throw new ArgumentOutOfRangeException(category.ToString())
};
}
private readonly List<Image> textures;
private readonly List<Frame> frames = new List<Frame>();

View File

@@ -15,6 +15,7 @@ using PckStudio.Forms.Additional_Popups.Animation;
using PckStudio.Forms.Utilities;
using PckStudio.Extensions;
using PckStudio.Properties;
using System.Diagnostics;
namespace PckStudio.Forms.Editor
{
@@ -96,8 +97,8 @@ namespace PckStudio.Forms.Editor
{
if (animationPictureBox.IsPlaying)
{
AnimationStartStopBtn.Text = "Play Animation";
animationPictureBox.Stop();
AnimationStartStopBtn.Text = "Play Animation";
return;
}
if (currentAnimation.FrameCount > 1)
@@ -135,7 +136,7 @@ namespace PckStudio.Forms.Editor
texture.Save(stream, ImageFormat.Png);
animationFile.SetData(stream.ToArray());
}
animationFile.Filename = $"res/textures/{AnimationResources.GetAnimationSection(currentAnimation.Category)}/{TileName}.png";
animationFile.Filename = $"res/textures/{currentAnimation.CategoryString}/{TileName}.png";
DialogResult = DialogResult.OK;
return;
}
@@ -270,8 +271,13 @@ namespace PckStudio.Forms.Editor
// Reworked import tool with new Animation classes by Miku
private void importJavaAnimationToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult query = MessageBox.Show("This feature will replace the existing animation data. It might fail if the selected animation script is invalid. Are you sure that you want to continue?", "Warning", MessageBoxButtons.YesNo);
if (query == DialogResult.No) return;
if (MessageBox.Show(
"This feature will replace the existing animation data. " +
"It might fail if the selected animation script is invalid. " +
"Are you sure that you want to continue?",
"Warning",
MessageBoxButtons.YesNo) == DialogResult.No)
return;
OpenFileDialog fileDialog = new OpenFileDialog
{
@@ -282,7 +288,7 @@ namespace PckStudio.Forms.Editor
Filter = "Animation Scripts (*.mcmeta)|*.png.mcmeta"
};
if (fileDialog.ShowDialog(this) != DialogResult.OK) return;
Console.WriteLine("Selected Animation Script: " + fileDialog.FileName);
Debug.WriteLine("Selected Animation Script: " + fileDialog.FileName);
string textureFile = fileDialog.FileName.Substring(0, fileDialog.FileName.Length - ".mcmeta".Length);
if (!File.Exists(textureFile))
@@ -313,13 +319,13 @@ namespace PckStudio.Forms.Editor
if (frame["index"] is JToken frame_index && frame_index.Type == JTokenType.Integer &&
frame["time"] is JToken frame_time && frame_time.Type == JTokenType.Integer)
{
Console.WriteLine("{0}*{1}", (int)frame["index"], (int)frame["time"]);
Debug.WriteLine("{0}*{1}", (int)frame["index"], (int)frame["time"]);
new_animation.AddFrame((int)frame["index"], (int)frame["time"]);
}
}
else if (frame.Type == JTokenType.Integer)
{
Console.WriteLine("{0}*{1}", (int)frame, frameTime);
Debug.WriteLine("{0}*{1}", (int)frame, frameTime);
new_animation.AddFrame((int)frame, frameTime);
}
}
@@ -348,7 +354,7 @@ namespace PckStudio.Forms.Editor
using (ChangeTile diag = new ChangeTile())
if (diag.ShowDialog(this) == DialogResult.OK)
{
Console.WriteLine(diag.SelectedTile);
Debug.WriteLine(diag.SelectedTile);
currentAnimation.Category = diag.Category;
TileName = diag.SelectedTile;
@@ -363,23 +369,26 @@ namespace PckStudio.Forms.Editor
private void SetTileLabel()
{
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;
return;
}
var textureInfos = currentAnimation.Category switch
{
Animation.AnimationCategory.Blocks => AnimationResources.BlockTileInfos,
Animation.AnimationCategory.Items => AnimationResources.ItemTileInfos,
_ => throw new ArgumentOutOfRangeException(currentAnimation.Category.ToString())
};
if (textureInfos.FirstOrDefault(p => p.InternalName == TileName) is AnimationResources.TileInfo textureInfo)
{
tileLabel.Text = textureInfo.DisplayName;
return;
}
switch(MessageBox.Show(this,
switch (MessageBox.Show(this,
$"{TileName} is not a valid tile for animation, and will not play in game. Would you like to choose a new tile?",
"Not a valid tile",
MessageBoxButtons.YesNo))
{
case DialogResult.Yes:
changeTileToolStripMenuItem_Click(null, null);
changeTileToolStripMenuItem_Click(null, EventArgs.Empty);
break;
default:
DialogResult = DialogResult.Abort;
@@ -394,7 +403,7 @@ namespace PckStudio.Forms.Editor
fileDialog.Filter = "Animation Scripts (*.mcmeta)|*.png.mcmeta";
if (fileDialog.ShowDialog(this) == DialogResult.OK)
{
JObject mcmeta = AnimationResources.ConvertAnimationToJson(currentAnimation, InterpolationCheckbox.Checked);
JObject mcmeta = AnimationResources.ConvertAnimationToJson(currentAnimation);
string jsondata = JsonConvert.SerializeObject(mcmeta, Formatting.Indented);
string filename = fileDialog.FileName;
File.WriteAllText(filename, jsondata);

View File

@@ -8,64 +8,80 @@ using Newtonsoft.Json.Linq;
using PckStudio.Properties;
using PckStudio.Extensions;
using PckStudio.Forms.Editor;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace PckStudio.Forms.Utilities
{
public static class AnimationResources
{
private const string __blocks = "blocks";
private const string __items = "items";
internal static string GetAnimationSection(Animation.AnimationCategory category)
internal class TileJson
{
return category switch
{
Animation.AnimationCategory.Items => __items,
Animation.AnimationCategory.Blocks => __blocks,
_ => throw new ArgumentOutOfRangeException(category.ToString())
};
[JsonProperty("blocks")]
public List<TileInfo> Blocks { get; set; }
[JsonProperty("items")]
public List<TileInfo> Items { get; set; }
}
private static JObject _jsonData = JObject.Parse(Resources.tileData);
public static JObject JsonTileData => _jsonData ??= JObject.Parse(Resources.tileData);
private static TileJson _jsonData;
internal static TileJson JsonTileData => _jsonData ??= JsonConvert.DeserializeObject<TileJson>(Resources.tileData);
public class TileInfo
{
[JsonProperty("displayName")]
public string DisplayName { get; set; }
[JsonProperty("internalName")]
public string InternalName { get; set; }
public TileInfo(string displayName, string internalName)
{
DisplayName = displayName;
InternalName = internalName;
}
}
public static List<TileInfo> ItemTileInfos => JsonTileData.Items;
public static List<TileInfo> BlockTileInfos => JsonTileData.Blocks;
private static Image[] _itemImages;
public static Image[] ItemImages => _itemImages ??= Resources.items_sheet.CreateImageList(16).ToArray();
private static Image[] _blockImages;
public static Image[] BlockImages => _blockImages ??= Resources.terrain_sheet.CreateImageList(16).ToArray();
private static ImageList _itemList;
public static ImageList ItemList
private static ImageList _itemImageList;
public static ImageList ItemImageList
{
get
{
if (_itemList is null)
if (_itemImageList is null)
{
_itemList = new ImageList();
_itemList.ColorDepth = ColorDepth.Depth32Bit;
_itemList.Images.AddRange(ItemImages);
_itemImageList = new ImageList();
_itemImageList.ColorDepth = ColorDepth.Depth32Bit;
_itemImageList.Images.AddRange(ItemImages);
}
return _itemList;
return _itemImageList;
}
}
private static ImageList _blockList;
public static ImageList BlockList
private static ImageList _blockImageList;
public static ImageList BlockImageList
{
get
{
if (_blockList is null)
if (_blockImageList is null)
{
_blockList = new ImageList();
_blockList.ColorDepth = ColorDepth.Depth32Bit;
_blockList.Images.AddRange(BlockImages);
_blockImageList = new ImageList();
_blockImageList.ColorDepth = ColorDepth.Depth32Bit;
_blockImageList.Images.AddRange(BlockImages);
}
return _blockList;
return _blockImageList;
}
}
internal static JObject ConvertAnimationToJson(Animation animation, bool interpolation)
internal static JObject ConvertAnimationToJson(Animation animation)
{
JObject janimation = new JObject();
JObject mcmeta = new JObject();
@@ -79,7 +95,7 @@ namespace PckStudio.Forms.Utilities
jframe["time"] = frame.Ticks;
jframes.Add(jframe);
};
janimation["interpolation"] = interpolation;
janimation["interpolation"] = animation.Interpolate;
janimation["frames"] = jframes;
return mcmeta;
}

View File

@@ -29,8 +29,8 @@ namespace PckStudio
_entityImages ??= Resources.entities_sheet.CreateImageList(32).ToArray();
DataCacher ??= new FileCacher(Program.AppDataCache);
_ = AnimationResources.JsonTileData;
_ = AnimationResources.ItemList;
_ = AnimationResources.BlockList;
_ = AnimationResources.ItemImageList;
_ = AnimationResources.BlockImageList;
SettingsManager.Initialize();
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
}

View File

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

File diff suppressed because it is too large Load Diff