Advertisement
celinedrules

ContextMenuItem

Jul 17th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3.  
  4. public class ContextMenuItem
  5. {
  6.     // The name that will be displayed to the user
  7.     public string Name { get; set; }
  8.     // The method that will be called when the menu item is clicked
  9.     public GenericMenu.MenuFunction Callback { get; }
  10.     // Does this menu item have children
  11.     public bool IsParent { get; }
  12.     // Is this menu item part of a sub menu
  13.     public bool IsChild { get; }
  14.     // If this a parent menu item, this holds the children
  15.     public List<ContextMenuItem> ChildItems { get; set; }
  16.     // Show the children of this parent menu item
  17.     public bool ShowChildren { get; set; }
  18.     // If this is a child menu item, store the parents name
  19.     public string ParentName { get; set; }
  20.    
  21.     /// <summary>
  22.     /// Creates a menu item with just a name
  23.     /// </summary>
  24.     /// <param name="name">The name that will be displayed</param>
  25.     public ContextMenuItem(string name) :
  26.         this(name, null, false, false, "")
  27.     {
  28.     }
  29.    
  30.     /// <summary>
  31.     /// Creates a menu item that performs an action
  32.     /// </summary>
  33.     /// <param name="name">The name that will be displayed</param>
  34.     /// <param name="callback">The action that is performed when clicked</param>
  35.     public ContextMenuItem(string name, GenericMenu.MenuFunction callback) :
  36.         this(name, callback, false, false, "")
  37.     {
  38.     }
  39.    
  40.     /// <summary>
  41.     /// Creates a menu item as a parent
  42.     /// </summary>
  43.     /// <param name="name">The name that will be displayed</param>
  44.     /// <param name="callback">The action that is performed when clicked</param>
  45.     /// <param name="isParent">Specifies if this is a parent menu</param>
  46.     public ContextMenuItem(string name, GenericMenu.MenuFunction callback, bool isParent) :
  47.         this(name, callback, isParent, false, "")
  48.     {
  49.     }
  50.  
  51.     /// <summary>
  52.     /// Creates a menu item a s a child
  53.     /// </summary>
  54.     /// <param name="name">The name that will be displayed</param>
  55.     /// <param name="callback">The action that is performed when clicked</param>
  56.     /// <param name="isParent">Specifies if this is a parent menu item</param>
  57.     /// <param name="isChild">Specifies if this is a child menu item</param>
  58.     /// <param name="parentName">The name of the parent the child belongs to</param>
  59.     public ContextMenuItem(string name, GenericMenu.MenuFunction callback, bool isParent, bool isChild, string parentName)
  60.     {
  61.         Name = name;
  62.         Callback = callback;
  63.         IsParent = isParent;
  64.        
  65.         if(IsParent)
  66.             ChildItems = new List<ContextMenuItem>();
  67.  
  68.         IsChild = isChild;
  69.         ParentName = parentName;
  70.     }
  71.    
  72.     /// <summary>
  73.     /// Add a child menu item
  74.     /// </summary>
  75.     /// <param name="child">The child to add</param>
  76.     public void AddChild(ContextMenuItem child)
  77.     {
  78.         ChildItems.Add(child);
  79.     }
  80.    
  81.     /// <summary>
  82.     /// Perform the menu items action
  83.     /// </summary>
  84.     public void Execute()
  85.     {
  86.         Callback?.Invoke();
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement