Search Unity

fixedWidth does not fix width

Discussion in 'Immediate Mode GUI (IMGUI)' started by AhrenM, Jun 11, 2015.

  1. AhrenM

    AhrenM

    Joined:
    Aug 30, 2014
    Posts:
    74
    Hi there,

    I might be out on how EditorGUILayout wants the behave, but his one has me scratching my head.
    Sample and muchly simplified code....

    Code (CSharp):
    1. EditorGUILayout.BeginHorizontal(new GUIStyle() { fixedWidth = 150});
    2. {
    3.        EditorGUILayout.LabelField("Label", new GUIStyle(GUI.skin.label) { fixedWidth = 50 });
    4.        EditorGUILayout.TextField("aa", new GUIStyle(GUI.skin.textArea) { fixedWidth = 100 });
    5. }
    6. EditorGUILayout.EndHorizontal();
    What I'd expect to see is a label and a text field side by side, but what I get is....


    It looks like there is a gap being inserted between the elements and I've tried pretty much every variant of GUIStyle property I can think of to get rid of it.

    I know that replacing the GUIStyle with a GUILayout.Width(XXX) call give the desired visual result but I'm doing a lot with custom styles so I kind of need to work out how to make them behave. And the GUILayout call just returns an option that updates the fixedWidth property anyway, so the code above should be achieving the same results.

    Any ideas out there?

    Ahren M
     
  2. mambo_2

    mambo_2

    Joined:
    Aug 19, 2013
    Posts:
    19
    Hey

    fixedwidth parameter sets the field rect and not the area rect so you need to specify it like this :

    Code (CSharp):
    1.  
    2. EditorGUILayout.BeginHorizontal(GUILayout.Width(150));
    3.         {
    4.             EditorGUILayout.LabelField("Label", new GUIStyle(GUI.skin.label), GUILayout.Width(50));
    5.             EditorGUILayout.TextField("aa", new GUIStyle(GUI.skin.textArea), GUILayout.Width(100));
    6.         }
    7.  
     
    hazmodan and Ghosthowl like this.
  3. AhrenM

    AhrenM

    Joined:
    Aug 30, 2014
    Posts:
    74
    Hi Mambo

    Thanks for pitching in. I figured it was something like that, but I still can work out how to make layout engine simply respect the size of the content that is passed to it. The numbers above are pretty dynamic so it looks like I'll need something like:
    Code (CSharp):
    1. GUIStyle _labelStyle =newGUIStyle(GUI.skin.label) { fixedWidth = w1 };
    2. GUIStyle _textFieldStyle =newGUIStyle(GUI.skin.textField) { fixedWidth = w2 };
    3.  
    4. EditorGUILayout.BeginHorizontal(GUILayout.Width(_labelStyle.fixedWidth + _textFieldStyle.fixedWidth));
    5. {
    6. EditorGUILayout.LabelField("Label", _labelStyle, GUILayout.Width(_labelStyle.fixedWidth));
    7. EditorGUILayout.TextField("aa", _textFieldStyle, GUILayout.Width(_textFieldStyle.fixedWidth));
    8. }
    9. EditorGUILayout.EndHorizontal();
    And that's not even taking height into consideration. It's very plausible in my solution for a font size to change at runtime, so I'd need to calculate that as well.

    This seems way over complicated, I must be doing something just 'the wrong way'. :(
     
  4. Tamerqarrain

    Tamerqarrain

    Joined:
    Mar 16, 2014
    Posts:
    2
    I would create a custom struct, in it I'll have guistyle field and a guilayout field.
    what does runtime have to do with this editor script you just posted?, anyway just use a struct with GUILayout and GUIStyle member fields and initialize them in the constructor.
     
  5. AhrenM

    AhrenM

    Joined:
    Aug 30, 2014
    Posts:
    74
    The code is a just demonstration of the challenge, which seems to boil down to "how to tell the GUILayout engine to respect the dimensions of the content it is rendering".

    For example in HTML if I have a Div, then that element will adjust it's size based on the rules and the content within it. If I change a font size of something within that div at design or run time, I don't have to adjust the sizes of parent containers. The layout just updates to respect the changed content.

    In a horizontal layout area I could easily have code that generates N controls based on runtime situation so the width of the area becomes (Control-Width * N). If Layout containers require their dimensions to be specified in the Begin declaration, then these will all need to be pre-calculated which feel like adding unnecessary complexity.

    I can't help but feel I'm just missing something basic.
     
  6. SonicBloomEric

    SonicBloomEric

    Joined:
    Sep 11, 2014
    Posts:
    1,087
    Ghosthowl likes this.