Skip to content

Commit d01a03a

Browse files
added first version of migrationhandler for bounds control
added missing public setters to configure bounds control in code
1 parent 21f0a6d commit d01a03a

File tree

4 files changed

+288
-17
lines changed

4 files changed

+288
-17
lines changed

Assets/MixedRealityToolkit.SDK/Experimental/Features/UX/BoundsControl/BoundsControl.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public BoundsControlActivationType BoundsControlActivation
131131
/// <summary>
132132
/// Check to draw a tether point from the handles to the hand when manipulating.
133133
/// </summary>
134-
public bool DrawTetherWhenManipulating => drawTetherWhenManipulating;
134+
public bool DrawTetherWhenManipulating { get => drawTetherWhenManipulating; set => drawTetherWhenManipulating = value;}
135135

136136
[SerializeField]
137137
[Tooltip("Add a Collider here if you do not want the handle colliders to interact with another object's collider.")]
@@ -140,7 +140,7 @@ public BoundsControlActivationType BoundsControlActivation
140140
/// <summary>
141141
/// Add a Collider here if you do not want the handle colliders to interact with another object's collider.
142142
/// </summary>
143-
public Collider HandlesIgnoreCollider => handlesIgnoreCollider;
143+
public Collider HandlesIgnoreCollider { get => handlesIgnoreCollider; set => handlesIgnoreCollider = value; }
144144

145145
[SerializeField]
146146
[Tooltip("Flatten bounds in the specified axis or flatten the smallest one if 'auto' is selected")]
@@ -207,46 +207,47 @@ public Vector3 BoxPadding
207207
/// <summary>
208208
/// Bounds control box display configuration section.
209209
/// </summary>
210-
public BoxDisplayConfiguration BoxDisplayConfiguration => boxDisplayConfiguration;
210+
public BoxDisplayConfiguration BoxDisplayConfiguration { get => boxDisplayConfiguration; set => boxDisplayConfiguration = value; }
211211

212212
[SerializeField]
213213
[Tooltip("This section defines the links / lines that are drawn between the corners of the control.")]
214214
private LinksConfiguration linksConfiguration;
215215
/// <summary>
216216
/// This section defines the links / lines that are drawn between the corners of the control.
217217
/// </summary>
218-
public LinksConfiguration LinksConfiguration => linksConfiguration;
218+
public LinksConfiguration LinksConfiguration { get => linksConfiguration; set => LinksConfiguration = value; }
219219

220220
[SerializeField]
221221
[Tooltip("Configuration of the scale handles.")]
222222
private ScaleHandlesConfiguration scaleHandlesConfiguration;
223223
/// <summary>
224224
/// Configuration of the scale handles.
225225
/// </summary>
226-
public ScaleHandlesConfiguration ScaleHandlesConfiguration => scaleHandlesConfiguration;
226+
public ScaleHandlesConfiguration ScaleHandlesConfiguration { get => scaleHandlesConfiguration; set => scaleHandlesConfiguration = value; }
227227

228228
[SerializeField]
229229
[Tooltip("Configuration of the rotation handles.")]
230230
private RotationHandlesConfiguration rotationHandlesConfiguration;
231231
/// <summary>
232232
/// Configuration of the rotation handles.
233233
/// </summary>
234-
public RotationHandlesConfiguration RotationHandles => rotationHandlesConfiguration;
234+
public RotationHandlesConfiguration RotationHandles { get => rotationHandlesConfiguration; set => rotationHandlesConfiguration = value; }
235235

236236
[SerializeField]
237237
[Tooltip("Configuration for Proximity Effect to scale handles or change materials on proximity.")]
238238
private ProximityEffectConfiguration handleProximityEffectConfiguration;
239239
/// <summary>
240240
/// Configuration for Proximity Effect to scale handles or change materials on proximity.
241241
/// </summary>
242-
public ProximityEffectConfiguration HandleProximityEffectConfiguration => handleProximityEffectConfiguration;
242+
public ProximityEffectConfiguration HandleProximityEffectConfiguration { get => handleProximityEffectConfiguration; set => handleProximityEffectConfiguration = value; }
243243

