new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / vgui / ToolQuickSelect.lua

  1.  
  2. local PANEL = {}
  3.  
  4. /*---------------------------------------------------------
  5.    Name: Init
  6. ---------------------------------------------------------*/
  7. function PANEL:Init()
  8.  
  9.         self.TargetOffset = 0
  10.         self.Offset = 0
  11.         self.Stick = false
  12.         self.Buttons = {}
  13.         self.In = false
  14.        
  15.         self.btnAdd = vgui.Create( "DImageButton", self )
  16.         self.btnAdd:SetImage( "gui/silkicons/add" )
  17.         self.btnAdd.DoClick = function () self:AddButton() end
  18.         self.btnAdd:SetToolTip( "Add Button" )
  19.        
  20.         self.btnStick = vgui.Create( "DImageButton", self )
  21.         self.btnStick:SetImage( "gui/silkicons/application_put" )
  22.         self.btnStick.DoClick = function () self:StickToggle() end
  23.         self.btnStick:SetToolTip( "Anchor" )
  24.        
  25.         self:LoadSettings()
  26.        
  27.         self:SetIn( false )
  28.  
  29. end
  30.  
  31. /*---------------------------------------------------------
  32.    Name: Paint
  33. ---------------------------------------------------------*/
  34. function PANEL:AddButton()
  35.  
  36.         local btn = vgui.Create( "DImageButton", self )
  37.         btn:SetImage( "gui/silkicons/wrench" )
  38.        
  39.         btn.DoClick =           function ( btn )
  40.        
  41.                                                         LocalPlayer():ConCommand( tostring( btn.strCommand ) .. "\n" )
  42.                                                        
  43.                                                 end
  44.                                                
  45.         btn.DoRightClick =      function ( btn )
  46.                                                        
  47.                                                         local menu = DermaMenu()
  48.                                                                 menu:AddOption( "Properties..", function() self:ButtonProperties( btn ) end )
  49.                                                                 menu:AddOption( "Remove", function() self:RemoveButton( btn ) end )
  50.                                                                 menu:Open()                            
  51.                                                        
  52.                                                 end
  53.                                                
  54.         btn.strCommand = ""
  55.  
  56.         table.insert( self.Buttons, btn )
  57.        
  58.         self:InvalidateLayout( true )
  59.        
  60.         self:SaveSettings()
  61.  
  62.         return btn
  63.        
  64. end
  65.  
  66.  
  67. /*---------------------------------------------------------
  68.    Name: RemoveButton
  69. ---------------------------------------------------------*/
  70. function PANEL:RemoveButton( btnToRemove )
  71.  
  72.         for k, btn in pairs( self.Buttons ) do
  73.        
  74.                 if ( btnToRemove == btn ) then
  75.                         table.remove( self.Buttons, k )
  76.                 end
  77.  
  78.         end
  79.        
  80.         btnToRemove:Remove()
  81.         self:PerformLayout()
  82.         self:SaveSettings()
  83.  
  84. end
  85.  
  86. /*---------------------------------------------------------
  87.    Name: ButtonProperties
  88. ---------------------------------------------------------*/
  89. function PANEL:ButtonProperties( btn )
  90.  
  91.         local window = vgui.Create( "ToolQuickSelectProperties" )
  92.                 window:Setup( btn, self )
  93.  
  94. end
  95.  
  96. /*---------------------------------------------------------
  97.    Name: StickToggle
  98. ---------------------------------------------------------*/
  99. function PANEL:StickToggle()
  100.  
  101.         self.Stick = !self.Stick
  102.        
  103.         if ( self.Stick ) then
  104.                 self.btnStick:SetImage( "gui/silkicons/anchor" )
  105.         else
  106.                 self.btnStick:SetImage( "gui/silkicons/application_put" )
  107.         end
  108.        
  109.         self:SaveSettings()
  110.  
  111. end
  112.  
  113. /*---------------------------------------------------------
  114.    Name: SetIn
  115. ---------------------------------------------------------*/
  116. function PANEL:SetIn( _in )
  117.  
  118.         if ( self.Stick ) then _in = true end
  119.  
  120.         self.In = _in
  121.        
  122.         if ( self.In ) then
  123.        
  124.                 self.TargetOffset = 24
  125.        
  126.         else
  127.        
  128.                 self.TargetOffset = 0
  129.        
  130.         end
  131.  
  132. end
  133.  
  134. /*---------------------------------------------------------
  135.    Name: Paint
  136. ---------------------------------------------------------*/
  137. function PANEL:Paint()
  138.  
  139.         draw.RoundedBox( 8, 0, 0, self:GetWide(), self:GetTall(), Color( 0, 0, 0, 150 ) )
  140.  
  141. end
  142.  
  143.  
  144. /*---------------------------------------------------------
  145.    Name: Paint
  146. ---------------------------------------------------------*/
  147. function PANEL:Think()
  148.  
  149.         if ( self.Offset != self.TargetOffset ) then
  150.        
  151.                 self.Offset = math.Approach( self.Offset, self.TargetOffset, 500 * FrameTime() )
  152.                 self.y = ScrH() - self.Offset
  153.        
  154.         end
  155.  
  156. end
  157.  
  158. /*---------------------------------------------------------
  159.    Name: PerformLayout
  160. ---------------------------------------------------------*/
  161. function PANEL:PerformLayout()
  162.        
  163.         local t = 24
  164.         local x = 8
  165.         local y = 4
  166.         local space = 8
  167.         local ypos = t
  168.        
  169.         for k, btn in pairs( self.Buttons ) do
  170.        
  171.                 btn:SetSize( 16, 16 )
  172.                 btn:SetPos( x, y )
  173.                 btn:SetToolTip( btn.strCommand )
  174.                
  175.                 x = x + 16 + space
  176.        
  177.         end
  178.  
  179.         // Extra space to seperate it from the others..
  180.         x = x + space
  181.  
  182.         // Add new button
  183.         self.btnAdd:SetSize( 16, 16 )
  184.         self.btnAdd:SetPos( x, y )
  185.         x = x + 16 + space
  186.        
  187.         // Stick
  188.         self.btnStick:SetSize( 16, 16 )
  189.         self.btnStick:SetPos( x, y )
  190.         x = x + 16 + space
  191.        
  192.        
  193.         self:SetSize( x, t * 2 )       
  194.         self:SetPos( ScrW() / 2 - x / 2, ScrH() - self.Offset )
  195.  
  196. end
  197.  
  198.  
  199. /*---------------------------------------------------------
  200.    Name: SaveSettings
  201. ---------------------------------------------------------*/
  202. function PANEL:SaveSettings()
  203.  
  204.         local tab = {}
  205.        
  206.         if ( self.Stick ) then tab.anchor = '1' else tab.anchor = '0' end
  207.        
  208.         for k, btn in pairs( self.Buttons ) do
  209.        
  210.                 local btntab = {}
  211.                 btntab.command = btn.strCommand or ""
  212.                 btntab.icon = btn:GetImage()
  213.                
  214.                 table.insert( tab, btntab )
  215.        
  216.         end
  217.        
  218.        
  219.         local data = util.TableToKeyValues( tab )
  220.         file.Write( "tool_quick_select.txt", data )
  221.        
  222. end
  223.  
  224. /*---------------------------------------------------------
  225.    Name: LoadSettings
  226. ---------------------------------------------------------*/
  227. function PANEL:LoadSettings()
  228.  
  229.         local data = file.Read( "tool_quick_select.txt" )
  230.         if (!data) then return end
  231.        
  232.         local tab = util.KeyValuesToTable( data )
  233.        
  234.         if ( tab.anchor == 1 ) then self:StickToggle() end
  235.        
  236.         for i=1, 100 do
  237.        
  238.                 local btntab = tab[ tostring(i) ]
  239.                 if ( !btntab ) then break end
  240.                                
  241.                 local btn = self:AddButton()
  242.                 btn:SetImage( btntab.icon )
  243.                 btn.strCommand = btntab.command
  244.        
  245.         end
  246.  
  247. end
  248.  
  249. vgui.Register( "ToolQuickSelect", PANEL, "Panel" )