new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / vgui / MatSelect.lua

  1.  
  2. local PANEL = {}
  3.  
  4. AccessorFunc( PANEL, "ItemWidth",                       "ItemWidth",    FORCE_NUMBER )
  5. AccessorFunc( PANEL, "ItemHeight",                      "ItemHeight",   FORCE_NUMBER )
  6. AccessorFunc( PANEL, "Height",                          "NumRows",              FORCE_NUMBER )
  7. AccessorFunc( PANEL, "m_bSizeToContent",        "AutoHeight",   FORCE_BOOL )
  8.  
  9. /*---------------------------------------------------------
  10.    Name: This function is used as the paint function for
  11.                    selected buttons.
  12. ---------------------------------------------------------*/
  13. local function HighlightedButtonPaint( self )
  14.  
  15.         surface.SetDrawColor( 255, 200, 0, 255 )
  16.        
  17.         for i=2, 3 do
  18.                 surface.DrawOutlinedRect( i, i, self:GetWide()-i*2, self:GetTall()-i*2 )
  19.         end
  20.  
  21. end
  22.  
  23. /*---------------------------------------------------------
  24.    Name: Init
  25. ---------------------------------------------------------*/
  26. function PANEL:Init()
  27.  
  28.         // A panellist is a panel that you shove other panels
  29.         // into and it makes a nice organised frame.
  30.         self.List = vgui.Create( "DPanelList", self )
  31.                 self.List:EnableHorizontal( true )
  32.                 self.List:EnableVerticalScrollbar()
  33.                 self.List:SetSpacing( 0 )
  34.                 self.List:SetPadding( 5 )
  35.        
  36.         self.Controls   = {}
  37.         self.Height             = 2
  38.        
  39.         self:SetItemWidth( 128 )
  40.         self:SetItemHeight( 128 )
  41.  
  42. end
  43.  
  44. /*---------------------------------------------------------
  45.    Name: SetAutoHeight
  46. ---------------------------------------------------------*/
  47. function PANEL:SetAutoHeight( bAutoHeight )
  48.  
  49.         self.m_bSizeToContent = bAutoHeight
  50.         self.List:SetAutoSize( bAutoHeight )
  51.        
  52.         self:InvalidateLayout()
  53.  
  54. end
  55.  
  56. /*---------------------------------------------------------
  57.    Name: AddMaterial
  58. ---------------------------------------------------------*/
  59. function PANEL:AddMaterial( label, value )
  60.  
  61.         // Creeate a spawnicon and set the model
  62.         local Mat = vgui.Create( "DImageButton", self )
  63.         Mat:SetOnViewMaterial( value, "models/wireframe" )
  64.         Mat.AutoSize = false
  65.         Mat.Value = value
  66.         Mat:SetSize( self.ItemWidth, self.ItemHeight )
  67.         Mat:SetToolTip( value )
  68.        
  69.         // Run a console command when the Icon is clicked
  70.         Mat.DoClick =   function ( button )
  71.                                                 RunConsoleCommand( self:ConVar(), value )
  72.                                         end
  73.  
  74.         // Add the Icon us
  75.         self.List:AddItem( Mat )
  76.         table.insert( self.Controls, Mat )
  77.        
  78.         self:InvalidateLayout()
  79.  
  80. end
  81.  
  82. /*---------------------------------------------------------
  83.    Name: SetItemSize
  84. ---------------------------------------------------------*/
  85. function PANEL:SetItemSize( pnl )
  86.  
  87.         local w = self.ItemWidth
  88.         if ( w < 1 ) then w = ( self:GetWide() - self.List:GetPadding()*2 ) * w end
  89.        
  90.         local h = self.ItemHeight
  91.         if ( h < 1 ) then h = ( self:GetWide() - self.List:GetPadding()*2 ) * h end
  92.        
  93.         pnl:SetSize( w, h )
  94.  
  95. end
  96.  
  97. /*---------------------------------------------------------
  98.    Name: AddMaterialEx
  99. ---------------------------------------------------------*/
  100. function PANEL:AddMaterialEx( label, material, value, convars )
  101.  
  102.         // Creeate a spawnicon and set the model
  103.         local Mat = vgui.Create( "DImageButton", self )
  104.         Mat:SetImage( material )
  105.         Mat.AutoSize = false
  106.         Mat.Value = value
  107.         Mat.ConVars = convars
  108.         self:SetItemSize( Mat )
  109.         Mat:SetToolTip( value )
  110.        
  111.         // Run a console command when the Icon is clicked
  112.         Mat.DoClick =   function ( button )
  113.        
  114.                                                 for k, v in pairs( convars ) do RunConsoleCommand( k, v ) end
  115.  
  116.                                         end
  117.  
  118.        
  119.         // Add the Icon us
  120.         self.List:AddItem( Mat )
  121.         table.insert( self.Controls, Mat )
  122.        
  123.         self:InvalidateLayout()
  124.  
  125. end
  126.  
  127. /*---------------------------------------------------------
  128.    Name: ControlValues
  129. ---------------------------------------------------------*/
  130. function PANEL:ControlValues( kv )
  131.  
  132.         self.BaseClass.ControlValues( self, kv )
  133.        
  134.         self.Height = kv.height or 2
  135.        
  136.         // Load the list of models from our keyvalues file
  137.         if (kv.options) then
  138.        
  139.                 for k, v in pairs( kv.options ) do
  140.                         self:AddMaterial( k, v )
  141.                 end
  142.                
  143.         end
  144.        
  145.         self.ItemWidth = kv.itemwidth or 32
  146.         self.ItemHeight = kv.itemheight or 32
  147.        
  148.         for k, v in pairs( self.Controls ) do
  149.                 v:SetSize( self.ItemWidth, self.ItemHeight )
  150.         end
  151.        
  152.         self:InvalidateLayout()
  153.  
  154. end
  155.  
  156. /*---------------------------------------------------------
  157.    Name: PerformLayout
  158. ---------------------------------------------------------*/
  159. function PANEL:PerformLayout()
  160.  
  161.         self.List:SetPos( 0, 0 )
  162.        
  163.         for k, v in pairs( self.List:GetItems() ) do
  164.                 self:SetItemSize( v )
  165.         end
  166.        
  167.         if ( self.m_bSizeToContent ) then
  168.        
  169.                 self.List:SetWide( self:GetWide() )
  170.                 self.List:InvalidateLayout( true )
  171.                 self:SetTall( self.List:GetTall() )
  172.                
  173.         return end
  174.        
  175.         local h = self.ItemHeight
  176.         if ( h < 1 ) then h = ( self:GetWide() - self.List:GetPadding()*2 ) * h end
  177.        
  178.         local Height = (h * self.Height) + (self.List:GetPadding() * 2) + 1
  179.        
  180.         self.List:SetSize( self:GetWide(), Height )
  181.         self:SetTall( Height + 5 )
  182.  
  183. end
  184.  
  185.  
  186.  
  187. /*---------------------------------------------------------
  188.    Name: FindAndSelectMaterial
  189. ---------------------------------------------------------*/
  190. function PANEL:FindAndSelectMaterial( Value )
  191.  
  192.         self.CurrentValue = Value
  193.  
  194.         for k, Mat in pairs( self.Controls ) do
  195.        
  196.                 if ( Mat.Value == Value ) then
  197.                
  198.                         // Remove the old overlay
  199.                         if ( self.SelectedMaterial ) then
  200.                                 self.SelectedMaterial.PaintOver = nil
  201.                         end
  202.                        
  203.                         // Add the overlay to this button
  204.                         Mat.PaintOver = HighlightedButtonPaint;
  205.                         self.SelectedMaterial = Mat
  206.  
  207.                 end
  208.        
  209.         end
  210.  
  211. end
  212.  
  213. /*---------------------------------------------------------
  214.    Name: TestForChanges
  215. ---------------------------------------------------------*/
  216. function PANEL:TestForChanges()
  217.  
  218.         local cvar = self:ConVar()
  219.         if (!cvar) then return end
  220.        
  221.         local Value = GetConVarString( cvar )
  222.        
  223.         if ( Value == self.CurrentValue ) then return end
  224.        
  225.         self:FindAndSelectMaterial( Value )
  226.  
  227. end
  228.  
  229.  
  230. vgui.Register( "MatSelect", PANEL, "ContextBase" )