Browse Source

GUI: Library window entries are now uniformly distributed and multi-resource background now properly spans the window edges

BearishSun 6 years ago
parent
commit
34b874a90f

+ 66 - 23
Source/EditorManaged/Windows/Library/LibraryGUIContent.cs

@@ -33,7 +33,8 @@ namespace bs.Editor
         private bool gridLayout;
 
         private int elementsPerRow;
-        private int horzElementSpacing;
+        private int paddingLeft;
+        private int paddingRight;
 
         private List<LibraryGUIEntry> entries = new List<LibraryGUIEntry>();
         private Dictionary<string, LibraryGUIEntry> entryLookup = new Dictionary<string, LibraryGUIEntry>();
@@ -55,11 +56,19 @@ namespace bs.Editor
         }
 
         /// <summary>
-        /// Returns horizontal spacing between elements in a grid layout.
+        /// Returns the padding to the left of the first element in a row of elements in a grid layout.
         /// </summary>
-        public int HorzElementSpacing
+        public int PaddingLeft
         {
-            get { return horzElementSpacing; }
+            get { return paddingLeft; }
+        }
+
+        /// <summary>
+        /// Returns the padding to the right of the last element in a row of elements in a grid layout.
+        /// </summary>
+        public int PaddingRight
+        {
+            get { return paddingRight; }
         }
 
         /// <summary>
@@ -194,12 +203,13 @@ namespace bs.Editor
                 }
             }
 
+            int minHorzElemSpacing = 0;
             if (viewType == ProjectViewType.List16)
             {
                 tileSize = 16;
                 gridLayout = false;
                 elementsPerRow = 1;
-                horzElementSpacing = 0;
+                minHorzElemSpacing = 0;
                 int elemWidth = bounds.width;
                 int elemHeight = tileSize;
 
@@ -209,7 +219,7 @@ namespace bs.Editor
                 {
                     ResourceToDisplay entry = resourcesToDisplay[i];
 
-                    LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, main, entry.path, i, elemWidth, elemHeight, 
+                    LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, main, entry.path, i, elemWidth, elemHeight, 0,
                         entry.type);
                     entries.Add(guiEntry);
                     entryLookup[guiEntry.path] = guiEntry;
@@ -232,21 +242,21 @@ namespace bs.Editor
                         tileSize = 64;
                         elemWidth = tileSize;
                         elemHeight = tileSize + 36;
-                        horzElementSpacing = 10;
+                        minHorzElemSpacing = 10;
                         vertElemSpacing = 12;
                         break;
                     case ProjectViewType.Grid48:
                         tileSize = 48;
                         elemWidth = tileSize;
                         elemHeight = tileSize + 36;
-                        horzElementSpacing = 8;
+                        minHorzElemSpacing = 8;
                         vertElemSpacing = 10;
                         break;
                     case ProjectViewType.Grid32:
                         tileSize = 32;
                         elemWidth = tileSize + 16;
                         elemHeight = tileSize + 48;
-                        horzElementSpacing = 6;
+                        minHorzElemSpacing = 6;
                         vertElemSpacing = 10;
                         break;
                 }
@@ -254,11 +264,9 @@ namespace bs.Editor
                 gridLayout = true;
 
                 int availableWidth = bounds.width;
+                elementsPerRow = MathEx.FloorToInt((availableWidth - minHorzElemSpacing) / (float)(elemWidth + minHorzElemSpacing));
 
-                elementsPerRow = MathEx.FloorToInt((availableWidth - horzElementSpacing) / (float)(elemWidth + horzElementSpacing));
-                elementsPerRow = Math.Max(elementsPerRow, 1);
-
-                int numRows = MathEx.CeilToInt(resourcesToDisplay.Count / (float)elementsPerRow);
+                int numRows = MathEx.CeilToInt(resourcesToDisplay.Count / (float)Math.Max(elementsPerRow, 1));
                 int neededHeight = numRows * elemHeight + TOP_MARGIN;
                 if (numRows > 0)
                     neededHeight += (numRows - 1)* vertElemSpacing;
@@ -267,43 +275,78 @@ namespace bs.Editor
                 if (requiresScrollbar)
                 {
                     availableWidth -= parent.ScrollBarWidth;
-                    elementsPerRow = MathEx.FloorToInt((availableWidth - horzElementSpacing) / (float)(elemWidth + horzElementSpacing));
-                    elementsPerRow = Math.Max(elementsPerRow, 1);
+                    elementsPerRow = MathEx.FloorToInt((availableWidth - minHorzElemSpacing) / (float)(elemWidth + minHorzElemSpacing));
+                }
+
+                int extraRowSpace = availableWidth - minHorzElemSpacing * 2 - elementsPerRow * elemWidth;
+
+                paddingLeft = minHorzElemSpacing;
+                paddingRight = minHorzElemSpacing;
+                float horzSpacing = 0.0f;
+
+                // Distribute the spacing to the padding, as there are not in-between spaces to apply it to
+                if (extraRowSpace > 0 && elementsPerRow < 2)
+                {
+                    int extraPadding = extraRowSpace / 2;
+                    paddingLeft += extraPadding;
+                    paddingRight += (extraRowSpace - extraPadding);
+
+                    extraRowSpace = 0;
                 }
+                else
+                    horzSpacing = extraRowSpace / (float)(elementsPerRow - 1);
 
