Example #1
0
        private static bool DrawDeleteStateButton(PersistedInt selectedLayer, PersistedInt selectedState)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            var deleteThisState = EditorUtilities.AreYouSureButton("Delete state", "are you sure", "DeleteState_" + selectedState + "_" + selectedLayer, 1f);

            EditorGUILayout.EndHorizontal();
            return(deleteThisState);
        }
        private void DrawLayerSelection()
        {
            var numLayers = animationPlayer.layers.Length;

            selectedLayer.SetTo(Mathf.Clamp(selectedLayer, 0, numLayers));

            var twoLines = Screen.width < 420f;

            EditorGUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();

                selectedLayer.SetTo(EditorUtilities.DrawLeftButton(selectedLayer));
                EditorGUILayout.LabelField("Selected layer: " + selectedLayer, GUILayout.Width(selectedLayerWidth));
                selectedLayer.SetTo(EditorUtilities.DrawRightButton(selectedLayer, numLayers));

                if (twoLines)
                {
                    GUILayout.FlexibleSpace();
                    EditorGUILayout.EndHorizontal();
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                }
                else
                {
                    GUILayout.Space(10f);
                }

                if (GUILayout.Button("Add layer", GUILayout.Width(100f)))
                {
                    EditorUtilities.RecordUndo(animationPlayer, "Add layer to animation player");
                    EditorUtilities.ExpandArrayByOne(ref animationPlayer.layers, AnimationLayer.CreateLayer);
                    selectedLayer.SetTo(animationPlayer.layers.Length - 1);
                    MarkDirty();
                }

                EditorGUI.BeginDisabledGroup(numLayers < 2);
                {
                    if (EditorUtilities.AreYouSureButton("Delete layer", "Are you sure?", "DeleteLayer" + selectedLayer, 1f, GUILayout.Width(100f)))
                    {
                        EditorUtilities.RecordUndo(animationPlayer, "Delete layer from animation player");
                        EditorUtilities.DeleteIndexFromArray(ref animationPlayer.layers, selectedLayer);
                        selectedLayer.SetTo(Mathf.Max(0, selectedLayer - 1));
                        MarkDirty();
                    }
                }
                EditorGUI.EndDisabledGroup();

                GUILayout.FlexibleSpace();
            }
            EditorGUILayout.EndHorizontal();
        }
