Skip to content

Commit 1a99814

Browse files
authored
Fix constraint highlighting for custom contraints (#1078)
* Add missing AddComponentMenu attributes * Formatting and updates * Fix constraint highlighting for custom contraints * Update CHANGELOG.md
1 parent 2baa865 commit 1a99814

9 files changed

Lines changed: 55 additions & 35 deletions

File tree

org.mixedrealitytoolkit.spatialmanipulation/BoundsControl/Visuals/SpatialManipulationReticle.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace MixedReality.Toolkit.SpatialManipulation
1212
/// A reticle used to visualize spatial manipulation capabilities when hovering over a bounding box handle.
1313
/// The reticle is oriented in relation to the bounding box, to indicate the direction for rotation or scaling.
1414
/// </summary>
15+
[AddComponentMenu("MRTK/Spatial Manipulation/Spatial Manipulation Reticle")]
1516
public class SpatialManipulationReticle : MonoBehaviour, IReticleVisual
1617
{
1718
/// <summary>
@@ -113,7 +114,7 @@ private void RotateReticle(Vector3 reticleNormal, Transform hitTargetTransform)
113114
}
114115
default: break;
115116
}
116-
117+
117118
// Cache the world rotation
118119
worldRotationCache = transform.rotation;
119120
}

org.mixedrealitytoolkit.spatialmanipulation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
1818
* Fixed issues with HandConstraint hand tracking events not being fired (OnFirstHandDetected/OnHandActivate/OnLastHandLost/OnHandDeactivate) when SolverHandler TrackedHand is set to Right or Left (not Both) [PR #956](https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity/pull/956)
1919
* Updated dependencies to match Unity Asset Store packages. [PR #1054](https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity/pull/1054)
2020
* org.mixedrealitytoolkit.core 3.2.2
21+
* Fixed `Constraint Manager` not properly highlighting custom constraints via the "Go to component" button. [PR #1078](https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity/pull/1078)
2122

2223
## [3.3.0] - 2024-04-30
2324

org.mixedrealitytoolkit.spatialmanipulation/Constraints/FixedDistanceConstraint.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace MixedReality.Toolkit.SpatialManipulation
1313
/// MRTK's constraint system might be redesigned in the near future. When
1414
/// this occurs, the old constraint components will be deprecated.
1515
/// </remarks>
16+
[AddComponentMenu("MRTK/Spatial Manipulation/Fixed Distance Constraint")]
1617
public class FixedDistanceConstraint : TransformConstraint
1718
{
1819
#region Properties

org.mixedrealitytoolkit.spatialmanipulation/Constraints/FixedRotationToUserConstraint.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace MixedReality.Toolkit.SpatialManipulation
1212
/// MRTK's constraint system might be redesigned in the near future. When
1313
/// this occurs, the old constraint components will be deprecated.
1414
/// </remarks>
15+
[AddComponentMenu("MRTK/Spatial Manipulation/Fixed Rotation to User Constraint")]
1516
public class FixedRotationToUserConstraint : TransformConstraint
1617
{
1718
#region Properties
@@ -46,4 +47,4 @@ public override void ApplyConstraint(ref MixedRealityTransform transform)
4647

4748
#endregion Public Methods
4849
}
49-
}
50+
}

org.mixedrealitytoolkit.spatialmanipulation/Constraints/FixedRotationToWorldConstraint.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Mixed Reality Toolkit Contributors
22
// Licensed under the BSD 3-Clause
33

4+
using UnityEngine;
5+
46
namespace MixedReality.Toolkit.SpatialManipulation
57
{
68
/// <summary>
@@ -10,6 +12,7 @@ namespace MixedReality.Toolkit.SpatialManipulation
1012
/// MRTK's constraint system might be redesigned in the near future. When
1113
/// this occurs, the old constraint components will be deprecated.
1214
/// </remarks>
15+
[AddComponentMenu("MRTK/Spatial Manipulation/Fixed Rotation to World Constraint")]
1316
public class FixedRotationToWorldConstraint : TransformConstraint
1417
{
1518
#region Properties
@@ -31,4 +34,4 @@ public override void ApplyConstraint(ref MixedRealityTransform transform)
3134

3235
#endregion Public Methods
3336
}
34-
}
37+
}

org.mixedrealitytoolkit.spatialmanipulation/Constraints/MaintainApparentSizeConstraint.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace MixedReality.Toolkit.SpatialManipulation
1313
/// MRTK's constraint system might be redesigned in the near future. When
1414
/// this occurs, the old constraint components will be deprecated.
1515
/// </remarks>
16+
[AddComponentMenu("MRTK/Spatial Manipulation/Maintain Apparent Size Constraint")]
1617
public class MaintainApparentSizeConstraint : TransformConstraint
1718
{
1819
#region Properties

org.mixedrealitytoolkit.spatialmanipulation/Constraints/MoveAxisConstraint.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace MixedReality.Toolkit.SpatialManipulation
1313
/// MRTK's constraint system might be redesigned in the near future. When
1414
/// this occurs, the old constraint components will be deprecated.
1515
/// </remarks>
16+
[AddComponentMenu("MRTK/Spatial Manipulation/Move Axis Constraint")]
1617
public class MoveAxisConstraint : TransformConstraint
1718
{
1819
#region Properties
@@ -103,4 +104,4 @@ public override void ApplyConstraint(ref MixedRealityTransform transform)
103104

104105
#endregion Public Methods
105106
}
106-
}
107+
}

