UnityEditor.EditorGUIExt.MultiSelection C# (CSharp) Method

MultiSelection() public static method

public static MultiSelection ( Rect rect, Rect positions, GUIContent content, Rect hitPositions, bool &selections, bool readOnly, int &clickedIndex, Vector2 &offset, float &startSelect, float &endSelect, GUIStyle style ) : HighLevelEvent
rect UnityEngine.Rect
positions UnityEngine.Rect
content UnityEngine.GUIContent
hitPositions UnityEngine.Rect
selections bool
readOnly bool
clickedIndex int
offset Vector2
startSelect float
endSelect float
style UnityEngine.GUIStyle
return HighLevelEvent
        public static HighLevelEvent MultiSelection(Rect rect, Rect[] positions, GUIContent content, Rect[] hitPositions, ref bool[] selections, bool[] readOnly, out int clickedIndex, out Vector2 offset, out float startSelect, out float endSelect, GUIStyle style)
        {
            int controlID = GUIUtility.GetControlID(0x27b1f9d, FocusType.Keyboard);
            Event current = Event.current;
            offset = Vector2.zero;
            clickedIndex = -1;
            startSelect = endSelect = 0f;
            if (current.type != EventType.Used)
            {
                int indexUnderMouse;
                bool flag = false;
                if (Event.current.type != EventType.Layout)
                {
                    if (GUIUtility.keyboardControl == controlID)
                    {
                        flag = true;
                    }
                    else
                    {
                        selections = new bool[selections.Length];
                    }
                }
                switch (current.GetTypeForControl(controlID))
                {
                    case EventType.MouseDown:
                        if (current.button != 0)
                        {
                            break;
                        }
                        GUIUtility.hotControl = controlID;
                        GUIUtility.keyboardControl = controlID;
                        s_StartSelectPos = current.mousePosition;
                        indexUnderMouse = GetIndexUnderMouse(hitPositions, readOnly);
                        if ((Event.current.clickCount != 2) || (indexUnderMouse < 0))
                        {
                            if (indexUnderMouse >= 0)
                            {
                                if ((!current.shift && !EditorGUI.actionKey) && !selections[indexUnderMouse])
                                {
                                    for (int j = 0; j < hitPositions.Length; j++)
                                    {
                                        selections[j] = false;
                                    }
                                }
                                if (current.shift || EditorGUI.actionKey)
                                {
                                    selections[indexUnderMouse] = !selections[indexUnderMouse];
                                }
                                else
                                {
                                    selections[indexUnderMouse] = true;
                                }
                                s_MouseDownPos = current.mousePosition;
                                s_MultiSelectDragSelection = DragSelectionState.None;
                                current.Use();
                                clickedIndex = indexUnderMouse;
                                return HighLevelEvent.SelectionChanged;
                            }
                            bool flag2 = false;
                            if (!current.shift && !EditorGUI.actionKey)
                            {
                                for (int k = 0; k < hitPositions.Length; k++)
                                {
                                    selections[k] = false;
                                }
                                flag2 = true;
                            }
                            else
                            {
                                flag2 = false;
                            }
                            s_SelectionBackup = new List<bool>(selections);
                            s_LastFrameSelections = new List<bool>(selections);
                            s_MultiSelectDragSelection = DragSelectionState.DragSelecting;
                            current.Use();
                            return (!flag2 ? HighLevelEvent.None : HighLevelEvent.SelectionChanged);
                        }
                        for (int i = 0; i < selections.Length; i++)
                        {
                            selections[i] = false;
                        }
                        selections[indexUnderMouse] = true;
                        current.Use();
                        clickedIndex = indexUnderMouse;
                        return HighLevelEvent.DoubleClick;

                    case EventType.MouseUp:
                        if (GUIUtility.hotControl == controlID)
                        {
                            GUIUtility.hotControl = 0;
                            if (s_StartSelectPos != current.mousePosition)
                            {
                                current.Use();
                            }
                            if (s_MultiSelectDragSelection != DragSelectionState.None)
                            {
                                s_MultiSelectDragSelection = DragSelectionState.None;
                                s_SelectionBackup = null;
                                s_LastFrameSelections = null;
                                return HighLevelEvent.EndDrag;
                            }
                            clickedIndex = GetIndexUnderMouse(hitPositions, readOnly);
                            if (current.clickCount == 1)
                            {
                                return HighLevelEvent.Click;
                            }
                        }
                        break;

                    case EventType.MouseDrag:
                    {
                        if (GUIUtility.hotControl != controlID)
                        {
                            break;
                        }
                        if (s_MultiSelectDragSelection != DragSelectionState.DragSelecting)
                        {
                            offset = current.mousePosition - s_MouseDownPos;
                            current.Use();
                            if (s_MultiSelectDragSelection == DragSelectionState.None)
                            {
                                s_MultiSelectDragSelection = DragSelectionState.Dragging;
                                return HighLevelEvent.BeginDrag;
                            }
                            return HighLevelEvent.Drag;
                        }
                        float num10 = Mathf.Min(s_StartSelectPos.x, current.mousePosition.x);
                        float num11 = Mathf.Max(s_StartSelectPos.x, current.mousePosition.x);
                        s_SelectionBackup.CopyTo(selections);
                        for (int m = 0; m < hitPositions.Length; m++)
                        {
                            if (!selections[m])
                            {
                                float num13 = hitPositions[m].x + (hitPositions[m].width * 0.5f);
                                if ((num13 >= num10) && (num13 <= num11))
                                {
                                    selections[m] = true;
                                }
                            }
                        }
                        current.Use();
                        startSelect = num10;
                        endSelect = num11;
                        bool flag3 = false;
                        for (int n = 0; n < selections.Length; n++)
                        {
                            if (selections[n] != s_LastFrameSelections[n])
                            {
                                flag3 = true;
                                s_LastFrameSelections[n] = selections[n];
                            }
                        }
                        return (!flag3 ? HighLevelEvent.None : HighLevelEvent.SelectionChanged);
                    }
                    case EventType.KeyDown:
                        if (!flag || ((current.keyCode != KeyCode.Backspace) && (current.keyCode != KeyCode.Delete)))
                        {
                            break;
                        }
                        current.Use();
                        return HighLevelEvent.Delete;

                    case EventType.Repaint:
                    {
                        if ((GUIUtility.hotControl == controlID) && (s_MultiSelectDragSelection == DragSelectionState.DragSelecting))
                        {
                            float num4 = Mathf.Min(s_StartSelectPos.x, current.mousePosition.x);
                            float num5 = Mathf.Max(s_StartSelectPos.x, current.mousePosition.x);
                            Rect position = new Rect(0f, 0f, rect.width, rect.height) {
                                x = num4,
                                width = num5 - num4
                            };
                            if (position.width != 0f)
                            {
                                GUI.Box(position, "", ms_Styles.selectionRect);
                            }
                        }
                        Color color = GUI.color;
                        for (int num6 = 0; num6 < positions.Length; num6++)
                        {
                            if ((readOnly != null) && readOnly[num6])
                            {
                                GUI.color = color * new Color(0.9f, 0.9f, 0.9f, 0.5f);
                            }
                            else if (selections[num6])
                            {
                                GUI.color = color * new Color(0.3f, 0.55f, 0.95f, 1f);
                            }
                            else
                            {
                                GUI.color = color * new Color(0.9f, 0.9f, 0.9f, 1f);
                            }
                            style.Draw(positions[num6], content, controlID, selections[num6]);
                        }
                        GUI.color = color;
                        break;
                    }
                    case EventType.ValidateCommand:
                    case EventType.ExecuteCommand:
                        if (flag)
                        {
                            bool flag4 = current.type == EventType.ExecuteCommand;
                            string commandName = current.commandName;
                            if ((commandName != null) && (commandName == "Delete"))
                            {
                                current.Use();
                                if (flag4)
                                {
                                    return HighLevelEvent.Delete;
                                }
                                break;
                            }
                        }
                        break;

                    case EventType.ContextClick:
                        indexUnderMouse = GetIndexUnderMouse(hitPositions, readOnly);
                        if (indexUnderMouse < 0)
                        {
                            break;
                        }
                        clickedIndex = indexUnderMouse;
                        GUIUtility.keyboardControl = controlID;
                        current.Use();
                        return HighLevelEvent.ContextClick;
                }
            }
            return HighLevelEvent.None;
        }

