new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / vgui / PropSelect.lua

  1.  
  2. local PANEL = {}
  3.  
  4. /*---------------------------------------------------------
  5.    Name: This function is used as the paint function for
  6.                    selected buttons.
  7. ---------------------------------------------------------*/
  8. local function HighlightedButtonPaint( self )
  9.  
  10.         surface.SetDrawColor( 255, 200, 0, 255 )
  11.        
  12.         for i=2, 3 do
  13.                 surface.DrawOutlinedRect( i, i, self:GetWide()-i*2, self:GetTall()-i*2 )
  14.         end
  15.  
  16. end
  17.  
  18. /*---------------------------------------------------------
  19.    Name: Init
  20. ---------------------------------------------------------*/
  21. function PANEL:Init()
  22.  
  23.         // A panellist is a panel that you shove other panels
  24.         // into and it makes a nice organised frame.
  25.         self.List = vgui.Create( "DPanelList", self )
  26.                 self.List:EnableHorizontal( true )
  27.                 self.List:EnableVerticalScrollbar()
  28.                 self.List:SetSpacing( 1 )
  29.                 self.List:SetPadding( 3 )
  30.        
  31.         self.Controls   = {}
  32.         self.Height             = 2
  33.  
  34. end
  35.  
  36. /*---------------------------------------------------------
  37.    Name: ControlValues
  38. ---------------------------------------------------------*/
  39. function PANEL:AddModel( model, ConVars )
  40.  
  41.         // Creeate a spawnicon and set the model
  42.         local Icon = vgui.Create( "SpawnIcon", self )
  43.         Icon:SetModel( model )
  44.         Icon:SetToolTip( model )
  45.         Icon.Model = model
  46.         Icon.ConVars = ConVars or {}
  47.  
  48.         local ConVarName = self:ConVar()
  49.        
  50.         // Run a console command when the Icon is clicked
  51.         Icon.DoClick =  function ( self )
  52.        
  53.                                                 for k, v in pairs( self.ConVars ) do
  54.                                                         LocalPlayer():ConCommand( Format( "%s \"%s\"\n", k, v ) )
  55.                                                 end
  56.                                                
  57.                                                 // Note: We run this command after all the optional stuff
  58.                                                 LocalPlayer():ConCommand( Format( "%s \"%s\"\n", ConVarName, model ) )
  59.                                                
  60.                                         end
  61.  
  62.         // Add the Icon us
  63.         self.List:AddItem( Icon )
  64.         table.insert( self.Controls, Icon )
  65.  
  66. end
  67.  
  68. /*---------------------------------------------------------
  69.    Name: ControlValues
  70. ---------------------------------------------------------*/
  71. function PANEL:ControlValues( kv )
  72.  
  73.         self.BaseClass.ControlValues( self, kv )
  74.        
  75.         self.Height = kv.height or 2
  76.        
  77.         // Load the list of models from our keyvalues file
  78.         if (kv.models) then
  79.        
  80.                 for k, v in pairs( kv.models ) do
  81.                         self:AddModel( k, v )
  82.                 end
  83.                
  84.         end
  85.        
  86.         self:InvalidateLayout( true )
  87.  
  88. end
  89.  
  90. /*---------------------------------------------------------
  91.    Name: PerformLayout
  92. ---------------------------------------------------------*/
  93. function PANEL:PerformLayout()
  94.  
  95.         local y = self.BaseClass.PerformLayout( self )
  96.        
  97.         local Height = 64 * self.Height + 6
  98.        
  99.         self.List:SetPos( 0, y )
  100.         self.List:SetSize( self:GetWide(), Height )
  101.        
  102.         y = y + Height
  103.         y = y + 5
  104.        
  105.         self:SetTall( y )
  106.  
  107. end
  108.  
  109.  
  110.  
  111. /*---------------------------------------------------------
  112.    Name: SelectButton
  113. ---------------------------------------------------------*/
  114. function PANEL:FindAndSelectButton( Value )
  115.  
  116.         self.CurrentValue = Value
  117.  
  118.         for k, Icon in pairs( self.Controls ) do
  119.        
  120.                 if ( Icon.Model == Value ) then
  121.                
  122.                         // Remove the old overlay
  123.                         if ( self.SelectedIcon ) then
  124.                                 self.SelectedIcon.PaintOver = nil
  125.                         end
  126.                        
  127.                         // Add the overlay to this button
  128.                         Icon.PaintOver = HighlightedButtonPaint;
  129.                         self.SelectedIcon = Icon
  130.  
  131.                 end
  132.        
  133.         end
  134.  
  135. end
  136.  
  137. /*---------------------------------------------------------
  138.    Name: TestForChanges
  139. ---------------------------------------------------------*/
  140. function PANEL:TestForChanges()
  141.  
  142.         local Value = GetConVarString( self:ConVar() )
  143.        
  144.         if ( Value == self.CurrentValue ) then return end
  145.        
  146.         self:FindAndSelectButton( Value )
  147.  
  148. end
  149.  
  150.  
  151. vgui.Register( "PropSelect", PANEL, "ContextBase" )