
/*---------------------------------------------------------
  Called when spawnmenu is trying to be opened. 
   Return false to dissallow it.
---------------------------------------------------------*/
function GM:SpawnMenuOpen()

	GAMEMODE:SuppressHint( "OpeningMenu" )
	GAMEMODE:AddHint( "OpeningContext", 20 )

	return true	
end

/*---------------------------------------------------------
  Called when context menu is trying to be opened. 
   Return false to dissallow it.
---------------------------------------------------------*/
function GM:ContextMenuOpen()

	GAMEMODE:SuppressHint( "OpeningContext" )
	GAMEMODE:AddHint( "ContextClick", 20 )

	return true	
end

local STOOLMenu = {}

	// Note - I'm hardcoding these in so the order is the same as it is now to prevent the 'I can't find nothing' crew
	STOOLMenu["Constraints"] 	= { Text = "#Constraints" }
	STOOLMenu["Construction"] 	= {	Text = "#Construction" }
	STOOLMenu["Poser"]			= { Text = "#Poser" }
	STOOLMenu["Render"] 		= { Text = "#Render" }

/*---------------------------------------------------------
  Called From the engine to populate the Scripted Tool menu.
---------------------------------------------------------*/
function GM:PopulateSTOOLMenu()	

	local Out = {}
	
	// sort out stool menu, alphabetically
	local stools = {}
	for k, v in pairs( STOOLMenu ) do
		table.insert( stools, v )
	end
	table.sort( stools, function (a,b) return (a.Text < b.Text) end )
	
	for k, v in ipairs( stools ) do
	
		table.insert( Out, v )
		
		if ( v.Children ) then
		
			local SortChild = {}
			for k, v in pairs( v.Children ) do
				table.insert( SortChild, v )
			end
		
			table.sort( SortChild, function (a,b) return (a.Text < b.Text) end )
			
			for k, v in ipairs( SortChild ) do
				table.insert( Out, v )
			end
 			
		end
		
	
	end

	return Out
end


/*---------------------------------------------------------
  Called by the toolgun to add a STOOL
---------------------------------------------------------*/
function GM:AddSTOOL( category, itemname, text, command, controls, cpanelfunction )
	
	// No table found.. lets create one
	if ( !STOOLMenu[ category ] ) then
		STOOLMenu[ category ] = { Text = "#"..category } 	
	end
	
	local cat = STOOLMenu[ category ]
	cat.Children = cat.Children or {}
	
	cat.Children[ itemname ] = { ItemName = itemname, Text = text, Command = command, Controls = controls, CPanelFunction = cpanelfunction }

end

local ModelIndex = {}
local ModelIndexTimer = CurTime()

local function BuildModelIndex( dir )

	// Add models from this folder
	local files = file.Find( "../" .. dir .. "*" )
	for k, v in pairs( files ) do
		
		if ( v:sub( -4, -1 ) == ".mdl" ) then
		
			// Filter out some of the un-usable crap
			if ( !v:find( "_gestures" ) && 
				!v:find( "_anim" ) && 
				!v:find( "_gst" ) && 
				!v:find( "_pst" ) && 
				!v:find( "_shd" ) && 
				!v:find( "_ss" ) && 
				!v:find( "_anm" ) ) then
			
				table.insert( ModelIndex, (dir .. v):lower() )
			
			end
			
		elseif ( v:sub( -4, -4 ) != '.' ) then
		
			//BuildModelIndex( dir..v.."/" )
			
			// Stagger the loading so we don't block.
			// This means that the data is inconsistent at first
			// but it's better than adding 5 seconds onto loadtime
			// or pausing for 5 seconds on the first search
			// or dumping all this to a text file and loading it
			ModelIndexTimer = ModelIndexTimer + 0.02
			timer.Simple( ModelIndexTimer - CurTime(), BuildModelIndex, ( dir..v.."/" ) )
			
		end
		
	end
	
end



/*---------------------------------------------------------
  Called by the toolgun to add a STOOL
---------------------------------------------------------*/
function GM:DoModelSearch( str )

	local ret = {}
	
	if ( #ModelIndex == 0 ) then
		ModelIndexTimer = CurTime()
		BuildModelIndex( "models/" )
	end

	if ( str:len() < 3 ) then
			
		table.insert( ret, "Enter at least 3 characters" )
	
	else

		str = str:lower()
		
		for k, v in pairs( ModelIndex ) do
		
			if ( v:find( str ) ) then
		
				table.insert( ret, v )
			
			end
		
		end
		
	end

	return ret

end
