diff --git a/PCK-Studio/Forms/Editor/AnimationEditor.cs b/PCK-Studio/Forms/Editor/AnimationEditor.cs index 673e116f..9f3dadec 100644 --- a/PCK-Studio/Forms/Editor/AnimationEditor.cs +++ b/PCK-Studio/Forms/Editor/AnimationEditor.cs @@ -34,34 +34,26 @@ using PckStudio.Properties; using PckStudio.Internal; using PckStudio.Internal.Deserializer; using PckStudio.Internal.Serializer; +using PckStudio.Interfaces; namespace PckStudio.Forms.Editor { - public partial class AnimationEditor : MetroForm + public partial class AnimationEditor : Editor { - public Animation Result => _animation; - - private Animation _animation; private bool _isSpecialTile; - private AnimationEditor() + internal AnimationEditor(Animation animation, ISaveContext saveContext, string displayName, bool isSpecialTile = false) + : base(animation, saveContext) { InitializeComponent(); - toolStripSeparator1.Visible = saveToolStripMenuItem1.Visible = !Settings.Default.AutoSaveChanges; - } - - internal AnimationEditor(Animation animation, string displayName, bool isSpecialTile = false) - : this() - { - _ = animation ?? throw new ArgumentNullException(nameof(animation)); - _animation = animation; + toolStripSeparator1.Visible = saveToolStripMenuItem1.Visible = !saveContext.AutoSave; tileLabel.Text = displayName; _isSpecialTile = isSpecialTile; animationPictureBox.Image = animation.CreateAnimationImage(); } - internal AnimationEditor(Animation animation, string displayName, Color blendColor) - : this(animation, displayName) + internal AnimationEditor(Animation animation, ISaveContext saveContext, string displayName, Color blendColor) + : this(animation, saveContext, displayName) { animationPictureBox.UseBlendColor = true; animationPictureBox.BlendColor = blendColor; @@ -83,20 +75,20 @@ namespace PckStudio.Forms.Editor private void LoadAnimationTreeView() { - if (_animation is null) + if (EditorValue is null) { AnimationStartStopBtn.Enabled = false; return; } AnimationStartStopBtn.Enabled = true; - InterpolationCheckbox.Checked = _animation.Interpolate; + InterpolationCheckbox.Checked = EditorValue.Interpolate; TextureIcons.Images.Clear(); - TextureIcons.Images.AddRange(_animation.GetTextures().ToArray()); + TextureIcons.Images.AddRange(EditorValue.GetTextures().ToArray()); UpdateTreeView(); - animationPictureBox.Image ??= _animation.CreateAnimationImage(); + animationPictureBox.Image ??= EditorValue.CreateAnimationImage(); - if (_animation.FrameCount > 0) + if (EditorValue.FrameCount > 0) { animationPictureBox.Image.SelectActiveFrame(FrameDimension.Page, 0); } @@ -106,10 +98,10 @@ namespace PckStudio.Forms.Editor { frameTreeView.Nodes.Clear(); frameTreeView.Nodes.AddRange( - _animation.GetFrames() + EditorValue.GetFrames() .Select(frame => { - var imageIndex = _animation.GetTextureIndex(frame.Texture); + var imageIndex = EditorValue.GetTextureIndex(frame.Texture); return new TreeNode($"for {frame.Ticks} ticks", imageIndex, imageIndex); }) .ToArray() @@ -122,7 +114,7 @@ namespace PckStudio.Forms.Editor { StopAnimation(); } - animationPictureBox.Image = _animation.GetFrame(frameTreeView.SelectedNode.Index).Texture; + animationPictureBox.Image = EditorValue.GetFrame(frameTreeView.SelectedNode.Index).Texture; } private void StopAnimation() @@ -139,9 +131,9 @@ namespace PckStudio.Forms.Editor return; } - if (_animation.FrameCount > 1) + if (EditorValue.FrameCount > 1) { - animationPictureBox.Image = _animation.CreateAnimationImage(); + animationPictureBox.Image = EditorValue.CreateAnimationImage(); animationPictureBox.Start(); AnimationStartStopBtn.Text = "Stop Animation"; } @@ -166,7 +158,7 @@ namespace PckStudio.Forms.Editor private void saveToolStripMenuItem1_Click(object sender, EventArgs e) { - if (!_isSpecialTile && _animation is not null && _animation.FrameCount > 0) + if (!_isSpecialTile && EditorValue is not null && EditorValue.FrameCount > 0) { DialogResult = DialogResult.OK; return; @@ -231,7 +223,7 @@ namespace PckStudio.Forms.Editor { int draggedIndex = draggedNode.Index; int targetIndex = targetNode.Index; - _animation.SwapFrames(draggedIndex, targetIndex); + EditorValue.SwapFrames(draggedIndex, targetIndex); UpdateTreeView(); } } @@ -255,8 +247,8 @@ namespace PckStudio.Forms.Editor private void treeView1_doubleClick(object sender, EventArgs e) { - Animation.Frame frame = _animation.GetFrame(frameTreeView.SelectedNode.Index); - using FrameEditor diag = new FrameEditor(frame.Ticks, _animation.GetTextureIndex(frame.Texture), TextureIcons); + Animation.Frame frame = EditorValue.GetFrame(frameTreeView.SelectedNode.Index); + using FrameEditor diag = new FrameEditor(frame.Ticks, EditorValue.GetTextureIndex(frame.Texture), TextureIcons); if (diag.ShowDialog(this) == DialogResult.OK) { /* Found a bug here. When passing the frame variable, @@ -266,7 +258,7 @@ namespace PckStudio.Forms.Editor * - Matt */ - _animation.SetFrame(frameTreeView.SelectedNode.Index, diag.FrameTextureIndex, diag.FrameTime); + EditorValue.SetFrame(frameTreeView.SelectedNode.Index, diag.FrameTextureIndex, diag.FrameTime); UpdateTreeView(); } } @@ -277,14 +269,14 @@ namespace PckStudio.Forms.Editor diag.SaveBtn.Text = "Add"; if (diag.ShowDialog(this) == DialogResult.OK) { - _animation.AddFrame(diag.FrameTextureIndex, _isSpecialTile ? Animation.MinimumFrameTime : diag.FrameTime); + EditorValue.AddFrame(diag.FrameTextureIndex, _isSpecialTile ? Animation.MinimumFrameTime : diag.FrameTime); UpdateTreeView(); } } private void removeFrameToolStripMenuItem_Click(object sender, EventArgs e) { - if (frameTreeView.SelectedNode is TreeNode t && _animation.RemoveFrame(t.Index)) + if (frameTreeView.SelectedNode is TreeNode t && EditorValue.RemoveFrame(t.Index)) { frameTreeView.SelectedNode.Remove(); } @@ -297,7 +289,7 @@ namespace PckStudio.Forms.Editor { if (animationPictureBox.IsPlaying) animationPictureBox.Stop(); - _animation.SetFrameTicks(diag.Ticks); + EditorValue.SetFrameTicks(diag.Ticks); UpdateTreeView(); } diag.Dispose(); @@ -334,10 +326,10 @@ namespace PckStudio.Forms.Editor } try { - var img = Image.FromFile(textureFile).ReleaseFromFile(); + Image img = Image.FromFile(textureFile).ReleaseFromFile(); JObject mcmeta = JObject.Parse(File.ReadAllText(fileDialog.FileName)); Animation javaAnimation = AnimationDeserializer.DefaultDeserializer.DeserializeJavaAnimation(mcmeta, img); - _animation = javaAnimation; + EditorValue = javaAnimation; LoadAnimationTreeView(); } catch (JsonException j_ex) @@ -354,11 +346,11 @@ namespace PckStudio.Forms.Editor fileDialog.Filter = "Animation Scripts (*.mcmeta)|*.png.mcmeta"; if (fileDialog.ShowDialog(this) == DialogResult.OK) { - JObject mcmeta = AnimationSerializer.SerializeJavaAnimation(_animation); + JObject mcmeta = AnimationSerializer.SerializeJavaAnimation(EditorValue); string jsondata = JsonConvert.SerializeObject(mcmeta, Formatting.Indented); string filename = fileDialog.FileName; File.WriteAllText(filename, jsondata); - Image finalTexture = AnimationSerializer.SerializeTexture(_animation); + Image finalTexture = AnimationSerializer.SerializeTexture(EditorValue); // removes ".mcmeta" from filename string texturePath = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename)); finalTexture.Save(texturePath); @@ -390,8 +382,8 @@ namespace PckStudio.Forms.Editor private void InterpolationCheckbox_CheckedChanged(object sender, EventArgs e) { - if (_animation is not null) - _animation.Interpolate = InterpolationCheckbox.Checked; + if (EditorValue is not null) + EditorValue.Interpolate = InterpolationCheckbox.Checked; } private void AnimationEditor_FormClosing(object sender, FormClosingEventArgs e) @@ -415,14 +407,14 @@ namespace PckStudio.Forms.Editor if (fileDialog.ShowDialog(this) != DialogResult.OK) return; - var gif = Image.FromFile(fileDialog.FileName).ReleaseFromFile(); + Image gif = Image.FromFile(fileDialog.FileName).ReleaseFromFile(); if (!gif.RawFormat.Equals(ImageFormat.Gif)) { MessageBox.Show(this, "Selected file is not a gif", "Invalid file", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } - var oldResolution = _animation.GetFrame(0).Texture.Width; + var oldResolution = EditorValue.GetFrame(0).Texture.Width; FrameDimension dimension = new FrameDimension(gif.FrameDimensionsList[0]); int frameCount = gif.GetFrameCount(dimension); @@ -436,8 +428,8 @@ namespace PckStudio.Forms.Editor textures.Add(new Bitmap(gif, oldResolution, oldResolution)); } - _animation = new Animation(textures, initFramesFromTextures: true); - _animation.Interpolate = InterpolationCheckbox.Checked; + EditorValue = new Animation(textures, initFramesFromTextures: true); + EditorValue.Interpolate = InterpolationCheckbox.Checked; LoadAnimationTreeView(); } @@ -452,7 +444,7 @@ namespace PckStudio.Forms.Editor return; using Image img = Image.FromFile(ofd.FileName); IEnumerable textures = img.Split(ImageLayoutDirection.Vertical); - _animation = new Animation(textures, initFramesFromTextures: true); + EditorValue = new Animation(textures, initFramesFromTextures: true); LoadAnimationTreeView(); } @@ -465,7 +457,7 @@ namespace PckStudio.Forms.Editor }; if (fileDialog.ShowDialog(this) != DialogResult.OK) return; - _animation.CreateAnimationImage().Save(fileDialog.FileName); + EditorValue.CreateAnimationImage().Save(fileDialog.FileName); } private void frameTimeandTicksToolStripMenuItem_Click(object sender, EventArgs e) diff --git a/PCK-Studio/Forms/Editor/AudioEditor.cs b/PCK-Studio/Forms/Editor/AudioEditor.cs index 202a1eee..ffdedc21 100644 --- a/PCK-Studio/Forms/Editor/AudioEditor.cs +++ b/PCK-Studio/Forms/Editor/AudioEditor.cs @@ -20,17 +20,16 @@ using PckStudio.External.API.Miles; using PckStudio.Internal.FileFormats; using PckStudio.Extensions; using PckStudio.Internal.App; +using PckStudio.Internal; +using PckStudio.Interfaces; // Audio Editor by MattNL and Miku-666 namespace PckStudio.Forms.Editor { - public partial class AudioEditor : MetroForm + public partial class AudioEditor : Editor { public string defaultType = "yes"; - PckAudioFile _audioFile = null; - PckAsset _audioAsset; - bool _isLittleEndian = false; MainForm parent = null; private static readonly List Categories = new List @@ -52,7 +51,15 @@ namespace PckStudio.Forms.Editor */ }; - private string GetCategoryFromId(PckAudioFile.AudioCategory.EAudioType categoryId) + public AudioEditor(PckAudioFile audioFile, ISaveContext saveContext) + : base(audioFile, saveContext) + { + InitializeComponent(); + saveToolStripMenuItem1.Visible = !saveContext.AutoSave; + SetUpTree(); + } + + private string GetCategoryFromId(PckAudioFile.AudioCategory.EAudioType categoryId) => categoryId >= PckAudioFile.AudioCategory.EAudioType.Overworld && categoryId <= PckAudioFile.AudioCategory.EAudioType.Unused ? Categories[(int)categoryId] @@ -63,26 +70,12 @@ namespace PckStudio.Forms.Editor return (PckAudioFile.AudioCategory.EAudioType)Categories.IndexOf(category); } - public AudioEditor(PckAsset asset, bool isLittleEndian) - { - InitializeComponent(); - - saveToolStripMenuItem1.Visible = !Settings.Default.AutoSaveChanges; - - _isLittleEndian = isLittleEndian; - - _audioAsset = asset; - _audioFile = _audioAsset.GetData(new PckAudioFileReader(isLittleEndian ? OMI.Endianness.LittleEndian : OMI.Endianness.BigEndian)); - - SetUpTree(); - } - public void SetUpTree() { treeView1.BeginUpdate(); treeView1.Nodes.Clear(); - foreach (PckAudioFile.AudioCategory category in _audioFile.Categories) + foreach (PckAudioFile.AudioCategory category in EditorValue.Categories) { // fix songs with directories using backslash instead of forward slash // Songs with a backslash instead of a forward slash would not play in RPCS3 @@ -92,7 +85,7 @@ namespace PckStudio.Forms.Editor if (category.AudioType == PckAudioFile.AudioCategory.EAudioType.Creative) { if (category.Name == "include_overworld" && - _audioFile.TryGetCategory(PckAudioFile.AudioCategory.EAudioType.Overworld, out PckAudioFile.AudioCategory overworldCategory)) + EditorValue.TryGetCategory(PckAudioFile.AudioCategory.EAudioType.Overworld, out PckAudioFile.AudioCategory overworldCategory)) { foreach (var name in category.SongNames.ToList()) { @@ -108,7 +101,7 @@ namespace PckStudio.Forms.Editor treeNode.Tag = category; treeView1.Nodes.Add(treeNode); } - playOverworldInCreative.Enabled = _audioFile.HasCategory(PckAudioFile.AudioCategory.EAudioType.Creative); + playOverworldInCreative.Enabled = EditorValue.HasCategory(PckAudioFile.AudioCategory.EAudioType.Creative); treeView1.EndUpdate(); } @@ -144,7 +137,7 @@ namespace PckStudio.Forms.Editor private void addCategoryStripMenuItem_Click(object sender, EventArgs e) { - string[] available = Categories.FindAll(str => !_audioFile.HasCategory(GetCategoryId(str))).ToArray(); + string[] available = Categories.FindAll(str => !EditorValue.HasCategory(GetCategoryId(str))).ToArray(); if (available.Length == 0) { MessageBox.Show(this, "There are no more categories that could be added", "All possible categories are used"); @@ -154,8 +147,8 @@ namespace PckStudio.Forms.Editor if (add.ShowDialog(this) != DialogResult.OK) return; - _audioFile.AddCategory(GetCategoryId(add.SelectedItem)); - PckAudioFile.AudioCategory category = _audioFile.GetCategory(GetCategoryId(add.SelectedItem)); + EditorValue.AddCategory(GetCategoryId(add.SelectedItem)); + PckAudioFile.AudioCategory category = EditorValue.GetCategory(GetCategoryId(add.SelectedItem)); if (GetCategoryId(add.SelectedItem) == PckAudioFile.AudioCategory.EAudioType.Creative) { @@ -194,7 +187,7 @@ namespace PckStudio.Forms.Editor private void removeCategoryStripMenuItem_Click(object sender, EventArgs e) { if (treeView1.SelectedNode is TreeNode main && - _audioFile.RemoveCategory(GetCategoryId(treeView1.SelectedNode.Text))) + EditorValue.RemoveCategory(GetCategoryId(treeView1.SelectedNode.Text))) { if(GetCategoryId(treeView1.SelectedNode.Text) == PckAudioFile.AudioCategory.EAudioType.Creative) { @@ -370,18 +363,18 @@ namespace PckStudio.Forms.Editor private void saveToolStripMenuItem1_Click(object sender, EventArgs e) { - if (!_audioFile.HasCategory(PckAudioFile.AudioCategory.EAudioType.Overworld) || - !_audioFile.HasCategory(PckAudioFile.AudioCategory.EAudioType.Nether) || - !_audioFile.HasCategory(PckAudioFile.AudioCategory.EAudioType.End)) + if (!EditorValue.HasCategory(PckAudioFile.AudioCategory.EAudioType.Overworld) || + !EditorValue.HasCategory(PckAudioFile.AudioCategory.EAudioType.Nether) || + !EditorValue.HasCategory(PckAudioFile.AudioCategory.EAudioType.End)) { MessageBox.Show(this, "Your changes were not saved. The game will crash when loading your pack if the Overworld, Nether and End categories don't all exist with at least one valid song.", "Mandatory Categories Missing"); return; } - PckAudioFile.AudioCategory overworldCategory = _audioFile.GetCategory(PckAudioFile.AudioCategory.EAudioType.Overworld); + PckAudioFile.AudioCategory overworldCategory = EditorValue.GetCategory(PckAudioFile.AudioCategory.EAudioType.Overworld); bool songs_missing = false; - foreach (PckAudioFile.AudioCategory category in _audioFile.Categories) + foreach (PckAudioFile.AudioCategory category in EditorValue.Categories) { if (category.SongNames.Count < 1) { @@ -420,7 +413,7 @@ namespace PckStudio.Forms.Editor return; } - _audioAsset.SetData(new PckAudioFileWriter(_audioFile, _isLittleEndian ? OMI.Endianness.LittleEndian : OMI.Endianness.BigEndian)); + Save(); DialogResult = DialogResult.OK; } @@ -444,7 +437,7 @@ namespace PckStudio.Forms.Editor if (dr != DialogResult.Yes) return; var totalSongList = new List(); - foreach (string song in _audioFile.Categories.SelectMany(cat => cat.SongNames)) + foreach (string song in EditorValue.Categories.SelectMany(cat => cat.SongNames)) { Console.WriteLine(song); totalSongList.Add(song); @@ -544,7 +537,7 @@ namespace PckStudio.Forms.Editor return; var totalSongList = new List(); - foreach (string song in _audioFile.Categories.SelectMany(cat => cat.SongNames)) + foreach (string song in EditorValue.Categories.SelectMany(cat => cat.SongNames)) { totalSongList.Add(song); } @@ -596,7 +589,7 @@ namespace PckStudio.Forms.Editor if (!(treeView1.SelectedNode is TreeNode t && t.Tag is PckAudioFile.AudioCategory category)) return; - string[] available = Categories.FindAll(str => !_audioFile.HasCategory(GetCategoryId(str))).ToArray(); + string[] available = Categories.FindAll(str => !EditorValue.HasCategory(GetCategoryId(str))).ToArray(); if (available.Length > 0) { using ItemSelectionPopUp add = new ItemSelectionPopUp(available); @@ -604,11 +597,11 @@ namespace PckStudio.Forms.Editor if (add.ShowDialog(this) != DialogResult.OK) return; - _audioFile.RemoveCategory(category.AudioType); + EditorValue.RemoveCategory(category.AudioType); - _audioFile.AddCategory(category.parameterType, GetCategoryId(add.SelectedItem), category.AudioType == PckAudioFile.AudioCategory.EAudioType.Overworld && playOverworldInCreative.Checked ? "include_overworld" : ""); + EditorValue.AddCategory(category.parameterType, GetCategoryId(add.SelectedItem), category.AudioType == PckAudioFile.AudioCategory.EAudioType.Overworld && playOverworldInCreative.Checked ? "include_overworld" : ""); - PckAudioFile.AudioCategory newCategory = _audioFile.GetCategory(GetCategoryId(add.SelectedItem)); + PckAudioFile.AudioCategory newCategory = EditorValue.GetCategory(GetCategoryId(add.SelectedItem)); category.SongNames.ForEach(c => newCategory.SongNames.Add(c)); @@ -628,7 +621,7 @@ namespace PckStudio.Forms.Editor return; string musicdir = Path.Combine(parent.GetDataPath(), "Music"); Directory.CreateDirectory(musicdir); - foreach (PckAudioFile.AudioCategory category in _audioFile.Categories) + foreach (PckAudioFile.AudioCategory category in EditorValue.Categories) { for (var i = 0; i < category.SongNames.Count; i++) // using standard for loop so the list can be modified { diff --git a/PCK-Studio/Forms/Editor/BehaviourEditor.cs b/PCK-Studio/Forms/Editor/BehaviourEditor.cs index c47957ee..517df132 100644 --- a/PCK-Studio/Forms/Editor/BehaviourEditor.cs +++ b/PCK-Studio/Forms/Editor/BehaviourEditor.cs @@ -16,32 +16,44 @@ using PckStudio.Internal; using PckStudio.Extensions; using PckStudio.Internal.Json; using PckStudio.Internal.App; +using PckStudio.Interfaces; namespace PckStudio.Forms.Editor { - public partial class BehaviourEditor : MetroForm + // Behaviours File Format research by Miku and MattNL + public partial class BehaviourEditor : Editor { - // Behaviours File Format research by Miku and MattNL - private readonly PckAsset _asset; - BehaviourFile _behaviourFile; private readonly List BehaviourData = Entities.BehaviourInfos; - void SetUpTree() + public BehaviourEditor(BehaviourFile behaviourFile, ISaveContext saveContext) + : base(behaviourFile, saveContext) + { + InitializeComponent(); + + saveToolStripMenuItem1.Visible = !saveContext.AutoSave; + + treeView1.ImageList = new ImageList(); + treeView1.ImageList.Images.AddRange(ApplicationScope.EntityImages); + treeView1.ImageList.ColorDepth = ColorDepth.Depth32Bit; + SetUpTree(); + } + + void SetUpTree() { treeView1.BeginUpdate(); treeView1.Nodes.Clear(); - foreach (var entry in _behaviourFile.entries) + foreach (BehaviourFile.RiderPositionOverride entry in EditorValue.entries) { TreeNode EntryNode = new TreeNode(entry.name); - var behaviour = BehaviourData.Find(b => b.InternalName == entry.name); + EntityInfo behaviour = BehaviourData.Find(b => b.InternalName == entry.name); EntryNode.Text = behaviour.DisplayName; EntryNode.ImageIndex = BehaviourData.IndexOf(behaviour); EntryNode.SelectedImageIndex = EntryNode.ImageIndex; EntryNode.Tag = entry; - foreach (var posOverride in entry.overrides) + foreach (BehaviourFile.RiderPositionOverride.PositionOverride posOverride in entry.overrides) { TreeNode OverrideNode = new TreeNode("Position Override"); OverrideNode.Tag = posOverride; @@ -55,21 +67,6 @@ namespace PckStudio.Forms.Editor treeView1.EndUpdate(); } - public BehaviourEditor(PckAsset asset) - { - InitializeComponent(); - - saveToolStripMenuItem1.Visible = !Settings.Default.AutoSaveChanges; - - _asset = asset; - _behaviourFile = asset.GetData(new BehavioursReader()); - - treeView1.ImageList = new ImageList(); - treeView1.ImageList.Images.AddRange(ApplicationScope.EntityImages); - treeView1.ImageList.ColorDepth = ColorDepth.Depth32Bit; - SetUpTree(); - } - private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (treeView1.SelectedNode is null) @@ -155,7 +152,7 @@ namespace PckStudio.Forms.Editor { if (string.IsNullOrEmpty(diag.SelectedEntity)) return; - if (_behaviourFile.entries.FindAll(behaviour => behaviour.name == diag.SelectedEntity).Count() > 0) + if (EditorValue.entries.FindAll(behaviour => behaviour.name == diag.SelectedEntity).Count() > 0) { MessageBox.Show(this, "You cannot have two entries for one entity. Please use the \"Add New Position Override\" tool to add multiple overrides for entities", "Error", MessageBoxButtons.OK); return; @@ -204,7 +201,7 @@ namespace PckStudio.Forms.Editor { if (string.IsNullOrEmpty(diag.SelectedEntity)) return; - if (_behaviourFile.entries.FindAll(behaviour => behaviour.name == diag.SelectedEntity).Count() > 0) + if (EditorValue.entries.FindAll(behaviour => behaviour.name == diag.SelectedEntity).Count() > 0) { MessageBox.Show(this, "You cannot have two entries for one entity. Please use the \"Add New Position Override\" tool to add multiple overrides for entities", "Error", MessageBoxButtons.OK); return; @@ -239,7 +236,7 @@ namespace PckStudio.Forms.Editor private void saveToolStripMenuItem1_Click(object sender, EventArgs e) { - _behaviourFile = new BehaviourFile(); + EditorValue = new BehaviourFile(); foreach (TreeNode node in treeView1.Nodes) { if(node.Tag is BehaviourFile.RiderPositionOverride entry) @@ -254,12 +251,11 @@ namespace PckStudio.Forms.Editor } } - _behaviourFile.entries.Add(entry); + EditorValue.entries.Add(entry); } } - _asset.SetData(new BehavioursWriter(_behaviourFile)); - + Save(); DialogResult = DialogResult.OK; } diff --git a/PCK-Studio/Forms/Editor/BoxEditor.cs b/PCK-Studio/Forms/Editor/BoxEditor.cs index 33b445f5..34faed9b 100644 --- a/PCK-Studio/Forms/Editor/BoxEditor.cs +++ b/PCK-Studio/Forms/Editor/BoxEditor.cs @@ -10,8 +10,8 @@ namespace PckStudio.Forms.Editor private SkinBOX result; public SkinBOX Result => result; - public BoxEditor(string box, bool hasInflation) - : this(SkinBOX.FromString(box), hasInflation) + public BoxEditor(string formattedBoxString, bool hasInflation) + : this(SkinBOX.FromString(formattedBoxString), hasInflation) { } diff --git a/PCK-Studio/Forms/Editor/COLEditor.cs b/PCK-Studio/Forms/Editor/COLEditor.cs index 73966b8a..7dfdd889 100644 --- a/PCK-Studio/Forms/Editor/COLEditor.cs +++ b/PCK-Studio/Forms/Editor/COLEditor.cs @@ -10,31 +10,38 @@ using OMI.Formats.Pck; using OMI.Workers.Color; using PckStudio.Extensions; using PckStudio.Properties; +using PckStudio.Internal; +using PckStudio.Interfaces; +using System.Collections.ObjectModel; namespace PckStudio.Forms.Editor { - public partial class COLEditor : MetroForm + public partial class COLEditor : Editor { ColorContainer _defaultColourfile; - ColorContainer _colourfile; string _clipboard_color = "#FFFFFF"; - private readonly PckAsset _asset; - List colorCache = new List(); List waterCache = new List(); List underwaterCache = new List(); List fogCache = new List(); - public COLEditor(PckAsset asset) + private static readonly ICollection PS4Biomes = new List + { + "bamboo_jungle", + "bamboo_jungle_hills", + "mesa_mutated", + "mega_spruce_taiga_mutated", + "mega_taiga_mutated" + }; + + public COLEditor(ColorContainer colorContainer, ISaveContext saveContext) + : base(colorContainer, saveContext) { InitializeComponent(); saveToolStripMenuItem1.Visible = !Settings.Default.AutoSaveChanges; - _asset = asset; - _colourfile = asset.GetData(new COLFileReader()); - TU12ToolStripMenuItem.Click += (sender, e) => SetUpDefaultFile(sender, e, 0); TU13ToolStripMenuItem.Click += (sender, e) => SetUpDefaultFile(sender, e, 1); TU14ToolStripMenuItem.Click += (sender, e) => SetUpDefaultFile(sender, e, 2); @@ -111,7 +118,7 @@ namespace PckStudio.Forms.Editor underwaterTreeView.Nodes.Clear(); fogTreeView.Nodes.Clear(); - ColorContainer temp = targetVersion ? _defaultColourfile : _colourfile; + ColorContainer temp = targetVersion ? _defaultColourfile : EditorValue; List CurrentEntries = new List(); @@ -123,7 +130,7 @@ namespace PckStudio.Forms.Editor // fixes the duplicate entry bug if (targetVersion) { - foreach(ColorContainer.Color col in _colourfile.Colors) + foreach(ColorContainer.Color col in EditorValue.Colors) { if (_defaultColourfile.Colors.Find(c => c.Name == col.Name) == null) continue; @@ -134,7 +141,7 @@ namespace PckStudio.Forms.Editor foreach (ColorContainer.Color col in temp.Colors) { - ColorContainer.Color entry = _colourfile.Colors.Find(color => color.Name == col.Name); + ColorContainer.Color entry = EditorValue.Colors.Find(color => color.Name == col.Name); if (CurrentEntries.Contains(col.Name)) continue; ColorContainer.Color color = entry ?? col; @@ -145,11 +152,11 @@ namespace PckStudio.Forms.Editor // fixes the duplicate entry bug if (targetVersion) { - foreach (ColorContainer.WaterColor col in _colourfile.WaterColors) + foreach (ColorContainer.WaterColor col in EditorValue.WaterColors) { if (_defaultColourfile.WaterColors.Find(c => c.Name == col.Name) == null) continue; - ColorContainer.WaterColor entry = _colourfile.WaterColors.Find(color => color.Name == col.Name); + ColorContainer.WaterColor entry = EditorValue.WaterColors.Find(color => color.Name == col.Name); ColorContainer.WaterColor color = entry ?? col; AddEntry(waterTreeView, waterCache, color.Name, color); AddEntry(underwaterTreeView, underwaterCache, color.Name, color); @@ -159,7 +166,7 @@ namespace PckStudio.Forms.Editor foreach (ColorContainer.WaterColor col in temp.WaterColors) { - ColorContainer.WaterColor entry = _colourfile.WaterColors.Find(color => color.Name == col.Name); + ColorContainer.WaterColor entry = EditorValue.WaterColors.Find(color => color.Name == col.Name); if (CurrentEntries.Contains(col.Name)) continue; ColorContainer.WaterColor color = entry ?? col; @@ -201,14 +208,14 @@ namespace PckStudio.Forms.Editor fogTreeView.SelectedNode = null; var colorEntry = (ColorContainer.Color)colorTreeView.SelectedNode.Tag; - var color = colorEntry.ColorPallette.ToArgb(); + Color color = colorEntry.ColorPallette; SetUpValueChanged(false); alphaUpDown.Visible = false; alphaLabel.Visible = false; - redUpDown.Value = color >> 16 & 0xff; - greenUpDown.Value = color >> 8 & 0xff; - blueUpDown.Value = color & 0xff; - pictureBox1.BackColor = Color.FromArgb(0xff << 24 | color); + redUpDown.Value = color.R; + greenUpDown.Value = color.G; + blueUpDown.Value = color.B; + pictureBox1.BackColor = Color.FromArgb(0xff, color); colorTextbox.Text = ColorTranslator.ToHtml(colorEntry.ColorPallette).TrimStart('#'); SetUpValueChanged(true); } @@ -226,15 +233,17 @@ namespace PckStudio.Forms.Editor fogTreeView.SelectedNode = null; var colorEntry = (ColorContainer.WaterColor)waterTreeView.SelectedNode.Tag; - int color = colorEntry.SurfaceColor.ToArgb(); + Color color = colorEntry.SurfaceColor; SetUpValueChanged(false); + + alphaUpDown.Value = color.A; + redUpDown.Value = color.R; + greenUpDown.Value = color.G; + blueUpDown.Value = color.B; + alphaUpDown.Enabled = true; alphaUpDown.Visible = true; alphaLabel.Visible = true; - alphaUpDown.Value = color >> 24 & 0xff; - redUpDown.Value = color >> 16 & 0xff; - greenUpDown.Value = color >> 8 & 0xff; - blueUpDown.Value = color & 0xff; pictureBox1.BackColor = colorEntry.SurfaceColor; colorTextbox.Text = ColorTranslator.ToHtml(colorEntry.SurfaceColor).TrimStart('#'); SetUpValueChanged(true); @@ -253,14 +262,14 @@ namespace PckStudio.Forms.Editor fogTreeView.SelectedNode = null; var colorEntry = (ColorContainer.WaterColor)underwaterTreeView.SelectedNode.Tag; - int color = colorEntry.UnderwaterColor.ToArgb(); + Color color = colorEntry.UnderwaterColor; SetUpValueChanged(false); alphaUpDown.Visible = false; alphaLabel.Visible = false; - redUpDown.Value = color >> 16 & 0xff; - greenUpDown.Value = color >> 8 & 0xff; - blueUpDown.Value = color & 0xff; - pictureBox1.BackColor = Color.FromArgb(255, Color.FromArgb(0xff << 24 | color)); + redUpDown.Value = color.R; + greenUpDown.Value = color.G; + blueUpDown.Value = color.B; + pictureBox1.BackColor = Color.FromArgb(0xff, color); colorTextbox.Text = ColorTranslator.ToHtml(colorEntry.UnderwaterColor).TrimStart('#'); SetUpValueChanged(true); } @@ -278,22 +287,21 @@ namespace PckStudio.Forms.Editor underwaterTreeView.SelectedNode = null; var colorEntry = (ColorContainer.WaterColor)fogTreeView.SelectedNode.Tag; - int color = colorEntry.FogColor.ToArgb(); + Color color = colorEntry.FogColor; SetUpValueChanged(false); alphaUpDown.Visible = false; alphaLabel.Visible = false; - redUpDown.Value = color >> 16 & 0xff; - greenUpDown.Value = color >> 8 & 0xff; - blueUpDown.Value = color & 0xff; - pictureBox1.BackColor = Color.FromArgb(255, Color.FromArgb(0xff << 24 | color)); + redUpDown.Value = color.R; + greenUpDown.Value = color.G; + blueUpDown.Value = color.B; + pictureBox1.BackColor = Color.FromArgb(0xff, color); colorTextbox.Text = ColorTranslator.ToHtml(colorEntry.FogColor).TrimStart('#'); SetUpValueChanged(true); } private void saveToolStripMenuItem1_Click(object sender, EventArgs e) { - _asset.SetData(new COLFileWriter(_colourfile)); - + Save(); DialogResult = DialogResult.OK; } @@ -605,21 +613,12 @@ namespace PckStudio.Forms.Editor private void stripPS4BiomesToolStripMenuItem_Click(object sender, EventArgs e) { - if(_colourfile.WaterColors.Count > 0) + if(EditorValue.WaterColors.Count > 0) { - List PS4Biomes = new List - { - "bamboo_jungle", - "bamboo_jungle_hills", - "mesa_mutated", - "mega_spruce_taiga_mutated", - "mega_taiga_mutated" - }; - - foreach (ColorContainer.WaterColor col in _colourfile.WaterColors.ToList()) + foreach (ColorContainer.WaterColor col in EditorValue.WaterColors.ToList()) { if (PS4Biomes.Contains(col.Name)) - _colourfile.WaterColors.Remove(col); + EditorValue.WaterColors.Remove(col); } SetUpTable(false); @@ -640,12 +639,12 @@ namespace PckStudio.Forms.Editor entry.Name = prompt.NewText; entry.ColorPallette = Color.FromArgb(0xFFFFFF); - if(_colourfile.Colors.Find(c => c.Name == entry.Name) != null) + if(EditorValue.Colors.Find(c => c.Name == entry.Name) != null) { MessageBox.Show(this, $"\"{entry.Name}\" already exists in this color table", "Color not added"); } - _colourfile.Colors.Add(entry); + EditorValue.Colors.Add(entry); AddEntry(colorTreeView, colorCache, entry.Name, entry); } } @@ -659,7 +658,7 @@ namespace PckStudio.Forms.Editor && entry != null && entry.Tag is ColorContainer.Color color) { - _colourfile.Colors.Remove(color); + EditorValue.Colors.Remove(color); RemoveEntry(entry, colorCache); } } diff --git a/PCK-Studio/Forms/Editor/GameRuleFileEditor.cs b/PCK-Studio/Forms/Editor/GameRuleFileEditor.cs index 0abbcf56..4c88a4c4 100644 --- a/PCK-Studio/Forms/Editor/GameRuleFileEditor.cs +++ b/PCK-Studio/Forms/Editor/GameRuleFileEditor.cs @@ -24,24 +24,18 @@ using PckStudio.Internal.Misc; using OMI.Formats.GameRule; using PckStudio.Properties; using PckStudio.ToolboxItems; +using PckStudio.Internal; +using PckStudio.Interfaces; namespace PckStudio.Forms.Editor { - public partial class GameRuleFileEditor : MetroFramework.Forms.MetroForm + public partial class GameRuleFileEditor : Editor { - private GameRuleFile _file; - - public GameRuleFile Result => _file; - - private GameRuleFileEditor() + public GameRuleFileEditor(GameRuleFile gameRuleFile, ISaveContext saveContext) + : base(gameRuleFile, saveContext) { InitializeComponent(); - saveToolStripMenuItem.Visible = !Settings.Default.AutoSaveChanges; - } - - public GameRuleFileEditor(GameRuleFile gameRuleFile) : this() - { - _file = gameRuleFile; + saveToolStripMenuItem.Visible = !saveContext.AutoSave; } private void OnLoad(object sender, EventArgs e) @@ -64,16 +58,16 @@ namespace PckStudio.Forms.Editor private void ReloadGameRuleTree() { GrfTreeView.Nodes.Clear(); - if (_file is not null) + if (EditorValue is not null) { SetCompressionLevel(); - LoadGameRuleTree(GrfTreeView.Nodes, _file.Root); + LoadGameRuleTree(GrfTreeView.Nodes, EditorValue.Root); } } private void SetCompressionLevel() { - switch (_file.Header.CompressionLevel) + switch (EditorValue.Header.CompressionLevel) { case GameRuleFile.CompressionLevel.None: noneToolStripMenuItem.Checked = true; @@ -161,7 +155,7 @@ namespace PckStudio.Forms.Editor bool isValidNode = GrfTreeView.SelectedNode is TreeNode t && t.Tag is GameRuleFile.GameRule; GameRuleFile.GameRule parentRule = isValidNode ? GrfTreeView.SelectedNode.Tag as GameRuleFile.GameRule - : _file.Root; + : EditorValue.Root; TreeNodeCollection root = isValidNode ? GrfTreeView.SelectedNode.Nodes @@ -205,11 +199,12 @@ namespace PckStudio.Forms.Editor private void saveToolStripMenuItem_Click(object sender, EventArgs e) { - if (_file.Header.unknownData[3] != 0) + if (EditorValue.Header.unknownData[3] != 0) { MessageBox.Show(this, "World grf saving is currently unsupported"); return; } + Save(); DialogResult = DialogResult.OK; MessageBox.Show("Saved!"); } @@ -222,43 +217,43 @@ namespace PckStudio.Forms.Editor private void noneToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripRadioButtonMenuItem radioButton && radioButton.Checked) - _file.Header.CompressionLevel = GameRuleFile.CompressionLevel.None; + EditorValue.Header.CompressionLevel = GameRuleFile.CompressionLevel.None; } private void compressedToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripRadioButtonMenuItem radioButton && radioButton.Checked) - _file.Header.CompressionLevel = GameRuleFile.CompressionLevel.Compressed; + EditorValue.Header.CompressionLevel = GameRuleFile.CompressionLevel.Compressed; } private void compressedRLEToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripRadioButtonMenuItem radioButton && radioButton.Checked) - _file.Header.CompressionLevel = GameRuleFile.CompressionLevel.CompressedRle; + EditorValue.Header.CompressionLevel = GameRuleFile.CompressionLevel.CompressedRle; } private void compressedRLECRCToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripRadioButtonMenuItem radioButton && radioButton.Checked) - _file.Header.CompressionLevel = GameRuleFile.CompressionLevel.CompressedRleCrc; + EditorValue.Header.CompressionLevel = GameRuleFile.CompressionLevel.CompressedRleCrc; } private void wiiUPSVitaToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripRadioButtonMenuItem radioButton && radioButton.Checked) - _file.Header.CompressionType = GameRuleFile.CompressionType.Zlib; + EditorValue.Header.CompressionType = GameRuleFile.CompressionType.Zlib; } private void pS3ToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripRadioButtonMenuItem radioButton && radioButton.Checked) - _file.Header.CompressionType = GameRuleFile.CompressionType.Deflate; + EditorValue.Header.CompressionType = GameRuleFile.CompressionType.Deflate; } private void xbox360ToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { if (sender is ToolStripRadioButtonMenuItem radioButton && radioButton.Checked) - _file.Header.CompressionType = GameRuleFile.CompressionType.XMem; + EditorValue.Header.CompressionType = GameRuleFile.CompressionType.XMem; } private void GameRuleFileEditor_FormClosing(object sender, FormClosingEventArgs e) diff --git a/PCK-Studio/Forms/Editor/LOCEditor.cs b/PCK-Studio/Forms/Editor/LOCEditor.cs index 28a13436..7daacd5b 100644 --- a/PCK-Studio/Forms/Editor/LOCEditor.cs +++ b/PCK-Studio/Forms/Editor/LOCEditor.cs @@ -4,34 +4,28 @@ using System.Data; using System.Linq; using System.Collections.Generic; using System.Windows.Forms; -using MetroFramework.Forms; +using PckStudio.Internal; using PckStudio.Internal.Misc; using PckStudio.Forms.Additional_Popups.Loc; using OMI.Formats.Languages; -using OMI.Workers.Language; -using OMI.Formats.Pck; using PckStudio.Properties; -using PckStudio.Extensions; +using PckStudio.Interfaces; namespace PckStudio.Forms.Editor { - public partial class LOCEditor : MetroForm + public partial class LOCEditor : Editor { - LOCFile _currentLoc; - PckAsset _asset; - - public LOCEditor(PckAsset asset) + public LOCEditor(LOCFile locFile, ISaveContext context) + : base(locFile, context) { InitializeComponent(); - _asset = asset; - _currentLoc = asset.GetData(new LOCFileReader()); - saveToolStripMenuItem.Visible = !Settings.Default.AutoSaveChanges; + saveToolStripMenuItem.Visible = !context.AutoSave; } private void LOCEditor_Load(object sender, EventArgs e) { RPC.SetPresence("LOC Editor", "Editing localization File."); - foreach(string locKey in _currentLoc.LocKeys.Keys) + foreach(string locKey in EditorValue.LocKeys.Keys) treeViewLocKeys.Nodes.Add(locKey); } @@ -39,7 +33,7 @@ namespace PckStudio.Forms.Editor { TreeNode node = e.Node; if (node == null || - !_currentLoc.LocKeys.ContainsKey(node.Text)) + !EditorValue.LocKeys.ContainsKey(node.Text)) { MessageBox.Show(this, "Selected Node does not seem to be in the loc file"); return; @@ -53,7 +47,7 @@ namespace PckStudio.Forms.Editor { prompt.OKButtonText = "Add"; if (prompt.ShowDialog(this) == DialogResult.OK && - _currentLoc.AddLocKey(prompt.NewText, "")) + EditorValue.AddLocKey(prompt.NewText, "")) { treeViewLocKeys.Nodes.Add(prompt.NewText); } @@ -62,7 +56,7 @@ namespace PckStudio.Forms.Editor private void deleteDisplayIDToolStripMenuItem_Click(object sender, EventArgs e) { - if (treeViewLocKeys.SelectedNode is TreeNode t && _currentLoc.RemoveLocKey(t.Text)) + if (treeViewLocKeys.SelectedNode is TreeNode t && EditorValue.RemoveLocKey(t.Text)) { treeViewLocKeys.SelectedNode.Remove(); ReloadTranslationTable(); @@ -81,7 +75,7 @@ namespace PckStudio.Forms.Editor string locKey = treeViewLocKeys.SelectedNode.Text; string language = row.Cells[0].Value.ToString(); string value = row.Cells[1].Value.ToString(); - _currentLoc.SetLocEntry(locKey, language, value); + EditorValue.SetLocEntry(locKey, language, value); } private void treeView1_KeyDown(object sender, KeyEventArgs e) @@ -97,7 +91,7 @@ namespace PckStudio.Forms.Editor dataGridViewLocEntryData.Rows[i].Cells[1].Value = textBoxReplaceAll.Text; } - _currentLoc.SetLocEntry(treeViewLocKeys.SelectedNode.Text, textBoxReplaceAll.Text); + EditorValue.SetLocEntry(treeViewLocKeys.SelectedNode.Text, textBoxReplaceAll.Text); } private void ReloadTranslationTable() @@ -105,7 +99,7 @@ namespace PckStudio.Forms.Editor dataGridViewLocEntryData.Rows.Clear(); if (treeViewLocKeys.SelectedNode is null) return; - foreach (KeyValuePair locEntry in _currentLoc.GetLocEntries(treeViewLocKeys.SelectedNode.Text)) + foreach (KeyValuePair locEntry in EditorValue.GetLocEntries(treeViewLocKeys.SelectedNode.Text)) dataGridViewLocEntryData.Rows.Add(locEntry.Key, locEntry.Value); } @@ -113,7 +107,7 @@ namespace PckStudio.Forms.Editor { foreach (var lang in LOCFile.ValidLanguages) { - if (_currentLoc.Languages.Contains(lang)) + if (EditorValue.Languages.Contains(lang)) continue; yield return lang; } @@ -126,14 +120,14 @@ namespace PckStudio.Forms.Editor using (var dialog = new AddLanguage(avalibleLang)) if (dialog.ShowDialog(this) == DialogResult.OK) { - _currentLoc.AddLanguage(dialog.SelectedLanguage); + EditorValue.AddLanguage(dialog.SelectedLanguage); ReloadTranslationTable(); } } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { - _asset.SetData(new LOCFileWriter(_currentLoc, 2)); + Save(); DialogResult = DialogResult.OK; } diff --git a/PCK-Studio/Forms/Editor/MaterialsEditor.cs b/PCK-Studio/Forms/Editor/MaterialsEditor.cs index 619c61f2..84e7cb30 100644 --- a/PCK-Studio/Forms/Editor/MaterialsEditor.cs +++ b/PCK-Studio/Forms/Editor/MaterialsEditor.cs @@ -1,94 +1,71 @@ using System; using System.Collections.Generic; -using System.Drawing; -using System.Diagnostics; -using System.IO; using System.Linq; using System.Windows.Forms; -using MetroFramework.Forms; -using Newtonsoft.Json.Linq; -using OMI.Formats.Pck; using OMI.Formats.Material; -using OMI.Workers.Material; using PckStudio.Internal; using PckStudio.Extensions; using PckStudio.Internal.Json; using PckStudio.Internal.App; +using PckStudio.Interfaces; namespace PckStudio.Forms.Editor { - public partial class MaterialsEditor : MetroForm + // Materials File Format research by PhoenixARC + public partial class MaterialsEditor : Editor { - // Materials File Format research by PhoenixARC - private readonly PckAsset _asset; - MaterialContainer _materialFile; - private readonly List MaterialData = Entities.BehaviourInfos; - private bool showInvalidEntries; + private bool ShowInvalidEntries; - //Holds invalid entries so they can be added back to the material file on save should the user decide to hide them - List hiddenInvalidEntries = new List(); + public MaterialsEditor(MaterialContainer materials, ISaveContext saveContext) + : base(materials, saveContext) + { + InitializeComponent(); + if (EditorValue.HasInvalidEntries()) + { + DialogResult dr = MessageBox.Show(this, "Unsupported entities were found in this file. Would you like to display them?", "Invalid data found", MessageBoxButtons.YesNo); - void SetUpTree() + ShowInvalidEntries = dr == DialogResult.Yes; + } + + treeView1.ImageList = new ImageList(); + ApplicationScope.EntityImages.ToList().ForEach(treeView1.ImageList.Images.Add); + treeView1.ImageList.ColorDepth = ColorDepth.Depth32Bit; + UpdateTreeview(); + } + + void UpdateTreeview() { treeView1.BeginUpdate(); treeView1.Nodes.Clear(); - foreach (MaterialContainer.Material entry in _materialFile) + foreach (MaterialContainer.Material entry in EditorValue) { - TreeNode EntryNode = new TreeNode(entry.Name); + TreeNode entryNode = new TreeNode(entry.Name); + // index for invalid entry + entryNode.ImageIndex = 127; + entryNode.SelectedImageIndex = 127; + entryNode.Tag = entry; EntityInfo material = MaterialData.Find(m => m.InternalName == entry.Name); - if(material != null) - { - EntryNode.Text = material.DisplayName; - EntryNode.ImageIndex = MaterialData.IndexOf(material); - EntryNode.Tag = entry; - } + // check for invalid material entry - else + if (material is null) { - EntryNode.ImageIndex = 127; // icon for invalid entry - EntryNode.Text += " (Invalid)"; - - if (!showInvalidEntries) - { - hiddenInvalidEntries.Add(entry); + entryNode.Text += " (Invalid)"; + if (!ShowInvalidEntries) continue; - } - } - - EntryNode.SelectedImageIndex = EntryNode.ImageIndex; - - treeView1.Nodes.Add(EntryNode); + } + else + { + entryNode.Text = material.DisplayName; + entryNode.SelectedImageIndex = entryNode.ImageIndex = MaterialData.IndexOf(material); + } + treeView1.Nodes.Add(entryNode); } treeView1.EndUpdate(); } - public MaterialsEditor(PckAsset asset) - { - InitializeComponent(); - _asset = asset; - - using (var stream = new MemoryStream(asset.Data)) - { - var reader = new MaterialFileReader(); - _materialFile = reader.FromStream(stream); - - if (_materialFile.hasInvalidEntries()) - { - DialogResult dr = MessageBox.Show(this, "Unsupported entities were found in this file. Would you like to display them?", "Invalid data found", MessageBoxButtons.YesNo); - - showInvalidEntries = dr == DialogResult.Yes; - } - - treeView1.ImageList = new ImageList(); - ApplicationScope.EntityImages.ToList().ForEach(treeView1.ImageList.Images.Add); - treeView1.ImageList.ColorDepth = ColorDepth.Depth32Bit; - SetUpTree(); - } - } - private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node == null) @@ -106,10 +83,11 @@ namespace PckStudio.Forms.Editor } private void removeToolStripMenuItem_Click(object sender, EventArgs e) { - if (treeView1.SelectedNode == null) - return; - - treeView1.SelectedNode.Remove(); + if (treeView1?.SelectedNode?.Tag is MaterialContainer.Material material) + { + EditorValue.Remove(material); + UpdateTreeview(); + } } private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) @@ -125,23 +103,7 @@ namespace PckStudio.Forms.Editor private void saveToolStripMenuItem1_Click(object sender, EventArgs e) { - _materialFile = new MaterialContainer(); - - foreach (TreeNode node in treeView1.Nodes) - { - if(node.Tag is MaterialContainer.Material entry) - { - _materialFile.Add(entry); - } - } - - foreach (MaterialContainer.Material mat in hiddenInvalidEntries) - { - _materialFile.Add(mat); - } - - _asset.SetData(new MaterialFileWriter(_materialFile)); - + Save(); DialogResult = DialogResult.OK; } @@ -153,21 +115,15 @@ namespace PckStudio.Forms.Editor { if (string.IsNullOrEmpty(diag.SelectedEntity)) return; - if (_materialFile.FindAll(mat => mat.Name == diag.SelectedEntity).Count() > 0) + if (EditorValue.FindAll(mat => mat.Name == diag.SelectedEntity).Count() > 0) { - MessageBox.Show(this, "You cannot have two entries for one entity. Please use the \"Add New Position Override\" tool to add multiple overrides for entities", "Error", MessageBoxButtons.OK); + MessageBox.Show(this, "You cannot have two entries for one entity.", "Error", MessageBoxButtons.OK); return; } var newEntry = new MaterialContainer.Material(diag.SelectedEntity, "entity_alphatest"); - TreeNode newEntryNode = new TreeNode(newEntry.Name); - newEntryNode.Tag = newEntry; - - EntityInfo material = MaterialData.Find(m => m.InternalName == newEntry.Name); - newEntryNode.Text = material.DisplayName; - newEntryNode.ImageIndex = MaterialData.IndexOf(material); - newEntryNode.SelectedImageIndex = newEntryNode.ImageIndex; - treeView1.Nodes.Add(newEntryNode); + EditorValue.Add(newEntry); + UpdateTreeview(); } } diff --git a/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs b/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs index 58865e18..ef1d23f4 100644 --- a/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs +++ b/PCK-Studio/Forms/Editor/TextureAtlasEditor.cs @@ -31,10 +31,12 @@ using OMI.Formats.Pck; using OMI.Workers.Color; using PckStudio.Extensions; +using PckStudio.Interfaces; using PckStudio.Internal; using PckStudio.Internal.Deserializer; using PckStudio.Internal.Json; using PckStudio.Internal.Serializer; +using PckStudio.Properties; namespace PckStudio.Forms.Editor { @@ -522,10 +524,14 @@ namespace PckStudio.Forms.Editor Animation animation = asset.GetDeserializedData(AnimationDeserializer.DefaultDeserializer); - var animationEditor = new AnimationEditor(animation, _selectedTile.Tile.DisplayName); + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (animation) => + { + asset.SetSerializedData(animation, AnimationSerializer.DefaultSerializer); + }); + + var animationEditor = new AnimationEditor(animation, saveContext, _selectedTile.Tile.DisplayName); if (animationEditor.ShowDialog(this) == DialogResult.OK) { - asset.SetSerializedData(animationEditor.Result, AnimationSerializer.DefaultSerializer); // so animations can automatically update upon saving SelectedIndex = _selectedTile.Index; } diff --git a/PCK-Studio/MainForm.cs b/PCK-Studio/MainForm.cs index 4d7517f6..ff7ee89f 100644 --- a/PCK-Studio/MainForm.cs +++ b/PCK-Studio/MainForm.cs @@ -9,6 +9,7 @@ using System.Diagnostics; using System.Drawing.Imaging; using System.Text.RegularExpressions; using OMI.Formats.Pck; +using OMI.Formats.Color; using OMI.Formats.Model; using OMI.Formats.GameRule; using OMI.Formats.Material; @@ -16,11 +17,12 @@ using OMI.Formats.Behaviour; using OMI.Formats.Languages; using OMI.Workers; using OMI.Workers.Pck; -using OMI.Workers.GameRule; -using OMI.Workers.Language; +using OMI.Workers.Color; using OMI.Workers.Model; -using OMI.Workers.Behaviour; +using OMI.Workers.GameRule; using OMI.Workers.Material; +using OMI.Workers.Language; +using OMI.Workers.Behaviour; using PckStudio.Properties; using PckStudio.Internal.FileFormats; using PckStudio.Forms; @@ -595,12 +597,17 @@ namespace PckStudio string displayname = textureInfos.FirstOrDefault(p => p.InternalName == internalName)?.DisplayName ?? internalName; string[] specialTileNames = { "clock", "compass" }; - using (AnimationEditor animationEditor = new AnimationEditor(animation, displayname, internalName.ToLower().EqualsAny(specialTileNames))) + + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (animation) => + { + asset.SetSerializedData(animation, AnimationSerializer.DefaultSerializer); + }); + + using (AnimationEditor animationEditor = new AnimationEditor(animation, saveContext, displayname, internalName.ToLower().EqualsAny(specialTileNames))) { if (animationEditor.ShowDialog(this) == DialogResult.OK) { wasModified = true; - asset.SetSerializedData(animationEditor.Result, AnimationSerializer.DefaultSerializer); BuildMainTreeView(); } } @@ -653,10 +660,14 @@ namespace PckStudio GameRuleFile grf = asset.GetData(new GameRuleFileReader(compressiontype)); - using GameRuleFileEditor grfEditor = new GameRuleFileEditor(grf); + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (gameRuleFile) => + { + asset.SetData(new GameRuleFileWriter(gameRuleFile)); + }); + + using GameRuleFileEditor grfEditor = new GameRuleFileEditor(grf, saveContext); if (grfEditor.ShowDialog(this) == DialogResult.OK) { - asset.SetData(new GameRuleFileWriter(grfEditor.Result)); wasModified = true; UpdateRichPresence(); } @@ -666,7 +677,13 @@ namespace PckStudio { try { - using AudioEditor audioEditor = new AudioEditor(asset, LittleEndianCheckBox.Checked); + OMI.Endianness endianness = LittleEndianCheckBox.Checked ? OMI.Endianness.LittleEndian : OMI.Endianness.BigEndian; + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (audioFile) => + { + asset.SetData(new PckAudioFileWriter(audioFile, endianness)); + }); + PckAudioFile audioFile = asset.GetData(new PckAudioFileReader(endianness)); + using AudioEditor audioEditor = new AudioEditor(audioFile, saveContext); wasModified = audioEditor.ShowDialog(this) == DialogResult.OK; } catch (OverflowException) @@ -684,14 +701,24 @@ namespace PckStudio private void HandleLocalisationFile(PckAsset asset) { - using LOCEditor locedit = new LOCEditor(asset); + LOCFile locFile = asset.GetData(new LOCFileReader()); + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (locFile) => + { + asset.SetData(new LOCFileWriter(locFile, 2)); + }); + using LOCEditor locedit = new LOCEditor(locFile, saveContext); wasModified = locedit.ShowDialog(this) == DialogResult.OK; UpdateRichPresence(); } private void HandleColourFile(PckAsset asset) { - using COLEditor diag = new COLEditor(asset); + ColorContainer colorContainer = asset.GetData(new COLFileReader()); + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (colorContainer) => + { + asset.SetData(new COLFileWriter(colorContainer)); + }); + using COLEditor diag = new COLEditor(colorContainer, saveContext); wasModified = diag.ShowDialog(this) == DialogResult.OK; } @@ -764,13 +791,23 @@ namespace PckStudio public void HandleBehavioursFile(PckAsset asset) { - using BehaviourEditor edit = new BehaviourEditor(asset); + BehaviourFile behaviourFile = asset.GetData(new BehavioursReader()); + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (behaviourFile) => + { + asset.SetData(new BehavioursWriter(behaviourFile)); + }); + using BehaviourEditor edit = new BehaviourEditor(behaviourFile, saveContext); wasModified = edit.ShowDialog(this) == DialogResult.OK; } public void HandleMaterialFile(PckAsset asset) { - using MaterialsEditor edit = new MaterialsEditor(asset); + MaterialContainer materials = asset.GetData(new MaterialFileReader()); + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (materials) => + { + asset.SetData(new MaterialFileWriter(materials)); + }); + using MaterialsEditor edit = new MaterialsEditor(materials, saveContext); wasModified = edit.ShowDialog(this) == DialogResult.OK; } @@ -1162,17 +1199,22 @@ namespace PckStudio } } - private static PckAsset CreateNewAudioFile(bool isLittle) + private static PckAsset CreateNewAudioAsset(bool isLittle, PckAudioFile audioFile) { - PckAudioFile audioPck = new PckAudioFile(); - audioPck.AddCategory(PckAudioFile.AudioCategory.EAudioType.Overworld); - audioPck.AddCategory(PckAudioFile.AudioCategory.EAudioType.Nether); - audioPck.AddCategory(PckAudioFile.AudioCategory.EAudioType.End); PckAsset newAsset = new PckAsset("audio.pck", PckAssetType.AudioFile); - newAsset.SetData(new PckAudioFileWriter(audioPck, isLittle ? OMI.Endianness.LittleEndian : OMI.Endianness.BigEndian)); + newAsset.SetData(new PckAudioFileWriter(audioFile, isLittle ? OMI.Endianness.LittleEndian : OMI.Endianness.BigEndian)); return newAsset; } + private static PckAudioFile CreateNewAudioFile() + { + PckAudioFile audioFile = new PckAudioFile(); + audioFile.AddCategory(PckAudioFile.AudioCategory.EAudioType.Overworld); + audioFile.AddCategory(PckAudioFile.AudioCategory.EAudioType.Nether); + audioFile.AddCategory(PckAudioFile.AudioCategory.EAudioType.End); + return audioFile; + } + private void audiopckToolStripMenuItem_Click(object sender, EventArgs e) { if (currentPCK.Contains(PckAssetType.AudioFile)) @@ -1187,11 +1229,18 @@ namespace PckStudio return; } - PckAsset asset = CreateNewAudioFile(LittleEndianCheckBox.Checked); - AudioEditor diag = new AudioEditor(asset, LittleEndianCheckBox.Checked); + PckAudioFile newAudioFile = CreateNewAudioFile(); + PckAsset newAudioAsset = CreateNewAudioAsset(LittleEndianCheckBox.Checked, newAudioFile); + + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (audioFile) => + { + newAudioAsset.SetData(new PckAudioFileWriter(audioFile, LittleEndianCheckBox.Checked ? OMI.Endianness.LittleEndian : OMI.Endianness.BigEndian)); + }); + + AudioEditor diag = new AudioEditor(newAudioFile, saveContext); if (diag.ShowDialog(this) == DialogResult.OK) { - currentPCK.AddAsset(asset); + currentPCK.AddAsset(newAudioAsset); } diag.Dispose(); BuildMainTreeView(); @@ -1211,12 +1260,18 @@ namespace PckStudio return; } - using AnimationEditor animationEditor = new AnimationEditor(Animation.CreateEmpty(), diag.SelectedTile.DisplayName, diag.SelectedTile.InternalName.EqualsAny("clock", "compass")); - if (animationEditor.ShowDialog() == DialogResult.OK) + Animation newAnimation = default; + DelegatedSaveContext saveContext = new DelegatedSaveContext(Settings.Default.AutoSaveChanges, (animation) => + { + newAnimation = animation; + }); + + using AnimationEditor animationEditor = new AnimationEditor(Animation.CreateEmpty(), saveContext, diag.SelectedTile.DisplayName, diag.SelectedTile.InternalName.EqualsAny("clock", "compass")); + if (animationEditor.ShowDialog() == DialogResult.OK && newAnimation is not null) { wasModified = true; PckAsset asset = currentPCK.CreateNewAsset(animationFilepath, PckAssetType.TextureFile); - asset.SetSerializedData(animationEditor.Result, AnimationSerializer.DefaultSerializer); + asset.SetSerializedData(newAnimation, AnimationSerializer.DefaultSerializer); BuildMainTreeView(); ReloadMetaTreeView(); }