void ShowLayerOptions(StateLayer layer, Rect rect)
        {
            bool isScrolling = (Screen.width - rect.xMax) > 5;

            GUIStyle style = new GUIStyle("MiniToolbarPopup");
            style.fontStyle = FontStyle.Bold;

            rect.x = Screen.width - (isScrolling ? 60 : 45) - EditorGUI.indentLevel * 15;
            rect.y += 1;
            rect.width = 24 + EditorGUI.indentLevel * 15;

            if (!Application.isPlaying && (Event.current.type == EventType.ExecuteCommand || rect.Contains(Event.current.mousePosition)))
            {
                List<UnityEngine.Object> validParents = new List<UnityEngine.Object>();
                List<Type> layerTypes = new List<Type>();
                List<string> options = new List<string> { "..." };
                bool machineIsParent = StateMachineUtility.IsParent(layer, machine);

                foreach (KeyValuePair<Type, List<Type>> pair in StateMachineUtility.LayerTypeStateTypeDict)
                {
                    PropertyInfo layerProperty = pair.Key.GetProperty("Layer", ReflectionExtensions.AllFlags);
                    PropertyInfo machineProperty = pair.Key.GetProperty("Machine", ReflectionExtensions.AllFlags);

                    if ((machineProperty == null || machineProperty.PropertyType.IsInstanceOfType(machine)) && (layerProperty == null || layerProperty.PropertyType.IsInstanceOfType(layer)) && Array.TrueForAll(existingLayers, existingLayer => existingLayer.GetType() != pair.Key))
                    {
                        layerTypes.Add(pair.Key);
                        options.Add("Add/" + StateMachineUtility.LayerTypeFormattedDict[pair.Key]);
                    }
                }

                if (!machineIsParent)
                {
                    validParents.Add(machine);
                    options.Add("Move To/Machine");
                }

                for (int i = 0; i < existingLayers.Length; i++)
                {
                    StateLayer existingLayer = existingLayers[i];

                    if (layer != existingLayer && !StateMachineUtility.IsParent(layer, existingLayer) && !StateMachineUtility.GetSubLayersRecursive(layer).Contains(existingLayer))
                    {
                        validParents.Add(existingLayer);
                        options.Add("Move To/" + StateMachineUtility.FormatLayer(existingLayer.GetType()));
                    }
                }

                options.Add("Copy");

                if (EditorPrefs.GetString("Clipboard Layer Type") == layer.GetType().Name)
                {
                    options.Add("Paste/Layer");
                    options.Add("Paste/Layer and States");
                    options.Add("Paste/Layer and Sublayers");
                    options.Add("Paste/All");
                }

                options.Add("Generate");

                if (Selection.gameObjects.Length <= 1)
                {
                    //					int index = EditorGUI.Popup(rect, layerTypes.IndexOf(selectedLayerType) + 1, options.ToArray(), style) - 1;
                    int index = EditorGUI.Popup(rect, 0, options.ToArray(), style) - 1;
                    string option = index == -1 ? "" : options[index + 1];

                    if (index < layerTypes.Count)
                    {
                        selectedLayerType = index == -1 ? null : layerTypes[Mathf.Clamp(index, 0, options.Count - 1)];

                        if (selectedLayerType != null)
                        {
                            StateMachineUtility.AddLayer(machine, selectedLayerType, layer);
                            selectedLayerType = null;
                        }
                    }
                    else if (option.StartsWith("Move To"))
                    {
                        UnityEngine.Object parent = validParents[index - layerTypes.Count];
                        StateMachineUtility.MoveLayerTo(layer, parent);
                    }
                    else if (option.StartsWith("Copy"))
                    {
                        EditorPrefs.SetString("Clipboard Layer Type", layer.GetType().Name);
                        EditorPrefs.SetInt("Clipboard Layer ID", layer.GetInstanceID());
                    }
                    else if (option.StartsWith("Paste"))
                    {
                        StateLayer layerToCopy = EditorUtility.InstanceIDToObject(EditorPrefs.GetInt("Clipboard Layer ID")) as StateLayer;

                        if (option == "Paste/Layer")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, false, false);
                        }
                        else if (option == "Paste/Layer and States")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, true, false);
                        }
                        else if (option == "Paste/Layer and Sublayers")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, false, true);
                        }
                        else if (option == "Paste/All")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, true, true);
                        }

                        EditorPrefs.SetString("Clipboard Layer Type", "");
                        EditorPrefs.SetInt("Clipboard Layer ID", 0);
                    }
                    else if (option.StartsWith("Generate"))
                    {
                        StateMachineGeneratorWindow generator = StateMachineGeneratorWindow.Create();

                        string layerTypeName = layer.GetTypeName();
                        string layerScriptPath = AssetDatabaseUtility.GetAssetPath(layerTypeName + ".cs");

                        generator.Path = string.IsNullOrEmpty(layerScriptPath) ? generator.Path : Path.GetDirectoryName(layerScriptPath);
                        generator.Machine = StateMachineUtility.FormatMachine(machine);
                        generator.Layer = layerTypeName;
                        generator.Inherit = StateMachineUtility.FormatLayer(layer.GetType().BaseType);
                        generator.SubLayer = layer.Layer == null ? "" : StateMachineUtility.FormatLayer(layer.Layer.GetType());
                    }
                }
            }
            else
            {
                EditorGUI.BeginDisabledGroup(Application.isPlaying);

                EditorGUI.Popup(rect, 0, new[] { "..." }, style);

                EditorGUI.EndDisabledGroup();
            }
        }
Esempio n. 2
0
        void ShowLayerOptions(StateLayer layer, Rect rect)
        {
            bool isScrolling = (Screen.width - rect.xMax) > 5;

            GUIStyle style = new GUIStyle("MiniToolbarPopup");

            style.fontStyle = FontStyle.Bold;

            rect.x     = Screen.width - (isScrolling ? 60 : 45) - EditorGUI.indentLevel * 15;
            rect.y    += 1;
            rect.width = 24 + EditorGUI.indentLevel * 15;

            if (!Application.isPlaying && (Event.current.type == EventType.ExecuteCommand || rect.Contains(Event.current.mousePosition)))
            {
                List <UnityEngine.Object> validParents = new List <UnityEngine.Object>();
                List <Type>   layerTypes = new List <Type>();
                List <string> options    = new List <string> {
                    "..."
                };
                bool machineIsParent = StateMachineUtility.IsParent(layer, machine);

                foreach (KeyValuePair <Type, List <Type> > pair in StateMachineUtility.LayerTypeStateTypeDict)
                {
                    PropertyInfo layerProperty   = pair.Key.GetProperty("Layer", ReflectionExtensions.AllFlags);
                    PropertyInfo machineProperty = pair.Key.GetProperty("Machine", ReflectionExtensions.AllFlags);

                    if ((machineProperty == null || machineProperty.PropertyType.IsInstanceOfType(machine)) && (layerProperty == null || layerProperty.PropertyType.IsInstanceOfType(layer)) && Array.TrueForAll(existingLayers, existingLayer => existingLayer.GetType() != pair.Key))
                    {
                        layerTypes.Add(pair.Key);
                        options.Add("Add/" + StateMachineUtility.LayerTypeFormattedDict[pair.Key]);
                    }
                }

                if (!machineIsParent)
                {
                    validParents.Add(machine);
                    options.Add("Move To/Machine");
                }

                for (int i = 0; i < existingLayers.Length; i++)
                {
                    StateLayer existingLayer = existingLayers[i];

                    if (layer != existingLayer && !StateMachineUtility.IsParent(layer, existingLayer) && !StateMachineUtility.GetSubLayersRecursive(layer).Contains(existingLayer))
                    {
                        validParents.Add(existingLayer);
                        options.Add("Move To/" + StateMachineUtility.FormatLayer(existingLayer.GetType()));
                    }
                }

                options.Add("Copy");

                if (EditorPrefs.GetString("Clipboard Layer Type") == layer.GetType().Name)
                {
                    options.Add("Paste/Layer");
                    options.Add("Paste/Layer and States");
                    options.Add("Paste/Layer and Sublayers");
                    options.Add("Paste/All");
                }

                options.Add("Generate");

                if (Selection.gameObjects.Length <= 1)
                {
                    //					int index = EditorGUI.Popup(rect, layerTypes.IndexOf(selectedLayerType) + 1, options.ToArray(), style) - 1;
                    int    index  = EditorGUI.Popup(rect, 0, options.ToArray(), style) - 1;
                    string option = index == -1 ? "" : options[index + 1];

                    if (index < layerTypes.Count)
                    {
                        selectedLayerType = index == -1 ? null : layerTypes[Mathf.Clamp(index, 0, options.Count - 1)];

                        if (selectedLayerType != null)
                        {
                            StateMachineUtility.AddLayer(machine, selectedLayerType, layer);
                            selectedLayerType = null;
                        }
                    }
                    else if (option.StartsWith("Move To"))
                    {
                        UnityEngine.Object parent = validParents[index - layerTypes.Count];
                        StateMachineUtility.MoveLayerTo(layer, parent);
                    }
                    else if (option.StartsWith("Copy"))
                    {
                        EditorPrefs.SetString("Clipboard Layer Type", layer.GetType().Name);
                        EditorPrefs.SetInt("Clipboard Layer ID", layer.GetInstanceID());
                    }
                    else if (option.StartsWith("Paste"))
                    {
                        StateLayer layerToCopy = EditorUtility.InstanceIDToObject(EditorPrefs.GetInt("Clipboard Layer ID")) as StateLayer;

                        if (option == "Paste/Layer")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, false, false);
                        }
                        else if (option == "Paste/Layer and States")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, true, false);
                        }
                        else if (option == "Paste/Layer and Sublayers")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, false, true);
                        }
                        else if (option == "Paste/All")
                        {
                            StateMachineUtility.CopyLayer(layer, layerToCopy, true, true);
                        }

                        EditorPrefs.SetString("Clipboard Layer Type", "");
                        EditorPrefs.SetInt("Clipboard Layer ID", 0);
                    }
                    else if (option.StartsWith("Generate"))
                    {
                        StateMachineGeneratorWindow generator = StateMachineGeneratorWindow.Create();

                        string layerTypeName   = layer.GetTypeName();
                        string layerScriptPath = AssetDatabaseUtility.GetAssetPath(layerTypeName + ".cs");

                        generator.Path     = string.IsNullOrEmpty(layerScriptPath) ? generator.Path : Path.GetDirectoryName(layerScriptPath);
                        generator.Machine  = StateMachineUtility.FormatMachine(machine);
                        generator.Layer    = layerTypeName;
                        generator.Inherit  = StateMachineUtility.FormatLayer(layer.GetType().BaseType);
                        generator.SubLayer = layer.Layer == null ? "" : StateMachineUtility.FormatLayer(layer.Layer.GetType());
                    }
                }
            }
            else
            {
                EditorGUI.BeginDisabledGroup(Application.isPlaying);

                EditorGUI.Popup(rect, 0, new[] { "..." }, style);

                EditorGUI.EndDisabledGroup();
            }
        }