Advertisement
Slawer

Slown PNet - Center Child

Jul 11th, 2019
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. if  IsValid(GLOBALMACHIN) then GLOBALMACHIN:Remove() end
  2.  
  3. local function centerChild(viewContainer, codeContainer, child)
  4.     local x, y = child:GetPos()
  5.     codeContainer:MoveTo(-x + viewContainer:GetWide() * 0.5 - child:GetWide() * 0.5, 0, 0.25)
  6.  
  7. end
  8.  
  9. GLOBALMACHIN = vgui.Create("DFrame")
  10. GLOBALMACHIN:SetSize(800, 400)
  11.  
  12. local viewContainer = vgui.Create("DPanel", GLOBALMACHIN)
  13. viewContainer:SetSize(500, 100)
  14. viewContainer:Center()
  15.  
  16. local codeContainer = vgui.Create("DPanel", viewContainer)
  17. codeContainer:SetSize(0, 100)
  18.  
  19. for i = 1, 9 do
  20.     local pnl = vgui.Create("DButton", codeContainer)
  21.     pnl:SetSize(100, 100)
  22.     pnl:SetPos((i - 1) * (pnl:GetWide() + 5), 0)
  23.     pnl:SetText(i)
  24.     codeContainer:SetWide(codeContainer:GetWide() + pnl:GetWide() + 5)
  25.     function pnl:OnMousePressed()
  26.         local x, y = self:GetPos()
  27.         codeContainer:MoveTo(-x + viewContainer:GetWide() * 0.5 - self:GetWide() * 0.5, 0, 0.25)
  28.     end
  29. end
  30.  
  31. codeContainer:SetWide(codeContainer:GetWide() - 5)
  32.  
  33. local intChildActual = math.Round(#codeContainer:GetChildren() * 0.5)
  34. centerChild(viewContainer, codeContainer, codeContainer:GetChild(intChildActual - 1))
  35.  
  36. timer.Simple(1, function()
  37.     intChildActual = intChildActual + 1
  38.     centerChild(viewContainer, codeContainer, codeContainer:GetChild(intChildActual - 1))
  39. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement