Advertisement
Guest User

dform.lua

a guest
Jun 12th, 2011
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.38 KB | None | 0 0
  1. /*   _                                
  2.     ( )                              
  3.    _| |   __   _ __   ___ ___     _ _
  4.  /'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
  5. ( (_| |(  ___/| |   | ( ) ( ) |( (_| |
  6. `\__,_)`\____)(_)   (_) (_) (_)`\__,_)
  7.  
  8.     DForm
  9.  
  10. */
  11. local PANEL = {}
  12.  
  13. AccessorFunc( PANEL, "m_bSizeToContents",       "AutoSize",         FORCE_BOOL)
  14. AccessorFunc( PANEL, "m_bBackground",           "DrawBackground",   FORCE_BOOL )
  15. AccessorFunc( PANEL, "m_iSpacing",              "Spacing" )
  16. AccessorFunc( PANEL, "m_Padding",               "Padding" )
  17.  
  18. Derma_Hook( PANEL, "Paint", "Paint", "Form" )
  19. Derma_Hook( PANEL, "ApplySchemeSettings", "Scheme", "Form" )
  20.  
  21. /*---------------------------------------------------------
  22.   Name: Init
  23. ---------------------------------------------------------*/
  24. function PANEL:Init()
  25.  
  26.     self.Items = {}
  27.    
  28.     self:SetSpacing( 4 )
  29.     self:SetPadding( 10 )
  30.    
  31.     self:SetDrawBackground( true )
  32.    
  33.     self:SetMouseInputEnabled( true )
  34.     self:SetKeyboardInputEnabled( true )
  35.    
  36.     self.Label = vgui.Create( "DLabel", self )
  37.  
  38. end
  39.  
  40.  
  41. /*---------------------------------------------------------
  42.   Name: SetName
  43. ---------------------------------------------------------*/
  44. function PANEL:SetName( name )
  45.  
  46.     self.Label:SetText( name )
  47.  
  48. end
  49.  
  50. /*---------------------------------------------------------
  51.   Name: Clear
  52. ---------------------------------------------------------*/
  53. function PANEL:Clear()
  54.  
  55.     for k, v in pairs( self.Items ) do
  56.         if ( v.Left ) then v.Left:Remove() end
  57.         if ( v.Right ) then v.Right:Remove() end
  58.     end
  59.  
  60.     self.Items = {}
  61.    
  62. end
  63.  
  64.  
  65. /*---------------------------------------------------------
  66.   Name: AddItem
  67. ---------------------------------------------------------*/
  68. function PANEL:AddItem( left, right )
  69.  
  70.     table.insert( self.Items, { Left = left, Right = right } )
  71.    
  72.     if ( left ) then left:SetParent( self ) end
  73.     if ( right ) then right:SetParent( self ) end
  74.    
  75.     self:InvalidateLayout()
  76.  
  77. end
  78.  
  79.  
  80. /*---------------------------------------------------------
  81.   Name: TextEntry
  82. ---------------------------------------------------------*/
  83. function PANEL:TextEntry( strLabel, strConVar )
  84.  
  85.     local left = vgui.Create( "DLabel", self )
  86.     left:SetText( strLabel )
  87.    
  88.     local right = vgui.Create( "DTextEntry", self )
  89.     right:SetConVar( strConVar )
  90.     right.Stretch = true
  91.    
  92.     self:AddItem( left, right )
  93.    
  94.     return right, left
  95.  
  96. end
  97.  
  98.  
  99. /*---------------------------------------------------------
  100.   Name: MultiChoice
  101. ---------------------------------------------------------*/
  102. function PANEL:MultiChoice( strLabel, strConVar )
  103.  
  104.     local left = vgui.Create( "DLabel", self )
  105.     left:SetText( strLabel )
  106.    
  107.     local right = vgui.Create( "DMultiChoice", self )
  108.     right:SetConVar( strConVar )
  109.     right.Stretch = true
  110.    
  111.     self:AddItem( left, right )
  112.    
  113.     return right, left
  114.  
  115. end
  116.  
  117. /*---------------------------------------------------------
  118.   Name: MultiChoice
  119. ---------------------------------------------------------*/
  120. function PANEL:NumberWang( strLabel, strConVar, numMin, numMax, numDecimals )
  121.  
  122.     local left = vgui.Create( "DLabel", self )
  123.     left:SetText( strLabel )
  124.    
  125.     local right = vgui.Create( "DNumberWang", self )
  126.     right:SetMinMax( numMin, numMax )
  127.    
  128.     if ( numDecimals != nil ) then right:SetDecimals( numDecimals ) end
  129.    
  130.     right:SetConVar( strConVar )
  131.     right:SizeToContents()
  132.    
  133.     self:AddItem( left, right )
  134.    
  135.     return right, left
  136.  
  137. end
  138.  
  139. /*---------------------------------------------------------
  140.   Name: MultiChoice
  141. ---------------------------------------------------------*/
  142. function PANEL:NumSlider( strLabel, strConVar, numMin, numMax, numDecimals )
  143.  
  144.     local left = vgui.Create( "DNumSlider", self )
  145.     left:SetText( strLabel )
  146.     left:SetMinMax( numMin, numMax )
  147.    
  148.     if ( numDecimals != nil ) then left:SetDecimals( numDecimals ) end
  149.    
  150.     left:SetConVar( strConVar )
  151.     left:SizeToContents()
  152.    
  153.     self:AddItem( left, nil )
  154.    
  155.     return left
  156.  
  157. end
  158.  
  159.  
  160. /*---------------------------------------------------------
  161.   Name: MultiChoice
  162. ---------------------------------------------------------*/
  163. function PANEL:CheckBox( strLabel, strConVar )
  164.  
  165.     local left = vgui.Create( "DCheckBoxLabel", self )
  166.     left:SetText( strLabel )
  167.     left:SetConVar( strConVar )
  168.     left:SetIndent( 5 )
  169.  
  170.     self:AddItem( left, nil )
  171.    
  172.     return left
  173.  
  174. end
  175.  
  176. /*---------------------------------------------------------
  177.   Name: Help
  178. ---------------------------------------------------------*/
  179. function PANEL:Help( strHelp )
  180.  
  181.     local left = vgui.Create( "DLabel", self )
  182.  
  183.     left:SetWrap( true )
  184.     left:SetFont( "DefaultSmall" )
  185.     left:SetTextInset( 8 )
  186.     left:SetText( strHelp )
  187.     left:SetContentAlignment( 7 )
  188.     left:SetAutoStretchVertical( true )
  189.  
  190.     self:AddItem( left, nil )
  191.    
  192.     return left
  193.  
  194. end
  195.  
  196. /*---------------------------------------------------------
  197.   Name: Button
  198.         Note: If you're running a console command like "maxplayers 10" you
  199.         need to add the "10" to the arguments, like so
  200.         Button( "LabelName", "maxplayers", "10" )
  201. ---------------------------------------------------------*/
  202. function PANEL:Button( strName, strConCommand, ... /* console command args!! */ )
  203.  
  204.     local left = vgui.Create( "DButton", self )
  205.  
  206.     if ( strConCommand ) then
  207.         left:SetConsoleCommand( strConCommand, ... )
  208.     end
  209.        
  210.     left:SetText( strName )
  211.     self:AddItem( left, nil )
  212.    
  213.     return left
  214.  
  215. end
  216.  
  217. /*---------------------------------------------------------
  218.    Name: PanelSelect
  219. ---------------------------------------------------------*/
  220. function PANEL:PanelSelect()
  221.  
  222.     local left = vgui.Create( "DPanelSelect", self )
  223.     self:AddItem( left, nil )
  224.     return left
  225.  
  226. end
  227.  
  228. /*---------------------------------------------------------
  229.    Name: ComboBox
  230. ---------------------------------------------------------*/
  231. function PANEL:ComboBox( strLabel )
  232.  
  233.     if ( strLabel ) then
  234.         local left = vgui.Create( "DLabel", self )
  235.         left:SetText( strLabel )
  236.         self:AddItem( left )
  237.     end
  238.    
  239.     local right = vgui.Create( "DComboBox", self )
  240.     //right:SetConVar( strConVar )
  241.     right.Stretch = true
  242.    
  243.     self:AddItem( right )
  244.    
  245.     return right, left
  246.  
  247. end
  248.  
  249. /*---------------------------------------------------------
  250.    Name: Rebuild
  251. ---------------------------------------------------------*/
  252. function PANEL:Rebuild()
  253.  
  254. end
  255.  
  256. /*---------------------------------------------------------
  257.    Name: Rebuild
  258. ---------------------------------------------------------*/
  259. function PANEL:PerformLayout()
  260.  
  261.     self.Label:SetPos( 5, 0 )
  262.     self.Label:SizeToContents()
  263.  
  264.     local y = self:GetPadding() + 15
  265.     local Col2 = self:GetWide() * 0.3
  266.    
  267.     for k, v in pairs( self.Items ) do
  268.    
  269.         if ( v.Right ) then
  270.        
  271.             v.Left:SizeToContents()
  272.             Col2 = math.max( Col2, v.Left:GetWide() + self:GetPadding() * 2 )
  273.        
  274.         end
  275.    
  276.     end
  277.  
  278.     for k, v in pairs( self.Items ) do
  279.    
  280.         if ( v.Right ) then
  281.        
  282.             v.Left:SetPos( self:GetPadding(), y )
  283.             v.Right:SetPos( Col2, y )
  284.    
  285.             if ( v.Right.Stretch ) then
  286.            
  287.                 v.Right:SetWide( self:GetWide() - Col2 - self:GetPadding() )
  288.                 v.Right:InvalidateLayout( true )
  289.            
  290.             end
  291.            
  292.             y = y + math.max( v.Left:GetTall(), v.Right:GetTall() ) + self.m_iSpacing
  293.        
  294.         else
  295.  
  296.             v.Left:SetPos( self:GetPadding(), y )
  297.             v.Left:SetWide( self:GetWide() - self:GetPadding() * 2 )
  298.             v.Left:InvalidateLayout( true )
  299.            
  300.             y = y + v.Left:GetTall() + self.m_iSpacing
  301.        
  302.         end
  303.    
  304.     end
  305.    
  306.     self:SetTall( y + self:GetPadding() )
  307.    
  308.     self:TellParentAboutSizeChanges()
  309.  
  310. end
  311.  
  312. derma.DefineControl( "DForm", "", PANEL, "Panel" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement