private static void OnSelectedTypeName(object userData)
        {
            selectedReference = SystemType.GetReference(userData as Type);
            var typeReferenceUpdatedEvent = EditorGUIUtility.CommandEvent("TypeReferenceUpdated");

            EditorWindow.focusedWindow.SendEvent(typeReferenceUpdatedEvent);
        }
Exemple #2
0
        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);
            menu.AddSeparator(string.Empty);

            foreach (var type in types)
            {
                var menuLabel = FormatGroupedTypeName(type, grouping);

                if (string.IsNullOrEmpty(menuLabel))
                {
                    continue;
                }

                var content = new GUIContent(menuLabel);
                menu.AddItem(content, type == selectedType, OnSelectedTypeName, type);
            }

            menu.DropDown(position);

            void OnSelectedTypeName(object typeRef)
            {
                SelectedType      = typeRef as Type;
                SelectedReference = SystemType.GetReference(SelectedType);
                EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent(TypeReferenceUpdated));
            }
        }
 private void OnGUI()
 {
     for (int i = 0; i < repairedTypeOptions.Length; i++)
     {
         if (GUILayout.Button(repairedTypeOptions[i].FullName, EditorStyles.miniButton))
         {
             property.stringValue = SystemType.GetReference(repairedTypeOptions[i]);
             property.serializedObject.ApplyModifiedProperties();
             Close();
         }
     }
 }
Exemple #4
0
 private void OnGUI()
 {
     foreach (var type in repairedTypeOptions)
     {
         if (GUILayout.Button(type.FullName, EditorStyles.miniButton))
         {
             property.stringValue = SystemType.GetReference(type);
             property.serializedObject.ApplyModifiedProperties();
             Close();
         }
     }
 }
