new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / vgui / FingerVar.lua

  1.  
  2.  
  3. local ConVar_RestrictFingers    = CreateClientConVar( "finger_restrict", "1", false, false )
  4.  
  5. local PANEL = {}
  6.  
  7. local MAX_ANGLE_X = 100
  8. local MAX_ANGLE_Y = 100
  9.  
  10. /*---------------------------------------------------------
  11.    Name: Init
  12. ---------------------------------------------------------*/
  13. function PANEL:Init()
  14.  
  15.         self.Value = { 0, 0 }
  16.         self.UpdateTimer = 0
  17.        
  18.         // Don't update convars straight away.
  19.         self.NextUpdate = CurTime() + 0.5
  20.        
  21.         // The parent will feed mouse presses to us
  22.         self:SetMouseInputEnabled( false )
  23.  
  24. end
  25.  
  26. /*---------------------------------------------------------
  27.    Name: PerformLayout
  28. ---------------------------------------------------------*/
  29. function PANEL:PerformLayout()
  30.  
  31.         self:SetSize( 48, 48 )
  32.  
  33. end
  34.  
  35.  
  36. /*---------------------------------------------------------
  37.    Name: SetVarName
  38. ---------------------------------------------------------*/
  39. function PANEL:SetVarName( _name_ )
  40.  
  41.         self.VarName = _name_
  42.  
  43. end
  44.  
  45. /*---------------------------------------------------------
  46.    Name: SetRestrictX
  47.    Desc: Restrict movement on the X axis.
  48. ---------------------------------------------------------*/
  49. function PANEL:SetRestrictX( bRestrict )
  50.  
  51.         self.RestrictX = bRestrict
  52.  
  53. end
  54.  
  55. /*---------------------------------------------------------
  56.    Name: IsRestricted
  57. ---------------------------------------------------------*/
  58. function PANEL:IsRestricted()
  59.  
  60.         return self.RestrictX && ConVar_RestrictFingers:GetBool()
  61.  
  62. end
  63.  
  64. /*---------------------------------------------------------
  65.    Name: GetValue
  66.    Desc: Returns the normalized value
  67. ---------------------------------------------------------*/
  68. function PANEL:GetValue()
  69.  
  70.         return { self.Value[1] / MAX_ANGLE_X, self.Value[2] / MAX_ANGLE_Y }
  71.  
  72. end
  73.  
  74.  
  75. /*---------------------------------------------------------
  76.    Name: UpdateConVar
  77. ---------------------------------------------------------*/
  78. function PANEL:UpdateConVar()
  79.  
  80.         if (!self.VarName) then return end
  81.         if ( self.NextUpdate > CurTime() ) then return end
  82.        
  83.         local Val = Format( "%.2f %.2f", self.Value[1], self.Value[2] )
  84.         RunConsoleCommand( self.VarName, Val )
  85.  
  86. end
  87.  
  88. /*---------------------------------------------------------
  89.    Name: SetValue
  90. ---------------------------------------------------------*/
  91. function PANEL:SetValue( x, y )
  92.  
  93.         x = math.Clamp( x, -0.5, 0.5 ) * MAX_ANGLE_X   
  94.         y = math.Clamp( y, -0.5, 0.5 ) * MAX_ANGLE_Y
  95.  
  96.         if ( self:IsRestricted() ) then x = 0 end
  97.        
  98.         self.Value = { x, y }
  99.  
  100. end
  101.  
  102.  
  103. /*---------------------------------------------------------
  104.    Name: OnMousePressed
  105. ---------------------------------------------------------*/
  106. function PANEL:OnMousePressed( mousecode )
  107.  
  108.         if ( mousecode == MOUSE_RIGHT ) then
  109.  
  110.                 self:SetValue( 0, 0 )
  111.                 self:UpdateConVar()
  112.        
  113.         return end
  114.  
  115.         self:SetMouseInputEnabled( true )
  116.         self:MouseCapture( true )
  117.         self.Dragging = 1
  118.         self:GetParent().Dragging = true
  119.        
  120. end
  121.  
  122. /*---------------------------------------------------------
  123.    Name: OnMouseReleased
  124. ---------------------------------------------------------*/
  125. function PANEL:OnMouseReleased()
  126.  
  127.         self:MouseCapture( false )
  128.         self.Dragging = nil
  129.         self:SetMouseInputEnabled( false )
  130.         self:GetParent().Dragging = false
  131.        
  132. end
  133.  
  134. /*---------------------------------------------------------
  135.    Name: OnCursorMoved
  136. ---------------------------------------------------------*/
  137. function PANEL:OnCursorMoved( x, y )
  138.  
  139.         if (!self.Dragging) then return end
  140.        
  141.         local w = self:GetWide()
  142.         local h = self:GetTall()
  143.        
  144.         self:SetValue( (x/w) - 0.5, (y/h) - 0.5 )
  145.         self:UpdateConVar()
  146.        
  147. end
  148.  
  149. /*---------------------------------------------------------
  150.    Name: Think
  151. ---------------------------------------------------------*/
  152. function PANEL:Think()
  153.  
  154.         if ( self.UpdateTimer > CurTime() ) then return end
  155.         self.UpdateTimer = CurTime() + 0.1
  156.  
  157.         local Value = GetConVarString( self.VarName )
  158.         local Value = string.Explode( " ", Value )
  159.        
  160.         self.Value[1] = tonumber( Value[1] )
  161.         self.Value[2] = tonumber( Value[2] )
  162.        
  163. end
  164.  
  165. /*---------------------------------------------------------
  166.    Name: Paint
  167. ---------------------------------------------------------*/
  168. function PANEL:Paint()
  169.  
  170.         local v = self:GetValue()
  171.        
  172.         local w = self:GetWide()
  173.         local h = self:GetTall()
  174.        
  175.         //surface.SetDrawColor( 0, 0, 0, 100 )
  176.         //surface.DrawRect( 0, 0, w, h )
  177.        
  178.         local x = (v[1] * w) + w/2
  179.         local y = (v[2] * h) + h/2
  180.        
  181.         x = math.Clamp( x, 3, w-3 )
  182.         y = math.Clamp( y, 3, h-3 )
  183.        
  184.         surface.SetDrawColor( 0, 0, 0, 250 )
  185.         if ( self.HoveredFingerVar ) then surface.SetDrawColor( 255, 255, 255, 255 ) end
  186.         surface.DrawLine( x, y, w/2, h/2 )
  187.         surface.DrawRect( w/2-1, h/2-1, 2, 2 )
  188.         surface.DrawRect( x-3, y-3, 6, 6 )
  189.        
  190.         surface.SetDrawColor( 255, 255, 0, 255 )
  191.         if ( self:IsRestricted() ) then surface.SetDrawColor( 30, 180, 255, 255 ) end
  192.         surface.DrawRect( x-2, y-2, 4, 4 )
  193.  
  194. end
  195.  
  196. vgui.Register( "FingerVar", PANEL, "Panel" )