Added MaterialsEditor

This commit is contained in:
MattNL
2023-03-04 10:40:58 -05:00
parent a75046a856
commit fb55adee7f
5 changed files with 514 additions and 7 deletions

View File

@@ -0,0 +1,185 @@
namespace PckStudio.Forms.Editor
{
partial class MaterialsEditor
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MaterialsEditor));
this.treeView1 = new System.Windows.Forms.TreeView();
this.metroContextMenu1 = new MetroFramework.Controls.MetroContextMenu(this.components);
this.addToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.xLabel = new MetroFramework.Controls.MetroLabel();
this.materialComboBox = new MetroFramework.Controls.MetroComboBox();
this.metroContextMenu1.SuspendLayout();
this.menuStrip.SuspendLayout();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.treeView1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.treeView1.ContextMenuStrip = this.metroContextMenu1;
this.treeView1.ForeColor = System.Drawing.Color.White;
this.treeView1.Location = new System.Drawing.Point(20, 84);
this.treeView1.Margin = new System.Windows.Forms.Padding(0);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(136, 176);
this.treeView1.TabIndex = 13;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
this.treeView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.treeView1_KeyDown);
this.treeView1.MouseHover += new System.EventHandler(this.treeView1_MouseHover);
//
// metroContextMenu1
//
this.metroContextMenu1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.addToolStripMenuItem,
this.removeToolStripMenuItem});
this.metroContextMenu1.Name = "metroContextMenu1";
this.metroContextMenu1.Size = new System.Drawing.Size(127, 48);
//
// addToolStripMenuItem
//
this.addToolStripMenuItem.Name = "addToolStripMenuItem";
this.addToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.addToolStripMenuItem.Text = "Add Entry";
this.addToolStripMenuItem.Click += new System.EventHandler(this.addToolStripMenuItem_Click);
//
// removeToolStripMenuItem
//
this.removeToolStripMenuItem.Name = "removeToolStripMenuItem";
this.removeToolStripMenuItem.Size = new System.Drawing.Size(180, 22);
this.removeToolStripMenuItem.Text = "Remove";
this.removeToolStripMenuItem.Click += new System.EventHandler(this.removeToolStripMenuItem_Click);
//
// menuStrip
//
this.menuStrip.AutoSize = false;
this.menuStrip.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
this.menuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem,
this.helpToolStripMenuItem});
this.menuStrip.Location = new System.Drawing.Point(20, 60);
this.menuStrip.Name = "menuStrip";
this.menuStrip.Size = new System.Drawing.Size(348, 24);
this.menuStrip.TabIndex = 14;
this.menuStrip.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.saveToolStripMenuItem1});
this.fileToolStripMenuItem.ForeColor = System.Drawing.Color.White;
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Text = "File";
//
// saveToolStripMenuItem1
//
this.saveToolStripMenuItem1.Image = ((System.Drawing.Image)(resources.GetObject("saveToolStripMenuItem1.Image")));
this.saveToolStripMenuItem1.Name = "saveToolStripMenuItem1";
this.saveToolStripMenuItem1.Size = new System.Drawing.Size(98, 22);
this.saveToolStripMenuItem1.Text = "Save";
this.saveToolStripMenuItem1.Click += new System.EventHandler(this.saveToolStripMenuItem1_Click);
//
// helpToolStripMenuItem
//
this.helpToolStripMenuItem.ForeColor = System.Drawing.Color.White;
this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
this.helpToolStripMenuItem.Text = "Help";
//
// xLabel
//
this.xLabel.AutoSize = true;
this.xLabel.Location = new System.Drawing.Point(159, 147);
this.xLabel.Name = "xLabel";
this.xLabel.Size = new System.Drawing.Size(91, 19);
this.xLabel.TabIndex = 30;
this.xLabel.Text = "Material Type:";
this.xLabel.Theme = MetroFramework.MetroThemeStyle.Dark;
//
// materialComboBox
//
this.materialComboBox.FormattingEnabled = true;
this.materialComboBox.ItemHeight = 23;
this.materialComboBox.Items.AddRange(new object[] {
"entity_alphatest",
"entity_emissive_alpha",
"entity_emissive_alpha_only",
"entity_alphatest_change_color",
"entity_change_color"});
this.materialComboBox.Location = new System.Drawing.Point(159, 169);
this.materialComboBox.Name = "materialComboBox";
this.materialComboBox.Size = new System.Drawing.Size(209, 29);
this.materialComboBox.TabIndex = 31;
this.materialComboBox.Theme = MetroFramework.MetroThemeStyle.Dark;
this.materialComboBox.UseSelectable = true;
this.materialComboBox.SelectedIndexChanged += new System.EventHandler(this.materialComboBox_SelectedIndexChanged);
//
// MaterialsEditor
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(388, 280);
this.Controls.Add(this.materialComboBox);
this.Controls.Add(this.xLabel);
this.Controls.Add(this.menuStrip);
this.Controls.Add(this.treeView1);
this.Name = "MaterialsEditor";
this.Style = MetroFramework.MetroColorStyle.Silver;
this.Text = "Materials Editor";
this.Theme = MetroFramework.MetroThemeStyle.Dark;
this.metroContextMenu1.ResumeLayout(false);
this.menuStrip.ResumeLayout(false);
this.menuStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.MenuStrip menuStrip;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem;
private MetroFramework.Controls.MetroLabel xLabel;
private MetroFramework.Controls.MetroContextMenu metroContextMenu1;
private System.Windows.Forms.ToolStripMenuItem addToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem removeToolStripMenuItem;
private MetroFramework.Controls.MetroComboBox materialComboBox;
}
}

