From 38d14661efc02e0c13f8f9237a453e3106d45461 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Sun, 14 May 2023 16:46:20 +0200 Subject: [PATCH] Improved performance im ChangeTile form --- .../Additional-Popups/Animation/ChangeTile.cs | 106 ++++++++---------- PCK-Studio/Forms/Editor/AnimationEditor.cs | 21 +--- PCK-Studio/Forms/Editor/BehaviourEditor.cs | 6 +- PCK-Studio/Forms/Editor/MaterialsEditor.cs | 4 +- .../Forms/Utilities/AnimationResources.cs | 67 ++++++++++- .../Forms/Utilities/BehaviourResources.cs | 7 +- .../Forms/Utilities/MaterialResources.cs | 6 +- PCK-Studio/Internals/ApplicationScope.cs | 25 ++++- PCK-Studio/Program.cs | 3 +- 9 files changed, 142 insertions(+), 103 deletions(-) diff --git a/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs b/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs index d09ace94..e5554a30 100644 --- a/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs +++ b/PCK-Studio/Forms/Additional-Popups/Animation/ChangeTile.cs @@ -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 treeViewBlockCache = new List(); + List treeViewBlockCache = new List(); List treeViewItemCache = new List(); 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 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 diff --git a/PCK-Studio/Forms/Editor/AnimationEditor.cs b/PCK-Studio/Forms/Editor/AnimationEditor.cs index 2694043c..125d35db 100644 --- a/PCK-Studio/Forms/Editor/AnimationEditor.cs +++ b/PCK-Studio/Forms/Editor/AnimationEditor.cs @@ -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"); diff --git a/PCK-Studio/Forms/Editor/BehaviourEditor.cs b/PCK-Studio/Forms/Editor/BehaviourEditor.cs index 8726bf9c..3d4174e7 100644 --- a/PCK-Studio/Forms/Editor/BehaviourEditor.cs +++ b/PCK-Studio/Forms/Editor/BehaviourEditor.cs @@ -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) { diff --git a/PCK-Studio/Forms/Editor/MaterialsEditor.cs b/PCK-Studio/Forms/Editor/MaterialsEditor.cs index f8345162..30633484 100644 --- a/PCK-Studio/Forms/Editor/MaterialsEditor.cs +++ b/PCK-Studio/Forms/Editor/MaterialsEditor.cs @@ -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) { diff --git a/PCK-Studio/Forms/Utilities/AnimationResources.cs b/PCK-Studio/Forms/Utilities/AnimationResources.cs index 237d153a..633cd1d1 100644 --- a/PCK-Studio/Forms/Utilities/AnimationResources.cs +++ b/PCK-Studio/Forms/Utilities/AnimationResources.cs @@ -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; + } } } diff --git a/PCK-Studio/Forms/Utilities/BehaviourResources.cs b/PCK-Studio/Forms/Utilities/BehaviourResources.cs index dcaf4431..16674b80 100644 --- a/PCK-Studio/Forms/Utilities/BehaviourResources.cs +++ b/PCK-Studio/Forms/Utilities/BehaviourResources.cs @@ -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(); diff --git a/PCK-Studio/Forms/Utilities/MaterialResources.cs b/PCK-Studio/Forms/Utilities/MaterialResources.cs index 861dbe6a..88c36b65 100644 --- a/PCK-Studio/Forms/Utilities/MaterialResources.cs +++ b/PCK-Studio/Forms/Utilities/MaterialResources.cs @@ -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(); diff --git a/PCK-Studio/Internals/ApplicationScope.cs b/PCK-Studio/Internals/ApplicationScope.cs index 18b154eb..049fb699 100644 --- a/PCK-Studio/Internals/ApplicationScope.cs +++ b/PCK-Studio/Internals/ApplicationScope.cs @@ -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"); + } } -} +} \ No newline at end of file diff --git a/PCK-Studio/Program.cs b/PCK-Studio/Program.cs index 243382fc..f246f0e2 100644 --- a/PCK-Studio/Program.cs +++ b/PCK-Studio/Program.cs @@ -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]);