org.mixedrealitytoolkit.spatialmanipulation/Constraints/RotationAxisConstraint.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace MixedReality.Toolkit.SpatialManipulation
1212
/// <remarks>
1313
/// The constraint system might be reworked in the future. In such a case, these existing components will be deprecated.
1414
/// </remarks>
15+
[AddComponentMenu("MRTK/Spatial Manipulation/Rotation Axis Constraint")]
1516
public class RotationAxisConstraint : TransformConstraint
1617
{
1718
#region Properties
@@ -78,4 +79,4 @@ public override void ApplyConstraint(ref MixedRealityTransform transform)
7879

7980
#endregion Public Methods
8081
}
81-
}
82+
}

org.mixedrealitytoolkit.spatialmanipulation/Editor/Constraints/ConstraintManagerEditor.cs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using System.Reflection;
89
using UnityEditor;
910
using UnityEngine;
1011

@@ -29,7 +30,7 @@ public class ConstraintManagerEditor : UnityEditor.Editor
2930
private ConstraintManager constraintManager;
3031

3132
private const string autoMsg = "Constraint manager is currently set to auto mode. In auto mode all" +
32-
" constraints attached to this gameobject will automatically be processed by this manager.";
33+
" constraints attached to this GameObject will automatically be processed by this manager.";
3334
private const string manualMsg = "Constraint manager is currently set to manual mode. In manual mode" +
3435
" only constraints that are linked in the below component list will be processed by this manager.";
3536

@@ -75,7 +76,7 @@ private static EntryAction RenderManualConstraintItem(SerializedProperty constra
7576
return EntryAction.Highlight;
7677
}
7778

