Advertisement
Powerhoof

Unity EditorLayouter.cs

Mar 23rd, 2023
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1.  
  2.  
  3. /**
  4.     EditorLayouter. For laying out horizontal unity editor guis that require a 'rect'
  5.  
  6.     Usage:
  7.         // init wiht gui's rect
  8.         LayoutRect layout = new LayoutRect(rect);
  9.  
  10.         // Set up fields. Chained together like this...
  11.         // This example has a 40px field, a little space, then a field that takes up 20% of spare space, another 20px field, and another that uses the remaining.
  12.         layout.Fixed(40).Space.Variable(.2).Fixed(20).Stretched;
  13.  
  14.         // Use layout instead of rect for fields
  15.  
  16.         GUI.Button(layout,"My Button");
  17.         GUI.Label(layout, "A label");
  18.         GUI.Button(layout,"My next Button");
  19.         etc
  20.  
  21.     Improvements: Could support 'minWidth' for variable fields pretty easily I guess...
  22.  
  23. */
  24. public class EditorLayouter
  25. {
  26.     enum eType { Fixed, Space, Variable };
  27.  
  28.     struct LayoutItem
  29.     {
  30.         public eType type;
  31.         public float size;
  32.     }
  33.    
  34.     int m_index = 0;   
  35.     List<LayoutItem> m_items = new List<LayoutItem>();
  36.  
  37.     Rect m_rect = new Rect();
  38.     Rect m_currRect = new Rect();
  39.  
  40.     public EditorLayouter(){}
  41.     public EditorLayouter(Rect rect)
  42.     {
  43.         m_rect = rect;
  44.         m_currRect = m_rect;
  45.         m_currRect.width = 0; // start with 0 width
  46.     }
  47.  
  48.  
  49.     // Implicitly cast to rect, then move to the next item rect
  50.     public static implicit operator Rect(EditorLayouter self)
  51.     {
  52.         return self.NextRect();
  53.     }
  54.  
  55.     // Skip this element ( eg: if don't actually need the rect for it for it in the ui)
  56.     public void Skip() {  NextRect(); }
  57.  
  58.     // Addes a fixed width item
  59.     public EditorLayouter Fixed(float width)
  60.     {
  61.         m_items.Add(new LayoutItem(){type=eType.Fixed,size=width});
  62.         return this;
  63.     }
  64.  
  65.     // Adds a little space, sometimes necessary. You never use the Rect from this, its added between getting the rect for other items
  66.     public EditorLayouter Space {get
  67.     {
  68.         m_items.Add(new LayoutItem(){type=eType.Space, size=2});
  69.         return this;
  70.     }}
  71.  
  72.     // Adds an item that stretches to fill space (multiple will divide up the space). Same as Variable(1);
  73.     public EditorLayouter Stretched {get
  74.     {
  75.         m_items.Add(new LayoutItem(){type=eType.Variable,size=1});
  76.             return this;
  77.     }}
  78.     // Adds an item that stretches, with a ratio for how much they should use (eg: you could have one that's 0.2 for 20% widdth, and one that's 0.8 for 80%... I dunno). A variable(2) will be twice as big as a variable(1)
  79.     public EditorLayouter Variable(float ratio=1)
  80.     {  
  81.         m_items.Add(new LayoutItem(){type=eType.Variable,size=ratio});
  82.         return this;
  83.     }
  84.  
  85.  
  86.     Rect NextRect()
  87.     {
  88.         // Offset from last item
  89.         m_currRect.x += m_currRect.width;      
  90.  
  91.         // Skip spaces
  92.         while ( m_index < m_items.Count && m_items[m_index].type == eType.Space )
  93.         {  
  94.             m_currRect.x += m_items[m_index].size; // space amount
  95.             m_index++;
  96.         }
  97.  
  98.         if ( m_index >= m_items.Count )
  99.         {
  100.             Debug.LogError("Tried to get too many items from Gui Layout!");
  101.             return new Rect();
  102.         }
  103.  
  104.         switch( m_items[m_index].type )
  105.         {
  106.             case eType.Fixed:
  107.             {
  108.                 m_currRect.width = m_items[m_index].size;
  109.             } break;
  110.             case eType.Variable:
  111.             {
  112.                 float ratio = m_items[m_index].size;
  113.                 float totalRatio = 0;
  114.                 float remainingWidth = m_rect.width;
  115.                 foreach( LayoutItem item in m_items )
  116.                 {
  117.                     if ( item.type == eType.Variable )
  118.                         totalRatio += item.size;
  119.                     else
  120.                     remainingWidth -= item.size;
  121.                 }
  122.                 m_currRect.width = remainingWidth * (ratio / totalRatio);
  123.             } break;
  124.         }
  125.         m_index++;
  126.        
  127.         return m_currRect;
  128.     }
  129.        
  130.  
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement