[WIP] refactor mcl_inventory formspec.

This commit is contained in:
iliekprogrammar 2021-10-03 02:46:54 +08:00
parent c815277b16
commit 3b39b04384
2 changed files with 200 additions and 133 deletions

View File

@ -4,6 +4,9 @@ local minetest, string, table =
mcl_formspec = {}
local mcl_formspec = mcl_formspec
local F = minetest.formspec_escape
local C = minetest.colorize
--------------------------------------------------------------------------------
-- Label --
--------------------------------------------------------------------------------
@ -16,6 +19,15 @@ mcl_formspec.label_y_offset = 0.25 * mcl_formspec.label_height -- offset to coun
mcl_formspec.label_style = string.format("style_type[label;font_size=%f]", mcl_formspec.label_size)
mcl_formspec.label_style_reset = "style_type[label;font=;font_size=;noclip=]"
function mcl_formspec.create_label(x, y, text)
return table.concat{
"label[",
x - mcl_formspec.itemslot_bu, ",", y + mcl_formspec.label_y_offset, ";",
F(C(mcl_formspec.label_color, text)),
"]",
}
end
function mcl_formspec.get_itemslot_bg(x, y, w, h)
local out = ""
for i = 0, w - 1, 1 do

View File

@ -51,6 +51,162 @@ local function return_fields(player, name)
end
end
function mcl_inventory.formspec_armor(x, y, extra)
-- extra params:
-- { inv=... }
local iu = mcl_formspec.itemslot_iu
local formspec = table.concat{
mcl_formspec.create_itemslot_bg(x, y, 1, 4),
"list[current_player;armor;",
x, ",", y, ";",
"1,4;",
"]",
}
local armor_slots = {"helmet", "chestplate", "leggings", "boots"}
for a=1,4 do
if extra.inv:get_stack("armor", a+1):is_empty() then
formspec = table.concat{
formspec,
"image[",
x, ",", y + (a - 1) * iu, ";",
"1,1;mcl_inventory_empty_armor_slot_", armor_slots[a], ".png",
"]"
}
end
end
return formspec
end
mcl_inventory.FORMSPEC_PLAYER_PREVIEW_WIDTH_FACTOR = 102 / 108 -- eyeballed value
function mcl_inventory.formspec_player_preview(x, y, extra)
-- extra params:
-- { player=... }
local bu = mcl_formspec.itemslot_bu
local iu = mcl_formspec.itemslot_iu
local width = mcl_inventory.FORMSPEC_PLAYER_PREVIEW_WIDTH_FACTOR * 3 * iu
local height = 4 * iu
local formspec = table.concat{
"background9[",
x - bu, ",", y - bu, ";",
width, ",", height, ";",
"mcl_inventory_player_preview_box9.png;false;3",
"]",
}
if minetest.settings:get_bool("3d_player_preview", true) then
formspec = table.concat{
formspec,
-- TODO: model is sometimes offsetted to the right due to an engine bug(or possibly a model bug).
mcl_player.get_player_formspec_model(extra.player, x - bu, y - bu, width, height, "")
}
else
formspec = table.concat{
formspec,
-- TODO: better image size+centered. Or better, remove it. see #1869
"image[", x, ",", y, ";", width, ",", height, ";", mcl_player.player_get_preview(extra.player), "]",
}
end
return formspec
end
function mcl_inventory.formspec_left_hand(x, y, extra)
-- extra params:
-- { inv=... }
-- TODO: implement left hand
-- NOTE: we're probably not using armor index #4, change as necessary.
if true then return "" end
-- Left hand slot formspec
local formspec = table.concat{
-- left hand presupport
mcl_formspec.create_itemslot_bg(x, y, 1, 1),
"list[current_player;armor;",
x, ",", y, ";",
"1,1;4",
"]",
}
if extra.inv:get_stack("armor", 5):is_empty() then
formspec = table.concat{
formspec,
"image[",
x, ",", y, ";",
"1,1;mcl_inventory_empty_armor_slot_shield.png",
"]"
}
end
return formspec
end
function mcl_inventory.formspec_crafting(x, y, extra)
-- Optional params: extra
-- extra params:
-- { <label_y=...>* }
local margin = mcl_formspec.margin
local bu = mcl_formspec.itemslot_bu
local iu = mcl_formspec.itemslot_iu
local length = 2 * iu
local arrow_x = x + length + margin
local arrow_y = y + iu / 2
local output_x = arrow_x + 1 + margin + bu
local formspec = table.concat{
(extra and extra.label_y) and mcl_formspec.create_label(x, extra.label_y, S("Crafting")) or "",
mcl_formspec.create_itemslot_bg(x, y, 2, 2),
"list[current_player;craft;",
x, ",", y, ";",
"2,2",
"]",
"image[",
arrow_x, ",", arrow_y, ";",
"1,1;crafting_formspec_arrow.png",
"]",
mcl_formspec.create_itemslot_bg(output_x, arrow_y, 1, 1),
"list[current_player;craftpreview;",
output_x, ",", arrow_y, ";",
"1,1;",
"]",
}
return formspec
end
function mcl_inventory.formspec_inventory(x, y, extra)
-- Optional params: extra
-- extra params:
-- { <hotbar_y=...,>* <label_y=...>* }
local iu = mcl_formspec.itemslot_iu
local formspec = table.concat{
(extra and extra.label_y) and mcl_formspec.create_label(x, extra.label_y, S("Inventory")) or "",
mcl_formspec.create_itemslot_bg(x, y, 9, 3),
"list[current_player;main;",
x, ",", y, ";",
"9,3;9",
"]",
}
if extra and extra.hotbar_y then
formspec = table.concat{
formspec,
mcl_formspec.create_itemslot_bg(x, extra.hotbar_y, 9, 1),
"list[current_player;main;",
x, ",", extra.hotbar_y, ";",
"9,1;",
"]",
}
end
return formspec
end
local function set_inventory(player, armor_change_only)
if minetest.is_creative_enabled(player:get_player_name()) then
if armor_change_only then
@ -62,150 +218,49 @@ local function set_inventory(player, armor_change_only)
return
end
local margin = mcl_formspec.margin
local padding = mcl_formspec.padding
local bu = mcl_formspec.itemslot_bu
local iu = mcl_formspec.itemslot_iu
local label_color = mcl_formspec.label_color
local label_height = mcl_formspec.label_height
local label_y_offset = mcl_formspec.label_y_offset
local inv = player:get_inventory()
inv:set_width("craft", 2)
inv:set_size("craft", 4)
-- Armor slots formspec
local armor_slots = {"helmet", "chestplate", "leggings", "boots"}
local armor_slots_formspec = ""
for a=1,4 do
local b = a - 1
local y = padding + b * iu
armor_slots_formspec = table.concat{
armor_slots_formspec,
mcl_formspec.create_itemslot_bg(padding, y, 1, 1),
}
local padding = mcl_formspec.padding
local padding_slim = mcl_formspec.padding_slim
local bu = mcl_formspec.itemslot_bu
local iu = mcl_formspec.itemslot_iu
if inv:get_stack("armor", a+1):is_empty() then
armor_slots_formspec = table.concat{
armor_slots_formspec,
"image[",
padding, ",", y, ";",
"1,1;mcl_inventory_empty_armor_slot_", armor_slots[a], ".png",
"]"
}
end
armor_slots_formspec = table.concat{
armor_slots_formspec,
"list[current_player;armor;",
padding, ",", y, ";",
"1,1;", a,
"]",
}
end
-- Player preview formspec
local ppreview_width_factor = 102 / 108 -- eyeballed value
local ppreview_width = ppreview_width_factor * 3 * iu
local ppreview_height = 4 * iu
local ppreview_x = padding + iu
local ppreview_formspec = table.concat{
"background9[",
ppreview_x - bu, ",", padding - bu, ";",
ppreview_width, ",", ppreview_height, ";",
"mcl_inventory_player_preview_box9.png;false;3",
"]",
}
if minetest.settings:get_bool("3d_player_preview", true) then
ppreview_formspec = table.concat{
ppreview_formspec,
-- TODO: model is offsetted to the right. investigate.
mcl_player.get_player_formspec_model(
player,
ppreview_x - bu, padding - bu,
ppreview_width, ppreview_height,
""
)
}
else
ppreview_formspec = table.concat{
ppreview_formspec,
-- TODO: better image size+centered.
"image[",
ppreview_x, ",", padding, ";",
3.5, ",", 4 + padding + 3 * between, ";",
mcl_player.player_get_preview(player),
"]",
}
end
-- Left hand slot formspec
-- TODO: implement left hand
local left_hand_x = ppreview_x + ppreview_width
local left_hand_y = padding + ppreview_height - iu
local left_hand_formspec = table.concat{
-- left hand presupport
mcl_formspec.create_itemslot_bg(left_hand_x, left_hand_y, 1, 1),
"list[current_player;armor;",
left_hand_x, ",", left_hand_y, ";",
"1,1;4",
"]",
}
-- Crafting formspec
local crafting_x = padding + 5 * iu
local crafting_y = padding + label_height
local crafting_length = 2 * iu
local crafting_arrow_x = crafting_x + crafting_length + margin
local crafting_arrow_y = crafting_y + iu / 2
local crafting_formspec = table.concat{
"label[",
crafting_x - bu, ",", padding + label_y_offset, ";",
F(C(mcl_formspec.label_color, S("Crafting"))),
"]",
mcl_formspec.create_itemslot_bg(crafting_x, crafting_y, 2, 2),
"list[current_player;craft;",
crafting_x, ",", crafting_y, ";",
"2,2",
"]",
"image[",
crafting_arrow_x, ",", crafting_arrow_y, ";",
"1,1;crafting_formspec_arrow.png",
"]",
mcl_formspec.create_itemslot_bg(crafting_arrow_x + 1 + margin + bu, crafting_arrow_y, 1, 1),
"list[current_player;craftpreview;",
crafting_arrow_x + 1 + margin + bu, ",", crafting_arrow_y, ";",
"1,1;",
"]",
}
local inventory_formspec = table.concat{
mcl_formspec.create_itemslot_bg(padding, 2 * padding + 4 * iu, 9, 3),
mcl_formspec.create_itemslot_bg(padding, 3 * padding + 7 * iu, 9, 1),
"list[current_player;main;",
padding, ",", 2 * padding + 4 * iu, ";",
"9,3;9",
"]",
"list[current_player;main;",
padding, ",", 3 * padding + 7 * iu, ";",
"9,1;",
"]",
}
local label_height = mcl_formspec.label_height
local form = table.concat({
"formspec_version[4]",
"size[", 2 * padding + 9 * iu - 2 * bu, ",", 4 * padding + 8 * iu - 2 * bu, "]",
"size[", 2 * padding + 9 * iu - 2 * bu, ",", 2 * padding + 2 * padding_slim + 8 * iu - 2 * bu, "]",
mcl_formspec.label_style,
mcl_formspec.itemslot_style,
armor_slots_formspec,
ppreview_formspec,
left_hand_formspec,
-- buttons_formspec,
crafting_formspec,
inventory_formspec,
mcl_inventory.formspec_armor(padding, padding, { inv = inv }),
mcl_inventory.formspec_player_preview(
padding + iu,
padding,
{ player = player }
),
mcl_inventory.formspec_left_hand(
padding + iu + mcl_inventory.FORMSPEC_PLAYER_PREVIEW_WIDTH_FACTOR * 3 * iu,
padding + 3 * iu,
{ inv = inv }
),
mcl_inventory.formspec_crafting(
padding + 5 * iu,
padding + label_height,
{ label_y = padding }
),
-- mcl_inventory.formspec_buttons()
mcl_inventory.formspec_inventory(
padding,
padding + padding_slim + 4 * iu,
{ hotbar_y = padding + 2 * padding_slim + 7 * iu }
),
-- TODO: skins button
-- "image_button[5.375,4.125;1,1;mcl_skins_button.png;__mcl_skins;]",