Skip to content

Commit 88e3c55

Browse files
authored
Merge pull request #26 from Atria1234/report-progress
Created permament gui elements for progress bar and show/hide them as needed
2 parents e6406af + 266c1dd commit 88e3c55

3 files changed

Lines changed: 79 additions & 18 deletions

File tree

control.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ require "scripts.remote"
3232
---@field include_requesters LuaGuiElement checkbox
3333
---@field include_signals LuaGuiElement checkbox
3434
---@field include_map_tags LuaGuiElement checkbox
35+
---@field searching_label LuaGuiElement label
36+
---@field search_progressbar LuaGuiElement progressbar
3537
---@field result_flow LuaGuiElement flow
3638
---@field highlighted_button? LuaGuiElement sprite-button
3739

@@ -74,6 +76,8 @@ require "scripts.remote"
7476
---@field not_started_surfaces LuaSurface[]
7577
---@field search_complete boolean
7678
---@field current_surface_search_data? CurrentSurfaceSearchData Used for non-blocking searches
79+
---@field total_chunk_count number
80+
---@field completed_chunk_count number
7781

7882
---@class (exact) EntityGroup
7983
---@field count number

scripts/search-gui.lua

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ function SearchGui.build_results(data, statistics, frame, check_result_found, in
262262
column_count = 10,
263263
style = "slot_table",
264264
children = SearchGui.build_surface_results(surface_name, surface_data.logistics)
265-
}, {
265+
},
266+
{
266267
type = "table",
267268
column_count = 10,
268269
style = "slot_table",
@@ -339,20 +340,29 @@ function SearchGui.build_invalid_state(frame)
339340
})
340341
end
341342

342-
---@param frame LuaGuiElement
343-
function SearchGui.add_loading_results(frame)
344-
gui.add(frame,
345-
{
346-
type = "label",
347-
caption = {"search-gui.searching"},
348-
tooltip = {"search-gui.searching-tooltip", {"", "[font=default-semibold]", {"mod-setting-name.fs-non-blocking-search"}, "[/font]"}}
349-
}
350-
)
343+
---@param refs SearchGuiRefs
344+
---@param progress? double
345+
function SearchGui.show_search_progress(refs, progress)
346+
refs.searching_label.visible = true
347+
refs.search_progressbar.visible = progress ~= nil
348+
349+
if progress ~= nil then
350+
refs.search_progressbar.value = progress
351+
refs.search_progressbar.tooltip = {'', math.floor(progress * 100), '%'}
352+
end
351353
end
352354

353-
function SearchGui.build_loading_results(frame)
354-
frame.clear()
355-
SearchGui.add_loading_results(frame)
355+
---@param refs SearchGuiRefs
356+
function SearchGui.hide_search_progress(refs)
357+
refs.searching_label.visible = false
358+
refs.search_progressbar.visible = false
359+
end
360+
361+
---@param refs SearchGuiRefs
362+
---@param progress? double
363+
function SearchGui.build_loading_results(refs, progress)
364+
refs.result_flow.clear()
365+
SearchGui.show_search_progress(refs, progress)
356366
end
357367

