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+ }
0 commit comments