Permalink
Browse files

Improved sandbox search

Queued search - a bit slower time-wise, but should not lag or freeze
Progress bar for search cache building
Fixed entity search not showing proper results in some cases
  • Loading branch information...
1 parent f8a7bdf commit 28d5dfbdfd31e611e0d0af2b3eab19520beb03cf @robotboy655 robotboy655 committed Jul 21, 2016
@@ -1,4 +1,9 @@
+local totalCalls = 0
+local expectedCalls = 1
+
+local queuedSearch = {}
+
local function GetAllFiles( tab, folder, extension, path )
local files, folders = file.Find( folder .. "/*", path )
@@ -11,17 +16,48 @@ local function GetAllFiles( tab, folder, extension, path )
end
+ totalCalls = totalCalls + 1
+
for k, v in pairs( folders ) do
- timer.Simple( k * 0.1, function()
- GetAllFiles( tab, folder .. v .. "/", extension, path )
- end )
+ expectedCalls = expectedCalls + 1
+ table.insert( queuedSearch, { tab, folder .. v .. "/", extension, path } )
end
-
- if ( !timer.Exists( "search_models_update" ) ) then
+
+end
+
+hook.Add( "Think", "sandbox_queued_search", function()
+ if ( #queuedSearch < 1 ) then return end
+
+ local call = queuedSearch[ 1 ]
+ GetAllFiles( call[ 1 ], call[ 2 ], call[ 3 ], call[ 4 ] )
+ table.remove( queuedSearch, 1 )
+
+ if ( !timer.Exists( "search_models_update" ) || #queuedSearch < 1 ) then
timer.Create( "search_models_update", 1, 1, function() hook.Run( "SearchUpdate" ) end )
end
+end )
-end
+hook.Add( "DrawOverlay","sandbox_search_progress", function()
+ if ( !IsValid( g_SpawnMenu ) || !IsValid( g_SpawnMenu.SearchPropPanel ) || expectedCalls == 1 || totalCalls == expectedCalls ) then return end
+
+ -- This code is bad
+ --[[local pnl = g_SpawnMenu.SearchPropPanel
+ local c = pnl:GetChildren()[1]:GetChildren()[1]:GetChildren()[1]
+ if ( IsValid( c ) ) then
+ c.OriginalText = c.OriginalText or c:GetText()
+ c:SetText( c.OriginalText .. " ( Scanning: " .. math.ceil( totalCalls / expectedCalls * 100 ) .. "% )")
+ end]]
+
+ if ( !g_SpawnMenu:IsVisible() ) then return end
+
+ local pnl = g_SpawnMenu.SearchPropPanel
+ local x, y = pnl:LocalToScreen( 0, 0 )
+ local maxw = pnl:GetWide()
+ if ( pnl.VBar && pnl.VBar.Enabled ) then maxw = maxw - 20 end
+
+ draw.RoundedBox( 0, x, y, maxw, 8, Color( 0, 0, 0, 200 ) )
+ draw.RoundedBox( 0, x, y, maxw * ( totalCalls / expectedCalls ), 8, Color( 0, 128, 255, 200 ) )
+end )
local model_list = nil
--
@@ -57,13 +93,13 @@ search.AddProvider( function( str )
end
- if ( #list >= 128 ) then break end
+ if ( #list >= 256 ) then break end
end
return list
-end )
+end, "props" )
--
-- Entity, vehicles
@@ -75,50 +111,49 @@ search.AddProvider( function( str )
local results = {}
local entities = {}
- --table.Add( entities, scripted_ents.GetSpawnable() )
- table.Add( entities, list.Get( "SpawnableEntities" ) )
- for k, v in pairs( list.Get( "Vehicles" ) ) do
+ //for k, v in pairs( scripted_ents.GetSpawnable() ) do
+ for k, v in pairs( list.Get( "SpawnableEntities" ) ) do
+ v.ClassName = k
+ v.ScriptedEntityType = "entity"
+ table.insert( entities, v )
+ end
+ for k, v in pairs( list.Get( "Vehicles" ) ) do
v.ClassName = k
v.PrintName = v.Name
- v.ScriptedEntityType = 'vehicle'
+ v.ScriptedEntityType = "vehicle"
table.insert( entities, v )
-
end
for k, v in pairs( list.Get( "NPC" ) ) do
-
v.ClassName = k
v.PrintName = v.Name
- v.ScriptedEntityType = 'npc'
+ v.ScriptedEntityType = "npc"
table.insert( entities, v )
-
end
for k, v in pairs( list.Get( "Weapon" ) ) do
-
v.ClassName = k
- v.PrintName = v.PrintName
- v.ScriptedEntityType = 'weapon'
+ v.ScriptedEntityType = "weapon"
table.insert( entities, v )
-
end
for k, v in pairs( entities ) do
local name = v.ClassName or v.PrintName
if ( !name ) then continue end
- if ( name:lower():find( str ) ) then
+ if ( name:lower():find( str ) ) then -- TODO: Search print name AND class name?
local entry = {
text = v.PrintName or v.ClassName,
icon = spawnmenu.CreateContentIcon( v.ScriptedEntityType or "entity", nil, {
- nicename = v.PrintName or v.ClassName,
- spawnname = v.ClassName,
- material = "entities/"..v.ClassName..".png",
- admin = v.AdminOnly
+ nicename = v.PrintName or v.ClassName,
+ spawnname = v.ClassName,
+ material = "entities/" .. v.ClassName .. ".png",
+
+ admin = v.AdminOnly
} ),
words = { v }
}
@@ -133,4 +168,4 @@ search.AddProvider( function( str )
return results
-end )
+end, "entities" )
@@ -1,22 +1,22 @@
-
module( "search", package.seeall )
local Providers = {}
+function AddProvider( func, id )
-function AddProvider( func )
-
- local prov =
- {
+ local prov = {
func = func,
}
- table.insert( Providers, prov )
+ if ( id ) then
+ Providers[ id ] = prov
+ else
+ table.insert( Providers, prov )
+ end
end
-
function GetResults( str )
local str = str:lower()
@@ -40,4 +40,3 @@ function GetResults( str )
return results
end
-

0 comments on commit 28d5dfb

Please sign in to comment.