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

Updated the controller mapping profile inspector #486

Merged
merged 18 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace XRTK.Definitions.Controllers
/// <summary>
/// Custom Mixed Reality Controller class to inherit from to enable custom profile support in mapping profile.
/// </summary>
public class CustomMixedRealityControllerMappingProfile : BaseMixedRealityControllerMappingProfile
public abstract class CustomMixedRealityControllerMappingProfile : BaseMixedRealityControllerMappingProfile
{
/// <inheritdoc />
public override SupportedControllerType ControllerType => SupportedControllerType.None;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using XRTK.Extensions;
using XRTK.Inspectors.Utilities;
using XRTK.Definitions.Controllers;
using XRTK.Definitions.Utilities;
using XRTK.Inspectors.PropertyDrawers;
using XRTK.Services;

namespace XRTK.Inspectors.Profiles.InputSystem
Expand All @@ -18,12 +21,20 @@ public class MixedRealityControllerMappingProfilesInspector : BaseMixedRealityPr
private static readonly GUIContent RemoveMappingDefinitionContent = new GUIContent("-", "Remove Mapping Definition");

private SerializedProperty controllerMappingProfiles;
private List<Type> mappingTypes;
private bool changed;
private Rect dropdownRect;

protected override void OnEnable()
{
base.OnEnable();

controllerMappingProfiles = serializedObject.FindProperty(nameof(controllerMappingProfiles));

mappingTypes = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => typeof(BaseMixedRealityControllerMappingProfile).IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
.ToList();
}

public override void OnInspectorGUI()
Expand All @@ -33,73 +44,50 @@ public override void OnInspectorGUI()
EditorGUILayout.LabelField("Controller Input Mappings", EditorStyles.boldLabel);
EditorGUILayout.HelpBox("Use this profile to define all the controllers and their inputs your users will be able to use in your application.\n\n" +
"You'll want to define all your Input Actions and Controller Data Providers first so you can wire up actions to hardware sensors, controllers, gestures, and other input devices.", MessageType.Info);
EditorGUILayout.Space();

ThisProfile.CheckProfileLock();
serializedObject.Update();

EditorGUILayout.Space();
EditorGUILayout.LabelField("Select a profile type:");

changed = false;

bool changed = false;
var showDropdown = EditorGUILayout.DropdownButton(AddMappingDefinitionContent, FocusType.Keyboard);

if (GUILayout.Button(AddMappingDefinitionContent, EditorStyles.miniButton))
if (Event.current.type == EventType.Repaint)
{
controllerMappingProfiles.arraySize += 1;
var newItem = controllerMappingProfiles.GetArrayElementAtIndex(controllerMappingProfiles.arraySize - 1);
newItem.objectReferenceValue = null;
changed = true;
dropdownRect = GUILayoutUtility.GetLastRect();
}

EditorGUILayout.Space();

for (int i = 0; i < controllerMappingProfiles.arraySize; i++)
if (showDropdown)
{
var profileChanged = false;
var controllerProfile = controllerMappingProfiles.GetArrayElementAtIndex(i);
var profileObject = controllerProfile.objectReferenceValue;
var profileName = "Assign or create a profile";
TypeReferencePropertyDrawer.DisplayDropDown(dropdownRect, mappingTypes, null, TypeGrouping.ByNamespaceFlat);
}

if (profileObject != null)
if (Event.current.type == EventType.ExecuteCommand)
{
if (Event.current.commandName == TypeReferencePropertyDrawer.TypeReferenceUpdated)
{
profileName = controllerProfile.objectReferenceValue.name.ToProperCase().Replace("Default ", string.Empty).Replace(" Controller Mapping Profile", string.Empty);
}
controllerMappingProfiles.arraySize += 1;
var newItem = controllerMappingProfiles.GetArrayElementAtIndex(controllerMappingProfiles.arraySize - 1);
CreateNewProfileInstance(ThisProfile, newItem, TypeReferencePropertyDrawer.SelectedType);

EditorGUILayout.BeginHorizontal();
profileChanged |= RenderProfile(ThisProfile, controllerProfile, new GUIContent(profileName), false);
TypeReferencePropertyDrawer.SelectedType = null;
TypeReferencePropertyDrawer.SelectedReference = null;

if (profileChanged && controllerProfile.objectReferenceValue != null)
{
var knownProfiles = new List<Object>();

for (int j = 0; j < controllerMappingProfiles.arraySize; j++)
{
var knownProfile = controllerMappingProfiles.GetArrayElementAtIndex(j);

if (knownProfile.objectReferenceValue != null)
{
knownProfiles.Add(knownProfile.objectReferenceValue);
}
}

var count = 0;

for (int j = 0; j < knownProfiles.Count; j++)
{
if (knownProfiles[j] == controllerProfile.objectReferenceValue)
{
count++;
}
}

if (count >= 2)
{
Debug.LogWarning($"{controllerProfile.objectReferenceValue.name} is already registered!");
controllerProfile.objectReferenceValue = null;
serializedObject.ApplyModifiedProperties();
break;
}
changed = true;
}
}

EditorGUILayout.Space();

for (int i = 0; i < controllerMappingProfiles.arraySize; i++)
{
var controllerProfile = controllerMappingProfiles.GetArrayElementAtIndex(i);

changed |= profileChanged;
EditorGUILayout.BeginHorizontal();
changed |= RenderProfile(ThisProfile, controllerProfile, GUIContent.none, false);

if (GUILayout.Button(RemoveMappingDefinitionContent, EditorStyles.miniButtonRight, GUILayout.Width(24f)))
{
Expand All @@ -108,6 +96,11 @@ public override void OnInspectorGUI()
}

EditorGUILayout.EndHorizontal();

if (changed)
{
break;
}
}

serializedObject.ApplyModifiedProperties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ namespace XRTK.Inspectors.PropertyDrawers
[CustomPropertyDrawer(typeof(SystemTypeAttribute), true)]
public class TypeReferencePropertyDrawer : PropertyDrawer
{
public const string TypeReferenceUpdated = "TypeReferenceUpdated";

private const string None = "(None)";
private const string Missing = " {missing}";
private const string TypeReferenceUpdated = "TypeReferenceUpdated";

public static Type SelectedType;
public static string SelectedReference;

private static int selectionControlId;
private static string selectedReference;
private static readonly int ControlHint = typeof(TypeReferencePropertyDrawer).GetHashCode();
private static readonly GUIContent TempContent = new GUIContent();
private static readonly GUIContent RepairContent = new GUIContent("Repair", "Try to repair the reference");
Expand Down Expand Up @@ -162,14 +165,15 @@ private static void DrawTypeSelectionControl(Rect position, GUIContent label, re
if (Event.current.commandName == TypeReferenceUpdated &&
selectionControlId == controlId)
{
if (classRef != selectedReference)
if (classRef != SelectedReference)
{
classRef = selectedReference;
classRef = SelectedReference;
GUI.changed = true;
}

selectionControlId = 0;
selectedReference = null;
SelectedType = null;
SelectedReference = null;
}

break;
Expand Down Expand Up @@ -215,7 +219,7 @@ private static void DrawTypeSelectionControl(Rect position, GUIContent label, re
if (triggerDropDown)
{
selectionControlId = controlId;
selectedReference = classRef;
SelectedReference = classRef;

DisplayDropDown(position, GetFilteredTypes(filter), TypeExtensions.ResolveType(classRef), filter?.Grouping ?? TypeGrouping.ByNamespaceFlat);
}
Expand Down Expand Up @@ -343,7 +347,7 @@ private static Type[] FindTypesByName(string typeName, SystemTypeAttribute filte
return GetFilteredTypes(filter).Where(type => type.Name.Equals(typeName)).ToArray();
}

private static void DisplayDropDown(Rect position, IEnumerable<Type> types, Type selectedType, TypeGrouping grouping)
public static void DisplayDropDown(Rect position, IEnumerable<Type> types, Type selectedType, TypeGrouping grouping)
{
var menu = new GenericMenu();
menu.AddItem(new GUIContent(None), selectedType == null, OnSelectedTypeName, null);
Expand All @@ -360,6 +364,13 @@ private static void DisplayDropDown(Rect position, IEnumerable<Type> types, Type
}

menu.DropDown(position);

void OnSelectedTypeName(object typeRef)
{
SelectedType = typeRef as Type;
SelectedReference = SystemType.GetReference(SelectedType);
EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent(TypeReferenceUpdated));
}
}

private static string FormatGroupedTypeName(Type type, TypeGrouping grouping)
Expand Down Expand Up @@ -396,12 +407,6 @@ private static string FormatGroupedTypeName(Type type, TypeGrouping grouping)
}
}

private static void OnSelectedTypeName(object typeRef)
{
selectedReference = SystemType.GetReference(typeRef as Type);
EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent(TypeReferenceUpdated));
}

#endregion Control Drawing / Event Handling

/// <inheritdoc />
Expand Down