new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / menu / ProgressBar.lua

  1. //=============================================================================//
  2. //  ___  ___   _   _   _    __   _   ___ ___ __ __
  3. // |_ _|| __| / \ | \_/ |  / _| / \ | o \ o \\ V /
  4. //  | | | _| | o || \_/ | ( |_n| o ||   /   / \ /
  5. //  |_| |___||_n_||_| |_|  \__/|_n_||_|\\_|\\ |_|  2009
  6. //                                                                               
  7. //=============================================================================//
  8.  
  9. local PANEL = {}
  10.  
  11. AccessorFunc( PANEL, "m_iMin",  "Min" )
  12. AccessorFunc( PANEL, "m_iMax",  "Max" )
  13. AccessorFunc( PANEL, "m_iValue",        "Value" )
  14. AccessorFunc( PANEL, "m_Color",         "Color" )
  15.  
  16. /*---------------------------------------------------------
  17.         Init
  18. ---------------------------------------------------------*/
  19. function PANEL:Init()
  20.        
  21.         self.Label = vgui.Create( "DLabel", self )
  22.         self.Label:SetFont( "DefaultSmall" )
  23.         self.Label:SetColor( Color( 0, 0, 0 ) )
  24.        
  25.         self:SetMin( 0 )
  26.         self:SetMax( 1000 )
  27.         self:SetValue( 253 )
  28.         self:SetColor( Color( 50, 205, 255, 255 ) )
  29.        
  30. end
  31.  
  32. function PANEL:LabelAsPecentage()
  33.         self.m_bLabelAsPercentage = true
  34.         self:UpdateText()
  35. end
  36.  
  37. function PANEL:SetMin( i )
  38.         self.m_iMin = i
  39.         self:UpdateText()
  40. end
  41.  
  42. function PANEL:SetMax( i )
  43.         self.m_iMax = i
  44.         self:UpdateText()
  45. end
  46.  
  47. function PANEL:SetValue( i )
  48.         self.m_iValue = i
  49.         self:UpdateText()
  50. end
  51.  
  52. function PANEL:UpdateText()
  53.        
  54.         if ( !self.m_iMax ) then return end
  55.         if ( !self.m_iMin ) then return end
  56.         if ( !self.m_iValue ) then return end
  57.        
  58.         local fDelta = 0;
  59.        
  60.         if ( self.m_iMax-self.m_iMin != 0 ) then
  61.                 fDelta = ( self.m_iValue - self.m_iMin ) / (self.m_iMax-self.m_iMin)
  62.         end
  63.        
  64.         if ( self.m_bLabelAsPercentage ) then
  65.                 self.Label:SetText( Format( "%.2f%%", fDelta * 100 ) )
  66.                 return
  67.         end
  68.        
  69.         if ( self.m_iMin == 0 ) then
  70.        
  71.                 self.Label:SetText( Format( "%i / %i", self.m_iValue, self.m_iMax ) )
  72.        
  73.         else
  74.        
  75.                 // Todo..
  76.        
  77.         end
  78.        
  79. end
  80.  
  81.  
  82. /*---------------------------------------------------------
  83.         PerformLayout
  84. ---------------------------------------------------------*/
  85. function PANEL:PerformLayout()
  86.  
  87.         //self.Label:CopyBounds( self )
  88.         self.Label:SizeToContents()
  89.         self.Label:AlignRight( 5 )
  90.         self.Label:CenterVertical()
  91.  
  92. end
  93.  
  94. function PANEL:Paint()
  95.  
  96.         local fDelta = 0;
  97.        
  98.         if ( self.m_iMax-self.m_iMin != 0 ) then
  99.                 fDelta = ( self.m_iValue - self.m_iMin ) / (self.m_iMax-self.m_iMin)
  100.         end
  101.        
  102.         local Width = self:GetWide()
  103.  
  104.         surface.SetDrawColor( 0, 0, 0, 170 )
  105.         surface.DrawRect( 0, 0, Width, self:GetTall() )
  106.        
  107.         surface.SetDrawColor( self.m_Color.r, self.m_Color.g, self.m_Color.b, self.m_Color.a * 0.5 )
  108.         surface.DrawRect( 2, 2, Width - 4, self:GetTall() - 4 )
  109.         surface.SetDrawColor( self.m_Color.r, self.m_Color.g, self.m_Color.b, self.m_Color.a )
  110.         surface.DrawRect( 2, 2, Width * fDelta - 4, self:GetTall() - 4 )
  111.  
  112. end
  113.  
  114. vgui.Register( "DProgressBar", PANEL, "DPanel" )
  115.