From a16d634ffdf7029eea80583966359d38aa4c2d7c Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Mon, 7 Nov 2016 22:51:41 +0100 Subject: Return widget ID in text widget tool function --- API.md | 5 ++++- init.lua | 11 +++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/API.md b/API.md index f275f78..34b0bbe 100644 --- a/API.md +++ b/API.md @@ -413,7 +413,10 @@ The default values for the optional parameters result in a widget which fills nearly the entire entry page. #### Return value -A string which contains a complete formspec definition building the widget. +Two values are returned, in this order: + +* String: Contains a complete formspec definition building the widget. +* String: Formspec element ID of the created widget #### Note When you use this function to build a formspec string, do not use identifiers diff --git a/init.lua b/init.lua index 9f08775..0b22c21 100644 --- a/init.lua +++ b/init.lua @@ -416,7 +416,8 @@ end -- Scrollable freeform text doc.entry_builders.text = function(data) - return doc.widgets.text(data, doc.FORMSPEC.ENTRY_START_X, doc.FORMSPEC.ENTRY_START_Y, doc.FORMSPEC.ENTRY_WIDTH - 0.2, doc.FORMSPEC.ENTRY_HEIGHT) + local formstring = doc.widgets.text(data, doc.FORMSPEC.ENTRY_START_X, doc.FORMSPEC.ENTRY_START_Y, doc.FORMSPEC.ENTRY_WIDTH - 0.2, doc.FORMSPEC.ENTRY_HEIGHT) + return formstring end doc.widgets = {} @@ -439,13 +440,15 @@ doc.widgets.text = function(data, x, y, width, height) local baselength = 80 local widget_basewidth = doc.FORMSPEC.WIDTH local linelength = math.max(20, math.floor(baselength * (width / widget_basewidth))) + + local widget_id = "doc_widget_text"..text_id + text_id = text_id + 1 -- TODO: Wait for Minetest to provide a native widget for scrollable read-only text with automatic line breaks. -- Currently, all of this had to be hacked into this script manually by using/abusing the table widget local formstring = "tablecolumns[text]".. "tableoptions[background=#00000000;highlight=#00000000;border=false]".. - "table["..tostring(x)..","..tostring(y)..";"..tostring(width)..","..tostring(height)..";doc_widget_text"..tostring(text_id)..";"..text_for_textlist(data, linelength).."]" - text_id = text_id + 1 - return formstring + "table["..tostring(x)..","..tostring(y)..";"..tostring(width)..","..tostring(height)..";"..widget_id..";"..text_for_textlist(data, linelength).."]" + return formstring, widget_id end -- Direct formspec -- cgit v1.2.3