
local PANEL = {}

local MatBackground = surface.GetTextureID( "vgui/hand" )

local FingerPositions = { 
							{ 56, 230 },
							{ 51, 180 },
							{ 59, 130 },
							
							{ 121, 114 },
							{ 132, 75 },
							{ 160, 34 },
							
							{ 165, 126 },
							{ 178, 92 },
							{ 197, 60 },
							
							{ 194, 156 },
							{ 208, 124 },
							{ 228, 92 },
							
							{ 229, 175 },
							{ 244, 146 },
							{ 259, 115 },
						}

/*---------------------------------------------------------
   Name: Init
---------------------------------------------------------*/
function PANEL:Init()

	self.Hand = 0
	self.FingerVars = {}
	
	for i=0, 14 do
	
		if ( self.NumVars == 18 && i > 10 ) then break end
	
		self.FingerVars[ i ] = vgui.Create( "FingerVar", self )
		
		self.FingerVars[ i ]:SetVarName( "finger_"..i )
		
		if ( i > 2 && i%3 != 0 ) then
		
			self.FingerVars[ i ]:SetRestrictX( true )
		
		end
	
	end

end


/*---------------------------------------------------------
   Name: ControlValues
   Desc: The keyvalues passed from the control defs
---------------------------------------------------------*/
function PANEL:ControlValues( kv )

	if ( kv.hand == '1' ) then
		self.Label:SetText( "#Right Hand" )
		self.Hand = 1
	else
		self.Label:SetText( "#Left Hand" )
		self.Hand = 0
	end
	
	self.NumVars = kv.numvars
	
	// Mitten Hand, remove the last 6 vars
	if ( self.NumVars == "18" ) then
	
		for i=9, 14 do
		
			self.FingerVars[ i ]:Remove()
			self.FingerVars[ i ] = nil
		
		end
	
	end
	
	self:InvalidateLayout( true )

end

/*---------------------------------------------------------
   Name: PerformLayout
---------------------------------------------------------*/
function PANEL:PerformLayout()

	local y = self.BaseClass.PerformLayout( self )
	
	y = y + 256
	y = y + 5
	
	for finger = 0, 4 do
	
		for var = 0, 2 do
		
			local ID =  ((finger*3) + var)
		
			local Pos = FingerPositions[ ID + 1 ]
			if ( Pos && self.FingerVars[ ID ] ) then
				self.FingerVars[ ID ]:SetPos( Pos[1] - 24, Pos[2] - 24 )
			end
		
		end
	
	end

	self:SetTall( y )

end



/*---------------------------------------------------------
   Name: Paint
---------------------------------------------------------*/
function PANEL:Paint()

	surface.SetTexture( MatBackground )
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.DrawTexturedRect( 0, 0, 512, 512 )
	
	return true

end

vgui.Register( "FingerPoser", PANEL, "ContextBase" )