Skip to content

Commit

Permalink
Changing Context Menu generation to use Menu items when available
Browse files Browse the repository at this point in the history
  • Loading branch information
lopezt-unity committed Nov 8, 2023
1 parent 484cef8 commit 5d4a985
Show file tree
Hide file tree
Showing 79 changed files with 255 additions and 199 deletions.
9 changes: 7 additions & 2 deletions Debug/Editor/GenerateMenuItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class MenuActionData

// path = "Editors/New Bezier Shape"
public string path { get; private set; }
public string iconPath { get; private set; }

// MenuItemAttribute shortcut string, ex "#%d"
public string menuItemShortcut { get; private set; }
Expand All @@ -77,6 +78,7 @@ public MenuActionData(string scriptPath)

if (valid)
{
iconPath = instance.iconPath;
PropertyInfo hasMenuEntryProperty = typeof(MenuAction).GetProperty("hasFileMenuEntry", BindingFlags.NonPublic | BindingFlags.Instance);
visibleInMenu = hasMenuEntryProperty != null && (bool)hasMenuEntryProperty.GetValue(instance, null);
menuItemShortcut = instance.tooltip.shortcut;
Expand Down Expand Up @@ -162,7 +164,7 @@ namespace UnityEditor.ProBuilder
{
static class EditorToolbarMenuItem
{
const string k_MenuPrefix = ""Tools/ProBuilder/"";");
internal const string k_MenuPrefix = ""Tools/ProBuilder/"";");
}

static void AppendMenuItem(StringBuilder sb, MenuActionData data)
Expand All @@ -184,7 +186,10 @@ static void AppendMenuItem(StringBuilder sb, MenuActionData data)
sb.AppendLine();

// Action
sb.AppendLine($"\t\t[MenuItem(k_MenuPrefix + \"{data.path}{menuItemShortcut}\", false, {priority})]");
if(string.IsNullOrEmpty(data.iconPath))
sb.AppendLine($"\t\t[MenuItem(k_MenuPrefix + \"{data.path}{menuItemShortcut}\", false, {priority})]");
else
sb.AppendLine($"\t\t[MenuItem(k_MenuPrefix + \"{data.path}{menuItemShortcut}\", false, {priority}, \"\",\"Packages/com.unity.probuilder/Content/Icons/{data.iconPath}.png\")]");
sb.AppendLine($"\t\tstatic void MenuPerform_{data.typeString}()");
sb.AppendLine("\t\t{");
sb.AppendLine($"\t\t\tvar instance = EditorToolbarLoader.GetInstance<{data.typeString}>();");
Expand Down
90 changes: 45 additions & 45 deletions Editor/EditorCore/EditorToolbarMenuItems.cs

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion Editor/EditorCore/MenuAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ internal static int CompareActionsByGroupAndPriority(MenuAction left, MenuAction
/// </summary>
public abstract Texture2D icon { get; }

/// <summary>
/// Gets the local path of the icon to display in the Context Menu for this action.
/// </summary>
internal abstract string iconPath { get; }

/// <summary>
/// Gets the contents of the tooltip to display for this menu action.
/// </summary>
Expand All @@ -147,7 +152,7 @@ internal static int CompareActionsByGroupAndPriority(MenuAction left, MenuAction
/// <summary>
/// Gets whether this class should have an entry built into the hardware menu. This is not implemented for custom actions.
/// </summary>
protected virtual bool hasFileMenuEntry { get { return true; } }
protected internal virtual bool hasFileMenuEntry { get { return true; } }

/// <summary>
/// Gets a flag that indicates both the visibility and enabled state of an action
Expand Down
37 changes: 25 additions & 12 deletions Editor/EditorCore/ProBuilderToolsContexts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,24 @@ public override void PopulateMenu(DropdownMenu menu)
}

var title = action.menuTitle;
// Geometry and Tool groups are not displayed in the menu
if (action.group != ToolbarGroup.Geometry && action.group != ToolbarGroup.Tool)
{
//STO-3001: For a better UX, Selection group is renamed to Select so that users don't think this is
//acting on the current selection
var groupName = action.group == ToolbarGroup.Selection ? "Select" : action.group.ToString();
title = $"{groupName}/{action.menuTitle}";
}

if (action.optionsEnabled)
if (action.hasFileMenuEntry)
{
string path = EditorToolbarMenuItem.k_MenuPrefix+action.group+"/"+title;
if (GetStatus(action) == DropdownMenuAction.Status.Normal || GetStatus(action) == DropdownMenuAction.Status.Disabled)
{
ContextMenuUtility.AddMenuItem(menu, path, GetMenuTitle(action, title));
}
}else if (action.optionsEnabled)
{
title = GetMenuTitle(action, title);
if(HasPreview(action))
menu.AppendAction(title, _ => EditorAction.Start(new MenuActionSettingsWithPreview(action)), GetStatus(action), action.icon);
else
menu.AppendAction(title, _ => EditorAction.Start(new MenuActionSettings(action)), GetStatus(action), action.icon);

}
else
menu.AppendAction(title, _ => action.PerformAction(), GetStatus(action), action.icon);
menu.AppendAction(GetMenuTitle(action, title), _ => action.PerformAction(), GetStatus(action), action.icon);
}

var trs = Selection.transforms;
Expand All @@ -159,9 +158,23 @@ static bool HasPreview(MenuAction action)
return !(action is DetachFaces || action is DuplicateFaces);
}

string GetMenuTitle(MenuAction action, string title)
{
// Geometry and Tool groups are not displayed in the menu
if (action.group != ToolbarGroup.Geometry && action.group != ToolbarGroup.Tool)
{
//STO-3001: For a better UX, Selection group is renamed to Select so that users don't think this is
//acting on the current selection
var groupName = action.group == ToolbarGroup.Selection ? "Select" : action.group.ToString();
title = $"{groupName}/{action.menuTitle}";
}
return title;
}

public override void OnActivated()
{
m_Editor = new ProBuilderEditor();
if(m_Editor == null)
m_Editor = new ProBuilderEditor();
}

public override void OnWillBeDeactivated()
Expand Down
6 changes: 4 additions & 2 deletions Editor/MenuActions/Editors/CutToolAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public override ToolbarGroup group
get { return ToolbarGroup.Tool; }
}