244244
[Header("Debug")]
245245
[Tooltip("Debug only. Component used to display debug messages.")]
246+
private TextMesh debugText;
246247
/// <summary>
247248
/// Component used to display debug messages.
248249
/// </summary>
249-
public TextMesh debugText;
250+
public TextMesh DebugText { get => debugText; set => debugText = value; }
250251

251252
[SerializeField]
252253
[Tooltip("Determines whether to hide GameObjects (i.e handles, links etc) created and managed by this component in the editor")]
@@ -275,31 +276,31 @@ public bool HideElementsInInspector
275276
/// <summary>
276277
/// Event that gets fired when interaction with a rotation handle starts.
277278
/// </summary>
278-
public UnityEvent RotateStarted => rotateStarted;
279+
public UnityEvent RotateStarted { get => rotateStarted; set => rotateStarted = value; }
279280

280281
[SerializeField]
281282
[Tooltip("Event that gets fired when interaction with a rotation handle stops.")]
282283
private UnityEvent rotateStopped = new UnityEvent();
283284
/// <summary>
284285
/// Event that gets fired when interaction with a rotation handle stops.
285286
/// </summary>
286-
public UnityEvent RotateStopped => rotateStopped;
287+
public UnityEvent RotateStopped { get => rotateStopped; set => rotateStopped = value; }
287288

288289
[SerializeField]
289290
[Tooltip("Event that gets fired when interaction with a scale handle starts.")]
290291
private UnityEvent scaleStarted = new UnityEvent();
291292
/// <summary>
292293
/// Event that gets fired when interaction with a scale handle starts.
293294
/// </summary>
294-
public UnityEvent ScaleStarted => scaleStarted;
295+
public UnityEvent ScaleStarted { get => scaleStarted; set => scaleStarted = value; }
295296

296297
[SerializeField]
297298
[Tooltip("Event that gets fired when interaction with a scale handle stops.")]
298299
private UnityEvent scaleStopped = new UnityEvent();
299300
/// <summary>
300301
/// Event that gets fired when interaction with a scale handle stops.
301302
/// </summary>
302-
public UnityEvent ScaleStopped => scaleStopped;
303+
public UnityEvent ScaleStopped { get => scaleStopped; set => scaleStopped = value; }
303304

304305
#endregion Serialized Fields
305306

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
using Microsoft.MixedReality.Toolkit.Experimental.UI.BoundsControl;
5+
using Microsoft.MixedReality.Toolkit.UI;
6+
using UnityEngine;
7+
using Microsoft.MixedReality.Toolkit.Experimental.UI.BoundsControlTypes;
8+
using UnityEditor;
9+
10+
namespace Microsoft.MixedReality.Toolkit.Experimental.Utilities
11+
{
12+
/// <summary>
13+
/// Migration handler for migrating bounding box gameobjects to bounds control gameobjects.
14+
/// </summary>
15+
public class BoundsControlMigrationHandler : IMigrationHandler
16+
{
17+
/// <inheritdoc />
18+
public bool CanMigrate(GameObject gameObject)
19+
{
20+
return gameObject.GetComponent<BoundingBox>() != null;
21+
}
22+
23+
/// <inheritdoc />
24+
public void Migrate(GameObject gameObject)
25+
{
26+
var boundingBox = gameObject.GetComponent<BoundingBox>();
27+
var boundsControl = gameObject.AddComponent<BoundsControl>();
28+
29+
// migrate logic settings
30+
boundsControl.Target = boundingBox.Target;
31+
boundsControl.BoundsOverride = boundingBox.BoundsOverride;
32+
boundsControl.CalculationMethod = MigrateCalculationMethod(boundingBox.CalculationMethod);
33+
boundsControl.BoundsControlActivation = MigrateActivationFlag(boundingBox.BoundingBoxActivation);
34+
35+
// only carry over min max scaling values if user hasn't attached min max scale constraint component yet
36+
if (gameObject.GetComponent<MinMaxScaleConstraint>() == null)
37+
{
38+
MinMaxScaleConstraint scaleConstraint = gameObject.AddComponent<MinMaxScaleConstraint>();
39+
#pragma warning disable 0618
40+
scaleConstraint.ScaleMinimum = boundingBox.ScaleMinimum;
41+
scaleConstraint.ScaleMaximum = boundingBox.ScaleMaximum;
42+
#pragma warning restore 0618
43+
}
44+
45+
// migrate visuals
46+
boundsControl.DrawTetherWhenManipulating = boundingBox.DrawTetherWhenManipulating;
47+
boundsControl.HandlesIgnoreCollider = boundingBox.HandlesIgnoreCollider;
48+
boundsControl.FlattenAxis = MigrateFlattenAxis(boundingBox.FlattenAxis);
49+
boundsControl.BoxPadding = boundingBox.BoxPadding;
50+
string configDir = GetBoundsControlConfigDirectory(boundingBox);
51+
MigrateBoxDisplay(boundsControl, boundingBox, configDir);
52+
MigrateLinks(boundsControl, boundingBox, configDir);
53+
MigrateScaleHandles(boundsControl, boundingBox, configDir);
54+
MigrateRotationHandles(boundsControl, boundingBox, configDir);
55+
MigrateProximityEffect(boundsControl, boundingBox, configDir);
56+
57+
// debug properties
58+
boundsControl.DebugText = boundingBox.debugText;
59+
boundsControl.HideElementsInInspector = boundingBox.HideElementsInInspector;
60+
61+
// events
62+
boundsControl.RotateStarted = boundingBox.RotateStarted;
63+
boundsControl.RotateStopped = boundingBox.RotateStopped;
64+
boundsControl.ScaleStarted = boundingBox.ScaleStarted;
65+
boundsControl.ScaleStopped = boundingBox.ScaleStopped;
66+
67+
// destroy obsolete component
68+
UnityEngine.Object.DestroyImmediate(boundingBox);
69+
}
70+
71+
private string GetBoundsControlConfigDirectory(BoundingBox boundingBox)
72+
{
73+
// todo: this needs a better logic but will work for converting the scene now
74+
var scene = boundingBox.gameObject.scene;
75+
if (scene != null)
76+
{
77+
string scenePath = scene.path;
78+
string dirPath = System.IO.Path.GetDirectoryName(scenePath);
79+
string configPath = System.IO.Path.Combine(dirPath, "BoundsControlConfigs/");
80+
return configPath;
81+
}
82+
83+
return "";
84+
}
85+
private string GenerateUniqueConfigName(string directory, GameObject migratingFrom, string configName)
86+
{
87+
return directory + migratingFrom.name + migratingFrom.GetInstanceID() + configName + ".asset";
88+
}
89+
90+
private BoundsCalculationMethod MigrateCalculationMethod(BoundingBox.BoundsCalculationMethod calculationMethod)
91+
{
92+
switch (calculationMethod)
93+
{
94+
case BoundingBox.BoundsCalculationMethod.RendererOverCollider:
95+
return BoundsCalculationMethod.RendererOverCollider;
96+
case BoundingBox.BoundsCalculationMethod.ColliderOverRenderer:
97+
return BoundsCalculationMethod.ColliderOverRenderer;
98+
case BoundingBox.BoundsCalculationMethod.ColliderOnly:
99+
return BoundsCalculationMethod.ColliderOnly;
100+
case BoundingBox.BoundsCalculationMethod.RendererOnly:
101+
return BoundsCalculationMethod.RendererOnly;
102+
}
103+
104+
Debug.Assert(false, "Tried to migrate unsupported bounds calculation method in bounding box / bounds control");
105+
return BoundsCalculationMethod.RendererOverCollider;
106+
}
107+
108+
private BoundsControlActivationType MigrateActivationFlag(BoundingBox.BoundingBoxActivationType activationFlag)
109+
{
110+
switch (activationFlag)
111+
{
112+
case BoundingBox.BoundingBoxActivationType.ActivateOnStart:
113+
return BoundsControlActivationType.ActivateOnStart;
114+
case BoundingBox.BoundingBoxActivationType.ActivateByProximity:
115+
return BoundsControlActivationType.ActivateByProximity;
116+
case BoundingBox.BoundingBoxActivationType.ActivateByPointer:
117+
return BoundsControlActivationType.ActivateByPointer;
118+
case BoundingBox.BoundingBoxActivationType.ActivateByProximityAndPointer:
119+
return BoundsControlActivationType.ActivateByProximityAndPointer;
120+
case BoundingBox.BoundingBoxActivationType.ActivateManually:
121+
return BoundsControlActivationType.ActivateManually;
122+
}
123+
124+
Debug.Assert(false, "Tried to migrate unsupported activation flag in bounding box / bounds control");
125+
return BoundsControlActivationType.ActivateOnStart;
126+
}
127+
128+
private FlattenModeType MigrateFlattenAxis(BoundingBox.FlattenModeType flattenAxisType)
129+
{
130+
switch (flattenAxisType)
131+
{
132+
case BoundingBox.FlattenModeType.DoNotFlatten:
133+
return FlattenModeType.DoNotFlatten;
134+
case BoundingBox.FlattenModeType.FlattenX:
135+
return FlattenModeType.FlattenX;
136+
case BoundingBox.FlattenModeType.FlattenY:
137+
return FlattenModeType.FlattenY;
138+
case BoundingBox.FlattenModeType.FlattenZ:
139+
return FlattenModeType.FlattenZ;
140+
case BoundingBox.FlattenModeType.FlattenAuto:
141+
return FlattenModeType.FlattenAuto;
142+
}
143+
144+
Debug.Assert(false, "Tried to migrate unsupported flatten axis type in bounding box / bounds control");
145+
return FlattenModeType.DoNotFlatten;
146+
}
147+
148+
WireframeType MigrateWireframeShape(BoundingBox.WireframeType wireframeType)
149+
{
150+
switch (wireframeType)
151+
{
152+
case BoundingBox.WireframeType.Cubic:
153+
return WireframeType.Cubic;
154+
case BoundingBox.WireframeType.Cylindrical:
155+
return WireframeType.Cylindrical;
156+
}
157+
158+
Debug.Assert(false, "Tried to migrate unsupported wireframe type in bounding box / bounds control");
159+
return WireframeType.Cubic;
160+
}
161+
162+
private RotationHandlePrefabCollider MigrateRotationHandleColliderType(BoundingBox.RotationHandlePrefabCollider rotationHandlePrefabColliderType)
163+
{
164+
switch (rotationHandlePrefabColliderType)
165+
{
166+
case BoundingBox.RotationHandlePrefabCollider.Sphere:
167+
return RotationHandlePrefabCollider.Sphere;
168+
case BoundingBox.RotationHandlePrefabCollider.Box:
169+
return RotationHandlePrefabCollider.Box;
170+
}
171+
172+
Debug.Assert(false, "Tried to migrate unsupported rotation handle collider type in bounding box / bounds control");
173+
return RotationHandlePrefabCollider.Sphere;
174+
}
175+
176+
private void MigrateBoxDisplay(BoundsControl control, BoundingBox box, string configAssetDirectory)
177+
{
178+
BoxDisplayConfiguration config = new BoxDisplayConfiguration();
179+
AssetDatabase.CreateAsset(config, GenerateUniqueConfigName(configAssetDirectory, box.gameObject, "BoxDisplayConfiguration"));
180+
181+
config.BoxMaterial = box.BoxMaterial;
182+
config.BoxGrabbedMaterial = box.BoxGrabbedMaterial;
183+
config.FlattenAxisDisplayScale = box.FlattenAxisDisplayScale;
184+
185+
control.BoxDisplayConfiguration = config;
186+
}
187+
188+
private void MigrateLinks(BoundsControl control, BoundingBox box, string configAssetDirectory)
189+
{
190+
LinksConfiguration config = new LinksConfiguration();
191+
AssetDatabase.CreateAsset(config, GenerateUniqueConfigName(configAssetDirectory, box.gameObject, "LinksConfiguration"));
192+
193+
config.WireframeMaterial = box.WireframeMaterial;
194+
config.WireframeEdgeRadius = box.WireframeEdgeRadius;
195+
config.WireframeShape = MigrateWireframeShape(box.WireframeShape);
196+
config.ShowWireFrame = box.ShowWireFrame;
197+
198+
control.LinksConfiguration = config;
199+
200+
}
201+
202+
private void MigrateScaleHandles(BoundsControl control, BoundingBox box, string configAssetDirectory)
203+
{
204+
ScaleHandlesConfiguration config = new ScaleHandlesConfiguration();
205+
AssetDatabase.CreateAsset(config, GenerateUniqueConfigName(configAssetDirectory, box.gameObject, "ScaleHandlesConfiguration"));
206+
207+
config.HandleSlatePrefab = box.ScaleHandleSlatePrefab;
208+
config.ShowScaleHandles = box.ShowScaleHandles;
209+
config.HandleMaterial = box.HandleMaterial;
210+
config.HandleGrabbedMaterial = box.HandleGrabbedMaterial;
211+
config.HandlePrefab = box.ScaleHandlePrefab;
212+
config.HandleSize = box.ScaleHandleSize;
213+
config.ColliderPadding = box.ScaleHandleColliderPadding;
214+
215+
control.ScaleHandlesConfiguration = config;
216+
}
217+
218+
private void MigrateRotationHandles(BoundsControl control, BoundingBox box, string configAssetDirectory)
219+
{
220+
RotationHandlesConfiguration config = new RotationHandlesConfiguration();
221+
AssetDatabase.CreateAsset(config, GenerateUniqueConfigName(configAssetDirectory, box.gameObject, "RotationHandlesConfiguration"));
222+
223+
config.RotationHandlePrefabColliderType = MigrateRotationHandleColliderType(box.RotationHandlePrefabColliderType);
224+
config.ShowRotationHandleForX = box.ShowRotationHandleForX;
225+
config.ShowRotationHandleForY = box.ShowRotationHandleForY;
226+
config.ShowRotationHandleForZ = box.ShowRotationHandleForZ;
227+
config.HandleMaterial = box.HandleMaterial;
228+
config.HandleGrabbedMaterial = box.HandleGrabbedMaterial;
229+
config.HandlePrefab = box.RotationHandleSlatePrefab;
230+
config.HandleSize = box.RotationHandleSize;
231+
config.ColliderPadding = box.RotateHandleColliderPadding;
232+
233+
control.RotationHandles = config;
234+
}
235+
236+
private void MigrateProximityEffect(BoundsControl control, BoundingBox box, string configAssetDirectory)
237+
{
238+
ProximityEffectConfiguration config = new ProximityEffectConfiguration();
239+
AssetDatabase.CreateAsset(config, GenerateUniqueConfigName(configAssetDirectory, box.gameObject, "ProximityEffectConfiguration"));
240+
241+
config.ProximityEffectActive = box.ProximityEffectActive;
242+
config.ObjectMediumProximity = box.HandleMediumProximity;
243+
config.ObjectCloseProximity = box.HandleCloseProximity;
244+
config.FarScale = box.FarScale;
245+
config.MediumScale = box.MediumScale;
246+
config.CloseScale = box.CloseScale;
247+
config.FarGrowRate = box.FarGrowRate;
248+
config.MediumGrowRate = box.MediumGrowRate;
249+
config.CloseGrowRate = box.CloseGrowRate;
250+
251+
control.HandleProximityEffectConfiguration = config;
252+
}
253+
}
254+
}

Assets/MixedRealityToolkit.SDK/Experimental/Features/Utilities/Migration/BoundsControlMigrationHandler.cs.meta

Lines changed: 11 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)