/// <summary> /// Generates the button's <see cref="GUIContent"/> object, /// containing the provided label and [optionally] tooltip. /// Tooltip could be modified to reflect the unusual state of the button. /// </summary> private static GUIContent ButtonContent( ButtonAttribute attr, State state, string errorMessage, GUIContent srcLabel ) { Func <string, bool> isEmptyStr = string.IsNullOrEmpty; string text = attr.Label.Trim(); if (isEmptyStr(text)) { text = srcLabel.text; } if (isEmptyStr(text)) { text = DefalutText; } var tooltip = attr.Tooltip; if (isEmptyStr(tooltip)) { tooltip = srcLabel.tooltip; } // modify text for non-regular modes: if (state == State.Error) { string msg = string.Format("[ERROR: {0}]", errorMessage); tooltip = isEmptyStr(tooltip) ? msg : msg + "\n" + tooltip ; text = "[ERROR] " + text; } else if (state == State.Multi) { tooltip = isEmptyStr(tooltip) ? MultiMsg : string.Format("{0}\n{1}", tooltip, MultiMsg) ; } GUIContent buttonText = new GUIContent(text); if (!isEmptyStr(tooltip)) { buttonText.tooltip = tooltip; } return(buttonText); }
/// <summary> /// Calculates the button's <see cref="Rect"/> and <see cref="GUIStyle"/> matching to the provided attribute params. /// </summary> /// <param name="attr">The main <see cref="ButtonAttribute"/> object this drawer is applied to.</param> /// <param name="position">The original <see cref="Rect"/> generated by Unity and passed to <see cref="OnGUI"/>.</param> /// <param name="buttonText">The <see cref="GUIContent"/> object, containing text and an [optional] tooltip.</param> /// <param name="style"> /// [out] Generated <see cref="GUIStyle"/> for the button. /// It's based on the default stule for inspector button, but has it's <see cref="GUIStyle.wordWrap"/> set /// acctordingly to the given button content. /// </param> private Rect ButtonRectAndStyle( ButtonAttribute attr, Rect position, GUIContent buttonText, out GUIStyle style ) { // first, init the style and assume the width from it: style = new GUIStyle( EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector).button ) { wordWrap = false }; if (attr.FontSize > 0) { style.fontSize = attr.FontSize; } var expectedWidth = style.CalcSize(buttonText).x; // get the width in pixels, depending on the mode: float srcWidth = position.width; float width = Mathf.Max( ButtonWidth(attr.Width, srcWidth, expectedWidth), ButtonWidth(attr.MinWidth, srcWidth, expectedWidth) ); width = Mathf.Min(width, srcWidth); // calculate the height: if (expectedWidth > width) { style.wordWrap = true; } _height = style.CalcHeight(buttonText, width); // generate the actual rect: return(new Rect( position.x + Mathf.Max(srcWidth - width, 0.0f) * 0.5f, position.y, width, _height )); }
private Rect ButtonRect(ButtonAttribute attr, Rect position, GUIContent buttonText) { GUIStyle style; return(ButtonRectAndStyle(attr, position, buttonText, out style)); }