Exemple #1
0
        /// <summary>
        /// Draws a label showing the `weight` aligned to the right side of the `area` and reduces its
        /// <see cref="Rect.width"/> to remove that label from its area.
        /// </summary>
        public static void DoWeightLabel(ref Rect area, float weight)
        {
            string label;

            if (weight < 0)
            {
                label = "-?";
            }
            else
            {
                if (_F1Cache == null)
                {
                    _F1Cache = new ConversionCache <float, string>((value) => value.ToString("F1"));
                }

                label = _F1Cache.Convert(weight);
            }

            if (_WeightLabelStyle == null)
            {
                _WeightLabelStyle = new GUIStyle(GUI.skin.label);
                _WeightValueWidth = _WeightLabelStyle.CalculateWidth("0.0");
            }

            _WeightLabelStyle.normal.textColor = Color.Lerp(Color.grey, TextColor, weight);
            _WeightLabelStyle.fontStyle        = Mathf.Approximately(weight * 10, (int)(weight * 10)) ?
                                                 FontStyle.Normal : FontStyle.Italic;

            var weightArea = StealFromRight(ref area, _WeightValueWidth);

            GUI.Label(weightArea, label, _WeightLabelStyle);
        }
        /// <inheritdoc/>
        protected override void OnRecalculate(GUIStyle style, string text)
        {
            var fontStyle = style.fontStyle;

            style.fontStyle = FontStyle.Bold;
            _BoldWidth      = style.CalculateWidth(text);
            style.fontStyle = fontStyle;
        }
        /************************************************************************************************************************/

        /// <summary>Returns the width the `text` would take up if drawn with the `style`.</summary>
        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);
        }
Exemple #4
0
        /// <summary>
        /// Draws a label showing the `weight` aligned to the right side of the `area` and reduces its
        /// <see cref="Rect.width"/> to remove that label from its area.
        /// </summary>
        public static void DoWeightLabel(ref Rect area, float weight)
        {
            var label = WeightToShortString(weight, out var isExact);

            if (_WeightLabelStyle == null)
            {
                _WeightLabelStyle = new GUIStyle(GUI.skin.label);
            }

            if (_WeightLabelWidth < 0)
            {
                _WeightLabelStyle.fontStyle = FontStyle.Italic;
                _WeightLabelWidth           = _WeightLabelStyle.CalculateWidth("0.0");
            }

            _WeightLabelStyle.normal.textColor = Color.Lerp(Color.grey, TextColor, weight);
            _WeightLabelStyle.fontStyle        = isExact ? FontStyle.Normal : FontStyle.Italic;

            var weightArea = StealFromRight(ref area, _WeightLabelWidth);

            GUI.Label(weightArea, label, _WeightLabelStyle);
        }
Exemple #5
0
        /************************************************************************************************************************/

        /// <summary>
        /// Creates a <see cref="ConversionCache{TKey, TValue}"/> for calculating the GUI width occupied by text using the
        /// specified `style`.
        /// </summary>
        public static ConversionCache <string, float> CreateWidthCache(GUIStyle style)
        => new ConversionCache <string, float>((text) => style.CalculateWidth(text));
Exemple #6
0
 /// <summary>
 /// Calls <see cref="GUIStyle.CalcMinMaxWidth"/> and returns the max width.
 /// <para></para>
 /// This method uses the <see cref="TempContent(string, string, bool)"/>.
 /// </summary>
 public static float CalculateWidth(this GUIStyle style, string content)
 => style.CalculateWidth(TempContent(content, null, false));
 /// <summary>
 /// Calls <see cref="GUIStyle.CalcMinMaxWidth"/> and returns the max width.
 /// <para></para>
 /// This method uses the <see cref="TempContent(string, string, bool)"/>.
 /// </summary>
 public static float CalculateWidth(this GUIStyle style, string content)
 {
     return(style.CalculateWidth(TempContent(content)));
 }
Exemple #8
0
 /// <summary>[Animancer Extension] Calls <see cref="GUIStyle.CalcMinMaxWidth"/> and returns the max width.</summary>
 public static float CalculateWidth(this GUIStyle style, string text)
 {
     using (ObjectPool.Disposable.AcquireContent(out var content, text, null, false))
         return(style.CalculateWidth(content));
 }