summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJP Guerrero <jeanpatrick.guerrero@gmail.com>2016-11-30 17:28:56 +0100
committerJP Guerrero <jeanpatrick.guerrero@gmail.com>2016-12-01 15:09:49 +0100
commit93c2d3dc8108edbcf10147ef61dd390915353dcf (patch)
tree99f93b298e8bd15792618e66712ababa9267d8a2
parent51a00b957e81428aa5af1b0b5eddbfdd57729d77 (diff)
Support for search on pressing Enter and use minetest.colorize
NOTE: Minetest 0.4.14-git or 0.4.15 required
-rw-r--r--README.md2
-rw-r--r--init.lua89
2 files changed, 46 insertions, 45 deletions
diff --git a/README.md b/README.md
index 1f25720..647f8b0 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,5 @@
##### 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)
diff --git a/init.lua b/init.lua
index a035056..aaa360d 100644
--- a/init.lua
+++ b/init.lua
@@ -1,4 +1,5 @@
local craftguide, datas, npp = {}, {}, 8*3
+local min, ceil, floor = math.min, math.ceil, math.floor
function craftguide:get_recipe(item)
if item:sub(1,6) == "group:" then
@@ -14,58 +15,59 @@ function craftguide:get_recipe(item)
return item
end
-function craftguide:get_formspec(player_name, pagenum, recipe_num)
+function craftguide:get_formspec(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;]
- 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[3.2,0.2;0.8,0.5;clear;X]
tooltip[search;Search]
tooltip[clear;Reset]
- table[6,0.18;1.1,0.5;pagenum;#FFFF00,]]..
- pagenum..",#FFFFFF,/ "..data.pagemax.."]"..
- "field[0.3,0.32;2.6,1;filter;;"..data.filter.."]"..
+ field_close_on_enter[craftguide_filter, false]
+ button[5.4,0;0.8,0.95;prev;<] ]]..
+ "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
- local first_item = (pagenum - 1) * npp
+ local first_item = (data.pagenum - 1) * npp
for i = first_item, first_item + npp - 1 do
local name = data.items[i + 1]
if not name then break end -- last page
-
local X = i % 8
local Y = ((i % npp - X) / 8) + 1
- formspec = formspec.."item_image_button["..X..","..Y..";1,1;"..
- name..";"..name..";]"
+ formspec = formspec.."item_image_button["..X..","..Y..";1,1;"..name..";"..name..";]"
end
if data.item and minetest.registered_items[data.item] then
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..
[[ 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
- local type = recipes[recipe_num].type
+ local type = recipes[data.recipe_num].type
if type == "cooking" then formspec = formspec..
"image[3.75,4.6;0.5,0.5;default_furnace_front.png]"
end
- local items = recipes[recipe_num].items
- local width = recipes[recipe_num].width
- if width == 0 then width = math.min(3, #items) end
+ local items = recipes[data.recipe_num].items
+ local width = recipes[data.recipe_num].width
+ if width == 0 then width = min(3, #items) end
-- Lua 5.3 removed `table.maxn`, use this alternative in case of breakage:
-- 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
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 = ""
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.."]"
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]
item_image_button[2.5,5;1,1;]]..output..";"..data.item..";]"
end
data.formspec = formspec
- minetest.show_formspec(player_name, "xdecor:craftguide", formspec)
+ minetest.show_formspec(player_name, "craftguide:book", formspec)
end
function craftguide:get_items(player_name)
@@ -97,44 +99,43 @@ function craftguide:get_items(player_name)
table.sort(items_list)
data.items = items_list
data.size = #items_list
- data.pagemax = math.ceil(data.size / npp)
+ data.pagemax = ceil(data.size / npp)
end
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 data = datas[player_name]
local formspec = data.formspec
- local pagenum = tonumber(formspec:match("#FFFF00,(%d+)")) or 1
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_formspec(player_name, 1, 1)
+ craftguide:get_formspec(player_name)
elseif fields.alternate then
- local recipe_num = tonumber(formspec:match("Recipe%s(%d+)")) or 1
- recipe_num = recipe_num + 1
- craftguide:get_formspec(player_name, pagenum, recipe_num)
- elseif fields.search then
- data.filter = fields.filter:lower()
+ data.recipe_num = (data.recipe_num and data.recipe_num + 1) or 1
+ craftguide:get_formspec(player_name)
+ elseif fields.search or fields.key_enter_field == "craftguide_filter" then
+ data.filter = fields.craftguide_filter:lower()
+ data.pagenum = 1
craftguide:get_items(player_name)
- craftguide:get_formspec(player_name, 1, 1)
+ craftguide:get_formspec(player_name)
elseif fields.prev or fields.next then
- if fields.prev then pagenum = pagenum - 1
- else pagenum = pagenum + 1 end
- if pagenum > data.pagemax then pagenum = 1
- elseif pagenum == 0 then pagenum = data.pagemax end
- craftguide:get_formspec(player_name, pagenum, 1)
+ if fields.prev then data.pagenum = data.pagenum - 1
+ else data.pagenum = data.pagenum + 1 end
+ if data.pagenum > data.pagemax then data.pagenum = 1
+ elseif data.pagenum == 0 then data.pagenum = data.pagemax end
+ craftguide:get_formspec(player_name)
else for item in pairs(fields) do
if minetest.get_craft_recipe(item).items then
data.item = item
- craftguide:get_formspec(player_name, pagenum, 1)
+ craftguide:get_formspec(player_name)
end
end
end
end)
-minetest.register_craftitem(":xdecor:crafting_guide", {
+minetest.register_craftitem("craftguide:book", {
description = "Crafting Guide",
inventory_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].filter = ""
craftguide:get_items(player_name)
- craftguide:get_formspec(player_name, 1, 1)
+ craftguide:get_formspec(player_name)
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
})
minetest.register_craft({
- output = "xdecor:crafting_guide",
+ output = "craftguide:book",
type = "shapeless",
recipe = {"default:book"}
})
+minetest.register_alias("xdecor:crafting_guide", "craftguide:book")
+