
if ( SERVER ) then return end

/*---------------------------------------------------------
   Name: vgui extensions
---------------------------------------------------------*/

/*

  -------
   Panel
  -------
  
   PANEL:Init()
	Overrides paint function for panel.
	Return true to cause it to not draw the default panel
  
  PANEL:Paint()
	Overrides paint function for panel.
	Return true to cause it to not draw the default panel
	
  PANEL::OnMousePressed( mousecode )
  PANEL::OnMouseReleased( mousecode )
    Called when the mouse is pressed/released on the panel.
	Return true to not allow any default actions
	
  PANEL::OnMouseWheeled( delta )
    Called when the mouse is wheeled over the panel
	
  PANEL::OnKeyCodePressed( keycode )
    Called when a key is pressed
	Return true to swallow the key
	
  PANEL:Think()
    Called every frame the panel is visible
	
  PANEL::ApplySchemeSettings( ///todo/// )
    You should use this to apply colours/fonts to your panel and its children

  PANEL::PerformLayout( )
    Use this to position your panel and its children
	
  PANEL::ActionSignal( key, value, ... )
    Receive an ActionSignal (various things are broadcast here)
	
  PANEL::OnCursorEntered()
    Cursor has entered the panel
	
  PANEL::OnCursorExited()
    Cursor has exited the panel
	
  --------
   Button
  --------
	
  PANEL::DoClick()
    Called when a the button is clicked
    

 ---------------
 - Member Vars -
 ---------------
	
  Button
	Selected		-	bool (true if button is selected)
	Depressed		-	bool (true if button is depressed)
	Armed			-	bool (true if button is hovered)
	DefaultButton	-	bool (true if button is default button)

*/



local PanelFactory = {}

// Keep the old function
vgui.CreateX = vgui.Create

function vgui.Create( classname, parent, name, ... )

	// Is this a user-created panel?
	if ( PanelFactory[ classname ] ) then
	
		local metatable = PanelFactory[ classname ]
		
		local panel = vgui.Create( metatable.Base, parent, name )
		table.Merge( panel:GetTable(), metatable )
		
		// Call the Init function if we have it
		if ( panel.Init ) then
			local b, e = pcall( panel.Init, panel )
			if (!b) then Msg( "vgui.Create: Error when calling '", classname ,"':Init (", e, ")\n" ) end
		end
		
		return panel
	
	end
	
	return vgui.CreateX( classname, parent, name )

end


function vgui.Register( name, mtable, base )

	// Default base is Panel
	base = base or "Panel"
	
	mtable.Base = base
	
	PanelFactory[ name ] = mtable

end
