Ejemplo n.º 1
0
        private static void DatePickerApplyChangeHandlers(string text,
                                                          EditorGUIBinding.TextFieldChangeHandler change,
                                                          EditorGUIBinding.TextFieldFailureHandler failure,
                                                          EditorGUIBinding.TextFieldValidationHandler validate)
        {
            bool validated = (validate == null) ? true : validate(text);

            if (validated)
            {
                change(text);
            }
            else if (!validated && failure != null)
            {
                failure();
            }
        }
Ejemplo n.º 2
0
        public static string DatePicker(string value,
                                        GUIStyle fieldStyle,
                                        EditorGUIBinding.TextFieldChangeHandler change,
                                        EditorGUIBinding.TextFieldFailureHandler failure     = null,
                                        EditorGUIBinding.TextFieldValidationHandler validate = null)
        {
            int  controlID   = GUIUtility.GetControlID(FocusType.Keyboard);
            Rect controlRect = EditorGUILayout.GetControlRect(false);

            fieldStyle.contentOffset = fieldOffset;
            fieldStyle.clipping      = TextClipping.Overflow;

            EditorGUI.BeginChangeCheck();
            EditorGUI.LabelField(controlRect, value, fieldStyle);
            DatePickerWindow window = null;

            switch (Event.current.GetTypeForControl(controlID))
            {
            case EventType.MouseDown:
                if (controlRect.Contains(Event.current.mousePosition) && Event.current.button == 0)
                {
                    GUIUtility.hotControl = controlID;
                    Event.current.Use();
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlID && Event.current.button == 0 &&
                    controlRect.Contains(Event.current.mousePosition))
                {
                    s_DoDropDown = true;
                    Event.current.Use();
                }
                break;

            case EventType.Repaint:
                if (GUIUtility.hotControl == controlID && s_DoDropDown)
                {
                    s_DoDropDown          = false;
                    s_CachedControlId     = controlID;
                    GUIUtility.hotControl = 0;
                    Vector2 position = EditorGUIUtility.GUIToScreenPoint(new Vector2(controlRect.x, controlRect.y + controlRect.height));
                    window = ScriptableObject.CreateInstance <DatePickerWindow>();
                    window.ShowAsDropDown(new Rect(position.x, position.y - winSize.y, winSize.x, winSize.y), winSize, value);
                }
                break;
            }

            if (s_CachedControlId == controlID && s_CachedValue != null)
            {
                value             = s_CachedValue;
                s_CachedControlId = 0;
                s_CachedValue     = null;
                GUI.changed       = true;
            }

            if (EditorGUI.EndChangeCheck())
            {
                DatePickerApplyChangeHandlers(value, change, failure, validate);
            }

            return(value);
        }