internal override string iconPath => "Toolbar/CutTool";

/// <inheritdoc/>
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/CutTool"); } }
public override Texture2D icon => IconUtility.GetIcon(iconPath);

/// <inheritdoc/>
public override TooltipContent tooltip
Expand All @@ -32,7 +34,7 @@ public override SelectMode validSelectModes
}

/// <inheritdoc/>
protected override bool hasFileMenuEntry
protected internal override bool hasFileMenuEntry
{
get { return false; }
}
Expand Down
4 changes: 3 additions & 1 deletion Editor/MenuActions/Editors/NewBezierShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ sealed class NewBezierShape : MenuAction
const string k_IconPath = "Toolbar/NewBezierSpline";

public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return IconUtility.GetIcon(k_IconPath, IconSkin.Pro); } }

internal override string iconPath => k_IconPath;
public override Texture2D icon => IconUtility.GetIcon(k_IconPath, IconSkin.Pro);
public override TooltipContent tooltip { get { return _tooltip; } }
public override string menuTitle { get { return "New Bezier Shape"; } }
public override int toolbarPriority { get { return 1; } }
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Editors/NewPolyShapeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class NewPolyShapeAction : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/CreatePolyShape"); } }
internal override string iconPath => "Toolbar/CreatePolyShape";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return _tooltip; } }
public override string menuTitle { get { return "New Poly Shape"; } }
public override int toolbarPriority { get { return 1; } }
Expand Down
4 changes: 3 additions & 1 deletion Editor/MenuActions/Editors/NewShapeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace UnityEditor.ProBuilder.Actions
sealed class NewShapeAction : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/AddShape"); } }

internal override string iconPath => "Toolbar/AddShape";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_Tooltip; } }
public override string menuTitle { get { return "New Shape"; } }
public override int toolbarPriority { get { return 0; } }
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Editors/OpenLightmapUVEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class OpenLightmapUVEditor : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return null; } }
internal override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return s_Tooltip; } }
public override string menuTitle { get { return "Lightmap UV Editor"; } }

Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Editors/OpenMaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class OpenMaterialEditor : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Panel_Materials"); } }
internal override string iconPath => "Toolbar/Panel_Materials";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_Tooltip; } }

static readonly TooltipContent s_Tooltip = new TooltipContent
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Editors/OpenSmoothingEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class OpenSmoothingEditor : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Panel_Smoothing"); } }
internal override string iconPath => "Toolbar/Panel_Smoothing";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_Tooltip; } }
public override string menuTitle { get { return "Smoothing"; } }
public override int toolbarPriority { get { return 2; } }
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Editors/OpenUVEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class OpenUVEditor : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Panel_UVEditor"); } }
internal override string iconPath => "Toolbar/Panel_UVEditor";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_Tooltip; } }

static readonly TooltipContent s_Tooltip = new TooltipContent
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Editors/OpenVertexColorEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class OpenVertexColorEditor : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Panel_VertColors"); } }
internal override string iconPath => "Toolbar/Panel_VertColors";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_Tooltip; } }
public override string menuTitle { get { return "Vertex Colors"; } }

Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Editors/OpenVertexPositionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class OpenVertexPositionEditor : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Tool; } }
public override Texture2D icon { get { return null; } }
internal override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return s_Tooltip; } }
public override string menuTitle { get { return "Vertex Editor"; } }

Expand Down
5 changes: 3 additions & 2 deletions Editor/MenuActions/Export/Export.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ namespace UnityEditor.ProBuilder.Actions
sealed class Export : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Object; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Object_Export"); } }
internal override string iconPath => "Toolbar/Object_Export";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return m_Tooltip; } }
protected override bool hasFileMenuEntry { get { return false; } }
protected override internal bool hasFileMenuEntry { get { return false; } }

