// Animancer // Copyright 2020 Kybernetik // #if UNITY_EDITOR using UnityEditor; using UnityEngine; namespace Animancer.Editor { /// [Editor-Only] A cached width calculation for GUI elements. public class GUIElementWidth { /************************************************************************************************************************/ private GUIStyle _Style; private string _Text; private float _Width; /************************************************************************************************************************/ /// Returns the width the `text` would take up if drawn with the `style`. public float GetWidth(GUIStyle style, string text) { if (_Style != style || _Text != text) { _Style = style; _Text = text; _Width = style.CalculateWidth(text); OnRecalculate(style, text); } return _Width; } /// Called when is called with different parameters. protected virtual void OnRecalculate(GUIStyle style, string text) { } /************************************************************************************************************************/ } /// [Editor-Only] /// A cached width calculation for GUI elements which accounts for boldness in prefab overrides. /// public sealed class GUIElementWidthBoldable : GUIElementWidth { /************************************************************************************************************************/ private float _BoldWidth; /// Called when is called with different parameters. protected override void OnRecalculate(GUIStyle style, string text) { var fontStyle = style.fontStyle; style.fontStyle = FontStyle.Bold; _BoldWidth = style.CalculateWidth(text); style.fontStyle = fontStyle; } /************************************************************************************************************************/ /// Returns the width the `text` would take up if drawn with the `style`. public float GetWidth(GUIStyle style, string text, bool bold) { var regularWidth = GetWidth(style, text); return bold ? _BoldWidth : regularWidth; } /// Returns the width the `text` would take up if drawn with the `style`. public float GetWidth(GUIStyle style, string text, SerializedProperty property) { return GetWidth(style, text, property != null && property.prefabOverride); } /************************************************************************************************************************/ } } #endif