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