From 84bd6c4002e2ff2d739e8135245224d7791c6ea9 Mon Sep 17 00:00:00 2001 From: Gregory Labute Date: Fri, 5 Jul 2024 12:37:33 -0400 Subject: [PATCH] layout tweaking for inspectors --- ...nemachineSplineDollyLookAtTargetsEditor.cs | 13 ++++++-- .../Editors/CinemachineSplineRollEditor.cs | 12 +++++-- .../Utility/CinemachineSceneToolHelpers.cs | 33 +++++++++++++++++++ .../Editor/Utility/InspectorUtility.cs | 8 ++--- .../Utility/SplineDataInspectorUtility.cs | 1 + 5 files changed, 57 insertions(+), 10 deletions(-) diff --git a/com.unity.cinemachine/Editor/Editors/CinemachineSplineDollyLookAtTargetsEditor.cs b/com.unity.cinemachine/Editor/Editors/CinemachineSplineDollyLookAtTargetsEditor.cs index d2a097255..76abc0fcb 100644 --- a/com.unity.cinemachine/Editor/Editors/CinemachineSplineDollyLookAtTargetsEditor.cs +++ b/com.unity.cinemachine/Editor/Editors/CinemachineSplineDollyLookAtTargetsEditor.cs @@ -82,8 +82,13 @@ public override VisualElement CreateInspectorGUI() "This component requires a CinemachineSplineDolly component referencing a nonempty Spline", HelpBoxMessageType.Warning); ux.Add(invalidHelp); - var toolButton = ux.AddChild(new Button(() => ToolManager.SetActiveTool(typeof(LookAtDataOnSplineTool))) - { text = "Edit Targets in Scene View" }); + + var tooltip = "Use the Scene View tool to Edit the LookAt targets on the spline"; + var buttonRow = ux.AddChild(new InspectorUtility.LabeledRow("Edit in Scene View", tooltip)); + var toolButton = buttonRow.Contents.AddChild( + CinemachineSceneToolHelpers.CreateSceneToolActivationButtonForInspector( + typeof(LookAtDataOnSplineTool), LookAtDataOnSplineTool.IconPath, tooltip)); + ux.TrackAnyUserActivity(() => { var haveSpline = splineData != null && splineData.GetGetSplineAndDolly(out _, out _); @@ -309,11 +314,13 @@ class LookAtDataOnSplineTool : EditorTool public static Action s_OnDataIndexDragged; public static Action s_OnDataLookAtDragged; + public static string IconPath => $"{CinemachineSceneToolHelpers.IconPath}/CmSplineLookAtTargetsTool@256.png"; + void OnEnable() { m_IconContent = new () { - image = AssetDatabase.LoadAssetAtPath($"{CinemachineSceneToolHelpers.IconPath}/CmSplineLookAtTargetsTool@256.png"), + image = AssetDatabase.LoadAssetAtPath(IconPath), tooltip = "Assign LookAt targets to positions on the spline." }; } diff --git a/com.unity.cinemachine/Editor/Editors/CinemachineSplineRollEditor.cs b/com.unity.cinemachine/Editor/Editors/CinemachineSplineRollEditor.cs index 523f580d1..e31cdb089 100644 --- a/com.unity.cinemachine/Editor/Editors/CinemachineSplineRollEditor.cs +++ b/com.unity.cinemachine/Editor/Editors/CinemachineSplineRollEditor.cs @@ -21,9 +21,13 @@ public override VisualElement CreateInspectorGUI() "This component should be associated with a non-empty spline", HelpBoxMessageType.Warning); ux.Add(invalidHelp); - var toolButton = ux.AddChild(new Button(() => ToolManager.SetActiveTool(typeof(SplineRollTool))) - { text = "Edit Data Points in Scene View" }); + var tooltip = "Use the Scene View tool to adjust the roll data points"; + var buttonRow = ux.AddChild(new InspectorUtility.LabeledRow("Edit in Scene View", tooltip)); + var toolButton = buttonRow.Contents.AddChild( + CinemachineSceneToolHelpers.CreateSceneToolActivationButtonForInspector( + typeof(SplineRollTool), SplineRollTool.IconPath, tooltip)); + ux.TrackAnyUserActivity(() => { var haveSpline = splineData != null && splineData.SplineContainer != null; @@ -244,11 +248,13 @@ sealed class SplineRollTool : EditorTool public static Action s_OnDataIndexDragged; public static Action s_OnDataLookAtDragged; + public static string IconPath => $"{CinemachineSceneToolHelpers.IconPath}/CmSplineRollTool@256.png"; + void OnEnable() { m_IconContent = new GUIContent { - image = AssetDatabase.LoadAssetAtPath($"{CinemachineSceneToolHelpers.IconPath}/CmSplineRollTool@256.png"), + image = AssetDatabase.LoadAssetAtPath(IconPath), tooltip = "Adjust the roll data points along the spline" }; } diff --git a/com.unity.cinemachine/Editor/Utility/CinemachineSceneToolHelpers.cs b/com.unity.cinemachine/Editor/Utility/CinemachineSceneToolHelpers.cs index 7d99ad2fe..8f913674f 100644 --- a/com.unity.cinemachine/Editor/Utility/CinemachineSceneToolHelpers.cs +++ b/com.unity.cinemachine/Editor/Utility/CinemachineSceneToolHelpers.cs @@ -1,6 +1,8 @@ using System; using UnityEditor; +using UnityEditor.EditorTools; using UnityEngine; +using UnityEngine.UIElements; namespace Unity.Cinemachine.Editor { @@ -25,6 +27,37 @@ static class CinemachineSceneToolHelpers static string ResourcePath => $"{CinemachineCore.kPackageRoot}/Editor/EditorResources"; public static string IconPath => $"{ResourcePath}/Icons/{SkinSuffix}"; + static Color s_NormalBkgColor = Color.black; + + /// Create a button for the inspector that activates a scene tool + public static VisualElement CreateSceneToolActivationButtonForInspector(Type toolType, string iconPath, string tooltip) + { + var toolButton = new Button(() => ToolManager.SetActiveTool(toolType)) + { + tooltip = tooltip, + style = + { + flexGrow = 0, + flexBasis = 2 * InspectorUtility.SingleLineHeight, + height = InspectorUtility.SingleLineHeight + 3, + }, + }; + toolButton.Add(new Image { image = AssetDatabase.LoadAssetAtPath(iconPath) }); + + // Capture "normal" colors + if (s_NormalBkgColor == Color.black) + toolButton.OnInitialGeometry(() => s_NormalBkgColor = toolButton.resolvedStyle.backgroundColor); + + toolButton.ContinuousUpdate(() => + { + if (ToolManager.activeToolType == toolType) + toolButton.style.backgroundColor = Color.Lerp(s_NormalBkgColor, new Color(0.1f, 0.3f, 0.6f, 1), 0.5f); + else + toolButton.style.backgroundColor = s_NormalBkgColor; + }); + return toolButton; + } + public static float SliderHandleDelta(Vector3 newPos, Vector3 oldPos, Vector3 forward) { var delta = newPos - oldPos; diff --git a/com.unity.cinemachine/Editor/Utility/InspectorUtility.cs b/com.unity.cinemachine/Editor/Utility/InspectorUtility.cs index ecb4a69c2..5a89d3bc7 100644 --- a/com.unity.cinemachine/Editor/Utility/InspectorUtility.cs +++ b/com.unity.cinemachine/Editor/Utility/InspectorUtility.cs @@ -360,9 +360,9 @@ public static Button MiniPopupButton(string tooltip = null, ContextualMenuManipu var button = new Button { tooltip = tooltip, style = { flexGrow = 0, - flexBasis = SingleLineHeight, + flexBasis = SingleLineHeight + 3, backgroundImage = (StyleBackground)EditorGUIUtility.IconContent("_Popup").image, - width = SingleLineHeight, height = SingleLineHeight, + width = SingleLineHeight, height = SingleLineHeight + 3, alignSelf = Align.Center, paddingRight = 0, marginRight = 0 }}; @@ -383,9 +383,9 @@ public static Button MiniDropdownButton(string tooltip = null, ContextualMenuMan var button = new Button { tooltip = tooltip, style = { flexGrow = 0, - flexBasis = SingleLineHeight, + flexBasis = SingleLineHeight + 3, backgroundImage = (StyleBackground)EditorGUIUtility.IconContent("dropdown").image, - width = SingleLineHeight, height = SingleLineHeight, + width = SingleLineHeight, height = SingleLineHeight + 3, alignSelf = Align.Center, paddingRight = 0, marginRight = 0 }}; diff --git a/com.unity.cinemachine/Editor/Utility/SplineDataInspectorUtility.cs b/com.unity.cinemachine/Editor/Utility/SplineDataInspectorUtility.cs index bb9719add..66ab45301 100644 --- a/com.unity.cinemachine/Editor/Utility/SplineDataInspectorUtility.cs +++ b/com.unity.cinemachine/Editor/Utility/SplineDataInspectorUtility.cs @@ -36,6 +36,7 @@ public static VisualElement CreatePathUnitField(SerializedProperty splineDataPro }); enumField.TrackPropertyValue(indexUnitProp, (p) => enumField.value = (PathIndexUnit)indexUnitProp.enumValueIndex); enumField.TrackAnyUserActivity(() => enumField.SetEnabled(getSpline?.Invoke() != null)); + enumField.AddToClassList(InspectorUtility.AlignFieldClassName); return enumField; }