Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

GUIStyle.Calcsize/Calcheight not working well with padding

Discussion in 'Immediate Mode GUI (IMGUI)' started by Suminsky, Jul 12, 2012.

  1. Suminsky

    Suminsky

    Joined:
    Aug 11, 2011
    Posts:
    50
    Check my function to calculate the correct rect size, considering word wrap:
    Code (csharp):
    1.  static public Vector2 ComputeRectToContainString(float minW_p, float maxW_p, float minH_p, float maxClampH_p, string daText_p, GUIStyle targetStyle_p )
    2.     {
    3.         GUIContent content = new GUIContent(daText_p);
    4.  
    5.         Vector2 result = targetStyle_p.CalcSize(content);//not wrapped size(includes total width)
    6.  
    7.         if (result.x < minW_p)
    8.         {
    9.             result.Set(minW_p, minH_p);
    10.         }
    11.  
    12.         else if (result.x > maxW_p)
    13.         {
    14.             result.x = maxW_p;
    15.             result.y = targetStyle_p.CalcHeight(content, maxW_p);
    16.  
    17.             if (result.y > maxClampH_p)
    18.                 result.y = maxClampH_p;
    19.  
    20.             else if (result.y < minH_p)
    21.                 result.y = minH_p;
    22.         }
    23.         else
    24.         {
    25.             result.y = minH_p;
    26.         }
    27.        
    28.  
    29.         return result;
    30.     }
    This seems to work osomly, till you put padding on the guistyle (something absolutely necessary, to make the characters not overlap the borders)
    When you insert padding the size it returns are ridiculous smaller (more than 50% smaller Id guess)..

    Anyone have a guess on how can I account for padding correctly?
     
  2. Suminsky

    Suminsky

    Joined:
    Aug 11, 2011
    Posts:
    50
    S*** solution:
    Code (csharp):
    1.     static public Vector2 ComputeRectToContainString(float minW_p, float maxW_p, float minH_p, float maxClampH_p, string daText_p, GUIStyle targetStyle_p )
    2.     {
    3.         GUIContent content = new GUIContent(daText_p);
    4.  
    5.         RectOffset paddingBKP = new RectOffset(targetStyle_p.padding.left, targetStyle_p.padding.right, targetStyle_p.padding.top, targetStyle_p.padding.bottom);
    6.         targetStyle_p.padding.left = targetStyle_p.padding.right = targetStyle_p.padding.top = targetStyle_p.padding.bottom = 0;//no padding, cause its giving me da S***
    7.  
    8.         Vector2 result = targetStyle_p.CalcSize(content);//not wrapped size(includes total width)
    9.  
    10.         if (result.x < minW_p)
    11.         {
    12.             result.Set(minW_p, minH_p);
    13.         }
    14.  
    15.         else if (result.x > maxW_p)
    16.         {
    17.             result.x = maxW_p;
    18.             result.y = targetStyle_p.CalcHeight(content, maxW_p);
    19.  
    20.             if (result.y > maxClampH_p)
    21.                 result.y = maxClampH_p;
    22.  
    23.             else if (result.y < minH_p)
    24.                 result.y = minH_p;
    25.         }
    26.         else
    27.         {
    28.             result.y = minH_p;
    29.         }
    30.  
    31.  
    32.         //add padding space:
    33.         result.x += ( paddingBKP.left + paddingBKP.right );
    34.         result.y += (paddingBKP.top + paddingBKP.bottom);
    35.  
    36.         //restore padding on the style:
    37.         targetStyle_p.padding = paddingBKP;
    38.        
    39.  
    40.         return result;
    41.     }