private Rect GetDisplayRect()
 {
     return(baseAttribute.buttonLocation == BoolAsButtonAttribute.ButtonLocation.Label
                                 ? EditorGUIHelper.GetLabelRect(0)
                         : baseAttribute.buttonLocation == BoolAsButtonAttribute.ButtonLocation.Field || baseAttribute.buttonLocation == BoolAsButtonAttribute.ButtonLocation.FieldWithLabel
                                 ? EditorGUIHelper.GetFieldRect(0)
                                 : EditorGUIHelper.GetLineRect(0));
 }
Esempio n. 2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUIHelper.UpdateRectData(position);

            EditorGUI.LabelField(EditorGUIHelper.GetLabelRect(0), label);

            Rect rect = GetContentRect(0, label);
            Type type = GetEnumType();

            if (!type.IsEnum)
            {
                Rect fieldRect = rect;
                fieldRect.height = EditorGUIHelper.GetPropertyHeight(2);

                EditorGUI.HelpBox(fieldRect, "EnumFlags attribute must be used on an enum.", MessageType.Error);
            }
            else
            {
                EditorGUIHelper.EnumMaskPopup(type, rect, property, options);
            }
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            bool enabledPrior = GUI.enabled;

            EditorGUIHelper.UpdateRectData(position);

            //	Display additional label for FieldWithLabel
            if (baseAttribute.buttonLocation == BoolAsButtonAttribute.ButtonLocation.FieldWithLabel)
            {
                EditorGUI.LabelField(EditorGUIHelper.GetLabelRect(0), label);
            }

            //	Verify that property type is bool
            if (isWrongType(property))
            {
                Rect helpRect = EditorGUIHelper.GetFieldRect(0);
                helpRect.height = EditorGUIHelper.GetPropertyHeight(2);

                EditorGUI.PrefixLabel(EditorGUIHelper.GetLineRect(0), label);
                EditorGUI.HelpBox(helpRect, "BoolAsButton Attribute can only be used on Booleans. (" + property.name + ")", MessageType.Error);
                return;
            }

            //	Layout settings, in regards to multi-object selections
            Rect     displayPos  = GetDisplayRect();
            bool?    selection   = GetSelection(property);
            GUIStyle buttonStyle = GetButtonStyle(selection);
            string   labelText   = GetLabelText(selection, label);

            //	Display toggle and label overlaying the toggle
            EditorGUI.BeginChangeCheck();
            boolValue = EditorGUI.Toggle(displayPos, property.boolValue, buttonStyle);
            if (EditorGUI.EndChangeCheck())
            {
                if (!String.IsNullOrEmpty(baseAttribute.onPress))
                {
                    switch (baseAttribute.boolStyle)
                    {
                    case BoolAsButtonAttribute.BoolStyle.Button:
                        foreach (SerializedProperty prop in EditorGUIHelper.Multiple(property))
                        {
                            SerializedPropertyUtility.InvokeMethod(prop, baseAttribute.onPress);

                            //	Buttons get turned off immediately
                            prop.boolValue = false;
                        }
                        break;

                    case BoolAsButtonAttribute.BoolStyle.Toggleable:
                        foreach (SerializedProperty prop in EditorGUIHelper.Multiple(property))
                        {
                            //	Update value for use in the method
                            prop.boolValue = boolValue;
                            prop.serializedObject.ApplyModifiedProperties();

                            SerializedPropertyUtility.InvokeMethod(prop, baseAttribute.onPress);
                        }
                        break;
                    }
                }
                else
                {
                    property.boolValue = boolValue;
                }
            }

            //	Draw the button label on top of the button
            EditorGUI.LabelField(displayPos, new GUIContent(labelText, baseAttribute.tooltip), EditorGUIHelper.LabelCenteredStyle);

            GUI.enabled = enabledPrior;
        }