Add pretty creative mode tab

This commit is contained in:
Wuzzy 2019-08-30 22:19:25 +02:00
parent 1fa07b17d7
commit 770ead8aa2
7 changed files with 95 additions and 30 deletions

View File

@ -1,8 +1,20 @@
local S = minetest.get_translator("creative")
local F = minetest.formspec_escape
local creative_inventory = {}
creative_inventory.creative_inventory_size = 0
creative = {}
local playerdata = {}
local form = default.ui.get_page("default:2part")
form = form .. "list[current_player;main;0.25,4.75;8,4;]"
form = form .. default.ui.get_hotbar_itemslot_bg(0.25, 4.75, 8, 1)
form = form .. default.ui.get_itemslot_bg(0.25, 5.75, 8, 3)
default.ui.register_page("creative:creative", form)
creative.creative_inventory_size = 0
creative.slots_num = 7*4
-- Create detached creative inventory after loading all mods
minetest.register_on_mods_loaded(function()
@ -46,7 +58,7 @@ minetest.register_on_mods_loaded(function()
for _,itemstring in ipairs(creative_list) do
inv:add_item("main", ItemStack(itemstring))
end
creative_inventory.creative_inventory_size = #creative_list
creative.creative_inventory_size = #creative_list
end)
-- Create the trash field
@ -67,60 +79,106 @@ local trash = minetest.create_detached_inventory("creative_trash", {
trash:set_size("main", 1)
creative_inventory.set_creative_formspec = function(player, start_i, pagenum)
creative.get_creative_formspec = function(player, start_i, pagenum)
pagenum = math.floor(pagenum)
local pagemax = math.floor((creative_inventory.creative_inventory_size-1) / (6*4) + 1)
player:set_inventory_formspec(
"size[13,7.5]"..
"list[current_player;main;5,3.5;8,1;]"..
"list[current_player;main;5,4.75;8,3;8]"..
"list[detached:creative;main;0.3,0.5;4,6;"..tostring(start_i).."]"..
"label[2.0,6.55;"..F(S("@1/@2", pagenum, pagemax)).."]"..
"button[0.3,6.5;1.6,1;creative_prev;<<]"..
"button[2.7,6.5;1.6,1;creative_next;>>]"..
"label[5,1.5;"..F(S("Trash:")).."]"..
"list[detached:creative_trash;main;5,2;1,1;]"..
"listring[current_player;main]"..
"listring[detached:creative_trash;main]"..
"listring[detached:creative;main]"..
"listring[current_player;main]"
)
local pagemax = math.floor((creative.creative_inventory_size-1) / (creative.slots_num) + 1)
return
"list[detached:creative;main;0.25,0.25;7,4;"..tostring(start_i).."]"..
"label[7.5,0.75;"..F(S("@1/@2", pagenum, pagemax)).."]"..
default.ui.button(7.25, 1.25, 1, 1, "creative_prev", "<<")..
default.ui.button(7.25, 2.25, 1, 1, "creative_next", ">>")..
default.ui.get_itemslot_bg(0.25, 0.25, 7,4)..
-- TODO: Add trash icon
"list[detached:creative_trash;main;7.25,3.25;1,1;]"..
default.ui.get_itemslot_bg(7.25, 3.25, 1,1)..
"listring[current_player;main]"..
"listring[detached:creative_trash;main]"..
"listring[detached:creative;main]"..
"listring[current_player;main]"
end
local get_page_and_start_i = function(playername)
local page = playerdata[playername].page
local start_i = (page - 1) * creative.slots_num
return page, start_i
end
creative.get_formspec = function(playername)
if not minetest.settings:get_bool("creative_mode") then
return ""
end
local player = minetest.get_player_by_name(playername)
if player then
local form = default.ui.get_page("creative:creative")
local page, start_i = get_page_and_start_i(playername)
form = form .. creative.get_creative_formspec(player, start_i, page)
return form
end
end
minetest.register_on_joinplayer(function(player)
-- If in creative mode, modify player's inventory forms
if not minetest.settings:get_bool("creative_mode") then
return
end
creative_inventory.set_creative_formspec(player, 0, 1)
playerdata[player:get_player_name()] = { page = 1 }
end)
minetest.register_on_leaveplayer(function(player)
playerdata[player:get_player_name()] = nil
end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
if not minetest.settings:get_bool("creative_mode") then
return
end
local playername = player:get_player_name()
-- Figure out current page from formspec
local current_page = 0
local formspec = player:get_inventory_formspec()
local start_i = string.match(formspec, "list%[detached:creative;main;[%d.]+,[%d.]+;[%d.]+,[%d.]+;(%d+)%]")
start_i = tonumber(start_i) or 0
local page, start_i = get_page_and_start_i(playername)
local changed = false
if fields.creative_prev then
start_i = start_i - 4*6
page = page - 1
start_i = start_i - creative.slots_num
changed = true
end
if fields.creative_next then
start_i = start_i + 4*6
page = page + 1
start_i = start_i + creative.slots_num
changed = true
end
if start_i < 0 then
start_i = start_i + 4*6
start_i = start_i + creative.slots_num
page = page + 1
end
if start_i >= creative_inventory.creative_inventory_size then
start_i = start_i - 4*6
if start_i >= creative.creative_inventory_size then
start_i = start_i - creative.slots_num
page = page - 1
end
playerdata[playername].page = page
if start_i < 0 or start_i >= creative_inventory.creative_inventory_size then
if start_i < 0 or start_i >= creative.creative_inventory_size then
start_i = 0
page = 1
end
creative_inventory.set_creative_formspec(player, start_i, start_i / (6*4) + 1)
local form = default.ui.get_page("creative:creative")
form = form .. creative.get_creative_formspec(player, start_i, start_i / (creative.slots_num) + 1)
if changed then
minetest.show_formspec(playername, "creative:creative", form)
player:set_inventory_formspec(form)
end
end)
-- Dummy implementation
-- TODO: Implement per-player creative mode
creative.is_enabled_for = function(player)
if minetest.settings:get_bool("creative_mode") then
return true
else
return false
end
end

View File

@ -279,6 +279,9 @@ end
if minetest.get_modpath("player_skins") ~= nil then
form_default_default = form_default_default .. default.ui.tab(-0.9, 2.84, "tab_player_skins", "ui_icon_player_skins.png", S("Player Skins"))
end
if minetest.get_modpath("creative") ~= nil and minetest.settings:get_bool("creative_mode") then
form_default_default = form_default_default .. default.ui.tab(-0.9, 3.64, "tab_creative", "ui_icon_creative.png", S("Creative Inventory"))
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]")
@ -322,6 +325,8 @@ function default.ui.receive_fields(player, form_name, fields)
minetest.show_formspec(name, "achievements:achievements", achievements.get_formspec(name))
elseif minetest.get_modpath("player_skins") ~= nil and fields.tab_player_skins then
minetest.show_formspec(name, "player_skins:player_skins", player_skins.get_formspec(name))
elseif minetest.get_modpath("creative") ~= nil and minetest.settings:get_bool("creative_mode") and fields.tab_creative then
minetest.show_formspec(name, "creative:creative", creative.get_formspec(name))
end
end

View File

@ -149,3 +149,4 @@ Any planks=Beliebige Planken
Any soil=Beliebiger Boden
Any stone=Beliebiger Stein
Any tree=Beliebiger Baum
Creative Inventory=Kreativinventar

View File

@ -149,3 +149,4 @@ Any planks=
Any soil=
Any stone=
Any tree=
Creative Inventory=

Binary file not shown.

Before

Width:  |  Height:  |  Size: 314 B

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB