/*   _                                
    ( )                               
   _| |   __   _ __   ___ ___     _ _ 
 /'_` | /'__`\( '__)/' _ ` _ `\ /'_` )
( (_| |(  ___/| |   | ( ) ( ) |( (_| |
`\__,_)`\____)(_)   (_) (_) (_)`\__,_) 

	DImage

*/

PANEL = {}

AccessorFunc( PANEL, "m_Material", 	"Material" )
AccessorFunc( PANEL, "m_Color", 	"ImageColor" )

/*---------------------------------------------------------

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

	self:SetImageColor( Color( 255, 255, 255, 255 ) )
	self:SetMouseInputEnabled( false )
	self:SetKeyboardInputEnabled( false )
	
	self.ImageName = ""
	
end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:SetImage( strImage )

	self.ImageName = strImage

	local Mat = Material( strImage )
	local Texture = Mat:GetMaterialTexture( "$basetexture" )
	if ( Texture ) then
		self.ActualWidth = Texture:GetActualWidth()
		self.ActualHeight = Texture:GetActualHeight()
	else
		self.ActualWidth = 32
		self.ActualHeight = 32
	end
	
	//
	// If it's a vertexlitgeneric material we need to change it to be
	// UnlitGeneric so it doesn't go dark when we enter a dark room
	// and flicker all about
	//
	if ( string.find( Mat:GetShader(), "VertexLitGeneric" ) ) then
	
		local t = Mat:GetMaterialTexture( "$basetexture" )
		
		local params = {}
		params[ "$basetexture" ] = t:GetName()
		params[ "$vertexcolor" ] = 1
		params[ "$vertexalpha" ] = 1
		
		Mat = CreateMaterial( "VGUIMaterial", "UnlitGeneric", params )
		
	end
	
	self:SetMaterial( Mat )
	
end


/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:GetImage()

	return self.ImageName
	
end

/*---------------------------------------------------------

---------------------------------------------------------*/
function PANEL:SizeToContents( strImage )

	self:SetSize( self.ActualWidth, self.ActualHeight )

end


/*---------------------------------------------------------

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

	local Mat = self:GetMaterial()
	if (!Mat) then return true end

	surface.SetMaterial( Mat )
	surface.SetDrawColor( self.m_Color.r, self.m_Color.g, self.m_Color.b, self.m_Color.a )
	surface.DrawTexturedRect( 0, 0, self:GetWide(), self:GetTall() )
	return true

end



derma.DefineControl( "DImage", "A simple image", PANEL, "Panel" )