2017-05-14 23:50:37 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
default.ui = {}
|
|
|
|
|
2019-08-28 17:31:41 +02:00
|
|
|
local S = minetest.get_translator("default")
|
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
-- Registered UI pages
|
|
|
|
|
|
|
|
default.ui.registered_pages = {
|
|
|
|
}
|
|
|
|
|
2017-05-14 23:50:37 +02:00
|
|
|
-- UI defaults
|
|
|
|
|
2017-05-13 20:50:15 +02:00
|
|
|
default.ui.default = {}
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-14 23:50:37 +02:00
|
|
|
-- Colors
|
|
|
|
|
2017-05-13 20:50:15 +02:00
|
|
|
default.ui.default.colors = "listcolors[#00000000;#00000010;#00000000;#68B259;#FFF]"
|
|
|
|
default.ui.default.bg = "bgcolor[#00000000;false]"
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-14 23:50:37 +02:00
|
|
|
-- Group default items
|
|
|
|
|
|
|
|
default.ui.group_defaults = {
|
2017-05-15 22:30:46 +02:00
|
|
|
fuzzy = "mobs:wool",
|
2017-05-14 23:50:37 +02:00
|
|
|
planks = "default:planks",
|
|
|
|
soil = "default:dirt",
|
2017-05-15 20:19:26 +02:00
|
|
|
stone = "default:stone",
|
|
|
|
tree = "default:tree",
|
2017-05-14 23:50:37 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
-- Itemslot backgrounds
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
function default.ui.get_itemslot_bg(x, y, w, h)
|
|
|
|
local out = ""
|
|
|
|
for i = 0, w - 1, 1 do
|
|
|
|
for j = 0, h - 1, 1 do
|
|
|
|
out = out .."image["..x+i..","..y+j..";1,1;ui_itemslot.png]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return out
|
|
|
|
end
|
|
|
|
|
|
|
|
function default.ui.get_hotbar_itemslot_bg(x, y, w, h)
|
|
|
|
local out = ""
|
|
|
|
for i = 0, w - 1, 1 do
|
|
|
|
for j = 0, h - 1, 1 do
|
2017-05-15 20:19:26 +02:00
|
|
|
out = out .."image["..x+i..","..y+j
|
|
|
|
..";1,1;ui_itemslot.png^ui_itemslot_dark.png]"
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return out
|
|
|
|
end
|
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
-- Buttons
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
function default.ui.image_button(x, y, w, h, name, image)
|
2017-05-12 04:29:55 +02:00
|
|
|
local image = minetest.formspec_escape(image)
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
return "image_button["..x..","..y..";"..w..","..h..";"
|
|
|
|
..image..";"..name..";;;false;"..image.."]"
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function default.ui.button(x, y, w, h, name, label, noclip)
|
|
|
|
local nc = "false"
|
2017-05-10 19:19:28 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
if noclip then
|
|
|
|
nc = "true"
|
|
|
|
end
|
|
|
|
|
2017-05-14 00:15:43 +02:00
|
|
|
if w == 1 then
|
2017-05-15 20:19:26 +02:00
|
|
|
return "image_button["..x..","..y..";"..w..","..h
|
2019-08-28 17:31:41 +02:00
|
|
|
..";ui_button_1w_inactive.png;"..name..";"..minetest.formspec_escape(label)..";"
|
2017-05-15 20:19:26 +02:00
|
|
|
..nc..";false;ui_button_1w_active.png]"
|
2017-05-14 00:15:43 +02:00
|
|
|
elseif w == 2 then
|
2017-05-15 20:19:26 +02:00
|
|
|
return "image_button["..x..","..y..";"..w..","..h
|
2019-08-28 17:31:41 +02:00
|
|
|
..";ui_button_2w_inactive.png;"..name..";"..minetest.formspec_escape(label)..";"
|
2017-05-15 20:19:26 +02:00
|
|
|
..nc..";false;ui_button_2w_active.png]"
|
2015-09-01 17:15:24 +02:00
|
|
|
else
|
2017-05-15 20:19:26 +02:00
|
|
|
return "image_button["..x..","..y..";"..w..","..h
|
2019-08-28 17:31:41 +02:00
|
|
|
..";ui_button_3w_inactive.png;"..name..";"..minetest.formspec_escape(label)..";"
|
2017-05-15 20:19:26 +02:00
|
|
|
..nc..";false;ui_button_3w_active.png]"
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function default.ui.button_exit(x, y, w, h, name, label, noclip)
|
|
|
|
local nc = "false"
|
2017-05-10 19:19:28 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
if noclip then
|
|
|
|
nc = "true"
|
|
|
|
end
|
|
|
|
|
|
|
|
if w == 2 then
|
2017-05-15 20:19:26 +02:00
|
|
|
return "image_button_exit["..x..","..y..";"..w..","..h
|
2019-08-28 17:31:41 +02:00
|
|
|
..";ui_button_2w_inactive.png;"..name..";"..minetest.formspec_escape(label)..";"
|
2017-05-15 20:19:26 +02:00
|
|
|
..nc..";false;ui_button_2w_active.png]"
|
2015-09-01 17:15:24 +02:00
|
|
|
else
|
2017-05-15 20:19:26 +02:00
|
|
|
return "image_button_exit["..x..","..y..";"..w..","..h
|
2019-08-28 17:31:41 +02:00
|
|
|
..";ui_button_3w_inactive.png;"..name..";"..minetest.formspec_escape(label)..";"
|
2017-05-15 20:19:26 +02:00
|
|
|
..nc..";false;ui_button_3w_active.png]"
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
-- Tabs
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
function default.ui.tab(x, y, name, icon, tooltip)
|
|
|
|
local tooltip = tooltip or ""
|
|
|
|
local shifted_icon = "[combine:16x16:0,0=ui_tab_active.png:0,1="..icon
|
|
|
|
|
|
|
|
local form = ""
|
2017-05-15 20:19:26 +02:00
|
|
|
|
|
|
|
form = form .. "image_button["..x..","..y..";1,1;ui_tab_inactive.png^"
|
|
|
|
..icon..";"..name..";;true;false;"
|
|
|
|
..minetest.formspec_escape(shifted_icon).."]"
|
|
|
|
|
2019-08-28 17:31:41 +02:00
|
|
|
form = form .. "tooltip["..name..";"..minetest.formspec_escape(tooltip).."]"
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
return form
|
|
|
|
end
|
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
-- Itemstacks
|
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
local function get_itemdef_field(itemname, fieldname)
|
2017-05-12 04:29:55 +02:00
|
|
|
if not minetest.registered_items[itemname] then
|
2015-09-01 17:15:24 +02:00
|
|
|
return nil
|
|
|
|
end
|
2017-05-12 04:29:55 +02:00
|
|
|
return minetest.registered_items[itemname][fieldname]
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function default.ui.fake_itemstack(x, y, itemstack, name)
|
|
|
|
local name = name or "fake_itemstack"
|
|
|
|
|
|
|
|
local itemname = itemstack:get_name()
|
|
|
|
local itemamt = itemstack:get_count()
|
|
|
|
|
|
|
|
local itemdesc = ""
|
2017-05-15 20:19:26 +02:00
|
|
|
if minetest.registered_items[itemname]
|
|
|
|
and minetest.registered_items[itemname].description ~= nil then
|
2017-05-12 04:29:55 +02:00
|
|
|
itemdesc = minetest.registered_items[itemname].description
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if itemamt <= 1 then itemamt = "" end
|
|
|
|
|
|
|
|
local result = ""
|
|
|
|
if itemname ~= "" then
|
2017-05-15 20:19:26 +02:00
|
|
|
result = result .. "image_button["..x..","..y..";1,1;ui_null.png;"
|
|
|
|
..name..";;false;false;ui_null.png]"
|
|
|
|
result = result .. "item_image["..x..","..y..";1,1;"
|
|
|
|
..minetest.formspec_escape(itemname .. " " .. itemamt).."]"
|
2017-05-14 23:50:37 +02:00
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
result = result .. "tooltip["..name..";"..minetest.formspec_escape(itemdesc).."]"
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function default.ui.fake_simple_itemstack(x, y, itemname, name)
|
|
|
|
local name = name or "fake_simple_itemstack"
|
|
|
|
|
|
|
|
local itemdesc = ""
|
2017-05-15 20:19:26 +02:00
|
|
|
if minetest.registered_items[itemname]
|
|
|
|
and minetest.registered_items[itemname].description ~= nil then
|
2017-05-12 04:29:55 +02:00
|
|
|
itemdesc = minetest.registered_items[itemname].description
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local result = ""
|
|
|
|
if itemname ~= "" then
|
2017-05-15 20:19:26 +02:00
|
|
|
result = result .. "image_button["..x..","..y..";1,1;ui_null.png;"
|
|
|
|
..name..";;false;false;ui_null.png]"
|
|
|
|
result = result .. "item_image["..x..","..y..";1,1;"
|
|
|
|
..minetest.formspec_escape(itemname).."]"
|
|
|
|
result = result .. "tooltip["..name..";"
|
|
|
|
..minetest.formspec_escape(itemdesc).."]"
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2017-05-14 23:50:37 +02:00
|
|
|
function default.ui.item_group(x, y, group, count, name)
|
2015-09-29 20:19:14 +02:00
|
|
|
local name = name or "fake_itemgroup"
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
local itemname = ""
|
|
|
|
|
2017-05-14 23:50:37 +02:00
|
|
|
local group_default = default.ui.group_defaults[group]
|
|
|
|
|
|
|
|
if group_default ~= nil and minetest.registered_items[group_default] then
|
|
|
|
itemname = group_default
|
|
|
|
else
|
|
|
|
for itemn, itemdef in pairs(minetest.registered_items) do
|
|
|
|
if minetest.get_item_group(itemn, group) ~= 0
|
2019-06-23 23:04:20 +02:00
|
|
|
and minetest.get_item_group(itemn, "not_in_craft_guide") ~= 1 then
|
2017-05-14 23:50:37 +02:00
|
|
|
itemname = itemn
|
|
|
|
end
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local result = ""
|
|
|
|
if itemname ~= "" then
|
2017-05-15 20:19:26 +02:00
|
|
|
result = result .. "image_button["..x..","..y..";1,1;ui_null.png;"
|
|
|
|
..name..";;false;false;ui_null.png]"
|
|
|
|
result = result .. "item_image["..x..","..y..";1,1;"
|
|
|
|
..minetest.formspec_escape(itemname .. " " .. count).."]"
|
2017-05-14 23:50:37 +02:00
|
|
|
|
2019-08-28 17:31:41 +02:00
|
|
|
result = result .. "tooltip["..name..";"..
|
|
|
|
minetest.formspec_escape(S("Group: @1", group)).."]"
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2017-05-14 23:50:37 +02:00
|
|
|
function default.ui.fake_itemstack_any(x, y, itemstack, name)
|
|
|
|
local group = string.match(itemstack:get_name(), "group:(.*)")
|
|
|
|
|
|
|
|
if group == nil then
|
|
|
|
return default.ui.fake_itemstack(x, y, itemstack, name)
|
|
|
|
else
|
|
|
|
return default.ui.item_group(x, y, group, itemstack:get_count(), name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
-- Pages
|
2015-09-01 17:15:24 +02:00
|
|
|
|
|
|
|
function default.ui.get_page(name)
|
|
|
|
local page= default.ui.registered_pages[name]
|
2017-05-10 19:19:28 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
if page == nil then
|
|
|
|
default.log("UI page '" .. name .. "' is not yet registered", "dev")
|
|
|
|
page = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
return page
|
|
|
|
end
|
|
|
|
|
|
|
|
function default.ui.register_page(name, form)
|
|
|
|
default.ui.registered_pages[name] = form
|
|
|
|
end
|
|
|
|
|
2017-05-15 20:19:26 +02:00
|
|
|
-- Default formspec boilerplates
|
|
|
|
|
2017-05-13 20:50:15 +02:00
|
|
|
local form_default_default = ""
|
|
|
|
form_default_default = form_default_default .. "size[8.5,9]"
|
|
|
|
form_default_default = form_default_default .. default.ui.default.colors
|
|
|
|
form_default_default = form_default_default .. default.ui.default.bg
|
2019-08-28 17:31:41 +02:00
|
|
|
form_default_default = form_default_default .. default.ui.tab(-0.9, 0.5, "tab_crafting", "ui_icon_crafting.png", S("Crafting"))
|
2017-05-12 04:29:55 +02:00
|
|
|
if minetest.get_modpath("armor") ~= nil then
|
2019-08-28 17:31:41 +02:00
|
|
|
form_default_default = form_default_default .. default.ui.tab(-0.9, 1.28, "tab_armor", "ui_icon_armor.png", S("Armor"))
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
2017-05-12 04:29:55 +02:00
|
|
|
if minetest.get_modpath("achievements") ~= nil then
|
2019-08-28 17:31:41 +02:00
|
|
|
form_default_default = form_default_default .. default.ui.tab(-0.9, 2.06, "tab_achievements", "ui_icon_achievements.png", S("Achievements"))
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
2017-05-12 04:29:55 +02:00
|
|
|
if minetest.get_modpath("player_skins") ~= nil then
|
2019-08-28 17:31:41 +02:00
|
|
|
form_default_default = form_default_default .. default.ui.tab(-0.9, 2.84, "tab_player_skins", "ui_icon_player_skins.png", S("Player Skins"))
|
2017-05-13 20:50:15 +02:00
|
|
|
end
|
|
|
|
form_default_default = form_default_default .. "background[0,0;8.5,9;ui_formspec_bg_tall.png]"
|
|
|
|
default.ui.register_page("default:default", form_default_default)
|
|
|
|
default.ui.register_page("default:2part", form_default_default .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]")
|
|
|
|
|
|
|
|
local form_default_notabs = ""
|
|
|
|
form_default_notabs = form_default_notabs .. "size[8.5,9]"
|
|
|
|
form_default_notabs = form_default_notabs .. default.ui.default.colors
|
|
|
|
form_default_notabs = form_default_notabs .. default.ui.default.bg
|
|
|
|
form_default_notabs = form_default_notabs .. "background[0,0;8.5,9;ui_formspec_bg_tall.png]"
|
|
|
|
default.ui.register_page("default:notabs", form_default_notabs)
|
|
|
|
default.ui.register_page("default:notabs_2part", form_default_notabs .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]")
|
|
|
|
|
|
|
|
local form_default_field = ""
|
|
|
|
form_default_field = form_default_field .. "size[8.5,5]"
|
|
|
|
form_default_field = form_default_field .. default.ui.default.colors
|
|
|
|
form_default_field = form_default_field .. default.ui.default.bg
|
|
|
|
form_default_field = form_default_field .. "background[0,0;8.5,4.5;ui_formspec_bg_short.png]"
|
2019-08-28 17:31:41 +02:00
|
|
|
form_default_field = form_default_field .. default.ui.button_exit(2.75, 3, 3, 1, "", S("Write"), false)
|
2017-05-13 20:50:15 +02:00
|
|
|
form_default_field = form_default_field .. "field[1,1.75;7,0;text;;${text}]"
|
|
|
|
default.ui.register_page("default:field", form_default_field)
|
|
|
|
|
|
|
|
local form_bookshelf = default.ui.get_page("default:2part")
|
2015-09-10 17:42:43 +02:00
|
|
|
form_bookshelf = form_bookshelf .. "list[current_player;main;0.25,4.75;8,4;]"
|
|
|
|
form_bookshelf = form_bookshelf .. "listring[current_player;main]"
|
|
|
|
form_bookshelf = form_bookshelf .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
|
|
|
|
form_bookshelf = form_bookshelf .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
|
|
|
|
|
|
|
|
form_bookshelf = form_bookshelf .. "list[current_name;main;2.25,1.25;4,2;]"
|
|
|
|
form_bookshelf = form_bookshelf .. "listring[current_name;main]"
|
|
|
|
form_bookshelf = form_bookshelf .. default.ui.get_itemslot_bg(2.25, 1.25, 4, 2)
|
2017-05-13 20:50:15 +02:00
|
|
|
default.ui.register_page("default:bookshelf", form_bookshelf)
|
2015-09-10 17:42:43 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
function default.ui.receive_fields(player, form_name, fields)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
if fields.tab_crafting then
|
2017-05-13 20:50:15 +02:00
|
|
|
minetest.show_formspec(name, "crafting:crafting", crafting.get_formspec(name))
|
2017-05-12 04:29:55 +02:00
|
|
|
elseif minetest.get_modpath("armor") ~= nil and fields.tab_armor then
|
2017-05-13 20:50:15 +02:00
|
|
|
minetest.show_formspec(name, "armor:armor", default.ui.get_page("armor:armor"))
|
2017-05-12 04:29:55 +02:00
|
|
|
elseif minetest.get_modpath("achievements") ~= nil and fields.tab_achievements then
|
2017-05-13 20:50:15 +02:00
|
|
|
minetest.show_formspec(name, "achievements:achievements", achievements.get_formspec(name))
|
2017-05-12 04:29:55 +02:00
|
|
|
elseif minetest.get_modpath("player_skins") ~= nil and fields.tab_player_skins then
|
2017-05-13 20:50:15 +02:00
|
|
|
minetest.show_formspec(name, "player_skins:player_skins", player_skins.get_formspec(name))
|
2015-09-01 17:15:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_on_player_receive_fields(
|
2015-09-01 17:15:24 +02:00
|
|
|
function(player, form_name, fields)
|
|
|
|
default.ui.receive_fields(player, form_name, fields)
|
2017-05-10 19:19:28 +02:00
|
|
|
end)
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_on_joinplayer(
|
2015-09-01 17:15:24 +02:00
|
|
|
function(player)
|
2017-05-14 04:15:17 +02:00
|
|
|
player:set_inventory_formspec(crafting.get_formspec(player:get_player_name()))
|
2017-05-10 19:19:28 +02:00
|
|
|
end)
|