Skip to content

Commit

Permalink
[HaRepacker, MapleLib] remove forced sorting of WzSubProperties; righ…
Browse files Browse the repository at this point in the history
…t click on nodes to sort manually.

ref: #217
  • Loading branch information
lastbattle committed Sep 28, 2023
1 parent b89febb commit 1356afc
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 58 deletions.
68 changes: 40 additions & 28 deletions HaRepacker/GUI/ContextMenuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@
using HaRepacker.GUI;
using HaRepacker.GUI.Panels;
using HaRepacker.GUI.Input;
using System.Diagnostics;

namespace HaRepacker
{
public class ContextMenuManager
{
private MainPanel parentPanel;

public ContextMenuStrip WzFileMenu;
public ContextMenuStrip WzDirectoryMenu;
public ContextMenuStrip PropertyContainerMenu;
public ContextMenuStrip SubPropertyMenu;
public ContextMenuStrip PropertyMenu;

private ToolStripMenuItem SaveFile;
private ToolStripMenuItem Remove;
private ToolStripMenuItem Unload;
private ToolStripMenuItem Reload;
private ToolStripMenuItem CollapseAllChildNode;
private ToolStripMenuItem ExpandAllChildNode;
private ToolStripMenuItem SortAllChildNode;
private ToolStripMenuItem SortAllChildViewNode, SortAllChildViewNode2;
private ToolStripMenuItem SortPropertiesByName;

private ToolStripMenuItem AddPropsSubMenu;
private ToolStripMenuItem AddDirsSubMenu;
private ToolStripMenuItem AddConvexSubMenu;
private ToolStripMenuItem AddEtcMenu;
private ToolStripMenuItem AddSortMenu;
private ToolStripMenuItem AddSortMenu_WithoutPropSort;
private ToolStripMenuItem AddImage;
private ToolStripMenuItem AddDirectory;
private ToolStripMenuItem AddByteFloat;
Expand Down Expand Up @@ -138,14 +136,28 @@ public ContextMenuManager(MainPanel haRepackerMainPanel, UndoRedoManager undoMan
node.ExpandAll();
}
}));
SortAllChildNode = new ToolStripMenuItem("Sort child nodes", Properties.Resources.sort, new EventHandler(

// This only sorts the view, does not affect the actual order of the
// wz properties
SortAllChildViewNode = new ToolStripMenuItem("Sort child nodes view", null, new EventHandler( // SortAllChildViewNode cant be in 2 place at once, gotta make copies
delegate (object sender, EventArgs e)
{
foreach (WzNode node in GetNodes(sender))
{
foreach (WzNode node in GetNodes(sender)) {
parentPanel.MainForm.SortNodesRecursively(node, true);
}
}));
SortAllChildViewNode2 = new ToolStripMenuItem("Sort child nodes view", null, new EventHandler( // SortAllChildViewNode cant be in 2 place at once, gotta make copies
delegate (object sender, EventArgs e) {
foreach (WzNode node in GetNodes(sender)) {
parentPanel.MainForm.SortNodesRecursively(node, true);
}
}));
SortPropertiesByName = new ToolStripMenuItem("Sort properties by name", null, new EventHandler(
delegate (object sender, EventArgs e) {
foreach (WzNode node in GetNodes(sender)) {
parentPanel.MainForm.SortNodeProperties(node);
}
}));

AddImage = new ToolStripMenuItem("Image", null, new EventHandler(
delegate (object sender, EventArgs e)
Expand Down Expand Up @@ -330,27 +342,22 @@ public ContextMenuManager(MainPanel haRepackerMainPanel, UndoRedoManager undoMan
FixLink = new ToolStripMenuItem("Fix linked image for old MapleStory ver.", null, new EventHandler(
delegate (object sender, EventArgs e)
{
haRepackerMainPanel.FixLinkForOldMS_Click();
haRepackerMainPanel.FixLinkForOldMapleStory_OnClick();
}));

AddConvexSubMenu = new ToolStripMenuItem("Add", Properties.Resources.add, AddVector);
AddDirsSubMenu = new ToolStripMenuItem("Add", Properties.Resources.add, AddDirectory, AddImage);
AddPropsSubMenu = new ToolStripMenuItem("Add", Properties.Resources.add, AddCanvas, AddConvex, AddDouble, AddByteFloat, AddLong, AddInt, AddNull, AddUshort, AddSound, AddString, AddSub, AddUOL, AddVector);
AddDirsSubMenu = new ToolStripMenuItem("Add", Properties.Resources.add,
AddDirectory, AddImage);

WzFileMenu = new ContextMenuStrip();
WzFileMenu.Items.AddRange(new ToolStripItem[] { AddDirsSubMenu, SaveFile, Unload, Reload });
AddPropsSubMenu = new ToolStripMenuItem("Add", Properties.Resources.add,
AddCanvas, AddConvex, AddDouble, AddByteFloat, AddLong, AddInt, AddNull, AddUshort, AddSound, AddString, AddSub, AddUOL, AddVector);

WzDirectoryMenu = new ContextMenuStrip();
WzDirectoryMenu.Items.AddRange(new ToolStripItem[] { AddDirsSubMenu, Rename, /*export, import,*/Remove });
AddEtcMenu = new ToolStripMenuItem("Etc", Properties.Resources.add,
FixLink);

PropertyContainerMenu = new ContextMenuStrip();
PropertyContainerMenu.Items.AddRange(new ToolStripItem[] { AddPropsSubMenu, Rename, /*export, import,*/Remove });
AddSortMenu = new ToolStripMenuItem("Sort", Properties.Resources.sort, SortAllChildViewNode, SortPropertiesByName);

PropertyMenu = new ContextMenuStrip();
PropertyMenu.Items.AddRange(new ToolStripItem[] { Rename, /*export, import,*/Remove });

SubPropertyMenu = new ContextMenuStrip();
SubPropertyMenu.Items.AddRange(new ToolStripItem[] { AddPropsSubMenu, Rename, /*export, import,*/Remove });
Debug.WriteLine(AddSortMenu.DropDown.Items.Count.ToString());
AddSortMenu_WithoutPropSort = new ToolStripMenuItem("Sort", Properties.Resources.sort, SortAllChildViewNode2);
}

/// <summary>
Expand Down Expand Up @@ -393,10 +400,15 @@ public ContextMenuStrip CreateMenu(WzNode node, WzObject Tag)
toolStripmenuItems.Add(Reload);
}

toolStripmenuItems.Add(FixLink);
toolStripmenuItems.Add(ExpandAllChildNode);
toolStripmenuItems.Add(CollapseAllChildNode);
toolStripmenuItems.Add(SortAllChildNode);
toolStripmenuItems.Add(AddEtcMenu);

if (Tag.GetType() == typeof(WzSubProperty)) {
toolStripmenuItems.Add(AddSortMenu);
} else {
toolStripmenuItems.Add(AddSortMenu_WithoutPropSort);
}

// Add
foreach (ToolStripItem toolStripItem in toolStripmenuItems)
Expand Down
20 changes: 20 additions & 0 deletions HaRepacker/GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
using HaRepacker.Comparer;

using HaSharedLibrary;
using MapleLib.WzLib.WzProperties;

namespace HaRepacker.GUI
{
Expand Down Expand Up @@ -203,6 +204,25 @@ public void SortNodesRecursively(WzNode parent, bool sortFromTheParentNode = fal
}
}

public void SortNodeProperties(WzNode node) {
if (node.Tag is WzSubProperty) {
WzNode nodeParent = (WzNode) node.Parent;

nodeParent.TreeView.BeginUpdate();

// sort the order in the WzSubProperty
WzSubProperty subProperties = (node.Tag as WzSubProperty);
subProperties.SortProperties();

// Refresh the TreeView view to be in synchronized with the new WzSubProperty's order
WzNode newNode = new WzNode(subProperties, true);
nodeParent.Nodes[node.Index] = newNode;
nodeParent.Nodes.Remove(node);

nodeParent.TreeView.EndUpdate();
}
}

/// <summary>
/// Insert the WZ file to the main panel UI
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion HaRepacker/GUI/Panels/MainPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ private void CheckImageNodeRecursively(WzNode node)
/// <summary>
/// Fix the '_inlink' and '_outlink' image property for compatibility to old MapleStory ver.
/// </summary>
public void FixLinkForOldMS_Click()
public void FixLinkForOldMapleStory_OnClick()
{
// handle multiple nodes...
int nodeCount = DataTree.SelectedNodes.Count;
Expand Down
62 changes: 33 additions & 29 deletions MapleLib/WzLib/WzProperties/WzSubProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,41 +126,15 @@ public override WzImageProperty GetFromPath(string path)
/// <param name="writer"></param>
public override void WriteValue(WzBinaryWriter writer)
{
bool bIsLuaProperty = false;
if (properties.Count == 1 && properties[0] is WzLuaProperty)
{
bIsLuaProperty = true;
}
bool bIsLuaProperty = properties.Count == 1 && properties[0] is WzLuaProperty;

if (!bIsLuaProperty)
writer.WriteStringValue("Property", WzImage.WzImageHeaderByte_WithoutOffset, WzImage.WzImageHeaderByte_WithOffset);

// Sort WzCanvasPropertys' in images by their name
// see https://github.com/lastbattle/Harepacker-resurrected/issues/173
properties.Sort((img1, img2) =>
{
if (img1 == null)
return 0;
else if (img1.GetType() == typeof(WzCanvasProperty) || // frames
img1.GetType() == typeof(WzSubProperty)) // footholds
{
int nodeId1, nodeId2;
if (int.TryParse(img1.Name, out nodeId1) && int.TryParse(img2.Name, out nodeId2))
{
if (nodeId1 == nodeId2)
return 0;
if (nodeId1 > nodeId2)
return 1;
return -1;
} else // default to string compare
return img1.Name.CompareTo(img2.Name);
}
else
return img1.Name.CompareTo(img2.Name); // (leave non canvas nodes at the very bottom, i.e "info")
});

WzImageProperty.WritePropertyList(writer, properties);
}


/// <summary>
/// Exports as XML
/// </summary>
Expand Down Expand Up @@ -242,6 +216,36 @@ public void ClearProperties()
foreach (WzImageProperty prop in properties) prop.Parent = null;
properties.Clear();
}

/// <summary>
/// Sort the properties by its its number, then alphabetical order.
/// i.e 1,2,3,4,5,6,7,8,9,10,11,a,b,c,d,e,f
/// </summary>
public void SortProperties()
{
// Sort WzCanvasPropertys' in images by their name
// see https://github.com/lastbattle/Harepacker-resurrected/issues/173
properties.Sort((img1, img2) => {
if (img1 == null)
return 0;
else if (img1.GetType() == typeof(WzCanvasProperty) || // frames
img1.GetType() == typeof(WzSubProperty)) // footholds
{
int nodeId1, nodeId2;
if (int.TryParse(img1.Name, out nodeId1) && int.TryParse(img2.Name, out nodeId2)) {
if (nodeId1 == nodeId2)
return 0;
if (nodeId1 > nodeId2)
return 1;
return -1;
}
else // default to string compare
return img1.Name.CompareTo(img2.Name);
}
else
return img1.Name.CompareTo(img2.Name); // (leave non canvas nodes at the very bottom, i.e "info")
});
}
#endregion
}
}

0 comments on commit 1356afc

Please sign in to comment.