From 1c7d61f8482199388757fe4422208fb7c2bb68b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20V=C3=A1zquez?= Date: Tue, 7 Sep 2021 11:23:10 +0200 Subject: [PATCH] Implementing Menu Manager --- .../Editor/MenuManager.cs | 79 +++++++++++++++++++ .../Editor/MenuManager.cs.meta | 11 +++ 2 files changed, 90 insertions(+) create mode 100644 com.unity.render-pipelines.core/Editor/MenuManager.cs create mode 100644 com.unity.render-pipelines.core/Editor/MenuManager.cs.meta diff --git a/com.unity.render-pipelines.core/Editor/MenuManager.cs b/com.unity.render-pipelines.core/Editor/MenuManager.cs new file mode 100644 index 00000000000..709d1fe00c1 --- /dev/null +++ b/com.unity.render-pipelines.core/Editor/MenuManager.cs @@ -0,0 +1,79 @@ +using System; +using System.Linq.Expressions; +using System.Reflection; + +namespace UnityEditor.Rendering +{ + /// + /// Contains a set of method to be able to manage Menu Items for the editor + /// + static class MenuManager + { + #region Add Menu Item + static Action> s_AddMenuItem = GetAddMenuItemMethod(); + static Action> GetAddMenuItemMethod() + { + MethodInfo addMenuItemMethodInfo = typeof(Menu).GetMethod("AddMenuItem", BindingFlags.Static | BindingFlags.NonPublic); + var nameParam = Expression.Parameter(typeof(string), "name"); + var shortcutParam = Expression.Parameter(typeof(string), "shortcut"); + var checkedParam = Expression.Parameter(typeof(bool), "checked"); + var priorityParam = Expression.Parameter(typeof(int), "priority"); + var executeParam = Expression.Parameter(typeof(Action), "execute"); + var validateParam = Expression.Parameter(typeof(Func), "validate"); + + var addMenuItemExpressionCall = Expression.Call(null, addMenuItemMethodInfo, + nameParam, + shortcutParam, + checkedParam, + priorityParam, + executeParam, + validateParam); + + return Expression.Lambda>>( + addMenuItemExpressionCall, + nameParam, + shortcutParam, + checkedParam, + priorityParam, + executeParam, + validateParam).Compile(); + } + + /// + /// Adds a menu Item to the editor + /// + /// The path to the menu item + /// The shortcut of the menu item + /// If the item can have an state, pressed or not + /// The priority of the menu item + /// The action that will be called once the menu item is pressed + /// The action that will be called to know if the menu itme is enabled + public static void AddMenuItem(string path, string shortcut, bool @checked, int priority, System.Action execute, System.Func validate) + { + s_AddMenuItem(path, shortcut, @checked, priority, execute, validate); + } + + #endregion + + #region Remove Menu Item + static Action s_RemoveMenuItem = GetRemoveMenuItemMethod(); + static Action GetRemoveMenuItemMethod() + { + MethodInfo removeMenuItemMethodInfo = typeof(Menu).GetMethod("RemoveMenuItem", BindingFlags.Static | BindingFlags.NonPublic); + var nameParam = Expression.Parameter(typeof(string), "name"); + return Expression.Lambda>( + Expression.Call(null, removeMenuItemMethodInfo, nameParam), + nameParam).Compile(); + } + #endregion + + /// + /// Removes a Menu item from the editor, if the path is not found it does nothing + /// + /// The path of the menu item to be removed + public static void RemoveMenuItem(string path) + { + s_RemoveMenuItem(path); + } + } +} diff --git a/com.unity.render-pipelines.core/Editor/MenuManager.cs.meta b/com.unity.render-pipelines.core/Editor/MenuManager.cs.meta new file mode 100644 index 00000000000..ac7bdfd5ea5 --- /dev/null +++ b/com.unity.render-pipelines.core/Editor/MenuManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 016ffaaf36db9a047ab182ffa07bb8de +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: