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)); }
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; }
/// <summary> /// Gets a content Rect appropriate, based on the presence of a label. /// If no label, returns a full LineRect. Otherwise, returns a FieldRect. /// </summary> /// <param name="line"></param> /// <param name="label"></param> /// <returns></returns> public static Rect GetContentRect(int line, GUIContent label) { return(HasLabel(label) ? EditorGUIHelper.GetFieldRect(line) : EditorGUIHelper.GetLineRect(line, false)); }