Return widget ID in text widget tool function

This commit is contained in:
Wuzzy 2016-11-07 22:51:41 +01:00
parent 105110d7d4
commit a16d634ffd
2 changed files with 11 additions and 5 deletions

5
API.md
View File

@ -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

View File

@ -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