예제 #1
0
        public static int DoPopup(Rect rect, int selectedIndex, GUIContent[] displayedOptions)
        {
            GUIContent content = GUIContent.none;

            if (selectedIndex >= 0 && selectedIndex < displayedOptions.Length)
            {
                content = displayedOptions[selectedIndex];
            }

            int id = EditorGUIUtility.GetControlID("AdvancedDropdown".GetHashCode(), FocusType.Keyboard, rect);

            if (EditorGUI.DropdownButton(id, rect, content, EditorStyles.popup))
            {
                s_CurrentControl = id;
                ResetAndCreateWindow();
                InitPopupWindow(rect, selectedIndex, displayedOptions);

                s_Instance.windowClosed += w =>
                {
                    m_Result       = w.GetSelectedItem().elementIndex;
                    m_WindowClosed = true;
                };
                GUIUtility.ExitGUI();
            }
            if (m_WindowClosed && s_CurrentControl == id)
            {
                s_CurrentControl = 0;
                m_WindowClosed   = false;
                return(m_Result);
            }

            return(selectedIndex);
        }
예제 #2
0
        internal static int DoSearchablePopup(Rect rect, int selectedIndex, string[] displayedOptions, GUIStyle style)
        {
            string contentLabel = "";

            if (selectedIndex >= 0)
            {
                contentLabel = displayedOptions[selectedIndex];
            }

            var content = new GUIContent(contentLabel);

            int id = EditorGUIUtility.GetControlID("AdvancedDropdown".GetHashCode(), FocusType.Keyboard, rect);

            if (EditorGUI.DropdownButton(id, rect, content, style))
            {
                s_CurrentControl = id;
                ResetAndCreateWindow();
                InitSearchableWindow(rect, content.text, selectedIndex, displayedOptions);

                s_Instance.windowClosed += w =>
                {
                    m_Result       = w.GetSelectedItem().elementIndex;
                    m_WindowClosed = true;
                };
            }
            if (m_WindowClosed && s_CurrentControl == id)
            {
                s_CurrentControl = 0;
                m_WindowClosed   = false;
                return(m_Result);
            }

            return(selectedIndex);
        }
예제 #3
0
        private void DrawElement(Rect rect, int index, bool selected, bool focused)
        {
            Debug.Log("DrawElement");
            //var property = m_Packables.GetArrayElementAtIndex(index);
            var controlID = EditorGUIUtility.GetControlID(-1, FocusType.Passive);
            //var previousObject = property.objectReferenceValue;
            var previousObject = this.m_paths[index];
            var changedObject  = EditorGUI.ObjectField(rect, previousObject, typeof(Object), false);

            if (changedObject != previousObject)
            {
                if (previousObject != null)
                {
                    Debug.Log(previousObject.name);
                }
            }

            this.m_paths[index] = changedObject;
            //Debug.Log("Controller:"+controlID);
            //Debug.Log("Selected:"+selected);
            //Debug.Log("key:"+GUIUtility.keyboardControl);
            if (GUIUtility.keyboardControl == controlID && !selected)
            {
                m_PackableList.index = index;
                Debug.Log("SelectIndex:" + index);
            }
        }
예제 #4
0
        internal static int DoLazySearchablePopup(Rect rect, string selectedOption, int selectedIndex, Func <Tuple <int, string[]> > displayedOptionsFunc, GUIStyle style)
        {
            var content = new GUIContent(selectedOption);

            int id = EditorGUIUtility.GetControlID("AdvancedDropdown".GetHashCode(), FocusType.Keyboard, rect);

            if (EditorGUI.DropdownButton(id, rect, content, style))
            {
                s_CurrentControl = id;
                ResetAndCreateWindow();

                var displayData = displayedOptionsFunc.Invoke();
                InitSearchableWindow(rect, content.text, displayData.Item1, displayData.Item2);

                s_Instance.windowClosed += w =>
                {
                    m_Result       = w.GetSelectedItem().elementIndex;
                    m_WindowClosed = true;
                };
            }
            if (m_WindowClosed && s_CurrentControl == id)
            {
                s_CurrentControl = 0;
                m_WindowClosed   = false;
                return(m_Result);
            }

            return(selectedIndex);
        }
