Add items search filter on Buy tab

This commit is contained in:
Sys Quatre 2020-09-20 15:03:52 +02:00
parent ce33c159fd
commit b22b72f5c2
2 changed files with 55 additions and 12 deletions

View File

@ -100,9 +100,12 @@ minetest.register_on_joinplayer(function(player)
buy_wear = wear_levels[1].text,
buy_price = "",
buy_amount = "1",
buy_filter = "",
sell_price = "",
buy_page = 1,
buy_pagemax = 0,
selected_index = 0,
filtered_list = nil,
}
end)
@ -131,6 +134,20 @@ minetest.after(0, function()
pagemax = math.max(math.ceil(#selectable_list / pageitems), 1)
end)
local function filter_list(filter)
local filtered_list = {}
if not filter or filter == "" then return nil, pagemax end
for index, name in ipairs(selectable_list) do
local match = string.match(name, filter) or nil
if match then
filtered_list[#filtered_list+1] = name
end
end
return filtered_list, math.max(math.ceil(#filtered_list / pageitems), 1)
end
local main_form = "global_exchange:exchange_main"
local function table_from_results(fs, results, name, x, y, w, h, selected)
@ -186,16 +203,20 @@ local function mk_main_buy_fs(fs, p_name, state)
fs:container(0,4, function()
fs:button( 0,0.25, 1,1, "buy_left", "<<")
fs:button( 5,0.25, 2,1, "position", state.buy_page .. "/" .. pagemax)
fs:button(11,0.25, 1,1, "buy_right", ">>")
fs:button( 1,0.25, 2,1, "position", state.buy_page .. "/" .. state.buy_pagemax)
fs:button(3,0.25, 1,1, "buy_right", ">>")
fs:field(5.28,0.55, 3,1, "buy_filter", S("Filter"), state.buy_filter, false)
fs:button(8, 0.25, 2,1, "filter", S("Search"))
fs:button(10, 0.25, 2,1, "reset_filter", S("Reset"))
local firstitem = ((state.buy_page - 1) * pageitems)
local item_list = state.filtered_list or selectable_list
for y=0,(pageheight-1) do
for x=0,(pagewidth-1) do
local index = firstitem + (pagewidth * y) + x + 1
if selectable_list[index] then
fs:item_image_button(x,1.25+y, 1,1, "select_" .. index,
selectable_list[index])
if item_list[index] then
fs:item_image_button(x,1.25+y, 1,1, "select_" .. index, item_list[index])
end
end
end
@ -385,6 +406,7 @@ local function handle_main(player, fields)
"buy_wear",
"buy_amount",
"buy_price",
"buy_filter",
"sell_price"
}
for _,k in ipairs(copy_fields) do
@ -393,25 +415,30 @@ local function handle_main(player, fields)
end
end
if state.buy_pagemax == 0 then
state.buy_pagemax = pagemax
end
if fields.tab then
state.tab = tonumber(fields.tab) or 1
show_main(p_name)
end
if fields.buy_left then
state.buy_page = (((state.buy_page or 1) + ((2*pagemax-1) - 1)) % pagemax) + 1
state.buy_page = (((state.buy_page or 1) + ((2*state.buy_pagemax-1) - 1)) % state.buy_pagemax) + 1
show_main(p_name)
end
if fields.buy_right then
state.buy_page = (((state.buy_page or 1) + ((2*pagemax-1) + 1)) % pagemax) + 1
state.buy_page = (((state.buy_page or 1) + ((2*state.buy_pagemax-1) + 1)) % state.buy_pagemax) + 1
show_main(p_name)
end
local item_list = state.filtered_list or selectable_list
for name in pairs(fields) do
local index = tonumber(string.match(name, "select_([0-9]+)"))
if index and index >= 1 and index < #selectable_list then
state.buy_item = selectable_list[index]
if index and index >= 1 and index <= #item_list then
state.buy_item = item_list[index]
show_main(p_name)
end
end
@ -430,6 +457,19 @@ local function handle_main(player, fields)
end
end
if fields.filter then
state.filtered_list, state.buy_pagemax = filter_list(state.buy_filter)
state.buy_page = 1
show_main(p_name)
end
if fields.reset_filter then
state.buy_filter = ""
state.filtered_list, state.buy_pagemax = filter_list(nil)
state.buy_page = 1
show_main(p_name)
end
if fields.sell then
local succ, err = post_sell(player, "", fields.sell_price)
if succ then

View File

@ -46,7 +46,7 @@ Transaction Log=Journaux transactions
ATM=Distributeur Automatique d'argent
Cash deposit (+@1)=Dépot d'espèces (+@1)
### exchange_macine.lua ###
### exchange_machine.lua ###
Wear=Usure
Buy Vol=Qté achat
Buy Max=Achat Max
@ -75,3 +75,6 @@ Invalid amount.=Montant invalide.
Invalid rate.=Taux invalide.
Cannot sell an item with metadata.=Ne peut vendre un item avec des métadonnées.
Qty=Qté
Filter=Filtre
Search=Chercher
Reset=Reset