View File

@@ -0,0 +1,175 @@
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 PckStudio.Classes.FileTypes;
using PckStudio.Classes.IO.Materials;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace PckStudio.Forms.Editor
{
public partial class MaterialsEditor : MetroForm
{
// Behaviours File Format research by Miku and MattNL
private readonly PCKFile.FileData _file;
MaterialsFile materialFile;
void SetUpTree()
{
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
foreach (var entry in materialFile.entries)
{
TreeNode EntryNode = new TreeNode(entry.name);
foreach (JObject content in Utilities.MaterialUtil.entityData["entities"].Children())
{
var prop = content.Properties().FirstOrDefault(prop => prop.Name == entry.name);
if (prop is JProperty)
{
EntryNode.Text = (string)prop.Value;
EntryNode.ImageIndex = Utilities.MaterialUtil.entityData["entities"].Children().ToList().IndexOf(content);
EntryNode.SelectedImageIndex = EntryNode.ImageIndex;
break;
}
}
EntryNode.Tag = entry;
treeView1.Nodes.Add(EntryNode);
}
treeView1.EndUpdate();
}
public MaterialsEditor(PCKFile.FileData file)
{
InitializeComponent();
_file = file;
using (var stream = new MemoryStream(file.Data))
{
materialFile = MaterialsReader.Read(stream);
}
treeView1.ImageList = new ImageList();
Utilities.MaterialUtil.entityImages.ToList().ForEach(img => treeView1.ImageList.Images.Add(img));
treeView1.ImageList.ColorDepth = ColorDepth.Depth32Bit;
SetUpTree();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if (e.Node == null) return;
bool enable = e.Node.Tag is MaterialsFile.MaterialEntry && treeView1.SelectedNode != null;
materialComboBox.Enabled = enable;
if (e.Node.Tag is MaterialsFile.MaterialEntry entry)
{
materialComboBox.SelectedIndexChanged -= materialComboBox_SelectedIndexChanged;
materialComboBox.SelectedIndex = materialComboBox.Items.IndexOf(entry.material_type);
materialComboBox.SelectedIndexChanged += materialComboBox_SelectedIndexChanged;
}
}
private void removeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (treeView1.SelectedNode == null) return;
treeView1.SelectedNode.Remove();
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
materialComboBox.Enabled = false;
}
private void addNewPositionOverrideToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void addNewEntryToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void treeView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete) removeToolStripMenuItem_Click(sender, e);
}
private void treeView1_MouseHover(object sender, EventArgs e)
{
}
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
using (var stream = new MemoryStream())
{
materialFile = new MaterialsFile();
foreach (TreeNode node in treeView1.Nodes)
{
if(node.Tag is MaterialsFile.MaterialEntry entry)
{
materialFile.entries.Add(entry);
}
}
MaterialsWriter.Write(stream, materialFile);
_file.SetData(stream.ToArray());
}
DialogResult = DialogResult.OK;
}
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
var diag = new Additional_Popups.EntityForms.AddEntry(Utilities.MaterialUtil.entityData, Utilities.MaterialUtil.entityImages);
if (diag.ShowDialog() == DialogResult.OK)
{
if (String.IsNullOrEmpty(diag.SelectedEntity)) return;
if (materialFile.entries.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);
return;
}
MaterialsFile.MaterialEntry NewEntry = new MaterialsFile.MaterialEntry();
NewEntry.name = diag.SelectedEntity;
NewEntry.material_type = "entity_alphatest";
TreeNode NewEntryNode = new TreeNode(NewEntry.name);
NewEntryNode.Tag = NewEntry;
foreach (JObject content in Utilities.MaterialUtil.entityData["entities"].Children())
{
var prop = content.Properties().FirstOrDefault(prop => prop.Name == NewEntry.name);
if (prop is JProperty)
{
NewEntryNode.Text = (string)prop.Value;
NewEntryNode.ImageIndex = Utilities.MaterialUtil.entityData["entities"].Children().ToList().IndexOf(content);
NewEntryNode.SelectedImageIndex = NewEntryNode.ImageIndex;
break;
}
}
treeView1.Nodes.Add(NewEntryNode);
addNewPositionOverrideToolStripMenuItem_Click(sender, e); // adds a Position Override to the new Override
}
}
private void materialComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (treeView1.SelectedNode.Tag is MaterialsFile.MaterialEntry entry)
{
entry.material_type = materialComboBox.SelectedItem.ToString();
treeView1.SelectedNode.Tag = entry;
}
}
}
}

View File

@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="metroContextMenu1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>125, 17</value>
</metadata>
<metadata name="menuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="saveToolStripMenuItem1.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO
vAAADrwBlbxySQAAABl0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC4yMfEgaZUAAADfSURBVDhPYxg8
QLt++3yTGbf/Fm599P/Nh49wfPXxq/+rTt37f+Dak/8gOSBgAGEMANIMxGBFyAasPf/0v8GE8//z1t8C
y4HU4DIALIluwLpLL+HiMANAGKoNAWASCavv/n/57gPcgOvP3oENOXj7NViOoAFGU6791+k4ghWD5Aga
QCyGakMAkODcU89R/I8Ng9TgNADk14dPn/8/c+kqVgySgwUqVBsCwAx49urN/zsPHmPFIDmaGvAXJInN
38gYasBfqDYE0K7dOn/Wvut/sfkdGYPUgJI9VNuAAwYGAGn6yvdevWgPAAAAAElFTkSuQmCC
</value>
</data>
</root>

View File

@@ -87,7 +87,7 @@ namespace PckStudio
[PCKFile.FileData.FileType.SkinDataFile] = null,
[PCKFile.FileData.FileType.ModelsFile] = HandleModelsFile,
[PCKFile.FileData.FileType.BehavioursFile] = HandleBehavioursFile,
[PCKFile.FileData.FileType.MaterialFile] = null,
[PCKFile.FileData.FileType.MaterialFile] = HandleMaterialFile,
};
}
@@ -450,19 +450,20 @@ namespace PckStudio
}
}
}
public void HandleModelsFile(PCKFile.FileData file)
{
MessageBox.Show("Models.bin support has not been implemented. You can use the Spark Editor for the time being to edit these files.", "Not implemented yet.");
//throw new NotImplementedException();
}
public void HandleBehavioursFile(PCKFile.FileData file)
{
using BehaviourEditor locedit = new BehaviourEditor(file);
wasModified = locedit.ShowDialog(this) == DialogResult.OK;
using BehaviourEditor edit = new BehaviourEditor(file);
wasModified = edit.ShowDialog(this) == DialogResult.OK;
}
public void HandleMaterialFile(PCKFile.FileData file)
{
using MaterialsEditor edit = new MaterialsEditor(file);
wasModified = edit.ShowDialog(this) == DialogResult.OK;
}
private void selectNode(object sender, TreeViewEventArgs e)
{
ReloadMetaTreeView();

View File

@@ -285,6 +285,12 @@
<Compile Include="Forms\Additional-Popups\TextPrompt.Designer.cs">
<DependentUpon>TextPrompt.cs</DependentUpon>
</Compile>
<Compile Include="Forms\Editor\MaterialsEditor.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Forms\Editor\MaterialsEditor.Designer.cs">
<DependentUpon>MaterialsEditor.cs</DependentUpon>
</Compile>
<Compile Include="Forms\Editor\BehaviourEditor.cs">
<SubType>Form</SubType>
</Compile>
@@ -513,6 +519,9 @@
<EmbeddedResource Include="Forms\Additional-Popups\TextPrompt.resx">
<DependentUpon>TextPrompt.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\Editor\MaterialsEditor.resx">
<DependentUpon>MaterialsEditor.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Forms\Editor\BehaviourEditor.resx">
<DependentUpon>BehaviourEditor.cs</DependentUpon>
</EmbeddedResource>