예제 #5
0
            internal static float Resize(Rect position, float size, float minSize, float maxSize, bool horizontal, out bool hasControl)
            {
                int   id  = EditorGUIUtility.GetControlID(s_MouseDeltaReaderHash, FocusType.Passive, position);
                Event evt = Event.current;

                switch (evt.GetTypeForControl(id))
                {
                case EventType.MouseDown:
                    if (GUIUtility.hotControl == 0 && position.Contains(evt.mousePosition) && evt.button == 0)
                    {
                        GUIUtility.hotControl      = id;
                        GUIUtility.keyboardControl = 0;
                        s_MouseDeltaReaderStartPos = GUIClip.Unclip(evt.mousePosition);     // We unclip to screenspace to prevent being affected by scrollviews
                        s_StartSize = size;
                        evt.Use();
                    }
                    break;

                case EventType.MouseDrag:
                    if (GUIUtility.hotControl == id)
                    {
                        evt.Use();
                        Vector2 screenPos = GUIClip.Unclip(evt.mousePosition);      // We unclip to screenspace to prevent being affected by scrollviews
                        float   delta     = horizontal ? (screenPos - s_MouseDeltaReaderStartPos).x : (screenPos - s_MouseDeltaReaderStartPos).y;
                        float   newSize   = s_StartSize + delta;
                        if (newSize >= minSize && newSize <= maxSize)
                        {
                            size = newSize;
                        }
                        else
                        {
                            size = Mathf.Clamp(newSize, minSize, maxSize);
                        }
                    }
                    break;

                case EventType.MouseUp:
                    if (GUIUtility.hotControl == id && evt.button == 0)
                    {
                        GUIUtility.hotControl = 0;
                        evt.Use();
                    }
                    break;

                case EventType.Repaint:
                    var cursor = horizontal ? MouseCursor.SplitResizeLeftRight : MouseCursor.SplitResizeUpDown;
                    EditorGUIUtility.AddCursorRect(position, cursor, id);
                    break;
                }

                hasControl = GUIUtility.hotControl == id;
                return(size);
            }
예제 #6
0
        static int DoMaskField(Rect rect, int mask, string[] displayedOptions, GUIStyle popup)
        {
            var flagValues = new int[displayedOptions.Length];

            for (int i = 0; i < flagValues.Length; ++i)
            {
                flagValues[i] = (1 << i);
            }

            string buttonText;

            string[] optionNames;
            int[]    optionMaskValues;
            int[]    selectedOptions;
            MaskFieldGUI.GetMenuOptions(mask, displayedOptions, flagValues, out buttonText, out optionNames, out optionMaskValues, out selectedOptions);

            var id = EditorGUIUtility.GetControlID("AdvancedDropdown".GetHashCode(), FocusType.Keyboard, rect);

            if (EditorGUI.DropdownButton(id, rect, GUIContent.Temp(buttonText), EditorStyles.popup))
            {
                s_CurrentControl = id;
                ResetAndCreateWindow();
                var dataSource = new MultiselectDataSource(mask, displayedOptions, flagValues);
                InitMultiselectPopupWindow(rect, dataSource);
                s_Instance.selectionChanged += dataSource.UpdateSelectedId;
                s_Instance.selectionChanged += i =>
                {
                    m_ShouldReturnValue = true;
                };
                s_Instance.windowClosed += w =>
                {
                    m_WindowClosed = true;
                };
            }

            if (m_ShouldReturnValue && s_CurrentControl == id)
            {
                m_ShouldReturnValue = false;
                return(s_DataSource.mask);
            }
            if (m_WindowClosed && s_CurrentControl == id)
            {
                s_CurrentControl = 0;
                m_WindowClosed   = false;
                var result = s_DataSource.mask;
                s_DataSource = null;
                return(result);
            }
            return(mask);
        }
예제 #7
0
        private static void TextureFieldGUI(string label, ref Texture2D texture, float alignmentOffset)
        {
            GUILayout.Space(6);
            GUILayout.BeginVertical(GUILayout.Width(80));
            GUILayout.Label(label);

            System.Type t = typeof(Texture2D);
            Rect        r = GUILayoutUtility.GetRect(64, 64, 64, 64, GUILayout.MaxWidth(64));

            r.x    += alignmentOffset;
            texture = EditorGUI.DoObjectField(r, r, EditorGUIUtility.GetControlID(12354, FocusType.Keyboard, r), texture, t, null, null, false) as Texture2D;

            GUILayout.EndVertical();
        }
예제 #8
0
        public static bool PopupButton(GUIContent label, GUIContent buttonContent, GUIStyle style, out Rect buttonRect, params GUILayoutOption[] options)
        {
            if (label != null)
            {
                Rect r  = EditorGUILayout.s_LastRect = EditorGUILayout.GetControlRect(true, EditorGUI.kSingleLineHeight, style, options);
                int  id = EditorGUIUtility.GetControlID("EditorPopup".GetHashCode(), FocusType.Keyboard, r);
                buttonRect = EditorGUI.PrefixLabel(r, id, label);
            }
            else
            {
                Rect r = GUILayoutUtility.GetRect(buttonContent, style, options);
                buttonRect = r;
            }

            return(EditorGUI.DropdownButton(buttonRect, buttonContent, FocusType.Passive, style));
        }
예제 #9
0
        public static Enum DoEnumMaskPopup(Rect rect, Enum options, GUIStyle style)
        {
            var    enumData    = EnumDataUtility.GetCachedEnumData(options.GetType());
            var    optionValue = EnumDataUtility.EnumFlagsToInt(enumData, options);
            string buttonText;

            string[] optionNames;
            int[]    optionMaskValues;
            int[]    selectedOptions;
            MaskFieldGUI.GetMenuOptions(optionValue, enumData.displayNames, enumData.flagValues, out buttonText, out optionNames, out optionMaskValues, out selectedOptions);

            var id = EditorGUIUtility.GetControlID("AdvancedDropdown".GetHashCode(), FocusType.Keyboard, rect);

            if (EditorGUI.DropdownButton(id, rect, GUIContent.Temp(buttonText), EditorStyles.popup))
            {
                s_CurrentControl = id;
                ResetAndCreateWindow();
                var dataSource = new MultiselectDataSource(options);
                InitMultiselectPopupWindow(rect, dataSource);
                s_Instance.selectionChanged += dataSource.UpdateSelectedId;
                s_Instance.selectionChanged += i =>
                {
                    m_ShouldReturnValue = true;
                };
                s_Instance.windowClosed += w =>
                {
                    m_WindowClosed = true;
                };
            }

            if (m_ShouldReturnValue && s_CurrentControl == id)
            {
                m_ShouldReturnValue = false;
                return(s_DataSource.enumFlags);
            }
            if (m_WindowClosed && s_CurrentControl == id)
            {
                s_CurrentControl = 0;
                m_WindowClosed   = false;
                var result = s_DataSource.enumFlags;
                s_DataSource = null;
                return(result);
            }
            return(options);
        }
