From a8bb37a7501dc7871e47149121adfc8a8cdbdc9b Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 16 Jan 2018 21:19:59 +0100 Subject: [PATCH] Add extended tooltip --- init.lua | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index 67033c4..dd140c1 100644 --- a/init.lua +++ b/init.lua @@ -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, -- 2.11.4.GIT