Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*~

.idea
34 changes: 34 additions & 0 deletions callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ core.register_on_joinplayer(function(player)
unified_inventory.alternate[player_name] = 1
unified_inventory.current_item[player_name] = nil
unified_inventory.current_craft_direction[player_name] = "recipe"
unified_inventory.current_craft_type_toggles[player_name] = {} -- values are reversed, false means true (so that nil means true)

-- Refill slot
local refill = core.create_detached_inventory(player_name.."refill", {
Expand Down Expand Up @@ -88,6 +89,21 @@ local function receive_fields_searchbox(player, formname, fields)
end
end

local function craftguide_toggle_recipe_type_visibility(player, field_name)
local player_name = player:get_player_name()
local toggled_recipe_type = field_name:match("craftguide_toggle_recipe_type_visibility_(.*)")

local toggles = ui.current_craft_type_toggles[player_name]
if toggled_recipe_type == "all" then
local all_state = toggles.all
for recipe_type in pairs(ui.current_visible_craft_type_toggles[player_name]) do
toggles[recipe_type] = not all_state
end
else
toggles[toggled_recipe_type] = not toggles[toggled_recipe_type]
end
end

core.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "" then
return
Expand All @@ -99,6 +115,16 @@ core.register_on_player_receive_fields(function(player, formname, fields)

local ui_peruser, _ = unified_inventory.get_per_player_formspec(player_name)

for name in pairs(fields) do
if name:match("craftguide_toggle_recipe_type_visibility_") then
craftguide_toggle_recipe_type_visibility(player, name)
unified_inventory.alternate[player_name] = 1
unified_inventory.set_inventory_formspec(player,
unified_inventory.current_page[player_name])
return
end
end

local clicked_category
for _, cat in ipairs(ui.category_list) do
if fields["category_" .. cat.name] then
Expand Down Expand Up @@ -252,6 +278,14 @@ core.register_on_player_receive_fields(function(player, formname, fields)
end
local crafts = ui.crafts_for[ui.current_craft_direction[
player_name]][selected_item:get_name()] or {}
local toggles = ui.current_craft_type_toggles[player_name]
local filtered_crafts = {}
for _, craft in ipairs(crafts) do
if not toggles[craft.type] then
table.insert(filtered_crafts, craft)
end
end
crafts = filtered_crafts
if #crafts <= 1 then
return
end
Expand Down
2 changes: 2 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ unified_inventory = {
current_index = {},
current_item = {},
current_craft_direction = {},
current_visible_craft_type_toggles = {},
current_craft_type_toggles = {}, -- values are reversed, so false means true (so that nil means true)
registered_craft_types = {},
crafts_for = {usage = {}, recipe = {} },
players = {},
Expand Down
4 changes: 4 additions & 0 deletions locale/template.pot
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ msgstr ""
msgid "All"
msgstr ""

#: register.lua
msgid "Show recipe types:"
msgstr ""

#: waypoints.lua
msgid "White"
msgstr ""
Expand Down
1 change: 1 addition & 0 deletions locale/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ Finish editing=
World position=
Name=
HUD text color=
Show recipe types:
68 changes: 62 additions & 6 deletions register.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,6 @@ ui.register_page("craftguide", {

local dir = ui.current_craft_direction[player_name]
local crafts = ui.crafts_for[dir][item_name]
local alternate = ui.alternate[player_name]
local alternates, craft
if crafts and #crafts > 0 then
alternates = #crafts
craft = crafts[alternate]
end
local has_give = player_privs.give or ui.is_creative(player_name)

local n = #formspec + 1
Expand All @@ -298,6 +292,68 @@ ui.register_page("craftguide", {
"button[" .. (give_x + 1.6) .. "," .. (craftguidey + 2.9) .. ";0.75,0.5;craftguide_giveme_99;99]"
end

if crafts then
local found_recipe_types = {}
for _, craft in ipairs(crafts) do
found_recipe_types[craft.type] = true
end

ui.current_visible_craft_type_toggles[player_name] = found_recipe_types

local buttonx = give_x
local buttony = craftguidey + 3.9
local button_size = 0.5
local button_spacing = 0.25
local button_interval = button_size + button_spacing
local allbuttonx = buttonx
local allbutton_width = button_size * 3/2
buttonx = buttonx + allbutton_width + button_spacing

formspec[n] = ("label[%f,%f;%s]"):format(allbuttonx + 0.1, buttony - 0.2, F(S("Show recipe types:")))
n = n + 1

local has_any = true

local toggles = ui.current_craft_type_toggles[player_name]
local type_i = 0
for recipe_type in pairs(found_recipe_types) do
local inactive = toggles[recipe_type]
if inactive then has_any = false end
local def = ui.registered_craft_types[recipe_type]
formspec[n] = ("style[craftguide_toggle_recipe_type_visibility_%s;bgcolor=#%s;bgimg=%s]")
:format(recipe_type, inactive and "808080" or "ffffff", def.icon)
formspec[n+1] = ("button[%f,%f;%f,%f;craftguide_toggle_recipe_type_visibility_%s;]")
:format(buttonx, buttony, button_size, button_size, recipe_type)
formspec[n+2] = ("tooltip[craftguide_toggle_recipe_type_visibility_%s;%s]")
:format(recipe_type, def.description)
n = n + 3
type_i = type_i + 1
buttonx = buttonx + button_interval
end
toggles.all = not has_any

formspec[n] = ("style[craftguide_toggle_recipe_type_visibility_%s;bgcolor=#%s;textcolor=#%s]")
:format("all", has_any and "3e3e3e" or "000000", has_any and "ffffff" or "808080")
formspec[n+1] = ("button[%f,%f;%f,%f;craftguide_toggle_recipe_type_visibility_%s;%s]")
:format(allbuttonx, buttony, allbutton_width, button_size, "all", F(S("All")))
n = n + 2

local filtered_crafts = {}
for _, craft in ipairs(crafts) do
if not toggles[craft.type] then
table.insert(filtered_crafts, craft)
end
end
crafts = filtered_crafts
end

local alternate = ui.alternate[player_name]
local alternates, craft
if crafts and #crafts > 0 then
alternates = #crafts
craft = crafts[alternate]
end

if not craft then
-- No craft recipes available for this item.
formspec[n] = string.format("label[%f,%f;%s]", craftguidex+2.5, craftguidey+1.5, F(no_recipe_text[dir]))
Expand Down