예제 #10
0
        internal static Vector2 MouseDeltaReader(Rect position, bool activated)
        {
            int   id  = EditorGUIUtility.GetControlID(s_MouseDeltaReaderHash, FocusType.Passive, position);
            Event evt = Event.current;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.MouseDown:
                if (activated && GUIUtility.hotControl == 0 && GUIUtility.HitTest(position, evt) && evt.button == 0)
                {
                    GUIUtility.hotControl      = id;
                    GUIUtility.keyboardControl = 0;
                    s_MouseDeltaReaderLastPos  = GUIClip.Unclip(evt.mousePosition);    // We unclip to screenspace to prevent being affected by scrollviews
                    evt.Use();
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    Vector2 screenPos = GUIClip.Unclip(evt.mousePosition);      // We unclip to screenspace to prevent being affected by scrollviews
                    Vector2 delta     = (screenPos - s_MouseDeltaReaderLastPos);
                    s_MouseDeltaReaderLastPos = screenPos;
                    evt.Use();
                    return(delta);
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl = 0;
                    evt.Use();
                }
                break;
            }
            return(Vector2.zero);
        }
        public static Gradient GradientField(Rect position, GUIContent label, Gradient gradient, bool hdr)
        {
            int id = EditorGUIUtility.GetControlID(s_GradientHash, FocusType.Keyboard, position);

            return(DoGradientField(PrefixLabel(position, id, label), id, gradient, null, hdr));
        }
        // Gradient versions
        public static Gradient GradientField(Rect position, Gradient gradient)
        {
            int id = EditorGUIUtility.GetControlID(s_GradientHash, FocusType.Keyboard, position);

            return(DoGradientField(position, id, gradient, null, false));
        }
        internal static Gradient GradientField(Rect position, GUIContent label, SerializedProperty property)
        {
            int id = EditorGUIUtility.GetControlID(s_GradientHash, FocusType.Keyboard, position);

            return(DoGradientField(PrefixLabel(position, id, label), id, null, property, false));
        }
        internal static Gradient GradientField(Rect position, SerializedProperty property, bool hdr)
        {
            int id = EditorGUIUtility.GetControlID(s_GradientHash, FocusType.Keyboard, position);

            return(DoGradientField(position, id, null, property, hdr));
        }
        private void DrawCustomTextField(EditorWindow editorWindow, Rect windowRect)
        {
            if (!m_Data.m_AllowCustom)
            {
                return;
            }

            Event evt = Event.current;
            bool  enableAutoCompletion    = m_Data.m_EnableAutoCompletion;
            bool  closeWindow             = false;
            bool  useEventBeforeTextField = false;
            bool  clearText = false;

            string textBeforeEdit = CurrentDisplayedText();

            // Handle "special" keyboard input
            if (evt.type == EventType.KeyDown)
            {
                switch (evt.keyCode)
                {
                case KeyCode.Comma:
                case KeyCode.Space:
                case KeyCode.Tab:
                case KeyCode.Return:
                    if (textBeforeEdit != "")
                    {
                        // Toggle state
                        if (m_Data.m_OnSelectCallback != null)
                        {
                            m_Data.m_OnSelectCallback(m_Data.NewOrMatchingElement(textBeforeEdit));
                        }

                        if (evt.keyCode == KeyCode.Tab || evt.keyCode == KeyCode.Comma)
                        {
                            clearText = true;      // to ease multiple entries (it is unlikely that the same filter is used more than once)
                        }
                        // Auto close
                        if (m_Data.m_CloseOnSelection || evt.keyCode == KeyCode.Return)
                        {
                            closeWindow = true;
                        }
                    }
                    useEventBeforeTextField = true;
                    break;

                case KeyCode.Delete:
                case KeyCode.Backspace:
                    enableAutoCompletion = false;
                    // Don't use the event yet, so the textfield below can get it and delete the selection
                    break;

                case KeyCode.DownArrow:
                    ChangeSelectedCompletion(1);
                    useEventBeforeTextField = true;
                    break;

                case KeyCode.UpArrow:
                    ChangeSelectedCompletion(-1);
                    useEventBeforeTextField = true;
                    break;

                case KeyCode.None:
                    if (evt.character == ' ' || evt.character == ',')
                    {
                        useEventBeforeTextField = true;
                    }
                    break;
                }
            }

            string textFieldText;

            // Draw textfield
            {
                bool dummy = false;
                Rect pos   = new Rect(windowRect.x + k_Margin / 2, windowRect.y + (m_Gravity == Gravity.Top ? (k_Margin / 2) : (windowRect.height - k_TextFieldHeight - k_Margin / 2)), windowRect.width - k_Margin - 14, k_TextFieldHeight);

                GUI.SetNextControlName(s_TextFieldName);
                EditorGUI.FocusTextInControl(s_TextFieldName);
                int id = EditorGUIUtility.GetControlID(s_TextFieldHash, FocusType.Keyboard, pos);

                if (useEventBeforeTextField)
                {
                    evt.Use();  // We have to delay this until after we get the control id, otherwise the id we get is just -1
                }
                if (GUIUtility.keyboardControl == 0)
                {
                    GUIUtility.keyboardControl = id;
                }

                textFieldText = EditorGUI.DoTextField(s_RecycledEditor, id, pos, textBeforeEdit, s_Styles.customTextField, null, out dummy, false, false, false);
                Rect buttonRect = pos;
                buttonRect.x    += pos.width;
                buttonRect.width = 14;
                // Draw "clear textfield" button (X)
                if ((GUI.Button(buttonRect, GUIContent.none, textFieldText != "" ? s_Styles.customTextFieldCancelButton : s_Styles.customTextFieldCancelButtonEmpty) && textFieldText != "") ||
                    clearText)
                {
                    textFieldText = EditorGUI.s_OriginalText = s_RecycledEditor.text = "";
                    s_RecycledEditor.cursorIndex = 0;
                    s_RecycledEditor.selectIndex = 0;
                    enableAutoCompletion         = false;
                }
            }

            // Handle autocompletion
            if (textBeforeEdit != textFieldText)
            {
                m_EnteredText = (0 <= s_RecycledEditor.cursorIndex && s_RecycledEditor.cursorIndex < textFieldText.Length) ? textFieldText.Substring(0, s_RecycledEditor.cursorIndex) : textFieldText;

                if (enableAutoCompletion)
                {
                    UpdateCompletion();
                }
                else
                {
                    SelectNoCompletion();
                }
            }

            if (closeWindow)
            {
                editorWindow.Close();
            }
        }