GUIContent gc_ExportFormat = new GUIContent("Export Format", "The type of file to export the current selection as.");
GUIContent gc_ExportRecursive = new GUIContent("Include Children", "Should the exporter include children of the current selection when searching for meshes to export?");
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Export/ExportAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct ExportAssetOptions
sealed class ExportAsset : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Export; } }
public override Texture2D icon { get { return null; } }
internal override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return s_Tooltip; } }

internal static Pref<ExportAssetOptions> s_ExportAssetOptions =new Pref<ExportAssetOptions>("export.assetOptions", ExportAssetOptions.defaults);
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Export/ExportObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class ExportObj : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Export; } }
public override Texture2D icon { get { return null; } }
internal override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return _tooltip; } }

static readonly TooltipContent _tooltip = new TooltipContent
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Export/ExportPly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class ExportPly : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Export; } }
public override Texture2D icon { get { return null; } }
internal override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return _tooltip; } }

static readonly TooltipContent _tooltip = new TooltipContent
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Export/ExportStlAscii.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class ExportStlAscii : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Export; } }
public override Texture2D icon { get { return null; } }
internal override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return _tooltip; } }

static readonly TooltipContent _tooltip = new TooltipContent
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Export/ExportStlBinary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace UnityEditor.ProBuilder.Actions
sealed class ExportStlBinary : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Export; } }
public override Texture2D icon { get { return null; } }
internal override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return _tooltip; } }

static readonly TooltipContent _tooltip = new TooltipContent
Expand Down
5 changes: 3 additions & 2 deletions Editor/MenuActions/Geometry/BevelEdges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ sealed class BevelEdges : MenuAction
Pref<float> m_BevelSize = new Pref<float>("BevelEdges.size", .2f);

public override ToolbarGroup group { get { return ToolbarGroup.Geometry; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Edge_Bevel"); } }
internal override string iconPath => "Toolbar/Edge_Bevel";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_Tooltip; } }

static readonly GUIContent gc_BevelDistance = EditorGUIUtility.TrTextContent("Distance", "The size of the bevel in meters. The value is clamped to the size of the smallest affected face.");

static readonly TooltipContent s_Tooltip = new TooltipContent
(
"Bevel",
"Bevel Edges",
@"Smooth the selected edges by adding a slanted face connecting the two adjacent faces."
);

Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Geometry/BridgeEdges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public override ToolbarGroup group
get { return ToolbarGroup.Geometry; }
}

public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Edge_Bridge"); } }
internal override string iconPath => "Toolbar/Edge_Bridge";
public override Texture2D icon => IconUtility.GetIcon(iconPath);

public override TooltipContent tooltip
{
Expand Down
3 changes: 2 additions & 1 deletion Editor/MenuActions/Geometry/CollapseVertices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public override ToolbarGroup group
get { return ToolbarGroup.Geometry; }
}

public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Vert_Collapse"); } }
internal override string iconPath => "Toolbar/Vert_Collapse";
public override Texture2D icon => IconUtility.GetIcon(iconPath);

public override TooltipContent tooltip
{
Expand Down
5 changes: 3 additions & 2 deletions Editor/MenuActions/Geometry/ConformFaceNormals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ namespace UnityEditor.ProBuilder.Actions
sealed class ConformFaceNormals : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Geometry; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Face_ConformNormals"); } }
internal override string iconPath => "Toolbar/Face_ConformNormals";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_TooltipContent; } }
public override string menuTitle { get { return "Conform Normals"; } }
public override string menuTitle { get { return "Conform Face Normals"; } }

static readonly TooltipContent s_TooltipContent = new TooltipContent
(
Expand Down
5 changes: 3 additions & 2 deletions Editor/MenuActions/Geometry/ConnectEdges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace UnityEditor.ProBuilder.Actions
sealed class ConnectEdges : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Geometry; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Edge_Connect"); } }
internal override string iconPath => "Toolbar/Edge_Connect";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return s_Tooltip; } }
protected override bool hasFileMenuEntry { get { return false; } }
protected internal override bool hasFileMenuEntry { get { return false; } }

static readonly TooltipContent s_Tooltip = new TooltipContent
(
Expand Down
5 changes: 3 additions & 2 deletions Editor/MenuActions/Geometry/ConnectVertices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ namespace UnityEditor.ProBuilder.Actions
sealed class ConnectVertices : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Geometry; } }
public override Texture2D icon { get { return IconUtility.GetIcon("Toolbar/Vert_Connect"); } }
internal override string iconPath => "Toolbar/Vert_Connect";
public override Texture2D icon => IconUtility.GetIcon(iconPath);
public override TooltipContent tooltip { get { return _tooltip; } }
protected override bool hasFileMenuEntry { get { return false; } }
protected internal override bool hasFileMenuEntry { get { return false; } }

static readonly TooltipContent _tooltip = new TooltipContent
(
Expand Down
Loading

0 comments on commit 5d4a985

Please sign in to comment.