Exemplo n.º 1
0
        void SequenceUI(SegmentSequence sequence)
        {
            //Toolbar
            Vector2 thumbSize = Vector2.one * thumbnailSize;

            if (sequence.type == SegmentSequence.Type.Random)
            {
                thumbSize.y += 16f;
            }
            float width           = viewRect.width;
            float totalThumbWidth = thumbSize.x + thumbnailPadding;
            int   elementsPerRow  = Mathf.FloorToInt(width / totalThumbWidth);
            int   rows            = Mathf.CeilToInt((float)sequence.segments.Length / elementsPerRow);

            if (rows == 0)
            {
                rows = 1;
            }
            int   row = 0, col = 0;
            float height = rows * (thumbSize.y + thumbnailPadding) + thumbnailPadding;

            #region Header
            GUI.backgroundColor = ForeverPrefs.highlightColor * DreamteckEditorGUI.lightColor;
            EditorGUILayout.BeginHorizontal(defaultBoxStyle, GUILayout.Width(width));
            GUI.backgroundColor = Color.white;
            if (sequenceAddress.Count == 0)
            {
                sequence.enabled = EditorGUILayout.Toggle(sequence.enabled, headerToggle, GUILayout.Width(20));
            }
            if (renameSequence && editSequence == sequence)
            {
                if (onWillChange != null)
                {
                    onWillChange();
                }
                sequence.name = GUILayout.TextField(sequence.name);
                if (input.enterDown)
                {
                    renameSequence = false;
                }
                if (input.mouseLeftDown && input.mouseRightDown)
                {
                    if (!GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
                    {
                        renameSequence = false;
                    }
                }
            }
            else
            {
                if (sequence.name == "")
                {
                    sequence.name = "Sequence";
                }
                string nameText = sequence.name;
                if (!sequence.isCustom)
                {
                    nameText += " - " + sequence.type.ToString();
                }
                GUILayout.Label(nameText);
            }

            if (GUILayout.Button("Settings", GUILayout.Width(60)))
            {
                if (sequenceSettingsWindow == null)
                {
                    sequenceSettingsWindow = EditorWindow.GetWindow <SegmentSequenceSettingsWindow>(true);
                    sequenceSettingsWindow.onWillChange += OnSequenceSettingsWillChange;
                }
                sequenceSettingsWindow.Init(sequence, this);
            }

            EditorGUILayout.EndVertical();
            Rect headerRect = GUILayoutUtility.GetLastRect();
            if (headerRect.Contains(Event.current.mousePosition))
            {
                if (input.mouseRightDown)
                {
                    GenericMenu menu = new GenericMenu();
                    menu.AddItem(new GUIContent("Move Up"), false, MoveSequenceUp, sequence);
                    menu.AddItem(new GUIContent("Move Down"), false, MoveSequenceDown, sequence);
                    menu.AddItem(new GUIContent("Rename"), false, SetRename, sequence);
                    menu.AddItem(new GUIContent("Duplicate"), false, DuplicateSequence, sequence);
                    menu.AddSeparator("");
                    menu.AddItem(new GUIContent("Delete"), false, RemoveSequence, sequence);
                    menu.ShowAsContext();
                    input.Use();
                }
            }
            #endregion

            //Sequence canvas
            GUI.backgroundColor = DreamteckEditorGUI.lightColor;
            if (sequence.isCustom)
            {
                GUILayout.Box("", defaultBoxStyle, GUILayout.Width(width), GUILayout.Height(thumbSize.y + thumbnailPadding * 2f));
            }
            else
            {
                GUILayout.Box("", defaultBoxStyle, GUILayout.Width(width), GUILayout.Height(height));
            }
            GUI.backgroundColor = Color.white;
            Rect groupRect = GUILayoutUtility.GetLastRect();
            GUI.BeginGroup(groupRect);

            if (sequence.isCustom)
            {
                GUI.Label(new Rect(0f, groupRect.height / 2f - 30f, groupRect.width, 50), "Custom Sequence", customSequenceLabel);
                CustomSequence customSequence = sequence.customSequence;
                customSequence = (CustomSequence)EditorGUI.ObjectField(new Rect(groupRect.width / 2f - 100, groupRect.height / 2f + 15, 200, 16), customSequence, typeof(CustomSequence), false);
                if (customSequence != sequence.customSequence)
                {
                    if (onWillChange != null)
                    {
                        onWillChange();
                    }
                    sequence.customSequence = customSequence;
                }
                GUI.EndGroup();
                EditorGUILayout.Space();
                return;
            }

            #region Drag Reorder
            int     dragTarget    = -1;
            float   closestDist   = float.MaxValue;
            Vector2 closestCenter = Vector2.zero;
            if (dragIndex >= 0 && editSequence == sequence)
            {
                for (int i = 0; i < sequence.segments.Length; i++)
                {
                    Vector2 thumbCenter = new Vector2(thumbnailPadding + col * totalThumbWidth + thumbnailSize / 2, (thumbnailSize + thumbnailPadding) * row + thumbnailPadding + thumbnailSize / 2);
                    float   dist        = Vector2.Distance(thumbCenter, Event.current.mousePosition);
                    if (dist < closestDist)
                    {
                        closestDist   = dist;
                        dragTarget    = i;
                        closestCenter = thumbCenter;
                    }
                    col++;
                    if (col >= elementsPerRow)
                    {
                        col = 0;
                        row++;
                    }
                }

                if (dragTarget >= 0 && dragTarget != dragIndex)
                {
                    bool before = Event.current.mousePosition.x < closestCenter.x;
                    int  side   = before ? -1 : 1;
                    EditorGUI.DrawRect(new Rect(closestCenter.x + side * thumbnailSize / 2 - thumbnailPadding, closestCenter.y - thumbnailSize / 2, thumbnailPadding * 2, thumbnailSize), ForeverPrefs.highlightColor);
                    if (input.mouseLeftUp)
                    {
                        if (dragTarget < 0)
                        {
                            dragTarget = 0;
                        }
                        ReorderSegments(ref sequence.segments, dragIndex, dragTarget);
                        dragIndex = -1;
                    }
                }
            }
            #endregion

            #region Draw Definitions
            col = row = 0;
            for (int i = 0; i < sequence.segments.Length; i++)
            {
                Rect thumbRect = new Rect(thumbnailPadding + col * totalThumbWidth, (thumbSize.y + thumbnailPadding) * row + thumbnailPadding, thumbSize.x, thumbSize.y);
                if (segmentArray != sequence.segments || dragIndex != i)
                {
                    if (!sequence.segments[i].nested && sequence.segments[i].prefab == null) //dont remove in future, just show as NULL in the window
                    {
                        RemoveSegment(ref sequence.segments, i);
                        i--;
                        continue;
                    }
                    if (DefinitionUI(new Vector2(thumbRect.x, thumbRect.y), sequence.segments[i], i))
                    {
                        if (input.mouseLeftDown && !renameSequence)
                        {
                            dragIndex    = i;
                            dragOffset   = new Vector2(thumbRect.x + thumbnailSize / 2f, thumbRect.y + thumbnailSize / 2f) - Event.current.mousePosition;
                            editSequence = sequence;
                            segmentArray = sequence.segments;
                            input.Use();
                        }
                        else if (input.mouseRightDown)
                        {
                            editSequence = sequence;
                            if (sequence.segments[i].nested)
                            {
                                GenericMenu menu = new GenericMenu();
                                menu.AddItem(new GUIContent("Edit"), false, EnterNestedSequence, sequence.segments[i]);
                                menu.AddItem(new GUIContent("Remove"), false, RemoveSegment, i);
                                menu.ShowAsContext();
                                input.Use();
                            }
                            else
                            {
                                int         index = i;
                                GenericMenu menu  = new GenericMenu();
                                menu.AddItem(new GUIContent("Change"), false, SetChangeSegment, sequence.segments[i]);
                                menu.AddItem(new GUIContent("Select in Project"), false, SelectDefinitionPrefab, sequence.segments[i]);
                                menu.AddItem(new GUIContent("Duplicate"), false, delegate {
                                    Undo.RecordObject(undoObject, "Duplicate Segment");
                                    ArrayUtility.Insert(ref sequence.segments, index, sequence.segments[index].Duplicate());
                                    changed = true;
                                });
                                menu.AddItem(new GUIContent("Remove"), false, RemoveSegment, i);
                                menu.ShowAsContext();
                                input.Use();
                            }

                            input.Use();
                        }
                    }
                    if (sequence.type == SegmentSequence.Type.Random)
                    {
                        float chance = sequence.segments[i].randomPickChance;
                        chance = GUI.HorizontalSlider(new Rect(thumbRect.x, thumbRect.y + thumbSize.y - 16f, thumbSize.x, 16f), chance, 0f, 1f);
                        GUI.Label(new Rect(thumbRect.x, thumbRect.y + thumbSize.y - 32f, thumbSize.x, 16f), Mathf.RoundToInt(chance * 100) + "% Chance", thumbnailLabel);
                        if (chance != sequence.segments[i].randomPickChance)
                        {
                            onWillChange();
                            sequence.segments[i].randomPickChance = chance;
                        }
                    }
                }
                col++;
                if (col >= elementsPerRow)
                {
                    col = 0;
                    row++;
                }
            }
            #endregion
            GUI.EndGroup();

            Rect canvasRect = GUILayoutUtility.GetLastRect();
            if (canvasRect.Contains(Event.current.mousePosition) && input.mouseRightDown)
            {
                GenericMenu menu = new GenericMenu();
                menu.AddItem(new GUIContent("New Nested Sequence"), false, CreateNestedSequence, sequence);
                menu.ShowAsContext();
            }

            SegmentDefinition newSegment = null;
            GameObject        newObj     = SegmentField(GUILayoutUtility.GetLastRect());
            if (newObj != null)
            {
                newSegment = new SegmentDefinition(newObj);
            }
            if (newSegment != null && newSegment.prefab != null)
            {
                if (onWillChange != null)
                {
                    onWillChange();
                }
                AddDefinition(sequence, newSegment);
                changed = true;
            }
            if (changed)
            {
                changed = false;
                if (onChanged != null)
                {
                    onChanged();
                }
            }
            EditorGUILayout.Space();
        }
        private void OnGUI()
        {
            if (editor == null)
            {
                Close();
                return;
            }
            string name = sequence.name;

            name = EditorGUILayout.TextField("Name", name);
            if (name != sequence.name)
            {
                if (onWillChange != null)
                {
                    onWillChange();
                }
                sequence.name = name;
            }


            if (!sequence.isCustom)
            {
                SegmentSequence.Type type = sequence.type;
                type = (SegmentSequence.Type)EditorGUILayout.EnumPopup("Shuffle Type", type);
                if (type != sequence.type)
                {
                    if (onWillChange != null)
                    {
                        onWillChange();
                    }
                    sequence.type = type;
                }
                if (type == SegmentSequence.Type.Random)
                {
                    int spawnCount = sequence.spawnCount;
                    spawnCount = EditorGUILayout.IntField(new GUIContent("Spawn Count", "How many segments should this random sequence spawn? Zero will go on forever."), spawnCount);
                    if (spawnCount != sequence.spawnCount)
                    {
                        if (onWillChange != null)
                        {
                            onWillChange();
                        }
                        sequence.spawnCount = spawnCount;
                    }
                    bool preventRepeat = sequence.preventRepeat;
                    preventRepeat = EditorGUILayout.Toggle(new GUIContent("Prevent Repeating", "If true, the random algorithm will make sure not to spawn the same segment twice in a row."), preventRepeat);
                    if (preventRepeat != sequence.preventRepeat)
                    {
                        if (onWillChange != null)
                        {
                            onWillChange();
                        }
                        sequence.preventRepeat = preventRepeat;
                    }
                }
                else if (sequence.type == SegmentSequence.Type.Custom)
                {
                    SegmentShuffle customShuffle = sequence.customShuffle;
                    customShuffle = (SegmentShuffle)EditorGUILayout.ObjectField("Shuffle", customShuffle, typeof(SegmentShuffle), false);
                    if (customShuffle != sequence.customShuffle)
                    {
                        if (onWillChange != null)
                        {
                            onWillChange();
                        }
                        sequence.customShuffle = customShuffle;
                    }
                }
            }
            EditorGUILayout.Space();
            LevelPathGenerator pathGenerator = sequence.customPathGenerator;

            pathGenerator = (LevelPathGenerator)EditorGUILayout.ObjectField("Custom Path Generator", pathGenerator, typeof(LevelPathGenerator), false);
            if (pathGenerator != sequence.customPathGenerator)
            {
                if (onWillChange != null)
                {
                    onWillChange();
                }
                sequence.customPathGenerator = pathGenerator;
            }

            EditorGUILayout.Space();

            bool isCustom = sequence.isCustom;

            isCustom = EditorGUILayout.Toggle("Custom Sequence", isCustom);
            if (isCustom != sequence.isCustom)
            {
                if (onWillChange != null)
                {
                    onWillChange();
                }
                sequence.isCustom = isCustom;
            }

            if (isCustom)
            {
                CustomSequence customSequence = sequence.customSequence;
                customSequence = (CustomSequence)EditorGUILayout.ObjectField("Sequence Asset", customSequence, typeof(CustomSequence), false);
                if (customSequence != sequence.customSequence)
                {
                    if (onWillChange != null)
                    {
                        onWillChange();
                    }
                    sequence.customSequence = customSequence;
                }
                return;
            }
        }