Added a dialog showing cuttable materials
This commit is contained in:
parent
0f71c8ff94
commit
037ee61f58
65
init.lua
65
init.lua
@ -30,9 +30,12 @@ letters = {
|
||||
letter_cutter = {}
|
||||
letter_cutter.known_nodes = {}
|
||||
|
||||
letter_cutter.show_item_list = dofile(
|
||||
minetest.get_modpath(minetest.get_current_modname())..'/itemlist.lua')
|
||||
|
||||
function letters.register_letters(modname, subname, from_node, description, tiles, def)
|
||||
|
||||
def = def and table.copy(def) or {}
|
||||
def = def and table.copy(def) or {}
|
||||
|
||||
--default node
|
||||
def.drawtype = "signlike"
|
||||
@ -54,26 +57,26 @@ function letters.register_letters(modname, subname, from_node, description, tile
|
||||
attached_node = 1
|
||||
}
|
||||
def.legacy_wallmounted = false
|
||||
|
||||
|
||||
for _, row in ipairs(letters) do
|
||||
|
||||
|
||||
for _, row in ipairs(letters) do
|
||||
|
||||
def = table.copy(def)
|
||||
def.description = description.. " " ..row[3]
|
||||
def.inventory_image = tiles.. "^letters_" ..row[1].. "_overlay.png^[makealpha:255,126,126"
|
||||
def.wield_image = def.inventory_image
|
||||
def.tiles = {def.inventory_image}
|
||||
|
||||
|
||||
minetest.register_node(":" ..modname..":"..subname.. "_letter_" ..row[1],def)
|
||||
|
||||
|
||||
def = table.copy(def)
|
||||
def.description = description.. " " ..row[4]
|
||||
def.inventory_image = tiles.. "^letters_" ..row[2].. "_overlay.png^[makealpha:255,126,126"
|
||||
def.wield_image = def.inventory_image
|
||||
def.tiles = {def.inventory_image}
|
||||
|
||||
|
||||
minetest.register_node(":" ..modname..":"..subname.. "_letter_" ..row[2], def)
|
||||
|
||||
|
||||
--[[minetest.register_craft({
|
||||
output = from_node,
|
||||
recipe = {
|
||||
@ -81,7 +84,7 @@ function letters.register_letters(modname, subname, from_node, description, tile
|
||||
{modname..":"..name, modname..":"..name, modname..":"..name},
|
||||
{modname..":"..name, modname..":"..name, modname..":"..name},
|
||||
},
|
||||
})--]]
|
||||
})--]]
|
||||
end
|
||||
letter_cutter.known_nodes[from_node] = {modname, subname}
|
||||
end
|
||||
@ -210,7 +213,7 @@ function letter_cutter:update_inventory_lower(pos, amount)
|
||||
self:reset_lower(pos)
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
local stack = inv:get_stack("input", 1)
|
||||
if stack:is_empty() then
|
||||
self:reset_lower(pos)
|
||||
@ -222,7 +225,7 @@ function letter_cutter:update_inventory_lower(pos, amount)
|
||||
local modname = name_parts[1] or ""
|
||||
local material = name_parts[2] or ""
|
||||
|
||||
inv:set_list("input", {
|
||||
inv:set_list("input", {
|
||||
node_name.. " " .. math.floor(amount)
|
||||
})
|
||||
|
||||
@ -248,7 +251,7 @@ function letter_cutter:update_inventory_upper(pos, amount)
|
||||
self:reset_upper(pos)
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
local stack = inv:get_stack("input", 1)
|
||||
if stack:is_empty() then
|
||||
self:reset_upper(pos)
|
||||
@ -260,7 +263,7 @@ function letter_cutter:update_inventory_upper(pos, amount)
|
||||
local modname = name_parts[1] or ""
|
||||
local material = name_parts[2] or ""
|
||||
|
||||
inv:set_list("input", {
|
||||
inv:set_list("input", {
|
||||
node_name.. " " .. math.floor(amount)
|
||||
})
|
||||
|
||||
@ -295,7 +298,7 @@ function letter_cutter.allow_metadata_inventory_put(
|
||||
local inv = meta:get_inventory()
|
||||
local stackname = stack:get_name()
|
||||
local count = stack:get_count()
|
||||
|
||||
|
||||
-- Only accept certain blocks as input which are known to be craftable into stairs:
|
||||
if listname == "input" then
|
||||
if not inv:is_empty("input") and
|
||||
@ -367,6 +370,7 @@ function letter_cutter.on_construct_lower(pos)
|
||||
"label[0,0;Input\nmaterial]" ..
|
||||
"list[current_name;input;1.5,0;1,1;]" ..
|
||||
"list[current_name;output;2.8,0;8,4;]" ..
|
||||
"button[0,1;2.5,1;itemlist;Cuttable materials]" ..
|
||||
"list[current_player;main;1.5,5;8,4;]")
|
||||
|
||||
meta:set_int("anz", 0) -- No microblocks inside yet.
|
||||
@ -386,6 +390,7 @@ function letter_cutter.on_construct_upper(pos)
|
||||
"label[0,0;Input\nmaterial]" ..
|
||||
"list[current_name;input;1.5,0;1,1;]" ..
|
||||
"list[current_name;output;2.8,0;8,4;]" ..
|
||||
"button[0,1;2.5,1;itemlist;Cuttable materials]" ..
|
||||
"list[current_player;main;1.5,5;8,4;]")
|
||||
|
||||
meta:set_int("anz", 0) -- No microblocks inside yet.
|
||||
@ -409,11 +414,21 @@ function letter_cutter.can_dig(pos,player)
|
||||
return true
|
||||
end
|
||||
|
||||
function letter_cutter.on_receive_fields(pos, formname, fields, sender)
|
||||
if fields.itemlist then
|
||||
local list = {}
|
||||
for name, t in pairs(letter_cutter.known_nodes) do
|
||||
list[#list+1] = name
|
||||
end
|
||||
letter_cutter.show_item_list(sender, 'Cuttable materials', list, pos)
|
||||
end
|
||||
end
|
||||
|
||||
minetest.register_node("letters:letter_cutter_lower", {
|
||||
description = "Lower Case Leter Cutter",
|
||||
drawtype = "nodebox",
|
||||
description = "Lower Case Leter Cutter",
|
||||
drawtype = "nodebox",
|
||||
node_box = {
|
||||
type = "fixed",
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{-0.4375, -0.5, -0.4375, -0.3125, 0.125, -0.3125}, -- NodeBox1
|
||||
{-0.4375, -0.5, 0.3125, -0.3125, 0.125, 0.4375}, -- NodeBox2
|
||||
@ -434,9 +449,9 @@ minetest.register_node("letters:letter_cutter_lower", {
|
||||
tiles = {"letters_letter_cutter_lower_top.png",
|
||||
"default_tree.png",
|
||||
"letters_letter_cutter_side.png"},
|
||||
paramtype = "light",
|
||||
paramtype = "light",
|
||||
sunlight_propagates = true,
|
||||
paramtype2 = "facedir",
|
||||
paramtype2 = "facedir",
|
||||
groups = {choppy = 2,oddly_breakable_by_hand = 2},
|
||||
sounds = default.node_sound_wood_defaults(),
|
||||
on_construct = letter_cutter.on_construct_lower,
|
||||
@ -457,6 +472,7 @@ minetest.register_node("letters:letter_cutter_lower", {
|
||||
-- Putting something in is slightly more complicated than taking anything because we have to make sure it is of a suitable material:
|
||||
on_metadata_inventory_put = letter_cutter.on_metadata_inventory_put_lower,
|
||||
on_metadata_inventory_take = letter_cutter.on_metadata_inventory_take_lower,
|
||||
on_receive_fields = letter_cutter.on_receive_fields,
|
||||
})
|
||||
|
||||
minetest.register_craft({
|
||||
@ -469,10 +485,10 @@ minetest.register_craft({
|
||||
})
|
||||
|
||||
minetest.register_node("letters:letter_cutter_upper", {
|
||||
description = "Upper Case Leter Cutter",
|
||||
drawtype = "nodebox",
|
||||
description = "Upper Case Leter Cutter",
|
||||
drawtype = "nodebox",
|
||||
node_box = {
|
||||
type = "fixed",
|
||||
type = "fixed",
|
||||
fixed = {
|
||||
{-0.4375, -0.5, -0.4375, -0.3125, 0.125, -0.3125}, -- NodeBox1
|
||||
{-0.4375, -0.5, 0.3125, -0.3125, 0.125, 0.4375}, -- NodeBox2
|
||||
@ -490,9 +506,9 @@ minetest.register_node("letters:letter_cutter_upper", {
|
||||
tiles = {"letters_letter_cutter_upper_top.png",
|
||||
"default_tree.png",
|
||||
"letters_letter_cutter_side.png"},
|
||||
paramtype = "light",
|
||||
paramtype = "light",
|
||||
sunlight_propagates = true,
|
||||
paramtype2 = "facedir",
|
||||
paramtype2 = "facedir",
|
||||
groups = {choppy = 2,oddly_breakable_by_hand = 2},
|
||||
sounds = default.node_sound_wood_defaults(),
|
||||
on_construct = letter_cutter.on_construct_upper,
|
||||
@ -513,6 +529,7 @@ minetest.register_node("letters:letter_cutter_upper", {
|
||||
-- Putting something in is slightly more complicated than taking anything because we have to make sure it is of a suitable material:
|
||||
on_metadata_inventory_put = letter_cutter.on_metadata_inventory_put_upper,
|
||||
on_metadata_inventory_take = letter_cutter.on_metadata_inventory_take_upper,
|
||||
on_receive_fields = letter_cutter.on_receive_fields,
|
||||
})
|
||||
|
||||
minetest.register_craft({
|
||||
|
231
itemlist.lua
Normal file
231
itemlist.lua
Normal file
@ -0,0 +1,231 @@
|
||||
--[[
|
||||
show_item_list form for Minetest mods
|
||||
(c) Pierre-Yves Rollo
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
--]]
|
||||
|
||||
--[[ The item list dialog can only be displayed from within a node formspec.
|
||||
To open the item list dialog, add a do_file :
|
||||
|
||||
local show_item_list = dofile(
|
||||
minetest.get_modpath(minetest.get_current_modname())..'/itemlist.lua')
|
||||
|
||||
...
|
||||
|
||||
-- From your on_receive_field, call :
|
||||
show_item_list(player, title, item_list, pos_context)
|
||||
|
||||
player: player name or object
|
||||
title: title to be displayed at the top of the dialog
|
||||
item_list: a list of item names
|
||||
pos_context: position of the context node (node that openned the formspec)
|
||||
]]--
|
||||
|
||||
-- Formspec style
|
||||
|
||||
local style={
|
||||
linesize = 0.9, -- Line height
|
||||
colsize = 4, -- Column width
|
||||
lines = 8, -- Number of lines of items displayed
|
||||
cols = 3, -- Number of columns of items displayed
|
||||
}
|
||||
|
||||
local modname = minetest.get_current_modname()
|
||||
|
||||
-- Context management functions (surely many improvements to do)
|
||||
|
||||
local contexts = {}
|
||||
|
||||
local function get_player_name(player)
|
||||
if type(player) == 'string' then return player end
|
||||
if type(player) == 'userdata' and player.get_player_name then
|
||||
return player:get_player_name()
|
||||
end
|
||||
minetest.log('warning', '['..modname..'] get_player_name could not identify player.')
|
||||
end
|
||||
|
||||
minetest.register_on_leaveplayer(function(player)
|
||||
local playername = get_player_name(player)
|
||||
if playername then contexts[playername] = nil end
|
||||
end)
|
||||
|
||||
local function new_context(player, context)
|
||||
local playername = get_player_name(player)
|
||||
if playername then
|
||||
contexts[playername] = context
|
||||
contexts[playername].playername = playername
|
||||
return contexts[playername]
|
||||
end
|
||||
end
|
||||
|
||||
local function get_context(player)
|
||||
local playername = get_player_name(player)
|
||||
if playername then
|
||||
if contexts[playername] then
|
||||
return contexts[playername]
|
||||
else
|
||||
minetest.log('warning', '['..modname..'] Context not found for player "'..playername..'"')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function update_context(player, changes)
|
||||
local playername = get_player_name(player)
|
||||
if playername then
|
||||
if not contexts[playername] then
|
||||
contexts[playername] = { playername = playername }
|
||||
end
|
||||
for key, value in pairs(changes) do
|
||||
contexts[playername][key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Show node formspec functions
|
||||
|
||||
local function show_node_formspec(player, pos)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local playername = get_player_name(player)
|
||||
|
||||
-- Decontextualize formspec
|
||||
local fs = meta:get_string('formspec')
|
||||
|
||||
-- Change context and currrent_name references to nodemeta references
|
||||
fs = fs:gsub("current_name", "nodemeta:"..pos.x..","..pos.y..","..pos.z)
|
||||
fs = fs:gsub("context", "nodemeta:"..pos.x..","..pos.y..","..pos.z)
|
||||
|
||||
-- Change all ${} to their corresponding metadata values
|
||||
local s, e
|
||||
repeat
|
||||
s, e = fs:find('%${.*}')
|
||||
if s and e then
|
||||
fs = fs:sub(1, s-1)..
|
||||
minetest.formspec_escape(meta:get_string(fs:sub(s+2,e-1)))..
|
||||
fs:sub(e+1)
|
||||
end
|
||||
until s == nil
|
||||
|
||||
-- Find node on_receive_fields
|
||||
local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
|
||||
|
||||
if ndef and ndef.on_receive_fields then
|
||||
update_context(player, { on_receive_fields = ndef.on_receive_fields } )
|
||||
end
|
||||
update_context(player, { node_pos = pos } )
|
||||
|
||||
-- Show formspec
|
||||
minetest.show_formspec(playername, modname..':context_formspec', fs)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname == modname..':context_formspec' then
|
||||
local context = get_context(player)
|
||||
if context == nil then return end
|
||||
|
||||
if context.on_receive_fields then
|
||||
context.on_receive_fields(context.node_pos, '', fields, player)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- Specific functions
|
||||
|
||||
local function get_nb_of_pages(context)
|
||||
return math.ceil((#context.items + 1) / (style.lines * style.cols))
|
||||
end
|
||||
|
||||
local function item_list_prepare(item_list)
|
||||
local list = {}
|
||||
for _, name in ipairs(item_list) do
|
||||
local ndef = minetest.registered_items[name]
|
||||
if ndef then
|
||||
list[#list+1] = ndef
|
||||
end
|
||||
end
|
||||
table.sort(list, function(a,b) return a.description < b.description end)
|
||||
return list
|
||||
end
|
||||
|
||||
local function show_fs(player)
|
||||
local context = get_context(player)
|
||||
if context == nil then return end
|
||||
|
||||
local fs = 'size['..(style.colsize * style.cols)..','
|
||||
..(style.linesize * style.lines + 1.3)..']'
|
||||
..default.gui_bg..default.gui_bg_img..default.gui_slots
|
||||
..'label[1,0;'..context.title..']'
|
||||
..'button_exit['..(style.colsize * style.cols - 2)..','
|
||||
..(style.linesize * style.lines + 0.7)..';2,1;close;Close]'
|
||||
|
||||
if context.page > 1 then
|
||||
fs = fs..'button['..(style.colsize * style.cols - 2)..',-0.2;1,1;prev;<]'
|
||||
end
|
||||
|
||||
if context.page < get_nb_of_pages(context) then
|
||||
fs = fs..'button['..(style.colsize * style.cols - 1)..',-0.2;1,1;next;>]'
|
||||
end
|
||||
|
||||
local index = (style.lines * style.cols) * (context.page - 1)
|
||||
|
||||
for col = 1, style.cols do
|
||||
local x = (col - 1) * style.colsize
|
||||
for line = 1, style.lines do
|
||||
index = index + 1
|
||||
local y = line * style.linesize - 0.2
|
||||
if context.items[index] then
|
||||
fs = fs..'item_image['..x..','..y..';1,1;'
|
||||
..context.items[index].name..']label['..(x+1)..','..(y+0.2)..';'
|
||||
..(context.items[index].description or context.items[index].name)
|
||||
..']'
|
||||
end
|
||||
end
|
||||
end
|
||||
minetest.show_formspec(context.playername, modname..':item_list', fs)
|
||||
end
|
||||
|
||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
if formname == modname..':item_list' then
|
||||
local context = get_context(player)
|
||||
if context == nil then return end
|
||||
|
||||
if fields.next then
|
||||
context.page = context.page + 1
|
||||
show_fs(context.playername)
|
||||
end
|
||||
if fields.prev then
|
||||
context.page = context.page - 1
|
||||
show_fs(context.playername)
|
||||
end
|
||||
if fields.quit == 'true' then
|
||||
if context.node_pos then
|
||||
-- Using after to avoid the "double close" bug
|
||||
minetest.after(0, show_node_formspec, player, context.node_pos)
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- Only exposed function - Entry point
|
||||
|
||||
local function show_item_list(player, title, item_list, pos_context)
|
||||
new_context(player, {
|
||||
items = item_list_prepare(item_list),
|
||||
title = title,
|
||||
node_pos = pos_context,
|
||||
page = 1,})
|
||||
show_fs(player)
|
||||
end
|
||||
|
||||
return show_item_list
|
Loading…
x
Reference in New Issue
Block a user