Usage Example

        public void EventLineGUI(Rect rect, AnimationWindowState state)
        {
            AnimationClip activeAnimationClip  = state.activeAnimationClip;
            GameObject    activeRootGameObject = state.activeRootGameObject;

            if (activeRootGameObject == null)
            {
                return;
            }
            GUI.BeginGroup(rect);
            Color color = GUI.color;
            Rect  rect2 = new Rect(0f, 0f, rect.width, rect.height);
            float time  = Mathf.Max((float)Mathf.RoundToInt(state.PixelToTime(Event.current.mousePosition.x, rect) * state.frameRate) / state.frameRate, 0f);

            if (activeAnimationClip != null)
            {
                AnimationEvent[] animationEvents = AnimationUtility.GetAnimationEvents(activeAnimationClip);
                Texture          image           = EditorGUIUtility.IconContent("Animation.EventMarker").image;
                Rect[]           array           = new Rect[animationEvents.Length];
                Rect[]           array2          = new Rect[animationEvents.Length];
                int num  = 1;
                int num2 = 0;
                for (int i = 0; i < animationEvents.Length; i++)
                {
                    AnimationEvent animationEvent = animationEvents[i];
                    if (num2 == 0)
                    {
                        num = 1;
                        while (i + num < animationEvents.Length && animationEvents[i + num].time == animationEvent.time)
                        {
                            num++;
                        }
                        num2 = num;
                    }
                    num2--;
                    float num3 = Mathf.Floor(state.FrameToPixel(animationEvent.time * activeAnimationClip.frameRate, rect));
                    int   num4 = 0;
                    if (num > 1)
                    {
                        float num5 = (float)Mathf.Min((num - 1) * (image.width - 1), (int)(state.FrameDeltaToPixel(rect) - (float)(image.width * 2)));
                        num4 = Mathf.FloorToInt(Mathf.Max(0f, num5 - (float)((image.width - 1) * num2)));
                    }
                    Rect rect3 = new Rect(num3 + (float)num4 - (float)(image.width / 2), (rect.height - 10f) * (float)(num2 - num + 1) / (float)Mathf.Max(1, num - 1), (float)image.width, (float)image.height);
                    array[i]  = rect3;
                    array2[i] = rect3;
                }
                if (this.m_DirtyTooltip)
                {
                    if (this.m_HoverEvent >= 0 && this.m_HoverEvent < array.Length)
                    {
                        this.m_InstantTooltipText  = AnimationEventPopup.FormatEvent(activeRootGameObject, animationEvents[this.m_HoverEvent]);
                        this.m_InstantTooltipPoint = new Vector2(array[this.m_HoverEvent].xMin + (float)((int)(array[this.m_HoverEvent].width / 2f)) + rect.x - 30f, rect.yMax);
                    }
                    this.m_DirtyTooltip = false;
                }
                if (this.m_EventsSelected == null || this.m_EventsSelected.Length != animationEvents.Length)
                {
                    this.m_EventsSelected = new bool[animationEvents.Length];
                    AnimationEventPopup.ClosePopup();
                }
                Vector2        zero = Vector2.zero;
                int            num6;
                float          num7;
                float          num8;
                HighLevelEvent highLevelEvent = EditorGUIExt.MultiSelection(rect, array2, new GUIContent(image), array, ref this.m_EventsSelected, null, out num6, out zero, out num7, out num8, GUIStyle.none);
                if (highLevelEvent != HighLevelEvent.None)
                {
                    switch (highLevelEvent)
                    {
                    case HighLevelEvent.DoubleClick:
                        if (num6 != -1)
                        {
                            AnimationEventPopup.Edit(activeRootGameObject, state.activeAnimationClip, num6, this.m_Owner);
                        }
                        else
                        {
                            this.EventLineContextMenuAdd(new AnimationEventTimeLine.EventLineContextMenuObject(activeRootGameObject, activeAnimationClip, time, -1));
                        }
                        break;

                    case HighLevelEvent.ContextClick:
                    {
                        GenericMenu genericMenu = new GenericMenu();
                        AnimationEventTimeLine.EventLineContextMenuObject userData = new AnimationEventTimeLine.EventLineContextMenuObject(activeRootGameObject, activeAnimationClip, animationEvents[num6].time, num6);
                        genericMenu.AddItem(new GUIContent("Edit Animation Event"), false, new GenericMenu.MenuFunction2(this.EventLineContextMenuEdit), userData);
                        genericMenu.AddItem(new GUIContent("Add Animation Event"), false, new GenericMenu.MenuFunction2(this.EventLineContextMenuAdd), userData);
                        genericMenu.AddItem(new GUIContent("Delete Animation Event"), false, new GenericMenu.MenuFunction2(this.EventLineContextMenuDelete), userData);
                        genericMenu.ShowAsContext();
                        this.m_InstantTooltipText = null;
                        this.m_DirtyTooltip       = true;
                        state.Repaint();
                        break;
                    }

                    case HighLevelEvent.BeginDrag:
                        this.m_EventsAtMouseDown = animationEvents;
                        this.m_EventTimes        = new float[animationEvents.Length];
                        for (int j = 0; j < animationEvents.Length; j++)
                        {
                            this.m_EventTimes[j] = animationEvents[j].time;
                        }
                        break;

                    case HighLevelEvent.Drag:
                    {
                        for (int k = animationEvents.Length - 1; k >= 0; k--)
                        {
                            if (this.m_EventsSelected[k])
                            {
                                AnimationEvent animationEvent2 = this.m_EventsAtMouseDown[k];
                                animationEvent2.time = this.m_EventTimes[k] + zero.x * state.PixelDeltaToTime(rect);
                                animationEvent2.time = Mathf.Max(0f, animationEvent2.time);
                                animationEvent2.time = (float)Mathf.RoundToInt(animationEvent2.time * activeAnimationClip.frameRate) / activeAnimationClip.frameRate;
                            }
                        }
                        int[] array3 = new int[this.m_EventsSelected.Length];
                        for (int l = 0; l < array3.Length; l++)
                        {
                            array3[l] = l;
                        }
                        Array.Sort(this.m_EventsAtMouseDown, array3, new AnimationEventTimeLine.EventComparer());
                        bool[]  array4 = (bool[])this.m_EventsSelected.Clone();
                        float[] array5 = (float[])this.m_EventTimes.Clone();
                        for (int m = 0; m < array3.Length; m++)
                        {
                            this.m_EventsSelected[m] = array4[array3[m]];
                            this.m_EventTimes[m]     = array5[array3[m]];
                        }
                        Undo.RegisterCompleteObjectUndo(activeAnimationClip, "Move Event");
                        AnimationUtility.SetAnimationEvents(activeAnimationClip, this.m_EventsAtMouseDown);
                        this.m_DirtyTooltip = true;
                        break;
                    }

                    case HighLevelEvent.Delete:
                        this.DeleteEvents(activeAnimationClip, this.m_EventsSelected);
                        break;

                    case HighLevelEvent.SelectionChanged:
                        state.ClearKeySelections();
                        if (num6 != -1)
                        {
                            AnimationEventPopup.UpdateSelection(activeRootGameObject, state.activeAnimationClip, num6, this.m_Owner);
                        }
                        break;
                    }
                }
                this.CheckRectsOnMouseMove(rect, animationEvents, array);
            }
            if (Event.current.type == EventType.ContextClick && rect2.Contains(Event.current.mousePosition))
            {
                Event.current.Use();
                GenericMenu genericMenu2 = new GenericMenu();
                genericMenu2.AddItem(new GUIContent("Add Animation Event"), false, new GenericMenu.MenuFunction2(this.EventLineContextMenuAdd), new AnimationEventTimeLine.EventLineContextMenuObject(activeRootGameObject, activeAnimationClip, time, -1));
                genericMenu2.ShowAsContext();
            }
            GUI.color = color;
            GUI.EndGroup();
        }
All Usage Examples Of UnityEditor.EditorGUIExt::MultiSelection