Skip to content

Commit 867759b

Browse files
authored
Merge pull request #6482 from Troy-Ferrell/users/trferrel/profile-inspector-reduce
Improve profile inspector code
2 parents 7f273b2 + 3b203a7 commit 867759b

16 files changed

+499
-477
lines changed

Assets/MixedRealityToolkit/Definitions/BaseMixedRealityProfile.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using System;
45
using UnityEngine;
56

67
namespace Microsoft.MixedReality.Toolkit
78
{
9+
/// <summary>
10+
/// Base abstract class for all Mixed Reality profile configurations.
11+
/// Extends ScriptableObject and used as a property container to initialize MRTK services.
12+
/// </summary>
13+
[Serializable]
814
public abstract class BaseMixedRealityProfile : ScriptableObject
915
{
1016
[SerializeField]

Assets/MixedRealityToolkit/Definitions/CameraSystem/BaseCameraSettingsProfile.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
using Microsoft.MixedReality.Toolkit.Utilities;
54
using System;
6-
using UnityEngine;
7-
using UnityEngine.Serialization;
85

96
namespace Microsoft.MixedReality.Toolkit.CameraSystem
107
{
118
/// <summary>
129
/// Base class used to derive custom camera settings profiles.
1310
/// </summary>
11+
[Serializable]
1412
public class BaseCameraSettingsProfile : BaseMixedRealityProfile
1513
{
1614
// This class is intentionally blank. It exists for future expansion of common functionality.

Assets/MixedRealityToolkit/Definitions/CameraSystem/MixedRealityCameraSettingsConfiguration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public struct MixedRealityCameraSettingsConfiguration : IMixedRealityServiceConf
4646
[SerializeField]
4747
private BaseCameraSettingsProfile settingsProfile;
4848

49+
/// <inheritdoc />
50+
public BaseMixedRealityProfile Profile => settingsProfile;
51+
4952
/// <summary>
5053
/// Camera settings specific configuration profile.
5154
/// </summary>

Assets/MixedRealityToolkit/Definitions/MixedRealityInputDataProviderConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ public struct MixedRealityInputDataProviderConfiguration : IMixedRealityServiceC
3939
[SerializeField]
4040
private BaseMixedRealityProfile deviceManagerProfile;
4141

42+
/// <inheritdoc />
43+
public BaseMixedRealityProfile Profile => deviceManagerProfile;
44+
4245
/// <summary>
4346
/// Device manager specific configuration profile.
4447
/// </summary>
4548
public BaseMixedRealityProfile DeviceManagerProfile => deviceManagerProfile;
4649

47-
4850
/// <summary>
4951
/// Constructor.
5052
/// </summary>

Assets/MixedRealityToolkit/Definitions/MixedRealityServiceConfiguration.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ public MixedRealityServiceConfiguration(
6464
[SerializeField]
6565
private BaseMixedRealityProfile configurationProfile;
6666

67+
/// <inheritdoc />
68+
public BaseMixedRealityProfile Profile => configurationProfile;
69+
6770
/// <summary>
6871
/// The configuration profile for the service.
6972
/// </summary>
73+
[Obsolete("Use Profile property instead")]
7074
public BaseMixedRealityProfile ConfigurationProfile => configurationProfile;
7175
}
7276
}

Assets/MixedRealityToolkit/Definitions/MixedRealitySpatialObserverConfiguration.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public struct MixedRealitySpatialObserverConfiguration : IMixedRealityServiceCon
3939
[SerializeField]
4040
private BaseSpatialAwarenessObserverProfile observerProfile;
4141

42+
/// <inheritdoc />
43+
public BaseMixedRealityProfile Profile => observerProfile;
44+
4245
/// <summary>
4346
/// Spatial Observer specific configuration profile.
4447
/// </summary>

Assets/MixedRealityToolkit/Definitions/SpatialAwareness/BaseSpatialAwarenessObserverProfile.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

44
using Microsoft.MixedReality.Toolkit.Utilities;
5+
using System;
56
using UnityEngine;
67

78
namespace Microsoft.MixedReality.Toolkit.SpatialAwareness
89
{
10+
/// <summary>
11+
/// Abstract class that provides base profile information for Spatial Awareness Observers and their configuration
12+
/// </summary>
13+
[Serializable]
914
public abstract class BaseSpatialAwarenessObserverProfile : BaseMixedRealityProfile
1015
{
1116
[SerializeField]
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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.Utilities.Editor;
5+
using System;
6+
using System.Collections.Generic;
7+
using UnityEditor;
8+
using UnityEngine;
9+
10+
namespace Microsoft.MixedReality.Toolkit.Editor
11+
{
12+
/// <summary>
13+
/// Abstract class providing base functionality for data provider management in inspector. Useful for core systems that follow dataprovider access model.
14+
/// Designed to target ScriptableObject profile classes that configure services who support data providers.
15+
/// These profile ScriptableObject classes should contain an array of IMixedRealityServiceConfigurations that configure a list of data providers for this service configuration
16+
/// </summary>
17+
public abstract class BaseDataProviderServiceInspector : BaseMixedRealityToolkitConfigurationProfileInspector
18+
{
19+
/// <summary>
20+
/// Container class used to store references to serialized properties on a <see cref="IMixedRealityServiceConfiguration"/> target
21+
/// </summary>
22+
protected class ServiceConfigurationProperties
23+
{
24+
internal SerializedProperty componentName;
25+
internal SerializedProperty componentType;
26+
internal SerializedProperty providerProfile;
27+
internal SerializedProperty runtimePlatform;
28+
}
29+
30+
/// <summary>
31+
/// Returns SerializedProperty object that wraps references to array of <see cref="IMixedRealityServiceConfiguration"/> stored on the inspected target object
32+
/// </summary>
33+
protected abstract SerializedProperty GetDataProviderConfigurationList();
34+
35+
/// <summary>
36+
/// Builds <see cref="ServiceConfigurationProperties"/> container object with SerializedProperty references to associated properties on the supplied <see cref="IMixedRealityServiceConfiguration"/> reference
37+
/// </summary>
38+
/// <param name="providerEntry">SerializedProperty reference pointing to <see cref="IMixedRealityServiceConfiguration"/> instance</param>
39+
protected abstract ServiceConfigurationProperties GetDataProviderConfigurationProperties(SerializedProperty providerEntry);
40+
41+
/// <summary>
42+
/// Returns direct <see cref="IMixedRealityServiceConfiguration"/> instance at provided index in target object's array of <see cref="IMixedRealityServiceConfiguration"/> configurations
43+
/// </summary>
44+
protected abstract IMixedRealityServiceConfiguration GetDataProviderConfiguration(int index);
45+
46+
private SerializedProperty providerConfigurations;
47+
private List<bool> providerFoldouts = new List<bool>();
48+
49+
private static readonly GUIContent ComponentTypeLabel = new GUIContent("Type");
50+
private static readonly GUIContent SupportedPlatformsLabel = new GUIContent("Supported Platform(s)");
51+
52+
/// <inheritdoc/>
53+
protected override void OnEnable()
54+
{
55+
base.OnEnable();
56+
57+
providerConfigurations = GetDataProviderConfigurationList();
58+
59+
if (providerFoldouts == null || providerFoldouts.Count != providerConfigurations.arraySize)
60+
{
61+
providerFoldouts = new List<bool>(new bool[providerConfigurations.arraySize]);
62+
}
63+
}
64+
65+
/// <summary>
66+
/// Adds a new data provider profile entry (i.e <see cref="IMixedRealityServiceConfiguration"/>) to array list of target object
67+
/// Utilizes GetDataProviderConfigurationList() to get SerializedProperty object that represents array to insert against
68+
/// </summary>
69+
protected virtual void AddDataProvider()
70+
{
71+
providerConfigurations.InsertArrayElementAtIndex(providerConfigurations.arraySize);
72+
SerializedProperty provider = providerConfigurations.GetArrayElementAtIndex(providerConfigurations.arraySize - 1);
73+
74+
var providerProperties = GetDataProviderConfigurationProperties(provider);
75+
providerProperties.componentName.stringValue = $"New data provider {providerConfigurations.arraySize - 1}";
76+
providerProperties.runtimePlatform.intValue = -1;
77+
providerProperties.providerProfile.objectReferenceValue = null;
78+
79+
serializedObject.ApplyModifiedProperties();
80+
81+
var providerType = GetDataProviderConfiguration(providerConfigurations.arraySize - 1).ComponentType;
82+
providerType.Type = null;
83+
84+
providerFoldouts.Add(false);
85+
}
86+
87+
/// <summary>
88+
/// Removed given index item from <see cref="IMixedRealityServiceConfiguration"/> array list.
89+
/// Utilizes GetDataProviderConfigurationList() to get SerializedProperty object that represents array to delete against.
90+
/// </summary>
91+
protected virtual void RemoveDataProvider(int index)
92+
{
93+
providerConfigurations.DeleteArrayElementAtIndex(index);
94+
serializedObject.ApplyModifiedProperties();
95+
96+
providerFoldouts.RemoveAt(index);
97+
}
98+
99+
/// <summary>
100+
/// Applies the given concrete dataprovider type properties to the provided <see cref="IMixedRealityServiceConfiguration"/> instance (as represented by <see cref="ServiceConfigurationProperties"/>).
101+
/// Requires <see cref="MixedRealityDataProviderAttribute"/> on concrete type class to pull initial values
102+
/// that will be applied to the <see cref="ServiceConfigurationProperties"/> container SerializedProperties
103+
/// </summary>
104+
protected virtual void ApplyProviderConfiguration(Type dataProviderType, ServiceConfigurationProperties providerProperties)
105+
{
106+
if (dataProviderType != null)
107+
{
108+
MixedRealityDataProviderAttribute providerAttribute = MixedRealityDataProviderAttribute.Find(dataProviderType) as MixedRealityDataProviderAttribute;
109+
if (providerAttribute != null)
110+
{
111+
providerProperties.componentName.stringValue = !string.IsNullOrWhiteSpace(providerAttribute.Name) ? providerAttribute.Name : dataProviderType.Name;
112+
providerProperties.providerProfile.objectReferenceValue = providerAttribute.DefaultProfile;
113+
providerProperties.runtimePlatform.intValue = (int)providerAttribute.RuntimePlatforms;
114+
}
115+
else
116+
{
117+
providerProperties.componentName.stringValue = dataProviderType.Name;
118+
}
119+
120+
serializedObject.ApplyModifiedProperties();
121+
}
122+
}
123+
124+
/// <summary>
125+
/// Render list of data provider configuration profiles in inspector. Use provided add and remove content labels for the insert/remove buttons
126+
/// Returns true if any property has changed in this render pass, false otherwise
127+
/// </summary>
128+
protected bool RenderDataProviderList(GUIContent addContentLabel, GUIContent removeContentLabel, string errorMsg, Type dataProviderProfileType = null)
129+
{
130+
bool changed = false;
131+
132+
using (new EditorGUILayout.VerticalScope())
133+
{
134+
if (providerConfigurations == null || providerConfigurations.arraySize == 0)
135+
{
136+
EditorGUILayout.HelpBox(errorMsg, MessageType.Info);
137+
}
138+
139+
if (InspectorUIUtility.RenderIndentedButton(addContentLabel, EditorStyles.miniButton))
140+
{
141+
AddDataProvider();
142+
return true;
143+
}
144+
145+
for (int i = 0; i < providerConfigurations.arraySize; i++)
146+
{
147+
changed |= RenderDataProviderEntry(i, removeContentLabel, dataProviderProfileType);
148+
}
149+
150+
return changed;
151+
}
152+
}
153+
154+
/// <summary>
155+
/// Renders properties of <see cref="IMixedRealityServiceConfiguration"/> instance at provided index in inspector.
156+
/// Also renders inspector view of data provider's profile object and it's contents if applicable and foldout is expanded.
157+
/// </summary>
158+
protected bool RenderDataProviderEntry(int index, GUIContent removeContent, System.Type dataProviderProfileType = null)
159+
{
160+
bool changed = false;
161+
SerializedProperty provider = providerConfigurations.GetArrayElementAtIndex(index);
162+
ServiceConfigurationProperties providerProperties = GetDataProviderConfigurationProperties(provider);
163+
164+
using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
165+
{
166+
using (new EditorGUILayout.HorizontalScope())
167+
{
168+
providerFoldouts[index] = EditorGUILayout.Foldout(providerFoldouts[index], providerProperties.componentName.stringValue, true);
169+
170+
if (GUILayout.Button(removeContent, EditorStyles.miniButtonRight, GUILayout.Width(24f)))
171+
{
172+
RemoveDataProvider(index);
173+
return true;
174+
}
175+
}
176+
177+
if (providerFoldouts[index])
178+
{
179+
var serviceType = GetDataProviderConfiguration(index).ComponentType;
180+
181+
using (var c = new EditorGUI.ChangeCheckScope())
182+
{
183+
EditorGUILayout.PropertyField(providerProperties.componentType, ComponentTypeLabel);
184+
if (c.changed)
185+
{
186+
serializedObject.ApplyModifiedProperties();
187+
ApplyProviderConfiguration(serviceType.Type, providerProperties);
188+
return true;
189+
}
190+
191+
EditorGUILayout.PropertyField(providerProperties.runtimePlatform, SupportedPlatformsLabel);
192+
changed = c.changed;
193+
}
194+
195+
changed |= RenderProfile(providerProperties.providerProfile, dataProviderProfileType, true, false, serviceType);
196+
197+
serializedObject.ApplyModifiedProperties();
198+
}
199+
}
200+
201+
return changed;
202+
}
203+
}
204+
}

Assets/MixedRealityToolkit/Inspectors/Profiles/DataProviderAccessServiceInspector.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)