Skip to content

Commit

Permalink
Code cleanup, reset page to 0 on pressing the clear button
Browse files Browse the repository at this point in the history
  • Loading branch information
random-geek committed Mar 24, 2019
1 parent 7adf41a commit 83ab26f
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions inventory.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
local F = minetest.formspec_escape

cg.update_filter = function(player, context, filter)
if (filter or "") == (context.cg_filter or "") then return end
cg.update_filter = function(player, context, filter, force)
if not force and (filter or "") == context.cg_filter then return end

context.cg_page = 0
context.cg_filter = filter
cg.filter_items(player, filter)
context.cg_filter = filter or ""
cg.filter_items(player, context.cg_filter)
end

cg.update_selected_item = function(player, context, item, force)
if not force and item == context.cg_selected_item then return end

if item then context.cg_craft_page = 0 end

context.cg_selected_item = item
context.cg_auto_menu = false
end

local make_item_button = function(formspec, x, y, size, name)
Expand Down Expand Up @@ -188,7 +198,7 @@ sfinv.register_page("cg_plus:crafting_guide", {
elseif fields.cg_search or fields.key_enter_field == "cg_filter" then
cg.update_filter(player, context, fields.cg_filter)
elseif fields.cg_clear then
cg.update_filter(player, context, "")
cg.update_filter(player, context, "", true)
elseif fields.cg_auto_menu and cg.autocrafting then
if not context.cg_auto_menu then
-- Make sure the craft is valid, in case the client is sending fake formspec fields.
Expand All @@ -210,21 +220,16 @@ sfinv.register_page("cg_plus:crafting_guide", {
if item:sub(1, 6) == "group:" then
if cg.group_search then
cg.update_filter(player, context, item:gsub("/", ","))
context.cg_selected_item = nil
context.cg_auto_menu = false
cg.update_selected_item(player, context, nil)
elseif cg.group_stereotypes[item:sub(7)] then
context.cg_selected_item = cg.group_stereotypes[item:sub(7)]
context.cg_craft_page = 0
context.cg_auto_menu = false
cg.update_selected_item(player, context, cg.group_stereotypes[item:sub(7)])
end
elseif item ~= context.cg_selected_item then
context.cg_selected_item = item
context.cg_craft_page = 0
context.cg_auto_menu = false
else
cg.update_selected_item(player, context, item)
end

break
elseif field:sub(1, 8) == "cg_auto_" and cg.autocrafting and context.cg_auto_menu then
elseif field:sub(1, 8) == "cg_auto_" and context.cg_auto_menu then
-- No need to sanity check, we already did that when showing the autocrafting menu.
local num = tonumber(field:sub(9))

Expand Down Expand Up @@ -253,6 +258,6 @@ sfinv.register_page("cg_plus:crafting_guide", {
end,

on_leave = function(self, player, context)
if context.cg_auto_menu then context.cg_auto_menu = false end
context.cg_auto_menu = false
end,
})

0 comments on commit 83ab26f

Please sign in to comment.