Example #3
0
        private static void DrawSelectedTransition(AnimationPlayer animationPlayer, StateTransition selectedTransition, string fromStateName,
                                                   string toStateName, AnimationLayer layer, List <StateTransition> allTransitionsFromState)
        {
            if (selectedTransition != null)
            {
                EditorGUILayout.LabelField($"Selected transition: From \"{fromStateName}\" to \"{toStateName}\"");
                Undo.RecordObject(animationPlayer, $"Edit of transition from  {fromStateName} to {toStateName}");

                EditorUtilities.DrawIndented(() =>
                {
                    EditorGUI.BeginChangeCheck();
                    selectedTransition.isDefault = EditorGUILayout.Toggle("Is Default", selectedTransition.isDefault);
                    if (EditorGUI.EndChangeCheck() && selectedTransition.isDefault)
                    {
                        foreach (var transition in allTransitionsFromState)
                        {
                            if (transition != selectedTransition && transition.ToState == selectedTransition.ToState)
                            {
                                transition.isDefault = false;
                            }
                        }
                    }

                    selectedTransition.name = EditorGUILayout.TextField("Name", selectedTransition.name);

                    selectedTransition.transitionData = DrawTransitionData(selectedTransition.transitionData);
                    GUILayout.Space(20f);
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (EditorUtilities.AreYouSureButton("Clear transition", "Are you sure?", $"Clear_Transition_{fromStateName}_{toStateName}", 1f, GUILayout.Width(150f)))
                    {
                        Undo.RecordObject(animationPlayer, $"Clear transition from  {fromStateName} to {toStateName}");
                        layer.transitions.Remove(selectedTransition);
                    }

                    EditorGUILayout.EndHorizontal();
                });
            }
        }
        public static void DrawTransitions(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedStateIdx,
                                           PersistedInt selectedToStateIdx, string[] stateNamesInLayer)
        {
            var layer = animationPlayer.layers[selectedLayer];

            if (layer.states.Count == 0)
            {
                EditorGUILayout.LabelField("No states, can't define transitions");
                return;
            }

            var selectedState      = layer.states[selectedStateIdx];
            var selectedToState    = layer.states[selectedToStateIdx];
            var selectedTransition = layer.transitions.Find(t => t.FromState == selectedState && t.ToState == selectedToState);
            var fromStateName      = selectedState.Name;
            var toStateName        = selectedToState.Name;

            if (selectedTransition != null)
            {
                EditorGUILayout.LabelField($"Selected transition: From \"{fromStateName}\" to \"{toStateName}\"");
                EditorUtilities.RecordUndo(animationPlayer, $"Edit of transition from  {fromStateName} to {toStateName}");

                EditorUtilities.DrawIndented(() =>
                {
                    selectedTransition.transitionData = DrawTransitionData(selectedTransition.transitionData);
                    GUILayout.Space(20f);
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.FlexibleSpace();
                    if (EditorUtilities.AreYouSureButton("Clear transition", "Are you sure?",
                                                         "Clear_Transition_" + fromStateName + "_" + toStateName,
                                                         1f, GUILayout.Width(150f)))
                    {
                        EditorUtilities.RecordUndo(animationPlayer, $"Clear transition from  {fromStateName} to {toStateName}");
                        layer.transitions.Remove(selectedTransition);
                    }

                    EditorGUILayout.EndHorizontal();
                });
            }

            EditorUtilities.Splitter();

            var transitionsFromState = layer.transitions.Where(t => t.FromState == selectedState).ToList();

            if (transitionsFromState.Count == 0)
            {
                EditorGUILayout.LabelField($"No defined transitions from {fromStateName}");
            }
            else
            {
                EditorGUILayout.LabelField($"Transitions from {fromStateName}:");

                EditorGUILayout.Space();
                EditorUtilities.DrawHorizontal(() =>
                {
                    GUILayout.FlexibleSpace();
                    EditorUtilities.DrawVertical(() =>
                    {
                        EditorUtilities.DrawIndented(() =>
                        {
                            foreach (var transition in transitionsFromState)
                            {
                                EditorGUI.BeginDisabledGroup(transition == selectedTransition);
                                if (GUILayout.Button(transition.ToState.Name, GUILayout.MinWidth(100f)))
                                {
                                    selectedToStateIdx.SetTo(layer.states.IndexOf(transition.ToState));
                                }
                                EditorGUI.EndDisabledGroup();
                            }
                        });
                    });
                });
            }

            EditorGUILayout.Space();

            EditorUtilities.DrawHorizontal(() =>
            {
                GUILayout.FlexibleSpace();
                if (GUILayout.Button("Create new transition"))
                {
                    GenericMenu menu = new GenericMenu();
                    foreach (var state in stateNamesInLayer)
                    {
                        menu.AddItem(new GUIContent($"Transition from {fromStateName} to {state}"), false, () =>
                        {
                            EditorUtilities.RecordUndo(animationPlayer, $"Adding transition from {fromStateName} to {toStateName}");
                            var newState = new StateTransition
                            {
                                FromState      = selectedState,
                                ToState        = layer.states.Find(s => s.Name == state),
                                transitionData = TransitionData.Linear(.2f)
                            };
                            layer.transitions.Add(newState);
                            selectedToStateIdx.SetTo(layer.states.FindIndex(s => s.Name == state));
                        });
                    }

                    menu.ShowAsContext();
                }
            });
        }
Example #5
0
        public static void DrawStateData(AnimationPlayer animationPlayer, int selectedLayer, PersistedInt selectedState, AnimationPlayerEditor currentEditor)
        {
            ReloadCheck();

            var markDirty = false;
            var layer     = animationPlayer.layers[selectedLayer];

            if (layer.states.Count == 0)
            {
                EditorGUILayout.LabelField("No states");
                return;
            }

            if (!layer.states.IsInBounds(selectedState))
            {
                Debug.LogError($"Out of bounds state: {selectedState} out of {layer.states.Count} states! Resetting to 0");
                selectedState.SetTo(0);
                return;
            }

            var stateName = layer.states[selectedState].Name;

            EditorGUILayout.LabelField($"Settings for \"{stateName}\":");

            EditorGUI.indentLevel++;
            DrawStateData(layer.states[selectedState], ref markDirty);

            GUILayout.Space(20f);

            EditorGUILayout.BeginHorizontal();
            if (selectedState == 0)
            {
                EditorGUILayout.LabelField("This is the default state of this layer", GUILayout.Width(250f));
            }
            else if (GUILayout.Button("Set as the default state of this layer", GUILayout.Width(250f)))
            {
                layer.states.Swap(selectedState, 0);
                selectedState.SetTo(0);
                markDirty = true;
            }
            GUILayout.FlexibleSpace();
            var deleteThisState = EditorUtilities.AreYouSureButton($"Delete {stateName}", "Are you sure", $"DeleteState_{selectedState}_{selectedLayer}", 1f);

            EditorGUILayout.EndHorizontal();
            EditorGUI.indentLevel--;

            GUILayout.Space(20f);

            currentEditor.previewer.DrawStatePreview(selectedLayer, selectedState);

            if (deleteThisState)
            {
                DeleteState(animationPlayer, layer, selectedState);
                markDirty = true;
            }

            if (markDirty)
            {
                currentEditor.MarkDirty();
            }
        }
        public static void DrawStateData(AnimationPlayer animationPlayer, PersistedInt selectedLayer, PersistedInt selectedState,
                                         AnimationPlayerEditor currentEditor)
        {
            if (reloadChecker == null)
            {
                reloadChecker       = new object();
                upDownButtonOptions = new[] { GUILayout.Width(25f), GUILayout.Height(15f) };

                upDownButtonStyle = new GUIStyle(GUI.skin.button)
                {
                    alignment = TextAnchor.UpperCenter,
                    clipping  = TextClipping.Overflow
                };
            }

            var markDirty = false;
            var layer     = animationPlayer.layers[selectedLayer];

            if (layer.states.Count == 0)
            {
                EditorGUILayout.LabelField("No states");
                return;
            }

            if (!layer.states.IsInBounds(selectedState))
            {
                Debug.LogError($"Out of bounds state: {selectedState} out of {layer.states.Count} states! Resetting to 0");
                selectedState.SetTo(0);
                return;
            }

            var stateName = layer.states[selectedState].Name;

            EditorGUILayout.LabelField($"Settings for \"{stateName}\":");

            EditorGUI.indentLevel++;
            DrawStateData(layer.states[selectedState], ref markDirty);

            GUILayout.Space(20f);

            EditorGUILayout.BeginHorizontal();
            if (selectedState == 0)
            {
                EditorGUILayout.LabelField("This is the default state of this layer", GUILayout.Width(250f));
            }
            else if (GUILayout.Button("Set as the default state of this layer", GUILayout.Width(250f)))
            {
                layer.states.Swap(selectedState, 0);
                selectedState.SetTo(0);
                markDirty = true;
            }
            GUILayout.FlexibleSpace();
            var deleteThisState = EditorUtilities.AreYouSureButton($"Delete {stateName}", "Are you sure", $"DeleteState_{selectedState}_{selectedLayer}", 1f);

            EditorGUILayout.EndHorizontal();
            EditorGUI.indentLevel--;

            if (deleteThisState)
            {
                DeleteState(animationPlayer, layer, selectedState);
                markDirty = true;
            }

            if (markDirty)
            {
                currentEditor.MarkDirty();
            }

            GUILayout.Space(20f);

            currentEditor.previewer.DrawStatePreview(selectedLayer, selectedState);
        }