Skip to content

Commit ee053b3

Browse files
authored
[UUM-133530] Ensure "Set Double Sided" is enabled when active selection is proper (#660)
* Fix the 'Set Double Sided' action in the Editor sample. It couldn't be enabled in the contextual menu due to a missing override. * Add changelog entry * Ensure to pass the current status of a MenuAction when there is no options, nor fileMenuEntry associated to it * Add tests to cover some cases of the contextual menu * Update changelog * Clean up * Remove unnecessary comment * Refactor * Fix changelog after merge * Clean up documentation in CustomAction.cs Removed empty return documentation from methods.
1 parent 02d8bb4 commit ee053b3

5 files changed

Lines changed: 161 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
### Fixed
1111

1212
- [UUM-133861] Fixed "Look rotation viewing vector is zero" log being spammed when holding shift while using a create tool such as Create Sprite.
13+
- [UUM-133530] Fixed the `Set Double Sided` custom action in the Editor Sample, which was previously remaining disabled.
14+
- [UUM-133530] Ensured that the context menu respects the value of `MenuAction.enabled`.
1315
- [UUM-133531] Fixed component icons in Light theme.
1416
- [UUM-133526] Material Editor: fixed a warning (`GUI Error: Invalid GUILayout state in MaterialEditor view.`) that was thrown when deleting an extra material slot.
1517
- Fixed warnings related to obsolete API calls with Unity 6.4 and onwards.

Editor/EditorCore/ProBuilderToolsContexts.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public override void PopulateMenu(DropdownMenu menu)
110110
menu.AppendAction(title, _ => EditorAction.Start(new MenuActionSettings(action, HasPreview(action))), GetStatus(action));
111111
}
112112
else
113-
menu.AppendAction(GetMenuTitle(action, title), _ => action.PerformAction());
113+
menu.AppendAction(GetMenuTitle(action, title), _ => action.PerformAction(), GetStatus(action));
114114
}
115115
}
116116

Samples~/Editor/CustomAction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ public class MakeFacesDoubleSided : MenuAction
3535
/// <summary>
3636
/// Determines if the action should be enabled or grayed out.
3737
/// </summary>
38-
/// <returns></returns>
3938
public override bool enabled
4039
{
41-
get { return MeshSelection.selectedFaceCount > 0; }
40+
get { return base.enabled && MeshSelection.selectedFaceCount > 0; }
4241
}
4342

