Suggestion: Add more sorting options to OdinMenuTree

Issue #424 resolved
Paddy Otterness created an issue

Hello,

My designer has been bugging me to add some way to sort RPG items and characters by their member variables within our editors. Currently, the only sorting I've seen usable is OdinMenuTree.SortMenuItemsByName(), which is nice for a lot of cases, but our team needs a bit more control while iterating over RPG items & such.

Initially I reached for a lambda expression like what follows:

myList.Sort(characters, c => c.Strength, SortOrder.Ascending);

using the same pattern as in this article https://www.c-sharpcorner.com/UploadFile/afenster/lambda-expressions-are-wonderful/, but quickly I realized that OdinMenuTree.AddAllAssetsAtPath wouldn't afford me the control I need over the lists as it just kinda dumps everything in within the path.

Would it be possible to control the order of assets added to a tree or subtree by perhaps adding another overload to OdinMenuTree.AddAllAssetsAtPath, precisely at that step? Or is there a workaround I'm not currently seeing with OdinMenuItem?

Thanks.

Comments (3)

  1. Bjarke Elias

    That should all be possible, everything should be exposed in order for you to do it a manually :)

    In its basic form, OdinMenuItems just looks like this: so from that, you can easily just sort the list of ChildMenuItems yourself.

    class OdinMenuItem
    {
        public object Value;
        List<OdinMenuItem> ChildMenuItems;
    }
    

    And calling AddAllAssetsAtPath does what you would expect, but it also returns all menu items it added. Which means you can do:

    IEnumerable<OdinMenuItem> query = tree.AddAllAssetsAtPath(...); // This will enumerate through all added menu items.
    IEnumerable<OdinMenuItem> query = tree.EnumerateTree(true); // You can also enumerate though all menu items in the tree, inlcuding the root node.
    IEnumerable<OdinMenuItem> query = someMenuItem.GetChildMenuItemsRecursive(true); // This will enumerate recursively though all menuitems of a single menu
    
    // etc..
    // When you've specified what exactly which items or item so sort, then you can just sort the List<OdinMenuItem> your self by manually calling Sort()
    
    Comparison<OdinMenuItem> comparer = (a, b) =>
    {
        if (!(a.Value is MyType && v.Value is MyType)) return 0;
        return a.Value.Somthing.CompareTo(b.Value.Something);
    }
    
    query.ForEach(x => x.ChildMenuItems.Sort(comparer))
    tree.MarkDirty();
    
  2. Paddy Otterness reporter

    That should all be possible, everything should be exposed in order for you to do it a manually :)

    Thanks so much for the clarification!

  3. Log in to comment