Improved performance im ChangeTile form

This commit is contained in:
miku-666
2023-05-14 16:46:20 +02:00
parent 45ea0aec21
commit 38d14661ef
9 changed files with 142 additions and 103 deletions

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using MetroFramework.Forms;
using Newtonsoft.Json.Linq;
using PckStudio.Extensions;
using PckStudio.Forms.Utilities;
namespace PckStudio.Forms.Additional_Popups.Animation
@@ -15,71 +17,25 @@ namespace PckStudio.Forms.Additional_Popups.Animation
public string SelectedTile => selectedTile;
public bool IsItem => isItem;
List<TreeNode> treeViewBlockCache = new List<TreeNode>();
List<TreeNode> treeViewBlockCache = new List<TreeNode>();
List<TreeNode> treeViewItemCache = new List<TreeNode>();
public ChangeTile()
{
InitializeComponent();
ImageList tiles = new ImageList();
tiles.ColorDepth = ColorDepth.Depth32Bit;
tiles.Images.AddRange(AnimationResources.tileImages);
treeViewBlocks.ImageList = tiles;
treeViewItems.ImageList = tiles;
treeViewBlocks.ImageList = AnimationResources.BlockList;
treeViewItems.ImageList = AnimationResources.ItemList;
InitializeTreeviews();
}
try
{
int i = 0;
if (AnimationResources.tileData["blocks"] != null)
{
foreach (JObject content in AnimationResources.tileData["blocks"].Children())
{
foreach (JProperty prop in content.Properties())
{
if (!string.IsNullOrEmpty((string)prop.Value))
{
TreeNode tileNode = new TreeNode((string)prop.Value)
{
Tag = prop.Name,
ImageIndex = i,
SelectedImageIndex = i,
};
treeViewBlocks.Nodes.Add(tileNode);
treeViewBlockCache.Add(tileNode);
}
i++;
}
}
}
if (AnimationResources.tileData["items"] != null)
{
foreach (JObject content in AnimationResources.tileData["items"].Children())
{
foreach (JProperty prop in content.Properties())
{
if (!string.IsNullOrEmpty((string)prop.Value))
{
TreeNode tileNode = new TreeNode((string)prop.Value)
{
Tag = prop.Name,
ImageIndex = i,
SelectedImageIndex = i,
};
treeViewItems.Nodes.Add(tileNode);
treeViewItemCache.Add(tileNode);
}
i++;
}
}
}
}
catch (Newtonsoft.Json.JsonException j_ex)
{
MessageBox.Show(j_ex.Message, "Error");
return;
}
}
private void InitializeTreeviews()
{
Stopwatch stopwatch = Stopwatch.StartNew();
GetTileDataToView("blocks", treeViewBlocks.Nodes, treeViewBlockCache.Add);
GetTileDataToView("items", treeViewItems.Nodes, treeViewItemCache.Add);
stopwatch.Stop();
Debug.WriteLine($"{nameof(InitializeTreeviews)} took {stopwatch.ElapsedMilliseconds}ms");
}
private void treeViews_AfterSelect(object sender, TreeViewEventArgs e)
{
@@ -91,6 +47,38 @@ namespace PckStudio.Forms.Additional_Popups.Animation
}
}
private void GetTileDataToView(string key, TreeNodeCollection collection, Action<TreeNode> additinalAction)
{
try
{
if (AnimationResources.JsonTileData[key] 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);
}
}
}
}
}
catch (Newtonsoft.Json.JsonException j_ex)
{
MessageBox.Show(j_ex.Message, "Error");
return;
}
}
void filter_TextChanged(object sender, EventArgs e)
{
// Some code in this function is modified code from this StackOverflow answer - MattNL

View File

@@ -371,7 +371,7 @@ namespace PckStudio.Forms.Editor
fileDialog.Filter = "Animation Scripts (*.mcmeta)|*.png.mcmeta";
if (fileDialog.ShowDialog(this) == DialogResult.OK)
{
JObject mcmeta = ConvertAnimationToJava(currentAnimation);
JObject mcmeta = AnimationResources.ConvertAnimationToJson(currentAnimation, InterpolationCheckbox.Checked);
string jsondata = JsonConvert.SerializeObject(mcmeta, Formatting.Indented);
string filename = fileDialog.FileName;
File.WriteAllText(filename, jsondata);
@@ -381,25 +381,6 @@ namespace PckStudio.Forms.Editor
}
}
private JObject ConvertAnimationToJava(Animation animation)
{
JObject janimation = new JObject();
JObject mcmeta = new JObject();
mcmeta["comment"] = $"Animation converted by {ProductName}";
mcmeta["animation"] = janimation;
JArray jframes = new JArray();
foreach (var frame in animation.GetFrames())
{
JObject jframe = new JObject();
jframe["index"] = animation.GetTextureIndex(frame.Texture);
jframe["time"] = frame.Ticks;
jframes.Add(jframe);
};
janimation["interpolation"] = InterpolationCheckbox.Checked;
janimation["frames"] = jframes;
return mcmeta;
}
private void howToInterpolation_Click(object sender, EventArgs e)
{
MessageBox.Show("The Interpolation effect is when the animtion smoothly translates between the frames instead of simply displaying the next one. This can be seen with some vanilla Minecraft textures such as Magma and Prismarine.\n\nThe \"Interpolates\" checkbox at the bottom controls this.", "Interpolation");

View File

@@ -70,7 +70,7 @@ namespace PckStudio.Forms.Editor
}
treeView1.ImageList = new ImageList();
treeView1.ImageList.Images.AddRange(Utilities.BehaviourResources.entityImages);
treeView1.ImageList.Images.AddRange(ApplicationScope.EntityImages);
treeView1.ImageList.ColorDepth = ColorDepth.Depth32Bit;
SetUpTree();
}
@@ -151,7 +151,7 @@ namespace PckStudio.Forms.Editor
if (treeView1.SelectedNode == null) return;
if (!(treeView1.SelectedNode.Tag is BehaviourFile.RiderPositionOverride entry)) return;
var diag = new AddEntry("behaviours", Utilities.BehaviourResources.entityImages);
var diag = new AddEntry("behaviours", ApplicationScope.EntityImages);
diag.acceptBtn.Text = "Save";
if (diag.ShowDialog() == DialogResult.OK)
@@ -205,7 +205,7 @@ namespace PckStudio.Forms.Editor
private void addNewEntryToolStripMenuItem_Click(object sender, EventArgs e)
{
var diag = new AddEntry("behaviours", Utilities.BehaviourResources.entityImages);
var diag = new AddEntry("behaviours", ApplicationScope.EntityImages);
if(diag.ShowDialog() == DialogResult.OK)
{

View File

@@ -60,7 +60,7 @@ namespace PckStudio.Forms.Editor
}
treeView1.ImageList = new ImageList();
Utilities.MaterialResources.entityImages.ToList().ForEach(img => treeView1.ImageList.Images.Add(img));
ApplicationScope.EntityImages.ToList().ForEach(treeView1.ImageList.Images.Add);
treeView1.ImageList.ColorDepth = ColorDepth.Depth32Bit;
SetUpTree();
}
@@ -134,7 +134,7 @@ namespace PckStudio.Forms.Editor
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
var diag = new Additional_Popups.EntityForms.AddEntry("materials", Utilities.MaterialResources.entityImages);
var diag = new Additional_Popups.EntityForms.AddEntry("materials", ApplicationScope.EntityImages);
if (diag.ShowDialog() == DialogResult.OK)
{

View File

@@ -1,9 +1,12 @@
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using PckStudio.Properties;
using PckStudio.Extensions;
using PckStudio.Forms.Editor;
namespace PckStudio.Forms.Utilities
{
@@ -11,8 +14,62 @@ namespace PckStudio.Forms.Utilities
{
public static string GetAnimationSection(bool isItem) => isItem ? "items" : "blocks";
public static readonly JObject tileData = JObject.Parse(Resources.tileData);
private static Image[] _tileImages;
public static Image[] tileImages => _tileImages ??= Resources.terrain_sheet.CreateImageList(16).Concat(Resources.items_sheet.CreateImageList(16)).ToArray();
private static JObject _jsonData = JObject.Parse(Resources.tileData);
public static JObject JsonTileData => _jsonData ??= JObject.Parse(Resources.tileData);
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
{
get
{
if (_itemList is null)
{
_itemList = new ImageList();
_itemList.ColorDepth = ColorDepth.Depth32Bit;
_itemList.Images.AddRange(ItemImages);
}
return _itemList;
}
}
private static ImageList _blockList;
public static ImageList BlockList
{
get
{
if (_blockList is null)
{
_blockList = new ImageList();
_blockList.ColorDepth = ColorDepth.Depth32Bit;
_blockList.Images.AddRange(BlockImages);
}
return _blockList;
}
}
internal static JObject ConvertAnimationToJson(Animation animation, bool interpolation)
{
JObject janimation = new JObject();
JObject mcmeta = new JObject();
mcmeta["comment"] = $"Animation converted by {Application.ProductName}";
mcmeta["animation"] = janimation;
JArray jframes = new JArray();
foreach (var frame in animation.GetFrames())
{
JObject jframe = new JObject();
jframe["index"] = animation.GetTextureIndex(frame.Texture);
jframe["time"] = frame.Ticks;
jframes.Add(jframe);
};
janimation["interpolation"] = interpolation;
janimation["frames"] = jframes;
return mcmeta;
}
}
}

View File

@@ -1,5 +1,4 @@
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing;
using System.Linq;
using System.IO;
@@ -12,10 +11,6 @@ namespace PckStudio.Forms.Utilities
{
public static class BehaviourResources
{
private static Image[] _entityImages;
public static Image[] entityImages => _entityImages ??= Resources.entities_sheet.CreateImageList(32).ToArray();
internal static byte[] BehaviourFileInitializer()
{
using var stream = new MemoryStream();

View File

@@ -1,5 +1,4 @@
using Newtonsoft.Json.Linq;
using System.Drawing;
using System.Drawing;
using System.Linq;
using System.IO;
@@ -14,9 +13,6 @@ namespace PckStudio.Forms.Utilities
{
public static class MaterialResources
{
private static Image[] _entityImages;
public static Image[] entityImages => _entityImages ??= Resources.entities_sheet.CreateImageList(32).ToArray();
public static byte[] MaterialsFileInitializer()
{
using var stream = new MemoryStream();

View File

@@ -1,15 +1,36 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PckStudio.Classes.Misc;
using PckStudio.Forms.Utilities;
using PckStudio.Properties;
using PckStudio.Extensions;
namespace PckStudio
{
internal static class ApplicationScope
{
public static FileCacher AppDataCacher = new FileCacher(Program.AppDataCache);
public static FileCacher AppDataCacher { get; private set; }
private static Image[] _entityImages;
public static Image[] EntityImages => _entityImages;
internal static void Initialize()
{
Stopwatch stopwatch = Stopwatch.StartNew();
{
_entityImages ??= Resources.entities_sheet.CreateImageList(32).ToArray();
AppDataCacher = new FileCacher(Program.AppDataCache);
_ = AnimationResources.JsonTileData;
_ = AnimationResources.ItemList;
_ = AnimationResources.BlockList;
}
stopwatch.Stop();
Debug.WriteLine($"{nameof(ApplicationScope.Initialize)} took {stopwatch.ElapsedMilliseconds}ms");
}
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using PckStudio.Forms.Utilities;
namespace PckStudio
{
@@ -22,7 +23,7 @@ namespace PckStudio
static void Main(string[] args)
{
System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
ApplicationScope.Initialize();
MainInstance = new MainForm();
if (args.Length > 0 && File.Exists(args[0]) && args[0].EndsWith(".pck"))
MainInstance.LoadPckFromFile(args[0]);