Advertisement
StefanBashkir

GUI Grid + Padding Math and Generator

Mar 14th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. local GUI = Game:GetService("Selection"):Get()[1]
  2.  
  3. if (not GUI or GUI.Name ~= "ItemContainer") then
  4.     error("Invalid inventory GUI selected")
  5. end
  6.  
  7. local AbsoluteWindowSizeX = (GUI.AbsoluteWindowSize.X-20); -- We take away 20 for 10 pixel padding on both left and right sides
  8. local AmountOfGUIsPerRow = 3;
  9. local TotalGUIs = 30;
  10. local TotalRows = math.floor( (TotalGUIs/AmountOfGUIsPerRow) + 0.05)
  11. -- Here's an interesting part.
  12. -- Depending on how many GUIs in each row we have, we need an amount of spaces in between them equal to AmountOfGUIsPerRow-1.
  13. -- Then we need to determine how many pixels in between each one and also subtract that from the AbsoluteWindowSizeX
  14. local PaddingInBetweenGUIs = 10;
  15. local AbsoluteWindowSizeX = AbsoluteWindowSizeX - (PaddingInBetweenGUIs * (AmountOfGUIsPerRow-1));
  16. local SizeOfEachGUI = math.floor( (AbsoluteWindowSizeX/AmountOfGUIsPerRow) + 0.05);
  17.  
  18. -- Should be it. Generate the GUIs in the selected container.
  19. for Row = 1, TotalRows do
  20.     for Item = 1, AmountOfGUIsPerRow do
  21.         local ShouldPadX, ShouldPadY = false, false;
  22.         local ImageGUI = Instance.new("ImageLabel");
  23.         ImageGUI.Size = UDim2.new(0,SizeOfEachGUI,0,SizeOfEachGUI);
  24.         ImageGUI.BackgroundTransparency = 1;
  25.         ImageGUI.Image = "";
  26.         ImageGUI.ZIndex = 2
  27.        
  28.         local FallbackText = Instance.new("TextLabel");
  29.         FallbackText.Size = UDim2.new(1,0,1,0);
  30.         FallbackText.BackgroundColor3 = Color3.new(30/255,30/255,30/255);
  31.         FallbackText.TextColor3 = BrickColor.White().Color;
  32.         FallbackText.Text = "Inventory Item Name"
  33.         FallbackText.TextWrapped = true;
  34.        
  35.         -- Do not pad first or last GUIs
  36.         if (Row > 1) then
  37.             ShouldPadY = true;
  38.         end
  39.        
  40.         if (Item > 1) then
  41.             ShouldPadX = true;
  42.         end
  43.        
  44.         ImageGUI.Position = UDim2.new(0,10 + ((SizeOfEachGUI + (ShouldPadX and PaddingInBetweenGUIs or 0)) * (Item-1)),0, 10 + ((SizeOfEachGUI + (ShouldPadY and PaddingInBetweenGUIs or 0)) * (Row-1)))
  45.         ImageGUI.Name = "[" .. tostring(Row) .. "," .. tostring(Item) .. "]"
  46.         ImageGUI.Parent = GUI
  47.         FallbackText.Parent = ImageGUI
  48.     end
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement