Books: Add support for multiple pages
This commit is contained in:
parent
8fdcfea9f9
commit
b57dd0f9b2
@ -11,14 +11,22 @@ minetest.register_craftitem("default:paper", {
|
|||||||
inventory_image = "default_paper.png",
|
inventory_image = "default_paper.png",
|
||||||
})
|
})
|
||||||
|
|
||||||
local function book_on_use(itemstack, user, pointed_thing)
|
local function book_on_use(itemstack, user)
|
||||||
local player_name = user:get_player_name()
|
local player_name = user:get_player_name()
|
||||||
local data = minetest.deserialize(itemstack:get_metadata())
|
local data = minetest.deserialize(itemstack:get_metadata())
|
||||||
local title, text, owner = "", "", player_name
|
local formspec, title, text, text_len, page, page_max, cpp, owner =
|
||||||
|
"", "", "", 1, 1, 1, 1, player_name
|
||||||
|
|
||||||
if data then
|
if data then
|
||||||
title, text, owner = data.title, data.text, data.owner
|
title = data.title
|
||||||
|
text = data.text
|
||||||
|
owner = data.owner
|
||||||
|
text_len = data.text_len
|
||||||
|
page = data.page
|
||||||
|
page_max = data.page_max
|
||||||
|
cpp = data.chars_per_page
|
||||||
end
|
end
|
||||||
local formspec
|
|
||||||
if owner == player_name then
|
if owner == player_name then
|
||||||
formspec = "size[8,8]" .. default.gui_bg ..
|
formspec = "size[8,8]" .. default.gui_bg ..
|
||||||
default.gui_bg_img ..
|
default.gui_bg_img ..
|
||||||
@ -31,48 +39,79 @@ local function book_on_use(itemstack, user, pointed_thing)
|
|||||||
formspec = "size[8,8]" .. default.gui_bg ..
|
formspec = "size[8,8]" .. default.gui_bg ..
|
||||||
default.gui_bg_img ..
|
default.gui_bg_img ..
|
||||||
"label[0.5,0.5;by " .. owner .. "]" ..
|
"label[0.5,0.5;by " .. owner .. "]" ..
|
||||||
"label[0.5,0;" .. minetest.formspec_escape(title) .. "]" ..
|
"tablecolumns[color;text]" ..
|
||||||
"textarea[0.5,1.5;7.5,7;text;;" ..
|
"tableoptions[background=#00000000;highlight=#00000000;border=false]" ..
|
||||||
minetest.formspec_escape(text) .. "]"
|
"table[0.4,0;4,0.5;title;#FFFF00," .. minetest.formspec_escape(title) .. "]"..
|
||||||
|
"textarea[0.5,1.5;7.5,7;;".. minetest.formspec_escape(text:sub(
|
||||||
|
(cpp * page) - cpp, cpp * page)) .. ";]" ..
|
||||||
|
"button[4.6,0.2;0.8,0.8;book_prev;<]" ..
|
||||||
|
"label[5.35,0.3;Page " .. page .. " of " .. page_max .. "]"..
|
||||||
|
"button[7,0.2;0.8,0.8;book_next;>]"
|
||||||
end
|
end
|
||||||
minetest.show_formspec(user:get_player_name(), "default:book", formspec)
|
|
||||||
|
minetest.show_formspec(player_name, "default:book", formspec)
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player, form_name, fields)
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
if form_name ~= "default:book" or not fields.save or
|
if formname ~= "default:book" then return end
|
||||||
fields.title == "" or fields.text == "" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local inv = player:get_inventory()
|
local inv = player:get_inventory()
|
||||||
local stack = player:get_wielded_item()
|
local stack = player:get_wielded_item()
|
||||||
local new_stack, data
|
|
||||||
if stack:get_name() ~= "default:book_written" then
|
if fields.save and fields.title ~= "" and fields.text ~= "" then
|
||||||
local count = stack:get_count()
|
local new_stack, data
|
||||||
if count == 1 then
|
if stack:get_name() ~= "default:book_written" then
|
||||||
stack:set_name("default:book_written")
|
local count = stack:get_count()
|
||||||
|
if count == 1 then
|
||||||
|
stack:set_name("default:book_written")
|
||||||
|
else
|
||||||
|
stack:set_count(count - 1)
|
||||||
|
new_stack = ItemStack("default:book_written")
|
||||||
|
end
|
||||||
else
|
else
|
||||||
stack:set_count(count - 1)
|
data = minetest.deserialize(stack:get_metadata())
|
||||||
new_stack = ItemStack("default:book_written")
|
|
||||||
end
|
end
|
||||||
else
|
|
||||||
data = minetest.deserialize(stack:get_metadata())
|
if not data then data = {} end
|
||||||
end
|
data.title = fields.title
|
||||||
if not data then data = {} end
|
data.text = fields.text
|
||||||
data.title = fields.title
|
data.text_len = fields.text:len()
|
||||||
data.text = fields.text
|
data.page = 1
|
||||||
data.owner = player:get_player_name()
|
data.chars_per_page = 650
|
||||||
local data_str = minetest.serialize(data)
|
data.page_max = math.ceil(data.text_len / data.chars_per_page)
|
||||||
if new_stack then
|
data.owner = player:get_player_name()
|
||||||
new_stack:set_metadata(data_str)
|
local data_str = minetest.serialize(data)
|
||||||
if inv:room_for_item("main", new_stack) then
|
|
||||||
inv:add_item("main", new_stack)
|
if new_stack then
|
||||||
|
new_stack:set_metadata(data_str)
|
||||||
|
if inv:room_for_item("main", new_stack) then
|
||||||
|
inv:add_item("main", new_stack)
|
||||||
|
else
|
||||||
|
minetest.add_item(player:getpos(), new_stack)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
minetest.add_item(player:getpos(), new_stack)
|
stack:set_metadata(data_str)
|
||||||
end
|
end
|
||||||
else
|
|
||||||
|
player:set_wielded_item(stack)
|
||||||
|
|
||||||
|
elseif fields.book_next or fields.book_prev then
|
||||||
|
local data = minetest.deserialize(stack:get_metadata())
|
||||||
|
if fields.book_next then
|
||||||
|
data.page = data.page + 1
|
||||||
|
if data.page > data.page_max then
|
||||||
|
data.page = 1
|
||||||
|
end
|
||||||
|
else
|
||||||
|
data.page = data.page - 1
|
||||||
|
if data.page == 0 then
|
||||||
|
data.page = data.page_max
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local data_str = minetest.serialize(data)
|
||||||
stack:set_metadata(data_str)
|
stack:set_metadata(data_str)
|
||||||
|
book_on_use(stack, player)
|
||||||
end
|
end
|
||||||
player:set_wielded_item(stack)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_craftitem("default:book", {
|
minetest.register_craftitem("default:book", {
|
||||||
|
Loading…
Reference in New Issue
Block a user