예제 #16
0
        public void DoTimeControl(Rect rect)
        {
            if (s_Styles == null)
            {
                s_Styles = new Styles();
            }

            var evt = Event.current;
            int id  = EditorGUIUtility.GetControlID(kScrubberIDHash, FocusType.Keyboard);

            // Play/Pause Button + Scrubber
            Rect timelineRect = rect;

            timelineRect.height = kScrubberHeight;
            // Only Scrubber
            Rect scrubberRect = timelineRect;

            scrubberRect.xMin += kPlayButtonWidth;

            // Handle Input
            switch (evt.GetTypeForControl(id))
            {
            case EventType.MouseDown:
                if (rect.Contains(evt.mousePosition))
                {
                    EditorGUIUtility.keyboardControl = id;
                }
                if (scrubberRect.Contains(evt.mousePosition))
                {
                    EditorGUIUtility.SetWantsMouseJumping(1);
                    EditorGUIUtility.hotControl = id;
                    m_MouseDrag       = evt.mousePosition.x - scrubberRect.xMin;
                    nextCurrentTime   = (m_MouseDrag * (stopTime - startTime) / scrubberRect.width + startTime);
                    m_WrapForwardDrag = false;
                    evt.Use();
                }
                break;

            case EventType.MouseDrag:
                if (EditorGUIUtility.hotControl == id)
                {
                    m_MouseDrag += evt.delta.x * playbackSpeed;
                    // We want to not wrap if we immediately drag to the beginning, but we do want to wrap if we drag past the end.
                    if (loop && ((m_MouseDrag < 0.0f && m_WrapForwardDrag) || (m_MouseDrag > scrubberRect.width)))
                    {
                        // scrubing out of range was generating a big deltaTime in wrong time direction
                        // this new code prevent this and it is compliant with new and more robust v5.0 root motion looping of animation clip
                        if (m_MouseDrag > scrubberRect.width)
                        {
                            currentTime -= (stopTime - startTime);
                        }
                        else if (m_MouseDrag < 0)
                        {
                            currentTime += (stopTime - startTime);
                        }

                        m_WrapForwardDrag = true;
                        m_MouseDrag       = Mathf.Repeat(m_MouseDrag, scrubberRect.width);
                    }
                    nextCurrentTime = (Mathf.Clamp(m_MouseDrag, 0.0f, scrubberRect.width) * (stopTime - startTime) / scrubberRect.width + startTime);
                    evt.Use();
                }
                break;

            case EventType.MouseUp:
                if (EditorGUIUtility.hotControl == id)
                {
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    EditorGUIUtility.hotControl = 0;
                    evt.Use();
                }
                break;

            case EventType.KeyDown:
                if (EditorGUIUtility.keyboardControl == id)
                {
                    // TODO: loop?
                    if (evt.keyCode == KeyCode.LeftArrow)
                    {
                        if (currentTime - startTime > kStepTime)
                        {
                            deltaTime = -kStepTime;
                        }
                        evt.Use();
                    }
                    if (evt.keyCode == KeyCode.RightArrow)
                    {
                        if (stopTime - currentTime > kStepTime)
                        {
                            deltaTime = kStepTime;
                        }
                        evt.Use();
                    }
                }
                break;
            }

            // background
            GUI.Box(timelineRect, GUIContent.none, s_Styles.timeScrubber);

            // Play/Pause Button
            playing = GUI.Toggle(timelineRect, playing, playing ? s_Styles.pauseIcon : s_Styles.playIcon, s_Styles.playButton);

            // Current time indicator
            float normalizedPosition = Mathf.Lerp(scrubberRect.x, scrubberRect.xMax, normalizedTime);

            TimeArea.DrawPlayhead(normalizedPosition, scrubberRect.yMin, scrubberRect.yMax, 2f, (EditorGUIUtility.keyboardControl == id) ? 1f : 0.5f);
        }
예제 #17
0
        public override void OnInspectorGUI()
        {
            if (m_Edited)
            {
                UpdateOrder(m_Edited);
                m_Edited = null;
            }

            EditorGUILayout.BeginVertical(EditorStyles.inspectorFullWidthMargins);
            {
                GUILayout.Label(Content.helpText, EditorStyles.helpBox);

                EditorGUILayout.Space();

                // Vertical that contains box and the toolbar below it
                Rect listRect = EditorGUILayout.BeginVertical();
                {
                    int        dropFieldId = EditorGUIUtility.GetControlID(s_DropFieldHash, FocusType.Passive, listRect);
                    MonoScript dropped     = EditorGUI.DoDropField(listRect, dropFieldId, typeof(MonoScript), MonoScriptValidatorCallback, false, Styles.dropField) as MonoScript;
                    if (dropped)
                    {
                        AddScriptToCustomOrder(dropped);
                    }

                    // Vertical that is used as a border around the scrollview
                    EditorGUILayout.BeginVertical(Styles.boxBackground);
                    {
                        // The scrollview itself
                        m_Scroll = EditorGUILayout.BeginVerticalScrollView(m_Scroll);
                        {
                            // List
                            Rect r       = GUILayoutUtility.GetRect(10, kListElementHeight * m_CustomTimeScripts.Count, GUILayout.ExpandWidth(true));
                            int  changed = DragReorderGUI.DragReorder(r, kListElementHeight, m_CustomTimeScripts, DrawElement);
                            if (changed >= 0)
                            {
                                // Give dragged item value in between neighbors
                                SetExecutionOrderAtIndexAccordingToNeighbors(changed, 0);
                                // Update neighbors if needed
                                UpdateOrder(m_CustomTimeScripts[changed]);
                                // Neighbors may have been moved so there's more space around dragged item,
                                // so set order again to get possible rounding benefits
                                SetExecutionOrderAtIndexAccordingToNeighbors(changed, 0);
                            }
                        } EditorGUILayout.EndScrollView();
                    } EditorGUILayout.EndVertical();

                    // The toolbar below the box
                    GUILayout.BeginHorizontal(Styles.toolbar);
                    {
                        GUILayout.FlexibleSpace();
                        Rect       r2;
                        GUIContent content = Content.iconToolbarPlus;
                        r2 = GUILayoutUtility.GetRect(content, Styles.toolbarDropDown);
                        if (EditorGUI.DropdownButton(r2, content, FocusType.Passive, Styles.toolbarDropDown))
                        {
                            ShowScriptPopup(r2);
                        }
                    } GUILayout.EndHorizontal();
                } GUILayout.EndVertical();

                ApplyRevertGUI();
            } GUILayout.EndVertical();

            GUILayout.FlexibleSpace();
        }
예제 #18
0
        internal static Gradient GradientField(GUIContent label, Rect position, Gradient gradient)
        {
            int id = EditorGUIUtility.GetControlID(s_GradientHash, FocusType.Keyboard, position);

            return(DoGradientField(PrefixLabel(position, id, label), id, gradient, null, false));
        }