358368
---@param player LuaPlayer
@@ -584,12 +594,37 @@ function SearchGui.build(player)
584594
children = {
585595
{
586596
type = "flow",
587-
ref = {"result_flow"},
588597
direction = "vertical",
589598
children = {
590599
{
591-
type = "label",
592-
caption = {"search-gui.explanation"},
600+
type = "flow",
601+
direction = "vertical",
602+
children = {
603+
{
604+
type = "label",
605+
ref = { "searching_label" },
606+
visible = false,
607+
caption = {"search-gui.searching"},
608+
tooltip = {"search-gui.searching-tooltip", {"", "[font=default-semibold]", {"mod-setting-name.fs-non-blocking-search"}, "[/font]"}}
609+
},
610+
{
611+
type = "progressbar",
612+
ref = { "search_progressbar" },
613+
visible = false,
614+
value = 0
615+
}
616+
}
617+
},
618+
{
619+
type = "flow",
620+
ref = {"result_flow"},
621+
direction = "vertical",
622+
children = {
623+
{
624+
type = "label",
625+
caption = {"search-gui.explanation"},
626+
}
627+
}
593628
}
594629
}
595630
}
@@ -755,7 +790,11 @@ function SearchGui.start_search(player, player_data, _, _, immediate)
755790
search_started = Search.find_machines(item, force, state, player, immediate)
756791
refs.subheader_title.caption = get_signal_name(item) or ""
757792
if search_started then
758-
SearchGui.build_loading_results(refs.result_flow)
793+
if storage.current_searches[player.index].blocking then
794+
SearchGui.build_loading_results(refs, nil)
795+
else
796+
SearchGui.build_loading_results(refs, 0)
797+
end
759798
else
760799
SearchGui.build_results({}, {}, refs.result_flow)
761800
end

scripts/search.lua

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,7 @@ function Search.blocking_search(force, state, target_item, surface_list, type_li
780780
local player_data = storage.players[player.index]
781781
local refs = player_data.refs
782782
SearchGui.build_results(data, statistics, refs.result_flow)
783+
SearchGui.hide_search_progress(refs)
783784
storage.current_searches[player.index] = nil
784785
end
785786

@@ -800,6 +801,7 @@ function on_tick()
800801
local player_data = storage.players[player_index]
801802
local refs = player_data.refs
802803
SearchGui.build_results(search_data.data, search_data.statistics, refs.result_flow)
804+
SearchGui.hide_search_progress(refs)
803805
storage.current_searches[player_index] = nil
804806
end
805807

@@ -832,7 +834,6 @@ function on_tick()
832834
local player_data = storage.players[player_index]
833835
local refs = player_data.refs
834836
SearchGui.build_results(search_data.data, search_data.statistics, refs.result_flow, false, true)
835-
SearchGui.add_loading_results(refs.result_flow)
836837
return -- Start next surface processing on next tick
837838
end
838839

@@ -854,6 +855,9 @@ function on_tick()
854855
search_data.data[current_surface.name] = current_surface_search_data.surface_data
855856
search_data.statistics[current_surface.name] = current_surface_search_data.surface_statistics
856857
search_data.current_surface_search_data = nil
858+
859+
search_data.completed_chunk_count = search_data.completed_chunk_count + chunks_processed
860+
SearchGui.show_search_progress(storage.players[player_index].refs, search_data.completed_chunk_count / search_data.total_chunk_count)
857861
return
858862
end
859863

@@ -941,6 +945,9 @@ function on_tick()
941945
end
942946
::continue::
943947
end
948+
949+
search_data.completed_chunk_count = search_data.completed_chunk_count + chunks_processed
950+
SearchGui.show_search_progress(storage.players[player_index].refs, search_data.completed_chunk_count / search_data.total_chunk_count)
944951
end
945952

946953
---@param target_item SignalID
@@ -1020,9 +1027,18 @@ function Search.find_machines(target_item, force, state, player, immediate)
10201027
surface_list = filtered_surfaces()
10211028
end
10221029

1030+
local total_chunk_count = 0
10231031
local non_blocking_setting = settings.global["fs-non-blocking-search"].value
10241032
if non_blocking_setting == "on" or (non_blocking_setting == "multiplayer" and game.is_multiplayer()) then
10251033
non_blocking_search = true
1034+
1035+
for _, surface in ipairs(surface_list) do
1036+
for chunk in surface.get_chunks() do
1037+
if force.is_chunk_charted(surface, chunk) then
1038+
total_chunk_count = total_chunk_count + 1
1039+
end
1040+
end
1041+
end
10261042
else
10271043
non_blocking_search = false
10281044
end
@@ -1039,6 +1055,8 @@ function Search.find_machines(target_item, force, state, player, immediate)
10391055
statistics = {},
10401056
not_started_surfaces = surface_list,
10411057
search_complete = false,
1058+
total_chunk_count = total_chunk_count,
1059+
completed_chunk_count = 0
10421060
}
10431061
storage.current_searches[player.index] = search_data
10441062
return true

0 commit comments

Comments
 (0)