Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- EditorLayouter. For laying out horizontal unity editor guis that require a 'rect'
- Usage:
- // init wiht gui's rect
- LayoutRect layout = new LayoutRect(rect);
- // Set up fields. Chained together like this...
- // 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.
- layout.Fixed(40).Space.Variable(.2).Fixed(20).Stretched;
- // Use layout instead of rect for fields
- GUI.Button(layout,"My Button");
- GUI.Label(layout, "A label");
- GUI.Button(layout,"My next Button");
- etc
- Improvements: Could support 'minWidth' for variable fields pretty easily I guess...
- */
- public class EditorLayouter
- {
- enum eType { Fixed, Space, Variable };
- struct LayoutItem
- {
- public eType type;
- public float size;
- }
- int m_index = 0;
- List<LayoutItem> m_items = new List<LayoutItem>();
- Rect m_rect = new Rect();
- Rect m_currRect = new Rect();
- public EditorLayouter(){}
- public EditorLayouter(Rect rect)
- {
- m_rect = rect;
- m_currRect = m_rect;
- m_currRect.width = 0; // start with 0 width
- }
- // Implicitly cast to rect, then move to the next item rect
- public static implicit operator Rect(EditorLayouter self)
- {
- return self.NextRect();
- }
- // Skip this element ( eg: if don't actually need the rect for it for it in the ui)
- public void Skip() { NextRect(); }
- // Addes a fixed width item
- public EditorLayouter Fixed(float width)
- {
- m_items.Add(new LayoutItem(){type=eType.Fixed,size=width});
- return this;
- }
- // Adds a little space, sometimes necessary. You never use the Rect from this, its added between getting the rect for other items
- public EditorLayouter Space {get
- {
- m_items.Add(new LayoutItem(){type=eType.Space, size=2});
- return this;
- }}
- // Adds an item that stretches to fill space (multiple will divide up the space). Same as Variable(1);
- public EditorLayouter Stretched {get
- {
- m_items.Add(new LayoutItem(){type=eType.Variable,size=1});
- return this;
- }}
- // 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)
- public EditorLayouter Variable(float ratio=1)
- {
- m_items.Add(new LayoutItem(){type=eType.Variable,size=ratio});
- return this;
- }
- Rect NextRect()
- {
- // Offset from last item
- m_currRect.x += m_currRect.width;
- // Skip spaces
- while ( m_index < m_items.Count && m_items[m_index].type == eType.Space )
- {
- m_currRect.x += m_items[m_index].size; // space amount
- m_index++;
- }
- if ( m_index >= m_items.Count )
- {
- Debug.LogError("Tried to get too many items from Gui Layout!");
- return new Rect();
- }
- switch( m_items[m_index].type )
- {
- case eType.Fixed:
- {
- m_currRect.width = m_items[m_index].size;
- } break;
- case eType.Variable:
- {
- float ratio = m_items[m_index].size;
- float totalRatio = 0;
- float remainingWidth = m_rect.width;
- foreach( LayoutItem item in m_items )
- {
- if ( item.type == eType.Variable )
- totalRatio += item.size;
- else
- remainingWidth -= item.size;
- }
- m_currRect.width = remainingWidth * (ratio / totalRatio);
- } break;
- }
- m_index++;
- return m_currRect;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement