Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
cleaned up service configuration profiles a bit (#852)
Browse files Browse the repository at this point in the history
# XRTK - Mixed Reality Toolkit Pull Request

## Overview
<!-- Please provide a clear and concise description of the pull request. -->
Cleaned up and simplified configuration serialized properties a bit.
  • Loading branch information
StephenHodgson authored Jun 11, 2021
1 parent 17284ab commit d224a36
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 132 deletions.
79 changes: 79 additions & 0 deletions Editor/Data/ConfigurationProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using UnityEditor;
using XRTK.Definitions.Utilities;

namespace XRTK.Editor.Data
{
internal class ConfigurationProperty
{
private readonly SerializedProperty configuration;

public bool IsExpanded
{
get => configuration.isExpanded;
set => configuration.isExpanded = value;
}

private readonly SerializedProperty name;

public string Name
{
get => name.stringValue;
set => name.stringValue = value;
}

private readonly SerializedProperty priority;

public uint Priority
{
get => (uint)priority.intValue;
set => priority.intValue = (int)value;
}

private readonly SerializedProperty instancedType;
private readonly SerializedProperty reference;

public Type InstancedType
{
get => new SystemType(instancedType);
set => reference.stringValue = value == null ? string.Empty : value.GUID.ToString();
}

private readonly SerializedProperty platformEntries;
private readonly SerializedProperty runtimePlatforms;

private readonly SerializedProperty profile;

public UnityEngine.Object Profile
{
get => profile.objectReferenceValue;
set => profile.objectReferenceValue = value;
}

public ConfigurationProperty(SerializedProperty property, bool clearPlatforms = false)
{
configuration = property;
name = property.FindPropertyRelative(nameof(name));
priority = property.FindPropertyRelative(nameof(priority));
instancedType = property.FindPropertyRelative(nameof(instancedType));
reference = instancedType.FindPropertyRelative(nameof(reference));
platformEntries = property.FindPropertyRelative(nameof(platformEntries));
runtimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms));

if (clearPlatforms)
{
runtimePlatforms.ClearArray();
}

profile = property.FindPropertyRelative(nameof(profile));
}

public void ApplyModifiedProperties()
{
configuration.serializedObject.ApplyModifiedProperties();
}
}
}
11 changes: 11 additions & 0 deletions Editor/Data/ConfigurationProperty.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using XRTK.Attributes;
using XRTK.Definitions;
using XRTK.Definitions.Utilities;
using XRTK.Editor.Data;
using XRTK.Editor.Extensions;
using XRTK.Editor.PropertyDrawers;
using XRTK.Extensions;
Expand All @@ -20,6 +21,7 @@ namespace XRTK.Editor.Profiles
[CustomEditor(typeof(MixedRealityPlatformServiceConfigurationProfile))]
public class MixedRealityPlatformServiceConfigurationProfileInspector : BaseMixedRealityProfileInspector
{

private readonly GUIContent profileContent = new GUIContent("Profile", "The settings profile for this service.");
private ReorderableList configurationList;
private int currentlySelectedConfigurationOption;
Expand Down Expand Up @@ -55,7 +57,8 @@ protected override void OnEnable()

private void UpdatePlatformList()
{
var runtimePlatforms = platformEntries.FindPropertyRelative("runtimePlatforms");
SerializedProperty runtimePlatforms;
runtimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms));

if (runtimePlatforms.arraySize > 0)
{
Expand All @@ -66,7 +69,7 @@ private void UpdatePlatformList()

for (int i = 0; i < runtimePlatforms.arraySize; i++)
{
platforms[i] = new SystemType(runtimePlatforms.GetArrayElementAtIndex(i).FindPropertyRelative("reference").stringValue);
platforms[i] = new SystemType(runtimePlatforms.GetArrayElementAtIndex(i));
}
}
else
Expand Down Expand Up @@ -143,34 +146,40 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive,

var configurationProperty = configurations.GetArrayElementAtIndex(index);

var nameProperty = configurationProperty.FindPropertyRelative("name");
var priorityProperty = configurationProperty.FindPropertyRelative("priority");
var instanceTypeProperty = configurationProperty.FindPropertyRelative("instancedType");
var platformEntriesProperty = configurationProperty.FindPropertyRelative("platformEntries");
var globalRuntimePlatformProperty = platformEntries.FindPropertyRelative("runtimePlatforms");
var runtimePlatformProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms");
var configurationProfileProperty = configurationProperty.FindPropertyRelative("profile");
var systemTypeReference = new SystemType(instanceTypeProperty.FindPropertyRelative("reference").stringValue);
SerializedProperty instancedType;
SerializedProperty priority;
SerializedProperty runtimePlatforms;
SerializedProperty profile;

var nameProperty = configurationProperty.FindPropertyRelative(nameof(name));
priority = configurationProperty.FindPropertyRelative(nameof(priority));
instancedType = configurationProperty.FindPropertyRelative(nameof(instancedType));
var platformEntriesProperty = configurationProperty.FindPropertyRelative(nameof(platformEntries));
var globalRuntimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms));
runtimePlatforms = platformEntriesProperty.FindPropertyRelative(nameof(runtimePlatforms));
profile = configurationProperty.FindPropertyRelative(nameof(profile));
var systemTypeReference = new SystemType(instancedType);

bool addPlatforms = false;

if (runtimePlatformProperty.arraySize != globalRuntimePlatformProperty.arraySize)
if (runtimePlatforms.arraySize != globalRuntimePlatforms.arraySize)
{
addPlatforms = true;
runtimePlatformProperty.ClearArray();
runtimePlatforms.ClearArray();
}

if (globalRuntimePlatformProperty.arraySize > 0)
if (globalRuntimePlatforms.arraySize > 0)
{
for (int i = 0; i < globalRuntimePlatformProperty.arraySize; i++)
for (int i = 0; i < globalRuntimePlatforms.arraySize; i++)
{
if (addPlatforms)
{
runtimePlatformProperty.InsertArrayElementAtIndex(i);
runtimePlatforms.InsertArrayElementAtIndex(i);
}

var globalPlatform = globalRuntimePlatformProperty.GetArrayElementAtIndex(i).FindPropertyRelative("reference").stringValue;
runtimePlatformProperty.GetArrayElementAtIndex(i).FindPropertyRelative("reference").stringValue = globalPlatform;
SerializedProperty reference;
reference = globalRuntimePlatforms.GetArrayElementAtIndex(i).FindPropertyRelative(nameof(reference));
runtimePlatforms.GetArrayElementAtIndex(i).FindPropertyRelative(nameof(reference)).stringValue = reference.stringValue;
}
}

Expand Down Expand Up @@ -210,7 +219,7 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive,
}
}

priorityProperty.intValue = index;
priority.intValue = index;

var lastMode = EditorGUIUtility.wideMode;
var prevLabelWidth = EditorGUIUtility.labelWidth;
Expand All @@ -227,8 +236,6 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive,
var profileRect = new Rect(rectX, rect.y + halfFieldHeight * 11, rectWidth, EditorGUIUtility.singleLineHeight);
var runtimeRect = new Rect(rectX, rect.y + halfFieldHeight * (hasProfile ? 16 : 11), rectWidth, EditorGUIUtility.singleLineHeight);

EditorGUI.BeginChangeCheck();

if (configurationProperty.isExpanded)
{
EditorGUI.PropertyField(nameRect, nameProperty);
Expand All @@ -244,12 +251,15 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive,
configurationProperty.isExpanded = EditorGUI.Foldout(nameRect, configurationProperty.isExpanded, nameProperty.stringValue, true);
}

var hasChanged = false;

if (configurationProperty.isExpanded)
{
EditorGUI.BeginChangeCheck();
TypeReferencePropertyDrawer.FilterConstraintOverride = IsConstraintSatisfied;
TypeReferencePropertyDrawer.GroupingOverride = TypeGrouping.NoneByNameNoNamespace;
EditorGUI.PropertyField(typeRect, instanceTypeProperty);
systemTypeReference = new SystemType(instanceTypeProperty.FindPropertyRelative("reference").stringValue);
EditorGUI.PropertyField(typeRect, instancedType);
systemTypeReference = new SystemType(instancedType);

GUI.enabled = false;

Expand All @@ -259,13 +269,19 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive,
if (hasProfile)
{
MixedRealityProfilePropertyDrawer.ProfileTypeOverride = profileType;
EditorGUI.PropertyField(profileRect, configurationProfileProperty, profileContent);
EditorGUI.PropertyField(profileRect, profile, profileContent);
}

hasChanged = EditorGUI.EndChangeCheck() &&
runtimePlatforms.arraySize > 0 &&
systemTypeReference.Type != null;
}

if (configurationProfileProperty.objectReferenceValue != null)
serializedObject.ApplyModifiedProperties();

if (profile.objectReferenceValue != null)
{
var renderedProfile = configurationProfileProperty.objectReferenceValue as BaseMixedRealityProfile;
var renderedProfile = profile.objectReferenceValue as BaseMixedRealityProfile;
Debug.Assert(renderedProfile != null);

if (renderedProfile.ParentProfile.IsNull() ||
Expand All @@ -275,16 +291,9 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive,
}
}

if (EditorGUI.EndChangeCheck())
if (MixedRealityToolkit.IsInitialized && hasChanged)
{
serializedObject.ApplyModifiedProperties();

if (MixedRealityToolkit.IsInitialized &&
runtimePlatformProperty.arraySize > 0 &&
systemTypeReference.Type != null)
{
MixedRealityToolkit.Instance.ResetProfile(MixedRealityToolkit.Instance.ActiveProfile);
}
MixedRealityToolkit.Instance.ResetProfile(MixedRealityToolkit.Instance.ActiveProfile);
}

EditorGUIUtility.wideMode = lastMode;
Expand Down Expand Up @@ -313,20 +322,16 @@ private void OnConfigurationOptionAdded(ReorderableList list)
configurations.arraySize += 1;
var index = configurations.arraySize - 1;

var configuration = configurations.GetArrayElementAtIndex(index);
configuration.isExpanded = true;
var nameProperty = configuration.FindPropertyRelative("name");
var priorityProperty = configuration.FindPropertyRelative("priority");
var instancedTypeProperty = configuration.FindPropertyRelative("instancedType");
var platformEntriesProperty = configuration.FindPropertyRelative("platformEntries");
var configurationProfileProperty = configuration.FindPropertyRelative("profile");
var runtimePlatformsProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms");

nameProperty.stringValue = $"New Configuration {index}";
instancedTypeProperty.FindPropertyRelative("reference").stringValue = string.Empty;
priorityProperty.intValue = index;
runtimePlatformsProperty.ClearArray();
configurationProfileProperty.objectReferenceValue = null;
var configuration = new ConfigurationProperty(configurations.GetArrayElementAtIndex(index), true)
{
IsExpanded = true,
Name = $"New Configuration {index}",
InstancedType = null,
Priority = (uint)index,
Profile = null,
};

configuration.ApplyModifiedProperties();
configListHeightFlags.Add(new Tuple<bool, bool>(true, false));
serializedObject.ApplyModifiedProperties();
}
Expand All @@ -346,4 +351,4 @@ private void OnConfigurationOptionRemoved(ReorderableList list)
}
}
}
}
}
Loading

0 comments on commit d224a36

Please sign in to comment.