
include('shared.lua')

surface.CreateFont( "coolvetica", 64, 500, true, false, "SandboxLabel" )

ENT.LabelColor = Color( 255, 255, 255, 255 )


// Default Draw Routine..
function ENT:Draw()

	if ( LocalPlayer():GetEyeTrace().Entity == self.Entity && EyePos():Distance( self.Entity:GetPos() ) < 512 ) then
	
		self:DrawEntityOutline( 1.0 )
		self.Entity:DrawModel()
		
		if ( self:GetOverlayText() != "" ) then
			AddWorldTip( self.Entity:EntIndex(), self:GetOverlayText(), 0.5, self.Entity:GetPos(), self.Entity  )
		end
	
	return end

	self.Entity:DrawModel()
	
end


function ENT:DrawOverlayText()
	
	if ( !self:SetLabelVariables() ) then return end
	
	self:DrawLabel()

end

function ENT:DrawFlatLabel( size )

	local TargetAngle 	= self.Entity:GetAngles()
	local TargetPos 	= self.Entity:GetPos() - TargetAngle:Forward() * 16
	
	TargetAngle:RotateAroundAxis( TargetAngle:Up(), 90 )
	
	cam.Start3D2D( TargetPos, TargetAngle, 0.05 * size * self.LabelScale )
	
		local Shadow = Color( 0, 0, 0, self.LabelAlpha * 255 )
		draw.DrawText( self.LabelText, self.LabelFont,  3,  3, Shadow, TEXT_ALIGN_CENTER )

		self.LabelColor.a = self.LabelAlpha * 255
		draw.DrawText( self.LabelText, self.LabelFont, 0, 0, self.LabelColor, TEXT_ALIGN_CENTER )
		
	cam.End3D2D()

end

function ENT:SetLabelVariables()

	// Override this to set the label position, return true to draw and false to not draw.
	
	self.LabelText = self:GetOverlayText()
	if ( self.LabelText == "" ) then return false end
	
	// Only draw if so close
	self.LabelDistance = EyePos():Distance( self.Entity:GetPos() )
	if ( self.LabelDistance > 256 ) then return false end
		
	// Which way should our quad face
	self.LabelAngles = self.Entity:GetAngles()
	self.LabelAngles:RotateAroundAxis( self.LabelAngles:Right(), 90 )
	
	// Make sure we're standing in front of it (so we can see it)
	local ViewNormal = EyePos() - self.Entity:GetPos()
	ViewNormal:Normalize()
	local ViewDot = ViewNormal:Dot( self.LabelAngles:Forward() )
	if ( ViewDot < 0 ) then return false end
	
	// Set the label position
	self.LabelPos = self.Entity:GetPos() + self.LabelAngles:Forward() + self.LabelAngles:Up() * 4
	
	// Alpha
	self.LabelAlpha = (1 - self.LabelDistance / 256)^0.4
	
	self.LabelFont 	= "SandboxLabel"
	self.LabelScale = 1
	
	return true

end


local matOutlineWhite = Material( "white_outline" )
local matOutlineBlack = Material( "black_outline" )

function ENT:DrawEntityOutline( size )
	
	size = size or 1.0
	
	// Place the camera a tiny bit closer to the entity - so it draw a tiny bit bigger
	local EyeNormal = self.Entity:GetPos() - EyePos()
	local Distance = EyeNormal:Length()
	EyeNormal:Normalize()
	
	
	// White Outline (big)
	SetMaterialOverride( matOutlineWhite )
	local Pos = EyePos() + EyeNormal * Distance * (0.15 * size )

	cam.Start3D( Pos, EyeAngles() )
			self.Entity:DrawModel()
	cam.End3D()
	
	
	// White Outline (smaller)
	SetMaterialOverride( matOutlineBlack )
	local Pos = EyePos() + EyeNormal * Distance * (0.08 * size )

	cam.Start3D( Pos, EyeAngles() )
			self.Entity:DrawModel()
	cam.End3D()
	
	
	SetMaterialOverride( nil )

end