-                int extraRowSpace = availableWidth - (elementsPerRow * (elemWidth + horzElementSpacing) + horzElementSpacing);
+                elementsPerRow = Math.Max(elementsPerRow, 1);
 
                 main.AddSpace(TOP_MARGIN);
                 GUILayoutX rowLayout = main.AddLayoutX();
-                rowLayout.AddSpace(horzElementSpacing);
+                rowLayout.AddSpace(paddingLeft);
 
+                float spacingCounter = 0.0f;
                 int elemsInRow = 0;
                 for (int i = 0; i < resourcesToDisplay.Count; i++)
                 {
                     if (elemsInRow == elementsPerRow && elemsInRow > 0)
                     {
                         main.AddSpace(vertElemSpacing);
-                        rowLayout.AddSpace(extraRowSpace);
                         rowLayout = main.AddLayoutX();
-                        rowLayout.AddSpace(horzElementSpacing);
+                        rowLayout.AddSpace(paddingLeft);
 
                         elemsInRow = 0;
+                        spacingCounter = 0.0f;
                     }
 
                     ResourceToDisplay entry = resourcesToDisplay[i];
+                    elemsInRow++;
+
+                    if (elemsInRow != elementsPerRow)
+                        spacingCounter += horzSpacing;
+
+                    int spacing = (int)spacingCounter;
+                    spacingCounter -= spacing;
 
                     LibraryGUIEntry guiEntry = new LibraryGUIEntry(this, rowLayout, entry.path, i, elemWidth, elemHeight,
-                        entry.type);
+                        spacing, entry.type);
                     entries.Add(guiEntry);
                     entryLookup[guiEntry.path] = guiEntry;
 
-                    rowLayout.AddSpace(horzElementSpacing);
+                    if (elemsInRow == elementsPerRow)
+                        rowLayout.AddSpace(paddingRight);
 
-                    elemsInRow++;
+                    rowLayout.AddSpace(spacing);
                 }
 
                 int extraElements = elementsPerRow - elemsInRow;
-                rowLayout.AddSpace((elemWidth + horzElementSpacing) * extraElements + extraRowSpace);
+                int extraSpacing = 0;
+
+                if (extraElements > 1)
+                {
+                    spacingCounter += (extraElements - 1) * horzSpacing;
+                    extraSpacing += (int)spacingCounter;
+                }
+
+                if (extraElements > 0)
+                    rowLayout.AddSpace(elemWidth * extraElements + extraSpacing + paddingRight);
 
                 main.AddFlexibleSpace();
             }

+ 30 - 6
Source/EditorManaged/Windows/Library/LibraryGUIEntry.cs

@@ -44,6 +44,7 @@ namespace bs.Editor
         public GUITexture icon;
         public GUILabel label;
         public Rect2I bounds;
+        public int spacing;
 
         private GUITexture underlay;
         private GUITexture groupUnderlay;
@@ -73,10 +74,11 @@ namespace bs.Editor
         /// <param name="path">Path to the project library entry to display data for.</param>
         /// <param name="index">Sequential index of the entry in the conent area.</param>
         /// <param name="width">Width of the GUI entry.</param>
-        /// <param name="height">Maximum allowed height for the label.</param>"
+        /// <param name="height">Maximum allowed height for the label.</param>
+        /// <param name="spacing">Spacing between this element and the next element on the same row. 0 if last.</param>
         /// <param name="type">Type of the entry, which controls its style and/or behaviour.</param>
         public LibraryGUIEntry(LibraryGUIContent owner, GUILayout parent, string path, int index, int width, int height,
-            LibraryGUIEntryType type)
+            int spacing, LibraryGUIEntryType type)
         {
             GUILayout entryLayout;
 
@@ -148,6 +150,7 @@ namespace bs.Editor
             this.underlay = null;
             this.type = type;
             this.width = width;
+            this.spacing = spacing;
         }
 
         /// <summary>
@@ -189,12 +192,33 @@ namespace bs.Editor
             {
                 if (owner.GridLayout)
                 {
-                    int offsetToNext = BG_HORZ_PADDING + owner.HorzElementSpacing;
+                    bool firstInRow = index % owner.ElementsPerRow == 0;
+                    bool lastInRow = index % owner.ElementsPerRow == (owner.ElementsPerRow - 1);
+
+                    int offsetToPrevious = 0;
+                    if (type == LibraryGUIEntryType.MultiFirst)
+                    {
+                        if (firstInRow)
+                            offsetToPrevious = owner.PaddingLeft / 3;
+                        else
+                            offsetToPrevious = spacing / 3;
+                    }
+                    else if (firstInRow)
+                        offsetToPrevious = owner.PaddingLeft;
+
+                    int offsetToNext = spacing;
                     if (type == LibraryGUIEntryType.MultiLast)
-                        offsetToNext = BG_HORZ_PADDING * 2;
+                    {
+                        if (lastInRow)
+                            offsetToNext = owner.PaddingRight / 3;
+                        else
+                            offsetToNext = spacing / 3;
+                    }
+                    else if (lastInRow)
+                        offsetToNext = owner.PaddingRight + spacing;
 
-                    Rect2I bgBounds = new Rect2I(bounds.x - BG_HORZ_PADDING, bounds.y, 
-                        bounds.width + offsetToNext, bounds.height);
+                    Rect2I bgBounds = new Rect2I(bounds.x - offsetToPrevious, bounds.y, 
+                        bounds.width + offsetToNext + offsetToPrevious, bounds.height);
                     groupUnderlay.Bounds = bgBounds;
                 }
                 else