Add enter press support to text boxes

This commit is contained in:
bell07 2017-11-01 13:58:27 +01:00 committed by rubenwardy
parent 080ad817c6
commit e829336b84
3 changed files with 44 additions and 6 deletions

@ -113,8 +113,11 @@
###Field and Text Area
* element:setText( text ) - set the caption of the button
* element:getText() - get the caption of the field
* element:setImage( filename ) - sets the background of the field
* element:getImage() - get the background filename of the field
* element:isPassword() - returns true if the field is a password field
* element:isMultiline() - returns true if the field is a miltiline textarea
* element:setCloseOnEnter(bool) - Set the field_close_on_enter string, usually set to false to disable the formspec close on enter
* element:getCloseOnEnter() - Get the field_close_on_enter value
* element:onKeyEnter(func(self, state, playername) - process the Enter key action for this fiels
###List box
* element:onClick( func(self,state,idx,playername) ) - function to run when listbox item idx is clicked

@ -8,7 +8,12 @@ local s = smartfs.create("smartfs:form", function(state)
end
usr:setSize(3,0.5)
usr:setBackground("halo.png")
state:field(7.25,1.25,3,1,"txt","Textbox")
local textbox = state:field(7.25,1.25,3,1,"txt","Textbox")
textbox:setCloseOnEnter(false)
textbox:onKeyEnter(function(self, state, playername)
print("Enter pressed in Textbox field")
end)
state:image(0,0,2,2,"img","default_stone.png")
local toggle = state:toggle(0,2,3,1,"tg",{"plenty..","of..","custom..","elements"})
toggle:onToggle(function(self, state, player)

@ -510,6 +510,13 @@ function smartfs._makeState_(form, params, location, newplayer)
todo.element:submit(todo.value, player)
end
end
-- handle key_enter
if fields.key_enter and fields.key_enter_field then
local element = self:_get_element_recursive_(fields.key_enter_field)
if element and element.submit_key_enter then
element:submit_key_enter(fields[fields.key_enter_field], player)
end
end
if not fields.quit and not self.closed then
self:show()
@ -1003,7 +1010,8 @@ smartfs.element("field", {
self:getAbsName()..
";"..
minetest.formspec_escape(self.data.label)..
"]"
"]"..
self:getCloseOnEnterString()
else
return "field["..
self.data.pos.x..","..self.data.pos.y..
@ -1015,7 +1023,8 @@ smartfs.element("field", {
minetest.formspec_escape(self.data.label)..
";"..
minetest.formspec_escape(self.data.value)..
"]"
"]"..
self:getCloseOnEnterString()
end
end,
setText = function(self,text)
@ -1029,7 +1038,28 @@ smartfs.element("field", {
end,
isMultiline = function(self,bool)
self.data.ml = bool
end
end,
getCloseOnEnterString = function(self)
if self.close_on_enter == nil then
return ""
else
return "field_close_on_enter["..self:getAbsName()..";"..tostring(self.close_on_enter).."]"
end
end,
setCloseOnEnter = function(self, value)
self.close_on_enter = value
end,
getCloseOnEnter = function(self)
return self.close_on_enter
end,
submit_key_enter = function(self, field, player)
if self._key_enter then
self:_key_enter(self.root, player)
end
end,
onKeyEnter = function(self,func)
self._key_enter = func
end,
})
smartfs.element("image", {