Add extended tooltip

This commit is contained in:
Wuzzy 2018-01-16 21:19:59 +01:00
parent d50eddda4e
commit a8bb37a750

View File

@ -1,8 +1,31 @@
local NEWLINE = "\n"
local DESCRIPTION = "Command Tool"
local MAX_CMD_TOOLTIP_LEN = 48
local split_commands = function(commands_string)
return string.split(commands_string, NEWLINE)
end
local set_commands = function(itemstack, commands_string)
local meta = itemstack:get_meta()
meta:set_string("cmd", commands_string)
local cmds = split_commands(commands_string)
local first_cmd
if #cmds >= 1 then
first_cmd = cmds[1]
if string.len(first_cmd) > MAX_CMD_TOOLTIP_LEN then
first_cmd = string.sub(first_cmd, 1, MAX_CMD_TOOLTIP_LEN) .. " (…)"
end
local tooltip = DESCRIPTION .. NEWLINE .. first_cmd
if #cmds == 2 then
tooltip = tooltip .. NEWLINE .. string.format("… and %d more command", #cmds - 1)
elseif #cmds > 2 then
tooltip = tooltip .. NEWLINE .. string.format("… and %d more commands", #cmds - 1)
end
meta:set_string("description", tooltip)
else
meta:set_string("description", "")
end
return itemstack
end
@ -10,7 +33,7 @@ end
local get_commands = function(itemstack)
local meta = itemstack:get_meta()
local cmd_str = meta:get_string("cmd")
local cmds = string.split(cmd_str, NEWLINE)
local cmds = split_commands(cmd_str)
return cmds
end
@ -74,14 +97,14 @@ local open_command_configuration = function(itemstack, player, pointed_thing)
end
local formspec =
"size[6,6]"..
"textarea[0.25,0;6,4;commands;Commands:;"..minetest.formspec_escape(commands_str).."]"..
"textarea[0.25,0;6,5;commands;Commands:;"..minetest.formspec_escape(commands_str).."]"..
"button_exit[0.5,5;2,1;ok;OK]"..
"button_exit[3.5,5;2,1;cancel;Cancel]"
minetest.show_formspec(player_name, "cmdtool", formspec)
end
minetest.register_tool("cmdtool:cmdtool", {
description = "Command Tool",
description = DESCRIPTION,
inventory_image = "cmdtool_cmdtool.png",
wield_imagee = "cmdtool_cmdtool.png",
on_use = execute_command,