Unify formspec.

master
None 2014-10-01 13:50:24 +04:00
parent fe25ae37b0
commit 90402b5ed3
1 changed files with 74 additions and 40 deletions

View File

@ -1,17 +1,68 @@
-- TODO
INVENTORY_W = 8
INVENTORY_H = 4
CHEST_W = 8
CHEST_H = 4
BIN_W = 6
BIN_H = 3
Y_OFFSET = 0.2
INV_MARGIN = 0.72
HOTBAR_MARGIN = 0.23
--{{{ Functions
-- Chest formspec
local function get_chest_formspec(pos)
local function get_container_formspec(pos, name)
local spos = pos.x .. "," .. pos.y .. "," ..pos.z
local formspec =
"size[8,9]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[nodemeta:".. spos .. ";main;0,0.3;8,4;]"..
"list[current_player;main;0,4.85;8,1;]"..
"list[current_player;main;0,6.08;8,3;8]"..
default.get_hotbar_bg(0,4.85)
local w,h = 0,0
if name == "containers:chest" then
w = CHEST_W
h = CHEST_H
elseif name == "containers:wood_bin" then
w = BIN_W
h = BIN_H
end
local label = minetest.registered_nodes[name].description
local form_w = math.max(INVENTORY_W, w)
local form_h = INVENTORY_H + h + 1
local x_offset = (INVENTORY_W - w)/2
local y_offset = Y_OFFSET
local inv_y = h + y_offset + INV_MARGIN
local label_y = inv_y - y_offset - 0.4
local hb_y = inv_y + INVENTORY_H - 1 + HOTBAR_MARGIN
local formspec =
"size["..form_w..","..form_h.."]"..
"label[0,-0.4;"..label.."]"..
"label[0,"..label_y..";Inventory]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"list[nodemeta:"..spos..";main;"..
x_offset..","..y_offset..";"..
w..","..h..
";]"..
"list[current_player;main;"..
"0,"..inv_y..";"..
INVENTORY_W..","..(INVENTORY_H-1)..
";]"..
"list[current_player;main;"..
"0,"..hb_y..";"..
INVENTORY_W..",1;24]"..
default.get_hotbar_bg(0,hb_y)
--local formspec =
-- "size[8,"..h.."]"..
-- default.gui_bg..
-- default.gui_bg_img..
-- default.gui_slots..
-- "list[nodemeta:".. spos .. ";main;0,0.3;8,4;]"..
-- "list[current_player;main;0,4.85;8,3;]"..
-- "list[current_player;main;0,8.08;8,1;24]"..
-- default.get_hotbar_bg(0,4.85)
return formspec
end
@ -45,7 +96,7 @@ local function handle_unlocked_container(pos, node, clicker, wield_item)
minetest.show_formspec(
clicker:get_player_name(),
node.name,
get_chest_formspec(pos)
get_container_formspec(pos, node.name)
)
end
end
@ -127,7 +178,7 @@ minetest.register_node("containers:chest", {
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Chest")
local inv = meta:get_inventory()
inv:set_size("main", 8*4)
inv:set_size("main", CHEST_W * CHEST_H)
end,
after_dig_node = dig_container,
on_rightclick = handle_unlocked_container,
@ -166,7 +217,7 @@ minetest.register_node("containers:chest_locked", {
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Locked chest")
local inv = meta:get_inventory()
inv:set_size("main", 8*4)
inv:set_size("main", CHEST_W * CHEST_H)
end,
after_place_node = place_locked_container,
after_dig_node = dig_container,
@ -193,39 +244,21 @@ minetest.register_node("containers:chest_locked", {
--{{{ Wooden bin
minetest.register_node("containers:wood_bin", {
description = "Wooden bin",
tiles = {
"default_chest_top.png", "default_chest_top.png",
"default_chest_side.png", "default_chest_side.png",
"default_chest_side.png", "default_chest_front.png"
},
tiles = {"containers_wood_bin.png"},
groups = {choppy=2,oddly_breakable_by_hand=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
drop = "",
on_construct = function(pos)
local meta = minetest.get_meta(pos)
-- Bin formspec
meta:set_string("formspec",
"size[7,9]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
[[
list[current_name;main;0,0.3;8,3;]
list[current_player;main;0,3.85;8,1;]
list[current_player;main;0,5.08;8,3;8]
]]..
default.get_hotbar_bg(0,3.85)
)
meta:set_string("infotext", "Bin")
meta:set_string("infotext", "Chest")
local inv = meta:get_inventory()
inv:set_size("main", 8*3)
end,
can_dig = function(pos,player)
local meta = minetest.get_meta(pos);
local inv = meta:get_inventory()
return inv:is_empty("main")
inv:set_size("main", BIN_W * BIN_H)
end,
after_dig_node = dig_container,
on_rightclick = handle_unlocked_container,
--{{{ Logging
on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
minetest.log("action", player:get_player_name()..
" moves stuff in bin at "..minetest.pos_to_string(pos))
@ -238,6 +271,7 @@ minetest.register_node("containers:wood_bin", {
minetest.log("action", player:get_player_name()..
" takes stuff from bin at "..minetest.pos_to_string(pos))
end,
--}}}
})
--}}}