Support for search on pressing Enter and use minetest.colorize

NOTE: Minetest 0.4.14-git or 0.4.15 required
This commit is contained in:
JP Guerrero 2016-11-30 17:28:56 +01:00
parent 51a00b957e
commit 93c2d3dc81
2 changed files with 46 additions and 45 deletions

View File

@ -4,7 +4,5 @@
##### Usable with a book named *"Crafting Guide"*. ##### ##### Usable with a book named *"Crafting Guide"*. #####
##### This mod is originating from [X-Decor](https://github.com/kilbith/xdecor). #####
![Preview](http://i.imgur.com/xblp1Vs.png) ![Preview](http://i.imgur.com/xblp1Vs.png)

View File

@ -1,4 +1,5 @@
local craftguide, datas, npp = {}, {}, 8*3 local craftguide, datas, npp = {}, {}, 8*3
local min, ceil, floor = math.min, math.ceil, math.floor
function craftguide:get_recipe(item) function craftguide:get_recipe(item)
if item:sub(1,6) == "group:" then if item:sub(1,6) == "group:" then
@ -14,58 +15,59 @@ function craftguide:get_recipe(item)
return item return item
end end
function craftguide:get_formspec(player_name, pagenum, recipe_num) function craftguide:get_formspec(player_name)
local data = datas[player_name] local data = datas[player_name]
data.pagenum = data.pagenum or 1
data.recipe_num = data.recipe_num or 1
local formspec = [[ size[8,6.6;] local formspec = [[ size[8,6.6;]
tablecolumns[color;text;color;text]
tableoptions[background=#00000000;highlight=#00000000;border=false]
button[5.4,0;0.8,0.95;prev;<]
button[7.2,0;0.8,0.95;next;>]
button[2.5,0.2;0.8,0.5;search;?] button[2.5,0.2;0.8,0.5;search;?]
button[3.2,0.2;0.8,0.5;clear;X] button[3.2,0.2;0.8,0.5;clear;X]
tooltip[search;Search] tooltip[search;Search]
tooltip[clear;Reset] tooltip[clear;Reset]
table[6,0.18;1.1,0.5;pagenum;#FFFF00,]].. field_close_on_enter[craftguide_filter, false]
pagenum..",#FFFFFF,/ "..data.pagemax.."]".. button[5.4,0;0.8,0.95;prev;<] ]]..
"field[0.3,0.32;2.6,1;filter;;"..data.filter.."]".. "label[6.1,0.18;"..minetest.colorize("#FFFF00",
data.pagenum).." / "..data.pagemax.."]"..
"button[7.2,0;0.8,0.95;next;>]"..
"field[0.3,0.32;2.6,1;craftguide_filter;;"..
minetest.formspec_escape(data.filter).."]"..
default.gui_bg..default.gui_bg_img default.gui_bg..default.gui_bg_img
local first_item = (pagenum - 1) * npp local first_item = (data.pagenum - 1) * npp
for i = first_item, first_item + npp - 1 do for i = first_item, first_item + npp - 1 do
local name = data.items[i + 1] local name = data.items[i + 1]
if not name then break end -- last page if not name then break end -- last page
local X = i % 8 local X = i % 8
local Y = ((i % npp - X) / 8) + 1 local Y = ((i % npp - X) / 8) + 1
formspec = formspec.."item_image_button["..X..","..Y..";1,1;".. formspec = formspec.."item_image_button["..X..","..Y..";1,1;"..name..";"..name..";]"
name..";"..name..";]"
end end
if data.item and minetest.registered_items[data.item] then if data.item and minetest.registered_items[data.item] then
local recipes = minetest.get_all_craft_recipes(data.item) local recipes = minetest.get_all_craft_recipes(data.item)
if recipe_num > #recipes then recipe_num = 1 end if data.recipe_num > #recipes then data.recipe_num = 1 end
if #recipes > 1 then formspec = formspec.. if #recipes > 1 then formspec = formspec..
[[ button[0,6;1.6,1;alternate;Alternate] [[ button[0,6;1.6,1;alternate;Alternate]
label[0,5.5;Recipe ]]..recipe_num.." of "..#recipes.."]" label[0,5.5;Recipe ]]..data.recipe_num.." of "..#recipes.."]"
end end
local type = recipes[recipe_num].type local type = recipes[data.recipe_num].type
if type == "cooking" then formspec = formspec.. if type == "cooking" then formspec = formspec..
"image[3.75,4.6;0.5,0.5;default_furnace_front.png]" "image[3.75,4.6;0.5,0.5;default_furnace_front.png]"
end end
local items = recipes[recipe_num].items local items = recipes[data.recipe_num].items
local width = recipes[recipe_num].width local width = recipes[data.recipe_num].width
if width == 0 then width = math.min(3, #items) end if width == 0 then width = min(3, #items) end
-- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage: -- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
-- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1 -- https://github.com/kilbith/xdecor/blob/master/handlers/helpers.lua#L1
local rows = math.ceil(table.maxn(items) / width) local rows = ceil(table.maxn(items) / width)
for i, v in pairs(items) do for i, v in pairs(items) do
local X = (i-1) % width + 4.5 local X = (i-1) % width + 4.5
local Y = math.floor((i-1) / width + (6 - math.min(2, rows))) local Y = floor((i-1) / width + (6 - min(2, rows)))
local label = "" local label = ""
if v:sub(1,6) == "group:" then label = "\nG" end if v:sub(1,6) == "group:" then label = "\nG" end
@ -73,13 +75,13 @@ function craftguide:get_formspec(player_name, pagenum, recipe_num)
self:get_recipe(v)..";"..self:get_recipe(v)..";"..label.."]" self:get_recipe(v)..";"..self:get_recipe(v)..";"..label.."]"
end end
local output = recipes[recipe_num].output local output = recipes[data.recipe_num].output
formspec = formspec..[[ image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90] formspec = formspec..[[ image[3.5,5;1,1;gui_furnace_arrow_bg.png^[transformR90]
item_image_button[2.5,5;1,1;]]..output..";"..data.item..";]" item_image_button[2.5,5;1,1;]]..output..";"..data.item..";]"
end end
data.formspec = formspec data.formspec = formspec
minetest.show_formspec(player_name, "xdecor:craftguide", formspec) minetest.show_formspec(player_name, "craftguide:book", formspec)
end end
function craftguide:get_items(player_name) function craftguide:get_items(player_name)
@ -97,44 +99,43 @@ function craftguide:get_items(player_name)
table.sort(items_list) table.sort(items_list)
data.items = items_list data.items = items_list
data.size = #items_list data.size = #items_list
data.pagemax = math.ceil(data.size / npp) data.pagemax = ceil(data.size / npp)
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= "xdecor:craftguide" then return end if formname ~= "craftguide:book" then return end
local player_name = player:get_player_name() local player_name = player:get_player_name()
local data = datas[player_name] local data = datas[player_name]
local formspec = data.formspec local formspec = data.formspec
local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
if fields.clear then if fields.clear then
data.filter, data.item = "", nil data.filter, data.item, data.pagenum, data.recipe_num = "", nil, 1, 1
craftguide:get_items(player_name) craftguide:get_items(player_name)
craftguide:get_formspec(player_name, 1, 1) craftguide:get_formspec(player_name)
elseif fields.alternate then elseif fields.alternate then
local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1 data.recipe_num = (data.recipe_num and data.recipe_num + 1) or 1
recipe_num = recipe_num + 1 craftguide:get_formspec(player_name)
craftguide:get_formspec(player_name, pagenum, recipe_num) elseif fields.search or fields.key_enter_field == "craftguide_filter" then
elseif fields.search then data.filter = fields.craftguide_filter:lower()
data.filter = fields.filter:lower() data.pagenum = 1
craftguide:get_items(player_name) craftguide:get_items(player_name)
craftguide:get_formspec(player_name, 1, 1) craftguide:get_formspec(player_name)
elseif fields.prev or fields.next then elseif fields.prev or fields.next then
if fields.prev then pagenum = pagenum - 1 if fields.prev then data.pagenum = data.pagenum - 1
else pagenum = pagenum + 1 end else data.pagenum = data.pagenum + 1 end
if pagenum > data.pagemax then pagenum = 1 if data.pagenum > data.pagemax then data.pagenum = 1
elseif pagenum == 0 then pagenum = data.pagemax end elseif data.pagenum == 0 then data.pagenum = data.pagemax end
craftguide:get_formspec(player_name, pagenum, 1) craftguide:get_formspec(player_name)
else for item in pairs(fields) do else for item in pairs(fields) do
if minetest.get_craft_recipe(item).items then if minetest.get_craft_recipe(item).items then
data.item = item data.item = item
craftguide:get_formspec(player_name, pagenum, 1) craftguide:get_formspec(player_name)
end end
end end
end end
end) end)
minetest.register_craftitem(":xdecor:crafting_guide", { minetest.register_craftitem("craftguide:book", {
description = "Crafting Guide", description = "Crafting Guide",
inventory_image = "crafting_guide.png", inventory_image = "crafting_guide.png",
wield_image = "crafting_guide.png", wield_image = "crafting_guide.png",
@ -146,16 +147,18 @@ minetest.register_craftitem(":xdecor:crafting_guide", {
datas[player_name] = {} datas[player_name] = {}
datas[player_name].filter = "" datas[player_name].filter = ""
craftguide:get_items(player_name) craftguide:get_items(player_name)
craftguide:get_formspec(player_name, 1, 1) craftguide:get_formspec(player_name)
else else
minetest.show_formspec(player_name, "xdecor:craftguide", datas[player_name].formspec) minetest.show_formspec(player_name, "craftguide:book", datas[player_name].formspec)
end end
end end
}) })
minetest.register_craft({ minetest.register_craft({
output = "xdecor:crafting_guide", output = "craftguide:book",
type = "shapeless", type = "shapeless",
recipe = {"default:book"} recipe = {"default:book"}
}) })
minetest.register_alias("xdecor:crafting_guide", "craftguide:book")