Skip to content

Commit b14ddcd

Browse files
committed
Add interactable theme support for non-reflection based runtime instantiation
1 parent 8201a94 commit b14ddcd

File tree

4 files changed

+33
-50
lines changed

4 files changed

+33
-50
lines changed

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Interactable.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,6 @@ protected virtual void SetupEvents()
304304
/// </summary>
305305
protected virtual void SetupThemes()
306306
{
307-
InteractableProfileItem.ThemeLists lists = InteractableProfileItem.GetThemeTypes();
308307
runningThemesList = new List<InteractableThemeBase>();
309308
runningProfileSettings = new List<ProfileSettings>();
310309
for (int i = 0; i < Profiles.Count; i++)
@@ -322,7 +321,7 @@ protected virtual void SetupThemes()
322321
{
323322
InteractableThemePropertySettings settings = theme.Settings[n];
324323

325-
settings.Theme = InteractableProfileItem.GetTheme(settings, Profiles[i].Target, lists);
324+
settings.Theme = InteractableProfileItem.GetTheme(settings, Profiles[i].Target);
326325

327326
// add themes to theme list based on dimension
328327
if (j == dimensionIndex)

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Profile/InteractableProfileItem.cs

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.MixedReality.Toolkit.Core.Utilities.InspectorFields;
55
using Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes;
6+
using Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.TypeResolution;
67
using System;
78
using System.Collections;
89
using System.Collections.Generic;
@@ -19,44 +20,28 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Profile
1920
[System.Serializable]
2021
public class InteractableProfileItem
2122
{
22-
[System.Serializable]
23-
public struct ThemeLists
24-
{
25-
public List<Type> Types;
26-
public List<String> Names;
27-
}
28-
2923
public GameObject Target;
3024
public List<Theme> Themes;
3125
public bool HadDefaultTheme;
26+
27+
/// <summary>
28+
/// The list of base classes that will be included in interactable theme
29+
/// selection dropdowns.
30+
/// </summary>
31+
private static readonly List<Type> candidateThemeTypes = new List<Type>()
32+
{
33+
typeof(InteractableThemeBase),
34+
typeof(InteractableShaderTheme),
35+
typeof(InteractableColorTheme)
36+
};
3237

3338
/// <summary>
3439
/// Get a list of themes
3540
/// </summary>
3641
/// <returns></returns>
37-
public static ThemeLists GetThemeTypes()
42+
public static InteractableTypesContainer GetThemeTypes()
3843
{
39-
List<Type> themeTypes = new List<Type>();
40-
List<string> names = new List<string>();
41-
42-
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
43-
foreach (var assembly in assemblies)
44-
{
45-
foreach (Type type in assembly.GetTypes())
46-
{
47-
TypeInfo info = type.GetTypeInfo();
48-
if (info.BaseType != null && (info.BaseType.Equals(typeof(InteractableThemeBase)) || info.BaseType.Equals(typeof(InteractableShaderTheme)) || info.BaseType.Equals(typeof(InteractableColorTheme))))
49-
{
50-
themeTypes.Add(type);
51-
names.Add(type.Name);
52-
}
53-
}
54-
}
55-
56-
ThemeLists lists = new ThemeLists();
57-
lists.Types = themeTypes;
58-
lists.Names = names;
59-
return lists;
44+
return InteractableTypeFinder.Find(candidateThemeTypes, InteractableTypeFinder.TypeRestriction.DerivedOnly);
6045
}
6146

6247
/// <summary>
@@ -66,14 +51,12 @@ public static ThemeLists GetThemeTypes()
6651
/// <param name="host"></param>
6752
/// <param name="lists"></param>
6853
/// <returns></returns>
69-
public static InteractableThemeBase GetTheme(InteractableThemePropertySettings settings, GameObject host, ThemeLists lists)
54+
public static InteractableThemeBase GetTheme(InteractableThemePropertySettings settings, GameObject host)
7055
{
71-
int index = InspectorField.ReverseLookup(settings.Name, lists.Names.ToArray());
72-
Type themeType = lists.Types[index];
56+
Type themeType = Type.GetType(settings.AssemblyQualifiedName);
7357
InteractableThemeBase theme = (InteractableThemeBase)Activator.CreateInstance(themeType, host);
7458
theme.Init(host ,settings);
7559
return theme;
7660
}
77-
7861
}
7962
}

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableThemePropertySettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class ThemeTarget
4343
public struct InteractableThemePropertySettings
4444
{
4545
public string Name;
46+
public string AssemblyQualifiedName;
4647
public Type Type;
4748
public InteractableThemeBase Theme;
4849
public List<InteractableThemeProperty> Properties;

Assets/MixedRealityToolkit.SDK/Inspectors/UX/Interactable/ThemeInspector.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public class ThemeInspector : Editor
2828
{
2929
protected SerializedProperty settings;
3030

31-
protected static string[] themeOptions;
32-
protected static Type[] themeTypes;
31+
protected static InteractableTypesContainer themeOptions;
3332
protected static string[] shaderOptions;
3433
protected static State[] themeStates;
3534

@@ -185,9 +184,7 @@ protected virtual State[] GetStates()
185184

186185
protected void SetupThemeOptions()
187186
{
188-
InteractableProfileItem.ThemeLists lists = InteractableProfileItem.GetThemeTypes();
189-
themeOptions = lists.Names.ToArray();
190-
themeTypes = lists.Types.ToArray();
187+
themeOptions = InteractableProfileItem.GetThemeTypes();
191188
}
192189

193190
protected virtual void AddThemeProperty(int[] arr, SerializedProperty prop = null)
@@ -209,13 +206,16 @@ protected virtual void AddThemePropertySettings(SerializedProperty themeSettings
209206
{
210207
SerializedProperty settingsItem = themeSettings.GetArrayElementAtIndex(themeSettings.arraySize - 1);
211208
SerializedProperty className = settingsItem.FindPropertyRelative("Name");
209+
SerializedProperty assemblyQualifiedName = settingsItem.FindPropertyRelative("AssemblyQualifiedName");
212210
if (themeSettings.arraySize == 1)
213211
{
214212
className.stringValue = "ScaleOffsetColorTheme";
213+
assemblyQualifiedName.stringValue = typeof(ScaleOffsetColorTheme).AssemblyQualifiedName;
215214
}
216215
else
217216
{
218-
className.stringValue = themeOptions[0];
217+
className.stringValue = themeOptions.ClassNames[0];
218+
assemblyQualifiedName.stringValue = themeOptions.AssemblyQualifiedNames[0];
219219
}
220220

221221
SerializedProperty easing = settingsItem.FindPropertyRelative("Easing");
@@ -244,21 +244,19 @@ public static SerializedProperty ChangeThemeProperty(int index, SerializedProper
244244

245245
SerializedProperty className = settingsItem.FindPropertyRelative("Name");
246246

247-
InteractableProfileItem.ThemeLists lists = InteractableProfileItem.GetThemeTypes();
248-
string[] options = lists.Names.ToArray();
249-
Type[] types = lists.Types.ToArray();
247+
InteractableTypesContainer themeTypes = InteractableProfileItem.GetThemeTypes();
250248

251249
// get class value types
252250
if (!String.IsNullOrEmpty(className.stringValue))
253251
{
254-
int propIndex = InspectorUIUtility.ReverseLookup(className.stringValue, options);
252+
int propIndex = InspectorUIUtility.ReverseLookup(className.stringValue, themeTypes.ClassNames);
255253
GameObject renderHost = null;
256254
if (target != null)
257255
{
258256
renderHost = (GameObject)target.objectReferenceValue;
259257
}
260258

261-
InteractableThemeBase themeBase = (InteractableThemeBase)Activator.CreateInstance(types[propIndex], renderHost);
259+
InteractableThemeBase themeBase = (InteractableThemeBase)Activator.CreateInstance(themeTypes.Types[propIndex], renderHost);
262260

263261
// does this object have the right component types
264262
SerializedProperty isValid = settingsItem.FindPropertyRelative("IsValid");
@@ -766,7 +764,7 @@ public static SerializedProperty SerializeThemeValues(InteractableThemePropertyV
766764
return copyTo;
767765
}
768766

769-
public static void RenderThemeSettings(SerializedProperty themeSettings, SerializedObject themeObj, string[] themeOptions, SerializedProperty gameObject, int[] listIndex, State[] states)
767+
public static void RenderThemeSettings(SerializedProperty themeSettings, SerializedObject themeObj, InteractableTypesContainer themeOptions, SerializedProperty gameObject, int[] listIndex, State[] states)
770768
{
771769
GUIStyle box = InspectorUIUtility.Box(0);
772770
if (themeObj != null)
@@ -784,10 +782,10 @@ public static void RenderThemeSettings(SerializedProperty themeSettings, Seriali
784782
EditorGUILayout.BeginVertical(box);
785783
// a dropdown for the type of theme, they should make sense
786784
// show theme dropdown
787-
int id = InspectorUIUtility.ReverseLookup(className.stringValue, themeOptions);
785+
int id = InspectorUIUtility.ReverseLookup(className.stringValue, themeOptions.ClassNames);
788786

789787
EditorGUILayout.BeginHorizontal();
790-
int newId = EditorGUILayout.Popup("Theme Property", id, themeOptions);
788+
int newId = EditorGUILayout.Popup("Theme Property", id, themeOptions.ClassNames);
791789

792790
if (n > 0)
793791
{
@@ -809,7 +807,9 @@ public static void RenderThemeSettings(SerializedProperty themeSettings, Seriali
809807

810808
if (id != newId)
811809
{
812-
className.stringValue = themeOptions[newId];
810+
SerializedProperty assemblyQualifiedName = settingsItem.FindPropertyRelative("AssemblyQualifiedName");
811+
className.stringValue = themeOptions.ClassNames[newId];
812+
assemblyQualifiedName.stringValue = themeOptions.AssemblyQualifiedNames[newId];
813813

814814
// add the themeOjects if in a profile?
815815
//themeObj = ChangeThemeProperty(n, themeObj, gameObject);

0 commit comments

Comments
 (0)