A game about forced loneliness, made by TACStudios
1using System;
2using System.Linq.Expressions;
3using System.Reflection;
4
5namespace UnityEditor.Rendering
6{
7 /// <summary>
8 /// Contains a set of method to be able to manage Menu Items for the editor
9 /// </summary>
10 static class MenuManager
11 {
12 #region Add Menu Item
13 static Action<string, string, bool, int, Action, Func<bool>> s_AddMenuItem = GetAddMenuItemMethod();
14 static Action<string, string, bool, int, Action, Func<bool>> GetAddMenuItemMethod()
15 {
16 MethodInfo addMenuItemMethodInfo = typeof(Menu).GetMethod("AddMenuItem", BindingFlags.Static | BindingFlags.NonPublic);
17 var nameParam = Expression.Parameter(typeof(string), "name");
18 var shortcutParam = Expression.Parameter(typeof(string), "shortcut");
19 var checkedParam = Expression.Parameter(typeof(bool), "checked");
20 var priorityParam = Expression.Parameter(typeof(int), "priority");
21 var executeParam = Expression.Parameter(typeof(Action), "execute");
22 var validateParam = Expression.Parameter(typeof(Func<bool>), "validate");
23
24 var addMenuItemExpressionCall = Expression.Call(null, addMenuItemMethodInfo,
25 nameParam,
26 shortcutParam,
27 checkedParam,
28 priorityParam,
29 executeParam,
30 validateParam);
31
32 return Expression.Lambda<Action<string, string, bool, int, Action, Func<bool>>>(
33 addMenuItemExpressionCall,
34 nameParam,
35 shortcutParam,
36 checkedParam,
37 priorityParam,
38 executeParam,
39 validateParam).Compile();
40 }
41
42 /// <summary>
43 /// Adds a menu Item to the editor
44 /// </summary>
45 /// <param name="path">The path to the menu item</param>
46 /// <param name="shortcut">The shortcut of the menu item</param>
47 /// <param name="checked">If the item can have an state, pressed or not</param>
48 /// <param name="priority">The priority of the menu item</param>
49 /// <param name="execute">The action that will be called once the menu item is pressed</param>
50 /// <param name="validate">The action that will be called to know if the menu itme is enabled</param>
51 public static void AddMenuItem(string path, string shortcut, bool @checked, int priority, System.Action execute, System.Func<bool> validate)
52 {
53 s_AddMenuItem(path, shortcut, @checked, priority, execute, validate);
54 }
55
56 #endregion
57
58 #region Remove Menu Item
59 static Action<string> s_RemoveMenuItem = GetRemoveMenuItemMethod();
60 static Action<string> GetRemoveMenuItemMethod()
61 {
62 MethodInfo removeMenuItemMethodInfo = typeof(Menu).GetMethod("RemoveMenuItem", BindingFlags.Static | BindingFlags.NonPublic);
63 var nameParam = Expression.Parameter(typeof(string), "name");
64 return Expression.Lambda<Action<string>>(
65 Expression.Call(null, removeMenuItemMethodInfo, nameParam),
66 nameParam).Compile();
67 }
68 #endregion
69
70 /// <summary>
71 /// Removes a Menu item from the editor, if the path is not found it does nothing
72 /// </summary>
73 /// <param name="path">The path of the menu item to be removed</param>
74 public static void RemoveMenuItem(string path)
75 {
76 s_RemoveMenuItem(path);
77 }
78 }
79}