UnityEditor.EditorGUIExt.GetIndexUnderMouse C# (CSharp) Method

GetIndexUnderMouse() private static method

private static GetIndexUnderMouse ( Rect hitPositions, bool readOnly ) : int
hitPositions UnityEngine.Rect
readOnly bool
return int
        private static int GetIndexUnderMouse(Rect[] hitPositions, bool[] readOnly)
        {
            Vector2 mousePosition = Event.current.mousePosition;
            for (int i = hitPositions.Length - 1; i >= 0; i--)
            {
                if (((readOnly == null) || !readOnly[i]) && hitPositions[i].Contains(mousePosition))
                {
                    return i;
                }
            }
            return -1;
        }

Usage Example

示例#1
0
        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(41623453, FocusType.Keyboard);
            Event current   = Event.current;

            offset       = Vector2.zero;
            clickedIndex = -1;
            startSelect  = (endSelect = 0f);
            HighLevelEvent result;

            if (current.type == EventType.Used)
            {
                result = HighLevelEvent.None;
            }
            else
            {
                bool flag = false;
                if (Event.current.type != EventType.Layout)
                {
                    if (GUIUtility.keyboardControl == controlID)
                    {
                        flag = true;
                    }
                }
                EventType typeForControl = current.GetTypeForControl(controlID);
                switch (typeForControl)
                {
                case EventType.MouseDown:
                {
                    if (current.button != 0)
                    {
                        goto IL_6A2;
                    }
                    GUIUtility.hotControl         = controlID;
                    GUIUtility.keyboardControl    = controlID;
                    EditorGUIExt.s_StartSelectPos = current.mousePosition;
                    int indexUnderMouse = EditorGUIExt.GetIndexUnderMouse(hitPositions, readOnly);
                    if (Event.current.clickCount == 2)
                    {
                        if (indexUnderMouse >= 0)
                        {
                            for (int i = 0; i < selections.Length; i++)
                            {
                                selections[i] = false;
                            }
                            selections[indexUnderMouse] = true;
                            current.Use();
                            clickedIndex = indexUnderMouse;
                            result       = HighLevelEvent.DoubleClick;
                            return(result);
                        }
                    }
                    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;
                        }
                        EditorGUIExt.s_MouseDownPos             = current.mousePosition;
                        EditorGUIExt.s_MultiSelectDragSelection = EditorGUIExt.DragSelectionState.None;
                        current.Use();
                        clickedIndex = indexUnderMouse;
                        result       = HighLevelEvent.SelectionChanged;
                        return(result);
                    }
                    bool flag2;
                    if (!current.shift && !EditorGUI.actionKey)
                    {
                        for (int k = 0; k < hitPositions.Length; k++)
                        {
                            selections[k] = false;
                        }
                        flag2 = true;
                    }
                    else
                    {
                        flag2 = false;
                    }
                    EditorGUIExt.s_SelectionBackup          = new List <bool>(selections);
                    EditorGUIExt.s_LastFrameSelections      = new List <bool>(selections);
                    EditorGUIExt.s_MultiSelectDragSelection = EditorGUIExt.DragSelectionState.DragSelecting;
                    current.Use();
                    result = ((!flag2) ? HighLevelEvent.None : HighLevelEvent.SelectionChanged);
                    return(result);
                }

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

                case EventType.MouseMove:
                case EventType.KeyUp:
                case EventType.ScrollWheel:
IL_98:
                    switch (typeForControl)
                    {
                    case EventType.ValidateCommand:
                    case EventType.ExecuteCommand:
                        if (flag)
                        {
                            bool   flag3       = current.type == EventType.ExecuteCommand;
                            string commandName = current.commandName;
                            if (commandName != null)
                            {
                                if (commandName == "Delete")
                                {
                                    current.Use();
                                    if (flag3)
                                    {
                                        result = HighLevelEvent.Delete;
                                        return(result);
                                    }
                                }
                            }
                        }
                        goto IL_6A2;

                    case EventType.DragExited:
                        goto IL_6A2;

                    case EventType.ContextClick:
                    {
                        int indexUnderMouse = EditorGUIExt.GetIndexUnderMouse(hitPositions, readOnly);
                        if (indexUnderMouse >= 0)
                        {
                            clickedIndex = indexUnderMouse;
                            GUIUtility.keyboardControl = controlID;
                            current.Use();
                            result = HighLevelEvent.ContextClick;
                            return(result);
                        }
                        goto IL_6A2;
                    }

                    default:
                        goto IL_6A2;
                    }
                    break;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl != controlID)
                    {
                        goto IL_6A2;
                    }
                    if (EditorGUIExt.s_MultiSelectDragSelection == EditorGUIExt.DragSelectionState.DragSelecting)
                    {
                        float num  = Mathf.Min(EditorGUIExt.s_StartSelectPos.x, current.mousePosition.x);
                        float num2 = Mathf.Max(EditorGUIExt.s_StartSelectPos.x, current.mousePosition.x);
                        EditorGUIExt.s_SelectionBackup.CopyTo(selections);
                        for (int l = 0; l < hitPositions.Length; l++)
                        {
                            if (!selections[l])
                            {
                                float num3 = hitPositions[l].x + hitPositions[l].width * 0.5f;
                                if (num3 >= num && num3 <= num2)
                                {
                                    selections[l] = true;
                                }
                            }
                        }
                        current.Use();
                        startSelect = num;
                        endSelect   = num2;
                        bool flag4 = false;
                        for (int m = 0; m < selections.Length; m++)
                        {
                            if (selections[m] != EditorGUIExt.s_LastFrameSelections[m])
                            {
                                flag4 = true;
                                EditorGUIExt.s_LastFrameSelections[m] = selections[m];
                            }
                        }
                        result = ((!flag4) ? HighLevelEvent.None : HighLevelEvent.SelectionChanged);
                        return(result);
                    }
                    offset = current.mousePosition - EditorGUIExt.s_MouseDownPos;
                    current.Use();
                    if (EditorGUIExt.s_MultiSelectDragSelection == EditorGUIExt.DragSelectionState.None)
                    {
                        EditorGUIExt.s_MultiSelectDragSelection = EditorGUIExt.DragSelectionState.Dragging;
                        result = HighLevelEvent.BeginDrag;
                        return(result);
                    }
                    result = HighLevelEvent.Drag;
                    return(result);

                case EventType.KeyDown:
                    if (flag)
                    {
                        if (current.keyCode == KeyCode.Backspace || current.keyCode == KeyCode.Delete)
                        {
                            current.Use();
                            result = HighLevelEvent.Delete;
                            return(result);
                        }
                    }
                    goto IL_6A2;

                case EventType.Repaint:
                {
                    if (GUIUtility.hotControl == controlID && EditorGUIExt.s_MultiSelectDragSelection == EditorGUIExt.DragSelectionState.DragSelecting)
                    {
                        float num4     = Mathf.Min(EditorGUIExt.s_StartSelectPos.x, current.mousePosition.x);
                        float num5     = Mathf.Max(EditorGUIExt.s_StartSelectPos.x, current.mousePosition.x);
                        Rect  position = new Rect(0f, 0f, rect.width, rect.height);
                        position.x     = num4;
                        position.width = num5 - num4;
                        if (position.width != 0f)
                        {
                            GUI.Box(position, "", EditorGUIExt.ms_Styles.selectionRect);
                        }
                    }
                    Color color = GUI.color;
                    for (int n = 0; n < positions.Length; n++)
                    {
                        if (readOnly != null && readOnly[n])
                        {
                            GUI.color = color * new Color(0.9f, 0.9f, 0.9f, 0.5f);
                        }
                        else if (selections[n])
                        {
                            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[n], content, controlID, selections[n]);
                    }
                    GUI.color = color;
                    goto IL_6A2;
                }
                }
                goto IL_98;
IL_6A2:
                result = HighLevelEvent.None;
            }
            return(result);
        }
All Usage Examples Of UnityEditor.EditorGUIExt::GetIndexUnderMouse