Only update formspec on search filter changes

This commit is contained in:
SmallJoker 2022-01-23 18:15:17 +01:00
parent d6688872c8
commit 23a45b8131

View File

@ -1,3 +1,5 @@
local ui = unified_inventory
local function default_refill(stack) local function default_refill(stack)
stack:set_count(stack:get_stack_max()) stack:set_count(stack:get_stack_max())
local itemdef = minetest.registered_items[stack:get_name()] local itemdef = minetest.registered_items[stack:get_name()]
@ -48,11 +50,11 @@ end)
local function apply_new_filter(player, search_text, new_dir) local function apply_new_filter(player, search_text, new_dir)
local player_name = player:get_player_name() local player_name = player:get_player_name()
minetest.sound_play("click", {to_player=player_name, gain = 0.1}) minetest.sound_play("click", {to_player=player_name, gain = 0.1})
unified_inventory.apply_filter(player, search_text, new_dir) ui.apply_filter(player, search_text, new_dir)
unified_inventory.current_searchbox[player_name] = search_text ui.current_searchbox[player_name] = search_text
unified_inventory.set_inventory_formspec(player, ui.set_inventory_formspec(player, ui.current_page[player_name])
unified_inventory.current_page[player_name])
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
@ -65,9 +67,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
-- always take new search text, even if not searching on it yet -- always take new search text, even if not searching on it yet
local dirty_search_filter = false
if fields.searchbox if fields.searchbox
and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then
unified_inventory.current_searchbox[player_name] = fields.searchbox unified_inventory.current_searchbox[player_name] = fields.searchbox
dirty_search_filter = true
end end
@ -88,19 +93,14 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
unified_inventory.current_page[player_name]) unified_inventory.current_page[player_name])
end end
if fields.next_category then if fields.next_category or fields.prev_category then
local scroll = math.min(#unified_inventory.category_list-ui_peruser.pagecols, unified_inventory.current_category_scroll[player_name] + 1) local step = fields.next_category and 1 or -1
if scroll ~= unified_inventory.current_category_scroll[player_name] then local scroll_old = ui.current_category_scroll[player_name]
unified_inventory.current_category_scroll[player_name] = scroll local scroll_new = math.max(0, math.min(#ui.category_list - ui_peruser.pagecols, scroll_old + step))
unified_inventory.set_inventory_formspec(player,
unified_inventory.current_page[player_name]) if scroll_old ~= scroll_new then
end ui.current_category_scroll[player_name] = scroll_new
end ui.set_inventory_formspec(player,
if fields.prev_category then
local scroll = math.max(0, unified_inventory.current_category_scroll[player_name] - 1)
if scroll ~= unified_inventory.current_category_scroll[player_name] then
unified_inventory.current_category_scroll[player_name] = scroll
unified_inventory.set_inventory_formspec(player,
unified_inventory.current_page[player_name]) unified_inventory.current_page[player_name])
end end
end end
@ -197,14 +197,17 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.searchbutton if fields.searchbutton
or fields.key_enter_field == "searchbox" then or fields.key_enter_field == "searchbox" then
unified_inventory.apply_filter(player, unified_inventory.current_searchbox[player_name], "nochange") if dirty_search_filter then
unified_inventory.set_inventory_formspec(player, ui.apply_filter(player, ui.current_searchbox[player_name], "nochange")
unified_inventory.current_page[player_name]) ui.set_inventory_formspec(player, ui.current_page[player_name])
minetest.sound_play("paperflip2", minetest.sound_play("paperflip2",
{to_player=player_name, gain = 1.0}) {to_player=player_name, gain = 1.0})
end
elseif fields.searchresetbutton then elseif fields.searchresetbutton then
if ui.current_searchbox[player_name] ~= "" then
apply_new_filter(player, "", "nochange") apply_new_filter(player, "", "nochange")
end end
end
-- alternate buttons -- alternate buttons
if not (fields.alternate or fields.alternate_prev) then if not (fields.alternate or fields.alternate_prev) then