43+
protected override bool hasFileMenuEntry => false;
44+
4445
/// <summary>
4546
/// This action is applicable in Face selection modes.
4647
/// </summary>
@@ -52,7 +53,6 @@ public override SelectMode validSelectModes
5253
/// <summary>
5354
/// Return a pb_ActionResult indicating the success/failure of action.
5455
/// </summary>
55-
/// <returns></returns>
5656
protected override ActionResult PerformActionImplementation()
5757
{
5858
var selection = MeshSelection.top.ToArray();
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
using NUnit.Framework;
2+
using UnityEditor;
3+
using UnityEditor.EditorTools;
4+
using UnityEditor.ProBuilder;
5+
using UnityEngine;
6+
using UnityEngine.ProBuilder;
7+
using UnityEngine.UIElements;
8+
9+
/// <summary>
10+
/// Test the construction of the contextual menu in the Scene View.
11+
/// </summary>
12+
public class ContextualMenuTests
13+
{
14+
[ProBuilderMenuAction]
15+
class ConfigurableMenuAction : MenuAction
16+
{
17+
internal const string actionName = "Action Without File Menu Entry";
18+
internal static bool userHasFileMenuEntry { get; set; }
19+
internal static SelectMode userSelectMode { get; set; }
20+
internal static bool userEnabled { get; set; }
21+
22+
public override ToolbarGroup group
23+
{
24+
get { return ToolbarGroup.Geometry; }
25+
}
26+
27+
public override Texture2D icon => null;
28+
29+
public override string iconPath => string.Empty;
30+
31+
public override TooltipContent tooltip => new TooltipContent(
32+
actionName,
33+
@"This action should not have a file menu entry."
34+
);
35+
36+
public ConfigurableMenuAction()
37+
{
38+
}
39+
40+
protected override ActionResult PerformActionImplementation()
41+
{
42+
return ActionResult.Success;
43+
}
44+
45+
public override SelectMode validSelectModes => userSelectMode;
46+
public override bool enabled => userEnabled;
47+
protected internal override bool hasFileMenuEntry => userHasFileMenuEntry;
48+
}
49+
50+
ProBuilderMesh m_PBMesh;
51+
52+
[SetUp]
53+
public void Setup()
54+
{
55+
m_PBMesh = ShapeFactory.Instantiate(typeof(UnityEngine.ProBuilder.Shapes.Plane));
56+
}
57+
58+
[TearDown]
59+
public void TearDown()
60+
{
61+
if (m_PBMesh)
62+
Object.DestroyImmediate(m_PBMesh.gameObject);
63+
}
64+
65+
PositionToolContext PrepareToolContext(SelectMode toolSelectMode)
66+
{
67+
MeshSelection.SetSelection(m_PBMesh.gameObject);
68+
ActiveEditorTracker.sharedTracker.ForceRebuild();
69+
70+
ToolManager.SetActiveContext<PositionToolContext>();
71+
Tools.current = Tool.Move;
72+
ProBuilderEditor.selectMode = toolSelectMode;
73+
ActiveEditorTracker.sharedTracker.ForceRebuild();
74+
75+
PositionToolContext ctx = Resources.FindObjectsOfTypeAll<PositionToolContext>()?[0];
76+
Assume.That(ctx, Is.Not.Null);
77+
return ctx;
78+
}
79+
80+
bool CheckDropdownMenuHasItem(string name, DropdownMenuAction.Status status, DropdownMenu menu)
81+
{
82+
DropdownMenuAction foundInMenu = null;
83+
foreach (var t in menu.MenuItems())
84+
{
85+
if (t is not DropdownMenuAction menuAction)
86+
continue;
87+
88+
if (menuAction.name == name)
89+
{
90+
foundInMenu = menuAction;
91+
break;
92+
}
93+
}
94+
95+
return (foundInMenu?.status == status);
96+
}
97+
98+
[Test]
99+
[TestCase(true, ExpectedResult = false)]
100+
[TestCase(false, ExpectedResult = true)]
101+
public bool MenuActionWithoutMenuItem_hasFileMenuEntry(bool hasFileMenuEntry)
102+
{
103+
var ctx = PrepareToolContext(SelectMode.Face);
104+
105+
ConfigurableMenuAction.userHasFileMenuEntry = hasFileMenuEntry;
106+
ConfigurableMenuAction.userSelectMode = SelectMode.Any;
107+
ConfigurableMenuAction.userEnabled = true;
108+
109+
DropdownMenu menu = new DropdownMenu();
110+
ctx.PopulateMenu(menu);
111+
menu.PrepareForDisplay(null);
112+
113+
return CheckDropdownMenuHasItem(ConfigurableMenuAction.actionName, DropdownMenuAction.Status.Normal, menu);
114+
}
115+
116+
[Test]
117+
[TestCase(SelectMode.Edge, ExpectedResult = false)]
118+
[TestCase(SelectMode.Face, ExpectedResult = true)]
119+
[TestCase(SelectMode.Vertex, ExpectedResult = false)]
120+
public bool MenuAction_SelectModeSetToFace_EnabledOnlyForFaceSelection(SelectMode mode)
121+
{
122+
var ctx = PrepareToolContext(mode);
123+
124+
ConfigurableMenuAction.userHasFileMenuEntry = false;
125+
ConfigurableMenuAction.userSelectMode = SelectMode.Face;
126+
ConfigurableMenuAction.userEnabled = true;
127+
128+
DropdownMenu menu = new DropdownMenu();
129+
ctx.PopulateMenu(menu);
130+
menu.PrepareForDisplay(null);
131+
132+
// MenuAction is expected to be present in the menu only when the mode matches.
133+
return CheckDropdownMenuHasItem(ConfigurableMenuAction.actionName, DropdownMenuAction.Status.Normal, menu);
134+
}
135+
136+
[Test]
137+
[TestCase(true, ExpectedResult = true)]
138+
[TestCase(false, ExpectedResult = false)]
139+
public bool MenuAction_enabledPropertyIsFollowedByMenu(bool enabled)
140+
{
141+
var ctx = PrepareToolContext(SelectMode.Face);
142+
143+
ConfigurableMenuAction.userHasFileMenuEntry = false;
144+
ConfigurableMenuAction.userSelectMode = SelectMode.Face;
145+
ConfigurableMenuAction.userEnabled = enabled;
146+
147+
DropdownMenu menu = new DropdownMenu();
148+
ctx.PopulateMenu(menu);
149+
menu.PrepareForDisplay(null);
150+
151+
return CheckDropdownMenuHasItem(ConfigurableMenuAction.actionName, DropdownMenuAction.Status.Normal, menu);
152+
}
153+
}

Tests/Editor/Actions/ContextualMenuTests.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)