78-
if (InspectorUIUtility.FlexButton(new GUIContent("Remove Entry", "Remove constraint from constraint manager but keep constraint component attached to the gameobject.")))
79+
if (InspectorUIUtility.FlexButton(new GUIContent("Remove Entry", "Remove constraint from constraint manager but keep constraint component attached to the GameObject.")))
7980
{
8081
return EntryAction.Detach;
8182
}
@@ -86,8 +87,8 @@ private static EntryAction RenderManualConstraintItem(SerializedProperty constra
8687

8788
private void AddNewConstraint(Type t)
8889
{
89-
var constraint = constraintManager.gameObject.AddComponent((Type)t);
90-
AttachConstraint((TransformConstraint)constraint);
90+
TransformConstraint constraint = constraintManager.gameObject.AddComponent(t) as TransformConstraint;
91+
AttachConstraint(constraint);
9192
}
9293

9394
private void AttachConstraint(TransformConstraint constraint)
@@ -101,23 +102,21 @@ private void AttachConstraint(TransformConstraint constraint)
101102
private void RenderAutoConstraintMenu()
102103
{
103104
// component list
104-
var constraints = constraintManager.gameObject.GetComponents<TransformConstraint>();
105-
foreach (var constraint in constraints)
105+
TransformConstraint[] constraints = constraintManager.gameObject.GetComponents<TransformConstraint>();
106+
foreach (TransformConstraint constraint in constraints)
106107
{
107108
using (new EditorGUILayout.HorizontalScope())
108109
{
109-
string constraintName = constraint.GetType().Name;
110-
EditorGUILayout.LabelField($"Priority {(constraint as TransformConstraint).ExecutionPriority}: {constraint.GetType().Name}");
110+
EditorGUILayout.LabelField($"Priority {constraint.ExecutionPriority}: {constraint.GetType().Name}");
111111
if (GUILayout.Button("Go to component"))
112112
{
113-
Highlighter.Highlight("Inspector", $"{ObjectNames.NicifyVariableName(constraintName)}");
114-
EditorGUIUtility.ExitGUI();
113+
HighlightConstraint(constraint.GetType());
115114
}
116115
}
117116
}
118117

119118
// add button
120-
if (EditorGUILayout.DropdownButton(new GUIContent("Add Constraint to GameObject", "Add a constraint to the gameobject that will be picked up by the constraint manager auto mode."), FocusType.Keyboard))
119+
if (EditorGUILayout.DropdownButton(new GUIContent("Add Constraint to GameObject", "Add a constraint to the GameObject that will be picked up by the constraint manager auto mode."), FocusType.Keyboard))
121120
{
122121
// create the menu and add items to it
123122
GenericMenu menu = new GenericMenu();
@@ -129,8 +128,7 @@ private void RenderAutoConstraintMenu()
129128

130129
foreach (var derivedType in types)
131130
{
132-
menu.AddItem(new GUIContent(derivedType.Name), false, t =>
133-
constraintManager.gameObject.AddComponent((Type)t), derivedType);
131+
menu.AddItem(new GUIContent(derivedType.Name), false, t => constraintManager.gameObject.AddComponent((Type)t), derivedType);
134132
}
135133

136134
menu.ShowAsContext();
@@ -142,24 +140,22 @@ private void RenderManualConstraintMenu()
142140
for (int i = 0; i < selectedConstraints.arraySize; i++)
143141
{
144142
SerializedProperty constraintProperty = selectedConstraints.GetArrayElementAtIndex(i);
145-
var buttonAction = RenderManualConstraintItem(constraintProperty, true);
143+
EntryAction buttonAction = RenderManualConstraintItem(constraintProperty, true);
146144
if (buttonAction == EntryAction.Detach)
147145
{
148146
indicesToRemove.Add(i);
149147
}
150148
else if (buttonAction == EntryAction.Highlight)
151149
{
152-
string constraintName = constraintProperty.objectReferenceValue.GetType().Name;
153-
Highlighter.Highlight("Inspector", $"{ObjectNames.NicifyVariableName(constraintName)}");
154-
EditorGUIUtility.ExitGUI();
150+
HighlightConstraint(constraintProperty.objectReferenceValue.GetType());
155151
}
156152
}
157153

158154
// add buttons
159155
{
160156
using (new EditorGUILayout.HorizontalScope())
161157
{
162-
if (EditorGUILayout.DropdownButton(new GUIContent("Add Entry", "Attach an already existing component from this gameobject to the constraint manager selection."), FocusType.Keyboard))
158+
if (EditorGUILayout.DropdownButton(new GUIContent("Add Entry", "Attach an already existing component from this GameObject to the constraint manager selection."), FocusType.Keyboard))
163159
{
164160
// create the menu and add items to it
165161
GenericMenu menu = new GenericMenu();
@@ -175,8 +171,7 @@ private void RenderManualConstraintMenu()
175171
{
176172
hasEntries = true;
177173
string constraintName = constraint.GetType().Name;
178-
menu.AddItem(new GUIContent(constraintName), false, t =>
179-
AttachConstraint((TransformConstraint)t), constraint);
174+
menu.AddItem(new GUIContent(constraintName), false, t => AttachConstraint((TransformConstraint)t), constraint);
180175
}
181176
}
182177

@@ -194,7 +189,7 @@ private void RenderManualConstraintMenu()
194189
menu.ShowAsContext();
195190
}
196191

197-
if (EditorGUILayout.DropdownButton(new GUIContent("Add New Constraint", "Add a constraint to the gameobject and attach to this constraint manager selection."), FocusType.Keyboard))
192+
if (EditorGUILayout.DropdownButton(new GUIContent("Add New Constraint", "Add a constraint to the GameObject and attach to this constraint manager selection."), FocusType.Keyboard))
198193
{
199194
// create the menu and add items to it
200195
GenericMenu menu = new GenericMenu();
@@ -206,8 +201,7 @@ private void RenderManualConstraintMenu()
206201

207202
foreach (var derivedType in types)
208203
{
209-
menu.AddItem(new GUIContent(derivedType.Name), false, t =>
210-
AddNewConstraint((Type)t), derivedType);
204+
menu.AddItem(new GUIContent(derivedType.Name), false, t => AddNewConstraint((Type)t), derivedType);
211205
}
212206

213207
menu.ShowAsContext();
@@ -216,6 +210,23 @@ private void RenderManualConstraintMenu()
216210
}
217211
}
218212

213+
private void HighlightConstraint(Type type)
214+
{
215+
string highlightName;
216+
AddComponentMenu addComponentMenu = type.GetCustomAttribute<AddComponentMenu>();
217+
if (addComponentMenu != null)
218+
{
219+
// Parse the "nice" name as the last item from the add component menu
220+
highlightName = addComponentMenu.componentMenu.Replace('\\', '/').Split('/')[^1];
221+
}
222+
else
223+
{
224+
highlightName = $"{ObjectNames.NicifyVariableName(type.Name)} (Script)";
225+
}
226+
Highlighter.Highlight("Inspector", highlightName);
227+
GUIUtility.ExitGUI();
228+
}
229+
219230
/// <summary>
220231
/// Called by the Unity editor to render custom inspector UI for this component.
221232
/// </summary>
@@ -233,8 +244,7 @@ public override void OnInspectorGUI()
233244
{
234245
EditorGUILayout.Space();
235246

236-
EditorGUILayout.HelpBox(autoConstraintSelection.boolValue == true ? autoMsg : manualMsg
237-
, UnityEditor.MessageType.Info);
247+
EditorGUILayout.HelpBox(autoConstraintSelection.boolValue == true ? autoMsg : manualMsg, MessageType.Info);
238248
EditorGUILayout.Space();
239249

240250
int tab = autoConstraintSelection.boolValue == true ? 0 : 1;
@@ -276,7 +286,7 @@ public override void OnInspectorGUI()
276286
// we render the instance id of this component so our highlighting function can distinguish between
277287
// the different instances of constraint manager - highlighting in the inspector is currently
278288
// only available for string search which causes problems with multiple components of the same type
279-
// attached to the same gameobject.
289+
// attached to the same GameObject.
280290
EditorGUILayout.Space();
281291
EditorGUILayout.LabelField("ComponentId: " + constraintManager.GetInstanceID(), EditorStyles.miniLabel);
282292

@@ -302,15 +312,15 @@ public override void OnInspectorGUI()
302312
}
303313

304314
/// <summary>
305-
/// Util method for drawing a consistent constraints section.
315+
/// Utility method for drawing a consistent constraints section.
306316
/// Use this method in a component inspector for linking to a constraint manager.
307317
/// </summary>
308318
/// <param name="gameObject">Game object the constraint manager is attached to.</param>
309319
/// <param name="managerEnabled">Serialized property for enabling the manager - needs to be of type bool.</param>
310320
/// <param name="managerRef">Serialized property of the constraint manager component link - needs to be type of ConstraintManager.</param>
311321
/// <param name="isExpanded">Flag for indicating if the constraint foldout was previously collapsed or expanded.</param>
312322
/// <returns>Current state of expanded or collapsed constraint foldout. Returns true if expanded / contents visible.</returns>
313-
static public bool DrawConstraintManagerFoldout(GameObject gameObject, SerializedProperty managerEnabled, SerializedProperty managerRef, bool isExpanded)
323+
public static bool DrawConstraintManagerFoldout(GameObject gameObject, SerializedProperty managerEnabled, SerializedProperty managerRef, bool isExpanded)
314324
{
315325
isExpanded = EditorGUILayout.Foldout(isExpanded, "Constraints", true);
316326

@@ -340,7 +350,7 @@ static public bool DrawConstraintManagerFoldout(GameObject gameObject, Serialize
340350

341351
// popups will only show unique elements
342352
// in case of auto selection we don't care which one we're selecting as the behavior will be the same.
343-
// in case of manual selection users might want to differentiate which constraintmanager they are referring to.
353+
// in case of manual selection users might want to differentiate which ConstraintManager they are referring to.
344354
if (manager.AutoConstraintSelection == true)
345355
{
346356
options[i] = manager.GetType().Name + " (auto)";
@@ -361,7 +371,7 @@ static public bool DrawConstraintManagerFoldout(GameObject gameObject, Serialize
361371
{
362372
EditorGUIUtility.PingObject(selectedConstraintManager);
363373
Highlighter.Highlight("Inspector", $"ComponentId: {selectedConstraintManager.GetInstanceID()}");
364-
EditorGUIUtility.ExitGUI();
374+
GUIUtility.ExitGUI();
365375
}
366376
}
367377

0 commit comments

Comments
 (0)