1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00

Advanced Generic menu that can be searched

This commit is contained in:
Kailey Joanette 2021-12-25 13:07:43 -05:00
parent 286b9a167a
commit 3666509002
7 changed files with 232 additions and 0 deletions

View File

@ -0,0 +1,206 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using static UnityEditor.GenericMenu;
namespace XNodeEditor
{
public class AdvancedGenericMenu : AdvancedDropdown
{
private class AdvancedGenericMenuItem : AdvancedDropdownItem
{
private MenuFunction func;
private MenuFunction2 func2;
private object userData;
public AdvancedGenericMenuItem( string name ) : base( name )
{
}
public AdvancedGenericMenuItem( string name, bool enabled, Texture2D icon, MenuFunction func ) : base( name )
{
Set( enabled, icon, func );
}
public AdvancedGenericMenuItem( string name, bool enabled, Texture2D icon, MenuFunction2 func, object userData ) : base( name )
{
Set( enabled, icon, func, userData );
}
public void Set( bool enabled, Texture2D icon, MenuFunction func )
{
this.enabled = enabled;
this.icon = icon;
this.func = func;
}
public void Set( bool enabled, Texture2D icon, MenuFunction2 func, object userData )
{
this.enabled = enabled;
this.icon = icon;
this.func2 = func;
this.userData = userData;
}
public void Run()
{
if ( func2 != null )
func2( userData );
else if ( func != null )
func();
}
}
private List<AdvancedGenericMenuItem> items = new List<AdvancedGenericMenuItem>();
private AdvancedGenericMenuItem FindOrCreateItem( string name, AdvancedGenericMenuItem currentRoot = null )
{
if ( string.IsNullOrWhiteSpace( name ) )
return null;
AdvancedGenericMenuItem item = null;
string[] paths = name.Split( '/' );
if ( currentRoot == null )
{
item = items.FirstOrDefault( x => x != null && x.name == paths[0] );
if ( item == null )
items.Add( item = new AdvancedGenericMenuItem( name ) );
}
else
{
item = currentRoot.children.OfType<AdvancedGenericMenuItem>().FirstOrDefault( x => x.name == paths[0] );
if ( item == null )
currentRoot.AddChild( item = new AdvancedGenericMenuItem( name ) );
}
if ( paths.Length > 1 )
return FindOrCreateItem( string.Join( "/", paths, 1, paths.Length - 1 ), item );
return item;
}
private AdvancedGenericMenuItem FindParent( string name )
{
string[] paths = name.Split( '/' );
return FindOrCreateItem( string.Join( "/", paths, 0, paths.Length - 1 ) );
}
private string Name { get; set; }
public AdvancedGenericMenu() : base( new AdvancedDropdownState() )
{
Name = "";
}
public AdvancedGenericMenu( string name, AdvancedDropdownState state ) : base( state )
{
Name = name;
}
//
// Summary:
// Add a disabled item to the menu.
//
// Parameters:
// content:
// The GUIContent to display as a disabled menu item.
public void AddDisabledItem( GUIContent content )
{
//var parent = FindParent( content.text );
var item = FindOrCreateItem( content.text );
item.Set( false, null, null );
}
//
// Summary:
// Add a disabled item to the menu.
//
// Parameters:
// content:
// The GUIContent to display as a disabled menu item.
//
// on:
// Specifies whether to show that the item is currently activated (i.e. a tick next
// to the item in the menu).
public void AddDisabledItem( GUIContent content, bool on )
{
}
public void AddItem( string name, bool on, MenuFunction func )
{
AddItem( new GUIContent( name ), on, func );
}
public void AddItem( GUIContent content, bool on, MenuFunction func )
{
//var parent = FindParent( content.text );
var item = FindOrCreateItem( content.text );
item.Set( true/*on*/, null, func );
}
public void AddItem( string name, bool on, MenuFunction2 func, object userData )
{
AddItem( new GUIContent( name ), on, func, userData );
}
public void AddItem( GUIContent content, bool on, MenuFunction2 func, object userData )
{
//var parent = FindParent( content.text );
var item = FindOrCreateItem( content.text );
item.Set( true/*on*/, null, func, userData );
}
//
// Summary:
// Add a seperator item to the menu.
//
// Parameters:
// path:
// The path to the submenu, if adding a separator to a submenu. When adding a separator
// to the top level of a menu, use an empty string as the path.
public void AddSeparator( string path = null )
{
var parent = string.IsNullOrWhiteSpace( path ) ? null : FindParent( path );
if ( parent == null )
items.Add( null );
else
parent.AddSeparator();
}
//
// Summary:
// Show the menu at the given screen rect.
//
// Parameters:
// position:
// The position at which to show the menu.
public void DropDown( Rect position )
{
Show( position );
}
protected override AdvancedDropdownItem BuildRoot()
{
var root = new AdvancedDropdownItem( Name );
foreach ( var m in items )
{
if ( m == null )
root.AddSeparator();
else
root.AddChild( m );
}
return root;
}
protected override void ItemSelected( AdvancedDropdownItem item )
{
if ( item is AdvancedGenericMenuItem gmItem )
gmItem.Run();
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ddde711109af02e42bfe8eb006577081
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -8,6 +8,9 @@ using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities; using Sirenix.Utilities;
using Sirenix.Utilities.Editor; using Sirenix.Utilities.Editor;
#endif #endif
#if USE_ADVANCED_GENERIC_MENU
using GenericMenu = XNodeEditor.AdvancedGenericMenu;
#endif
namespace XNodeEditor { namespace XNodeEditor {
/// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary> /// <summary> Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. </summary>

View File

@ -4,6 +4,9 @@ using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using XNodeEditor.Internal; using XNodeEditor.Internal;
#if USE_ADVANCED_GENERIC_MENU
using GenericMenu = XNodeEditor.AdvancedGenericMenu;
#endif
namespace XNodeEditor { namespace XNodeEditor {
public partial class NodeEditorWindow { public partial class NodeEditorWindow {

View File

@ -4,6 +4,9 @@ using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using XNodeEditor.Internal; using XNodeEditor.Internal;
#if USE_ADVANCED_GENERIC_MENU
using GenericMenu = XNodeEditor.AdvancedGenericMenu;
#endif
namespace XNodeEditor { namespace XNodeEditor {
/// <summary> Contains GUI methods </summary> /// <summary> Contains GUI methods </summary>

View File

@ -5,6 +5,9 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
#if USE_ADVANCED_GENERIC_MENU
using GenericMenu = XNodeEditor.AdvancedGenericMenu;
#endif
namespace XNodeEditor { namespace XNodeEditor {
/// <summary> Contains reflection-related extensions built for xNode </summary> /// <summary> Contains reflection-related extensions built for xNode </summary>

View File

@ -2,6 +2,9 @@
using System.Linq; using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
#if USE_ADVANCED_GENERIC_MENU
using GenericMenu = XNodeEditor.AdvancedGenericMenu;
#endif
namespace XNodeEditor { namespace XNodeEditor {
/// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary> /// <summary> Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. </summary>