Exemple #5
0
        private static bool TypeSearch(SerializedProperty property, ref string typeName, SystemTypeAttribute filter, bool showPickerWindow)
        {
            if (typeName.Contains(Missing))
            {
                return(false);
            }

            var typeNameWithoutAssembly  = typeName.Split(new[] { "," }, StringSplitOptions.None)[0];
            var typeNameWithoutNamespace = System.Text.RegularExpressions.Regex.Replace(typeNameWithoutAssembly, @"[.\w]+\.(\w+)", "$1");
            var repairedTypeOptions      = FindTypesByName(typeNameWithoutNamespace, filter);

            switch (repairedTypeOptions.Length)
            {
            case 0:
                if (showPickerWindow)
                {
                    EditorApplication.delayCall += () =>
                                                   EditorUtility.DisplayDialog(
                        "No types found",
                        $"No types with the name '{typeNameWithoutNamespace}' were found.",
                        "OK");
                }

                return(false);

            case 1:
                typeName = SystemType.GetReference(repairedTypeOptions[0]);
                return(true);

            default:
                if (showPickerWindow)
                {
                    EditorApplication.delayCall += () =>
                                                   SystemTypeRepairWindow.Display(repairedTypeOptions, property);
                }

                return(false);
            }
        }
        private static void DrawTypeSelectionControl(Rect position, SerializedProperty property, GUIContent label, SystemTypeAttribute filter)
        {
            try
            {
                Color restoreColor          = GUI.color;
                bool  restoreShowMixedValue = EditorGUI.showMixedValue;
                bool  typeResolved          = string.IsNullOrEmpty(property.stringValue) || ResolveType(property.stringValue) != null;
                EditorGUI.showMixedValue = property.hasMultipleDifferentValues;

                GUI.color = enabledColor;

                if (typeResolved)
                {
                    property.stringValue = DrawTypeSelectionControl(position, label, property.stringValue, filter, true);
                }
                else
                {
                    if (SelectRepairedTypeWindow.WindowOpen)
                    {
                        GUI.color = disabledColor;
                        DrawTypeSelectionControl(position, label, property.stringValue, filter, false);
                    }
                    else
                    {
                        Rect dropdownPosition = new Rect(position.x, position.y, position.width - 90, position.height);
                        Rect buttonPosition   = new Rect(position.x + position.width - 75, position.y, 75, position.height);

                        Color defaultColor = GUI.color;
                        GUI.color            = errorColor;
                        property.stringValue = DrawTypeSelectionControl(dropdownPosition, label, property.stringValue, filter, false);
                        GUI.color            = defaultColor;

                        if (GUI.Button(buttonPosition, "Try Repair", EditorStyles.miniButton))
                        {
                            string typeNameWithoutAssembly  = property.stringValue.Split(new string[] { "," }, StringSplitOptions.None)[0];
                            string typeNameWithoutNamespace = System.Text.RegularExpressions.Regex.Replace(typeNameWithoutAssembly, @"[.\w]+\.(\w+)", "$1");

                            Type[] repairedTypeOptions = FindTypesByName(typeNameWithoutNamespace, filter);
                            if (repairedTypeOptions.Length > 1)
                            {
                                SelectRepairedTypeWindow.Display(repairedTypeOptions, property);
                            }
                            else if (repairedTypeOptions.Length > 0)
                            {
                                property.stringValue = SystemType.GetReference(repairedTypeOptions[0]);
                            }
                            else
                            {
                                EditorUtility.DisplayDialog("No types found", "No types with the name '" + typeNameWithoutNamespace + "' were found.", "OK");
                            }
                        }
                    }
                }

                GUI.color = restoreColor;
                EditorGUI.showMixedValue = restoreShowMixedValue;
            }
            finally
            {
                ExcludedTypeCollectionGetter = null;
            }
        }
        private static void DrawTypeSelectionControl(Rect position, SerializedProperty runtimePlatformsProperty)
        {
            var triggerDropDown = false;
            var controlId       = GUIUtility.GetControlID(ControlHint, FocusType.Keyboard, position);

            switch (Event.current.GetTypeForControl(controlId))
            {
            case EventType.ExecuteCommand:
                if (Event.current.commandName == TypeReferenceUpdated &&
                    selectionControlId == controlId)
                {
                    if (runtimePlatformsProperty.arraySize != arraySize)
                    {
                        GUI.changed = true;
                    }

                    arraySize          = 0;
                    selectionControlId = 0;
                }

                break;

            case EventType.MouseDown:
                if (GUI.enabled && position.Contains(Event.current.mousePosition))
                {
                    GUIUtility.keyboardControl = controlId;
                    triggerDropDown            = true;
                    Event.current.Use();
                }

                break;

            case EventType.KeyDown:
                if (GUI.enabled && GUIUtility.keyboardControl == controlId)
                {
                    if (Event.current.keyCode == KeyCode.Return ||
                        Event.current.keyCode == KeyCode.Space)
                    {
                        triggerDropDown = true;
                        Event.current.Use();
                    }
                }

                break;

            case EventType.Repaint:
                switch (runtimePlatformsProperty.arraySize)
                {
                case 0:
                    TempContent.text = Nothing;
                    break;

                case 1:
                    // Remove assembly name and namespace from content of popup control.
                    var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(0);
                    var classRefProperty   = systemTypeProperty.FindPropertyRelative("reference");
                    var classRefParts      = classRefProperty.stringValue.Split(',');
                    var className          = classRefParts[0].Trim();
                    className        = className.Substring(className.LastIndexOf(".", StringComparison.Ordinal) + 1);
                    TempContent.text = className.Replace("Platform", "").ToProperCase();

                    if (TempContent.text == string.Empty)
                    {
                        TempContent.text = Nothing;
                    }

                    break;

                default:
                    TempContent.text = runtimePlatformsProperty.arraySize == MixedRealityToolkit.AvailablePlatforms.Count ? "Everything" : "Multiple...";
                    break;
                }

                EditorStyles.popup.Draw(position, TempContent, controlId);
                break;
            }

            if (triggerDropDown)
            {
                arraySize          = runtimePlatformsProperty.arraySize;
                selectionControlId = controlId;

                var menu = new GenericMenu();

                menu.AddItem(NothingContent, arraySize == 0, OnNothingSelected, null);
                menu.AddItem(EverythingContent, arraySize == MixedRealityToolkit.AvailablePlatforms.Count, OnEverythingSelected, null);

                for (var i = 0; i < MixedRealityToolkit.AvailablePlatforms.Count; i++)
                {
                    var platform     = MixedRealityToolkit.AvailablePlatforms[i];
                    var platformType = platform.GetType();
                    if (platformType == typeof(AllPlatforms))
                    {
                        continue;
                    }
                    menu.AddItem(new GUIContent(platformType.Name.Replace("Platform", "").ToProperCase()), IsPlatformActive(platformType), OnSelectedTypeName, platformType);
                }

                menu.DropDown(position);

                void OnNothingSelected(object _)
                {
                    runtimePlatformsProperty.ClearArray();
                    runtimePlatformsProperty.serializedObject.ApplyModifiedProperties();
                    EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent(TypeReferenceUpdated));
                }

                void OnEverythingSelected(object _)
                {
                    runtimePlatformsProperty.ClearArray();

                    for (int i = 0; i < MixedRealityToolkit.AvailablePlatforms.Count; i++)
                    {
                        AddPlatformReference(SystemType.GetReference(MixedRealityToolkit.AvailablePlatforms[i].GetType()));
                    }

                    runtimePlatformsProperty.serializedObject.ApplyModifiedProperties();
                    EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent(TypeReferenceUpdated));
                }

                void OnSelectedTypeName(object typeRef)
                {
                    var selectedPlatformType = typeRef as Type;
                    var selectedReference    = SystemType.GetReference(selectedPlatformType);

                    if (!TryRemovePlatformReference(selectedReference))
                    {
                        if (selectedPlatformType != null)
                        {
                            AddPlatformReference(selectedReference);
                        }

                        if (runtimePlatformsProperty.arraySize + 1 == MixedRealityToolkit.AvailablePlatforms.Count)
                        {
                            AddPlatformReference(SystemType.GetReference(typeof(AllPlatforms)));
                        }
                    }

                    EditorWindow.focusedWindow.SendEvent(EditorGUIUtility.CommandEvent(TypeReferenceUpdated));
                }

                bool IsPlatformActive(Type platformType)
                {
                    for (int i = 0; i < runtimePlatformsProperty.arraySize; i++)
                    {
                        var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(i);
                        var referenceProperty  = systemTypeProperty.FindPropertyRelative("reference");
                        var referenceType      = TypeExtensions.ResolveType(referenceProperty.stringValue);

                        // Clean up any broken references
                        if (referenceType == null)
                        {
                            Debug.LogError($"Failed to resolve {referenceProperty.stringValue}! Removing from runtime platform entry...");
                            runtimePlatformsProperty.DeleteArrayElementAtIndex(i);
                            runtimePlatformsProperty.serializedObject.ApplyModifiedProperties();
                            continue;
                        }

                        if (platformType == referenceType)
                        {
                            return(true);
                        }
                    }

                    return(false);
                }

                void AddPlatformReference(string classReference)
                {
                    var index = runtimePlatformsProperty.arraySize;

                    runtimePlatformsProperty.serializedObject.ApplyModifiedProperties();
                    runtimePlatformsProperty.InsertArrayElementAtIndex(index);
                    var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(index);
                    var referenceProperty  = systemTypeProperty.FindPropertyRelative("reference");

                    referenceProperty.stringValue = classReference;
                    runtimePlatformsProperty.serializedObject.ApplyModifiedProperties();
                }

                bool TryRemovePlatformReference(string classReference)
                {
                    var selectedPlatformType = TypeExtensions.ResolveType(classReference);

                    for (int i = 0; i < runtimePlatformsProperty.arraySize; i++)
                    {
                        var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(i);
                        var referenceProperty  = systemTypeProperty.FindPropertyRelative("reference");
                        var referenceType      = TypeExtensions.ResolveType(referenceProperty.stringValue);

                        if (selectedPlatformType == referenceType)
                        {
                            bool removeEveryPlatform = selectedPlatformType != typeof(AllPlatforms) &&
                                                       runtimePlatformsProperty.arraySize == MixedRealityToolkit.AvailablePlatforms.Count;

                            runtimePlatformsProperty.DeleteArrayElementAtIndex(i);

                            if (removeEveryPlatform)
                            {
                                TryRemovePlatformReference(SystemType.GetReference(typeof(AllPlatforms)));
                            }
                            else
                            {
                                runtimePlatformsProperty.serializedObject.ApplyModifiedProperties();
                            }

                            return(true);
                        }
                    }

                    return(false);
                }
            }
        }
 public InteractableType(Type type)
 {
     ClassName             = type.Name;
     AssemblyQualifiedName = SystemType.GetReference(type);
     Type = type;
 }