From efa45da437d72389dc45a1af66f4eeb3a248dd52 Mon Sep 17 00:00:00 2001 From: Gregory Labute Date: Tue, 18 Jul 2023 14:36:42 -0400 Subject: [PATCH] extensions dropdown disables already-added extensions (#889) --- .../Utility/CmCameraInspectorUtility.cs | 76 +++++++++++-------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/com.unity.cinemachine/Editor/Utility/CmCameraInspectorUtility.cs b/com.unity.cinemachine/Editor/Utility/CmCameraInspectorUtility.cs index b6aba2cc2..43e1b18ee 100644 --- a/com.unity.cinemachine/Editor/Utility/CmCameraInspectorUtility.cs +++ b/com.unity.cinemachine/Editor/Utility/CmCameraInspectorUtility.cs @@ -234,34 +234,48 @@ static int GetTypeIndexFromSelection(string selection, int stage) /// Draw the Extensions dropdown in the inspector public static void AddExtensionsDropdown(this UnityEditor.Editor editor, VisualElement ux) { - var targets = editor.targets; - var dropdown = new DropdownField - { - name = "extensions selector", - label = "Add Extension", - choices = PipelineStageMenu.s_ExtentionNames, - index = 0, - }; - dropdown.AddToClassList(InspectorUtility.kAlignFieldClass); - dropdown.RegisterValueChangedCallback(evt => + var row = new InspectorUtility.LabeledRow( + "Add Extension", "Extensions are behaviours that inject themselves into " + + "the Cinemachine pipeline to alter the camera's behaviour."); + + var menu = new ContextualMenuManipulator((evt) => { - Type extType = PipelineStageMenu.s_ExtentionTypes[GetTypeIndexFromSelection(evt.newValue)]; - for (int i = 0; i < targets.Length; i++) + for (int i = 0; i < PipelineStageMenu.s_ExtensionTypes.Count; ++i) { - var targetGO = (targets[i] as CinemachineVirtualCameraBase).gameObject; - if (targetGO != null && targetGO.GetComponent(extType) == null) - Undo.AddComponent(targetGO, extType); + var type = PipelineStageMenu.s_ExtensionTypes[i]; + if (type == null) + continue; + var name = PipelineStageMenu.s_ExtensionNames[i]; + evt.menu.AppendAction(name, + (action) => + { + var target = editor.target as CinemachineVirtualCameraBase; + Undo.AddComponent(target.gameObject, type); + }, + (status) => + { + var target = editor.target as CinemachineVirtualCameraBase; + var disable = target == null || target.GetComponent(type) != null; + return disable ? DropdownMenuAction.Status.Disabled : DropdownMenuAction.Status.Normal; + } + ); } - - static int GetTypeIndexFromSelection(string selection) - { - for (var j = 0; j < PipelineStageMenu.s_ExtentionNames.Count; ++j) - if (PipelineStageMenu.s_ExtentionNames[j].Equals(selection)) - return j; - return 0; + }); + var button = row.Contents.AddChild(new Button + { + text = "(select)", + style = + { + flexGrow = 1, marginRight = 0, marginLeft = 3, + paddingTop = 0, paddingBottom = 0, paddingLeft = 1, + height = InspectorUtility.SingleLineHeight + 2, + unityTextAlign = TextAnchor.MiddleLeft } }); - ux.Add(dropdown); + menu.activators.Clear(); + menu.activators.Add(new ManipulatorActivationFilter { button = MouseButton.LeftMouse }); + button.AddManipulator(menu); + ux.Add(row); } [InitializeOnLoad] @@ -278,8 +292,8 @@ public struct StageData public static StageData[] s_StageData = null; // Extensions - public static List s_ExtentionTypes; - public static List s_ExtentionNames; + public static List s_ExtensionTypes; + public static List s_ExtensionNames; public static int GetSelectedComponent(int stage, CinemachineComponentBase component) { @@ -325,10 +339,10 @@ static PipelineStageMenu() } // Populate the extension list - s_ExtentionTypes = new List(); - s_ExtentionNames = new List(); - s_ExtentionTypes.Add(null); - s_ExtentionNames.Add("(select)"); + s_ExtensionTypes = new List(); + s_ExtensionNames = new List(); + s_ExtensionTypes.Add(null); + s_ExtensionNames.Add("(select)"); var allExtensions = ReflectionHelpers.GetTypesInAllDependentAssemblies( (Type t) => typeof(CinemachineExtension).IsAssignableFrom(t) @@ -337,8 +351,8 @@ var allExtensions while (iter2.MoveNext()) { var t = iter2.Current; - s_ExtentionTypes.Add(t); - s_ExtentionNames.Add(t.Name); + s_ExtensionTypes.Add(t); + s_ExtensionNames.Add(t.Name); } } }