smartfs update. search fields ported to new enter_field handling

This commit is contained in:
Alexander Weber 2017-04-08 21:07:37 +02:00
parent 7a87ce4cc5
commit e227d8baf9
3 changed files with 69 additions and 68 deletions

View File

@ -252,7 +252,6 @@ local function create_lookup_inv(state, name)
state.param.survival_proposal_mode = "lookup" state.param.survival_proposal_mode = "lookup"
state:get("groups_sel"):setSelected(1) state:get("groups_sel"):setSelected(1)
state:get("search"):setText("") state:get("search"):setText("")
state.param.survival_search_string = ""
end end
-- we are outsite of usual smartfs processing. So trigger the formspec update byself -- we are outsite of usual smartfs processing. So trigger the formspec update byself
smartfs.inv[name]:show() smartfs.inv[name]:show()
@ -396,7 +395,6 @@ local function crafting_callback(state)
state.param.survival_proposal_mode = "craftable" state.param.survival_proposal_mode = "craftable"
state:get("groups_sel"):setSelected(1) state:get("groups_sel"):setSelected(1)
state:get("search"):setText("") state:get("search"):setText("")
state.param.survival_search_string = ""
end end
state.param.crafting_items_in_inventory = get_inventory_items(player) state.param.crafting_items_in_inventory = get_inventory_items(player)
local craftable = cache.crecipes.get_recipes_craftable(player, state.param.crafting_items_in_inventory) local craftable = cache.crecipes.get_recipes_craftable(player, state.param.crafting_items_in_inventory)
@ -407,21 +405,17 @@ local function crafting_callback(state)
end) end)
-- search -- search
state:field(13.3, 4.5, 3, 0.5, "search"):setCloseOnEnter(false) local searchfield = state:field(13.3, 4.5, 3, 0.5, "search")
-- filter on input searchfield:setCloseOnEnter(false)
state:onInput(function(state, fields, player) searchfield:onKeyEnter(function(self, state, player)
local search_string = state:get("search"):getText() local filtered_list = ui_tools.search_in_list(cache.get_revealed_items(player), self:getText(), player)
if search_string ~= "" and search_string ~= state.param.survival_search_string then state.param.crafting_grouped_items = cache.get_list_grouped(filtered_list)
local filtered_list = ui_tools.search_in_list(cache.get_revealed_items(player), search_string, player) -- reset group selection if proposal mode is changed
state.param.survival_search_string = search_string if state.param.survival_proposal_mode ~= "search" then
state.param.crafting_grouped_items = cache.get_list_grouped(filtered_list) state.param.survival_proposal_mode = "search"
-- reset group selection if proposal mode is changed state:get("groups_sel"):setSelected(1)
if state.param.survival_proposal_mode ~= "search" then
state.param.survival_proposal_mode = "search"
state:get("groups_sel"):setSelected(1)
end
update_group_selection(state, true)
end end
update_group_selection(state, true)
end) end)
-- groups toggle -- groups toggle

View File

@ -114,17 +114,15 @@ local function creative_callback(state)
end) end)
-- functions -- functions
state:field(7.5, 4, 4, 0.5, "search"):setCloseOnEnter(false) local searchfield = state:field(7.5, 4, 4, 0.5, "search")
state:onInput(function(state, fields, player) searchfield:setCloseOnEnter(false)
local search_string = state:get("search"):getText() searchfield:onKeyEnter(function(self, state, player)
if search_string ~= (state.param.creative_search_string or "") then local search_string = self:getText()
local filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_all, search_string) local filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_all, search_string)
state.param.creative_grouped_items = cache.get_list_grouped(filtered_list) state.param.creative_grouped_items = cache.get_list_grouped(filtered_list)
filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_material_all, search_string) filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_material_all, search_string)
state.param.creative_search_string = search_string state.param.creative_grouped_material_items = filtered_list
state.param.creative_grouped_material_items = filtered_list update_group_selection(state, 0)
update_group_selection(state, 0)
end
end) end)
-- craftable items grid -- craftable items grid

View File

@ -455,38 +455,24 @@ function smartfs._makeState_(form, params, location, newplayer)
return res return res
end, end,
show = location._show_, show = location._show_,
-- process /apply received field value _get_element_recursive_ = function(self, field)
_sfs_process_value_ = function(self, field, value) local topfield
local cur_namespace = self:getNamespace() for z in field:gmatch("[^#]+") do
if cur_namespace == "" or cur_namespace == string.sub(field, 1, string.len(cur_namespace)) then -- Check current namespace topfield = z
local rel_fieldname = string.sub(field, string.len(cur_namespace)+1) --cut the namespace break
if self._ele[rel_fieldname] then -- direct top-level assignment
self._ele[rel_fieldname].data.value = value
else
for elename, eledef in pairs(self._ele) do
if eledef.getContainerState then -- element supports sub-states
eledef:getContainerState():_sfs_process_value_(field, value)
end
end
end
end end
end, local element = self._ele[topfield]
-- process action for received field if supported if element and field == topfield then
_sfs_process_action_ = function(self, field, value, player) return element
local cur_namespace = self:getNamespace() elseif element then
if cur_namespace == "" or cur_namespace == string.sub(field, 1, string.len(cur_namespace)) then -- Check current namespace if element._getSubElement_ then
local rel_fieldname = string.sub(field, string.len(cur_namespace)+1) --cut the namespace local rel_field = string.sub(field, string.len(topfield)+2)
if self._ele[rel_fieldname] then -- direct top-level assignment return element:_getSubElement_(rel_field)
if self._ele[rel_fieldname].submit then
self._ele[rel_fieldname]:submit(value, player)
end
else else
for elename, eledef in pairs(self._ele) do return element
if eledef.getContainerState then -- element supports sub-states
eledef:getContainerState():_sfs_process_action_(field, value, player)
end
end
end end
else
return nil
end end
end, end,
-- process onInput hook for the state -- process onInput hook for the state
@ -503,16 +489,28 @@ function smartfs._makeState_(form, params, location, newplayer)
end, end,
-- Receive fields and actions from formspec -- Receive fields and actions from formspec
_sfs_on_receive_fields_ = function(self, player, fields) _sfs_on_receive_fields_ = function(self, player, fields)
--print("smartfs: fields received", dump(fields))
-- fields assignment -- fields assignment
for field, value in pairs(fields) do for field, value in pairs(fields) do
self:_sfs_process_value_(field, value) local element = self:_get_element_recursive_(field)
if element then
element:setValue(value)
end
end end
-- process onInput hooks -- process onInput hooks
self:_sfs_process_oninput_(fields, player) self:_sfs_process_oninput_(fields, player)
-- do actions -- do actions
for field, value in pairs(fields) do for field, value in pairs(fields) do
self:_sfs_process_action_(field, value, player) local element = self:_get_element_recursive_(field)
if element and element.submit then
element:submit(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 end
-- handle quit/exit -- handle quit/exit
if not fields.quit and not self.closed and not self.obsolete then if not fields.quit and not self.closed and not self.obsolete then
@ -632,6 +630,9 @@ function smartfs._makeState_(form, params, location, newplayer)
return "" return ""
end end
end, end,
setValue = function(self, value)
self.data.value = value
end,
} }
ele.data.visible = true --visible by default ele.data.visible = true --visible by default
@ -871,7 +872,7 @@ smartfs.element("button", {
self._click = func self._click = func
end, end,
setText = function(self,text) setText = function(self,text)
self.data.value = text self:setValue(text)
end, end,
getText = function(self) getText = function(self)
return self.data.value return self.data.value
@ -964,7 +965,7 @@ smartfs.element("label", {
"]" "]"
end, end,
setText = function(self,text) setText = function(self,text)
self.data.value = text self:setValue(text)
end, end,
getText = function(self) getText = function(self)
return self.data.value return self.data.value
@ -1019,7 +1020,7 @@ smartfs.element("field", {
end end
end, end,
setText = function(self,text) setText = function(self,text)
self.data.value = text self:setValue(text)
end, end,
getText = function(self) getText = function(self)
return self.data.value return self.data.value
@ -1042,7 +1043,15 @@ smartfs.element("field", {
end, end,
getCloseOnEnter = function(self) getCloseOnEnter = function(self)
return self.close_on_enter return self.close_on_enter
end 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", { smartfs.element("image", {
@ -1076,7 +1085,7 @@ smartfs.element("image", {
if self.data.imgtype == "background" then if self.data.imgtype == "background" then
self.data.background = text self.data.background = text
else else
self.data.value = text self:setValue(text)
end end
end, end,
getImage = function(self) getImage = function(self)
@ -1105,8 +1114,6 @@ smartfs.element("checkbox", {
";" .. boolToStr(self.data.value) .."]" ";" .. boolToStr(self.data.value) .."]"
end, end,
submit = function(self, field, player) submit = function(self, field, player)
-- self.data.value already set by value transfer, but as string
self.data.value = minetest.is_yes(field)
-- call the toggle function if defined -- call the toggle function if defined
if self._tog then if self._tog then
self:_tog(self.root, player) self:_tog(self.root, player)
@ -1379,8 +1386,10 @@ smartfs.element("container", {
end, end,
getContainerState = function(self) getContainerState = function(self)
return self._state return self._state
end end,
-- submit is handled by framework for elements with getContainerState _getSubElement_ = function(self, field)
return self:getContainerState():_get_element_recursive_(field)
end,
}) })
return smartfs return smartfs