Search Unity

Using bracketed blocks for GUI code organization?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Tinus, May 2, 2011.

  1. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    I kind of got fed up with the long, unreadable blobs of GUI code I've been writing recently, and tried some easy methods to make them easier to understand. Specifically, I want to easily be able to see different levels of nesting.

    First I tried this:

    Code (csharp):
    1. EditorGUILayout.BeginHorizontal();
    2.     GUILayout.FlexibleSpace();
    3.     EditorGUILayout.BeginVertical();
    4.         GUILayout.Label("Foofoofoo");
    5.         GUILayout.Label("Barbarbar");
    6.     EditorGUILayout.EndVertical();
    7. EditorGUILayout.EndHorizontal();
    This show how things are nested a lot better, but it doesn't play nice with a typical IDE's auto-indentation. The IDE wants to put everything on the same level of indentation. So then I tried this:

    Code (csharp):
    1. EditorGUILayout.BeginHorizontal();
    2. {
    3.     GUILayout.FlexibleSpace();
    4.     EditorGUILayout.BeginVertical();
    5.     {
    6.         GUILayout.Label("Foofoofoo");
    7.         GUILayout.Label("Barbarbar");
    8.     }
    9.     EditorGUILayout.EndVertical();
    10. }
    11. EditorGUILayout.EndHorizontal();
    With the brackets in place the editor understands where you want your indentation, and editing large chunks of GUI code becomes more manageable.

    So my question is: Does this have any nasty side effects? Do bracketed blocks of code that are not associated with language keywords have any special meaning in C# or Java? Or is this perfectly safe?
     
    Last edited: May 2, 2011
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    i'm surprised that even works. what you can do is break up the code block in parts.
    Code (csharp):
    1. void OnGUI ()
    2. {
    3.      GuiPart1 ();
    4.      GuiPart2 ();
    5. }
    6.  
    7. void GuiPart1 ()
    8. {
    9.     your code in here
    10. }
    11.  
    12. ....
    13.  
    or working with delegates is also a nice way
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    there are no side effects. Brackets have an association in all these language and that is that they create a scope so local variables defined inside will cease to exist when the block is exited
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    If you're doing a lot of GUILayout stuff:

    Code (csharp):
    1. function OnGUI () {
    2.     GUILayout.BeginHorizontal();
    3.     GUILayout.Label("Whee!", GUILayout.Width(300), GUILayout.Height(100));
    4.     GUILayout.Space(20);
    5.     GUILayout.Button("Ack!", GUILayout.MaxHeight(200));
    6.     GUILayout.EndHorizontal();
    7. }
    import the namespace instead:

    Code (csharp):
    1. import UnityEngine.GUILayout;
    2.  
    3. function OnGUI () {
    4.     BeginHorizontal();
    5.     Label("Whee!", Width(300), Height(100));
    6.     Space(20);
    7.     Button("Ack!", MaxHeight(200));
    8.     EndHorizontal();
    9. }
    --Eric
     
  5. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Thanks guys, that's excellent advice. :)
     
  6. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    Just to let you know; I use the scope brackets for exactly the same purpose and I don't consider it as 'bad'.
    Visual Studio is excellent for formatting with the use of scope brackets.
    Just hit CTRL + A --> CTRL + K --> CTRL + F once in a while. Or another trick is to remove and add the last closing scope bracket and VS will format every code in that scope.