filter: add get_keyword method to filter, use them for searches instead of group name
get_keyword is now responsible if a group is visible or not
This commit is contained in:
parent
c07c4fcabd
commit
09490fbd5f
7
API.md
7
API.md
@ -31,6 +31,7 @@ smart_inventory.filter.register_filter({
|
||||
name = string,
|
||||
check_item_by_def = function,
|
||||
get_description = function,
|
||||
get_keyword = function,
|
||||
})
|
||||
```
|
||||
- name - unique filter name
|
||||
@ -40,8 +41,9 @@ smart_inventory.filter.register_filter({
|
||||
- string -> dimension, steps splitted by ":" (`a:b:c:d results in filtername, filtername:a, filtername:a:b, filtername:a:b:c, filtername:a:b:c:d`)
|
||||
- key/value table -> multiple groups assignment. Values could be dimensions as above (`{a,b} results in filtername, filtername:a, filtername:b`)
|
||||
- nil -> no group assingment by this filter
|
||||
- shortdesc_func(fltobj, filterstring, textstring) - optional - get human readable description for the dimension string (`filtername:a:b:c`). "false" means the group should be ignored. Usefull so skip some dimension characteristics
|
||||
- if no function is given the descriptions can be added to smart_inventory.txt[filterstring].label See classify_description.lua. If nothing defined the filterstring will be displayed
|
||||
- get_description(fltobj, group) - optional - get human readable description for the dimension string (`filtername:a:b:c`)
|
||||
- get_keyword(fltobj, group) - get string that should be used for searches or nil if the group should not be matched
|
||||
|
||||
|
||||
### Filter Object methods
|
||||
|
||||
@ -49,3 +51,4 @@ smart_inventory.filter.get(name) get filter object by registered name. Ret
|
||||
- fltobj:check_item_by_name(itemname) classify by itemname (wrapper for check_item_by_def)
|
||||
- fltobj:check_item_by_def(def) classify by item definition
|
||||
- fltobj:get_description(group) get group description
|
||||
- fltobj:get_keyword(group) get string that should be used for searches
|
||||
|
@ -250,17 +250,9 @@ function cache.add_to_cache_group(group_name, itemdef, flt, parent, parent_value
|
||||
childs = {},
|
||||
}
|
||||
|
||||
if flt then
|
||||
group.group_desc = flt:get_description(group)
|
||||
else
|
||||
group.group_desc = group_name
|
||||
end
|
||||
if not group.group_desc then
|
||||
if parent_ref then
|
||||
parent_ref.childs[group_name] = nil
|
||||
end
|
||||
return
|
||||
end
|
||||
group.group_desc = flt:get_description(group)
|
||||
group.keyword = flt:get_keyword(group)
|
||||
|
||||
cache.cgroups[group_name] = group
|
||||
end
|
||||
cache.cgroups[group_name].items[itemdef.name] = itemdef
|
||||
|
@ -1,11 +1,12 @@
|
||||
local txt = smart_inventory.txt
|
||||
local txt_usage = minetest.setting_get("smart_inventory_friendly_group_names") or false
|
||||
local txt_usage = minetest.setting_get("smart_inventory_friendly_group_names") or true
|
||||
|
||||
--------------------------------------------------------------
|
||||
-- Filter class
|
||||
--------------------------------------------------------------
|
||||
local filter_class = {}
|
||||
filter_class.__index = filter_class
|
||||
|
||||
function filter_class:check_item_by_name(itemname)
|
||||
if minetest.registered_items[itemname] then
|
||||
return self:check_item_by_def(minetest.registered_items[itemname])
|
||||
@ -25,7 +26,7 @@ function filter_class:_get_description(group)
|
||||
else
|
||||
ret_desc = group.name
|
||||
end
|
||||
if not txt_usage or ret_desc == false then
|
||||
if txt_usage then
|
||||
return ret_desc
|
||||
else
|
||||
return group.name
|
||||
@ -33,6 +34,25 @@ function filter_class:_get_description(group)
|
||||
end
|
||||
filter_class.get_description = filter_class._get_description
|
||||
|
||||
|
||||
function filter_class:_get_keyword(group)
|
||||
-- parent exists - return the top-level information only
|
||||
if group.parent and group.parent.childs[group.name] and tonumber(group.parent.childs[group.name]) == nil then
|
||||
return group.parent.childs[group.name]
|
||||
end
|
||||
end
|
||||
|
||||
function filter_class:_get_keyword_groupname(group) -- a variant for get_keyword, can be assigned in filter definition
|
||||
if txt_usage then
|
||||
return group.name.." "..group.group_desc
|
||||
else
|
||||
return group.name
|
||||
end
|
||||
end
|
||||
|
||||
filter_class.get_keyword = filter_class._get_keyword
|
||||
|
||||
|
||||
local filter = {}
|
||||
filter.registered_filter = {}
|
||||
|
||||
@ -49,6 +69,9 @@ function filter.register_filter(def)
|
||||
filter.registered_filter[def.name] = def
|
||||
end
|
||||
|
||||
--------------------------------------------------------------
|
||||
-- Filter group
|
||||
--------------------------------------------------------------
|
||||
filter.register_filter({
|
||||
name = "group",
|
||||
check_item_by_def = function(self, def)
|
||||
@ -68,6 +91,7 @@ filter.register_filter({
|
||||
else
|
||||
mk = k
|
||||
end
|
||||
|
||||
-- stack wear related value
|
||||
if k == "armor_use" then
|
||||
mv = tostring(math.floor(v / 65535 * 10000 + 0.5)/100).." %"
|
||||
@ -78,12 +102,29 @@ filter.register_filter({
|
||||
mv = true
|
||||
end
|
||||
|
||||
if v ~= 0 then
|
||||
-- replacements
|
||||
if mk == "group:customnode:default" then
|
||||
mk = "group:customnode"
|
||||
end
|
||||
|
||||
-- apply
|
||||
if v ~= 0 and not (
|
||||
mk == "group:armor:count" or --internally used only
|
||||
mk == "group:leafdecay" ) -- represented by group:leaves
|
||||
then
|
||||
ret[mk] = mv
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end,
|
||||
get_keyword = function(self, group)
|
||||
local keyword = self:_get_keyword(group)
|
||||
if txt_usage and keyword then
|
||||
return keyword.." "..group.group_desc
|
||||
else
|
||||
return keyword
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
filter.register_filter({
|
||||
@ -104,7 +145,8 @@ filter.register_filter({
|
||||
name = "transluc",
|
||||
check_item_by_def = function(self, def)
|
||||
return def.sunlight_propagates
|
||||
end
|
||||
end,
|
||||
get_keyword = filter_class._get_keyword_groupname
|
||||
})
|
||||
|
||||
filter.register_filter({
|
||||
@ -113,7 +155,8 @@ filter.register_filter({
|
||||
if def.light_source and def.light_source ~= 0 then
|
||||
return def.light_source
|
||||
end
|
||||
end
|
||||
end,
|
||||
get_keyword = filter_class._get_keyword_groupname
|
||||
})
|
||||
|
||||
filter.register_filter({
|
||||
@ -124,7 +167,8 @@ filter.register_filter({
|
||||
def.on_metadata_inventory_put then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end,
|
||||
get_keyword = filter_class._get_keyword_groupname
|
||||
})
|
||||
|
||||
--[[ does it sense to filter them? I cannot define the human readable groups for them
|
||||
@ -154,7 +198,8 @@ filter.register_filter({
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
get_keyword = filter_class._get_keyword_groupname
|
||||
})
|
||||
|
||||
filter.register_filter({
|
||||
@ -166,7 +211,8 @@ filter.register_filter({
|
||||
return tostring(change)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
get_keyword = filter_class._get_keyword_groupname
|
||||
})
|
||||
|
||||
filter.register_filter({
|
||||
@ -178,7 +224,8 @@ filter.register_filter({
|
||||
return tostring(change)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
get_keyword = filter_class._get_keyword_groupname
|
||||
})
|
||||
|
||||
filter.register_filter({
|
||||
@ -211,11 +258,11 @@ filter.register_filter({
|
||||
]]
|
||||
return rettab
|
||||
end,
|
||||
get_description = function(self, group)
|
||||
get_keyword = function(self, group)
|
||||
if group.name == "max_drop_level" or group.name == "full_punch_interval" or group.name == "damage" then
|
||||
return false
|
||||
return nil
|
||||
else
|
||||
return self:_get_description(group)
|
||||
return self:_get_keyword(group)
|
||||
end
|
||||
end
|
||||
})
|
||||
@ -224,7 +271,8 @@ filter.register_filter({
|
||||
name = "armor",
|
||||
check_item_by_def = function(self, def)
|
||||
return def.armor_groups
|
||||
end
|
||||
end,
|
||||
get_keyword = filter_class._get_keyword_groupname
|
||||
})
|
||||
|
||||
-- Group assignment done in cache framework internally
|
||||
@ -245,7 +293,14 @@ filter.register_filter({
|
||||
else
|
||||
return group.name
|
||||
end
|
||||
end,
|
||||
get_keyword = function(self, group)
|
||||
local itemname = group.name:sub(12)
|
||||
if minetest.registered_items[itemname] then
|
||||
return minetest.registered_items[itemname].description
|
||||
end
|
||||
end
|
||||
|
||||
})
|
||||
|
||||
----------------
|
||||
|
@ -6,7 +6,6 @@ return {
|
||||
["group:armor"] = { label = "Rüstung" },
|
||||
["group:armor:state"] = { label = "Rüstungsstatus" },
|
||||
["group:armor:level"] = { label = "Level der Rüstung" },
|
||||
["group:armor:count"] = { label = false },
|
||||
["group:physics:jump"] = { label = "Sprunghöhe" },
|
||||
["group:physics:speed"] = { label = "Geschwindigkeit" },
|
||||
["group:physics:gravity"] = { label = "Erdanziehung" },
|
||||
@ -22,11 +21,9 @@ return {
|
||||
|
||||
-- http://dev.minetest.net/Groups
|
||||
-- http://dev.minetest.net/Groups/Custom_groups
|
||||
["group"] = { label = false },
|
||||
["group:level"] = { label = "Wertvoll" },
|
||||
["group:oddly_breakable_by_hand"] = { label = "Ohne Werkzeug abbaubar" },
|
||||
["group:dig_immediate"] = { label = false },
|
||||
["group:dig_immediate:2"] = { label = "Schnell abbaubar" },
|
||||
["group:dig_immediate"] = { label = "Schnell abbaubar" },
|
||||
["group:dig_immediate:3"] = { label = "Sofort abbaubar" },
|
||||
["group:disable_jump"] = { label = "Klebrig" },
|
||||
["group:bouncy"] = { label = "Federnd" },
|
||||
@ -40,10 +37,9 @@ return {
|
||||
["group:choppy"] = { label = "Abgehackt" },
|
||||
["group:explody"] = { label = "Explosiv" },
|
||||
["group:flammable"] = { label = "Brennbar" },
|
||||
["group:not_in_creative_inventory"] = { label = false },
|
||||
["group:not_in_creative_inventory"] = { label = "Versteckt" },
|
||||
|
||||
["group:tree"] = { label = "Baum" },
|
||||
["group:leafdecay"] = { label = false }, -- represented by group:leaves
|
||||
["group:flora"] = { label = "Flora" },
|
||||
["group:soil"] = { label = "Erde" },
|
||||
["group:soil:1"] = { label = "Ackerboden" },
|
||||
@ -72,8 +68,6 @@ return {
|
||||
["group:panel"] = {label = "Paneel"},
|
||||
["group:slope"] = {label = "Neigung"},
|
||||
["group:customnode"] = {label = "Dekorativer Block"},
|
||||
["group:customnode:default"] = {label = false},
|
||||
|
||||
|
||||
["type:tool"] = { label = "Werkzeuge" },
|
||||
["type:node"] = { label = "Blöcke" },
|
||||
@ -83,12 +77,11 @@ return {
|
||||
["mod"] = { label = "Mod" },
|
||||
["transluc"] = { label = "Lichtdurchlässig" },
|
||||
["vessel"] = { label = "Behälter" },
|
||||
["eatable"] = { label = "Nahrung" },
|
||||
["food"] = { label = "Nahrung" },
|
||||
["shape"] = { label = "Geformt"},
|
||||
["light"] = { label = "Lichtquelle"},
|
||||
["ingredient"] = { label = "Erzeugt aus"},
|
||||
|
||||
["tool"] = {label = false},
|
||||
["tool:full_punch_interval"] = { label = "Verwendungsinterval" },
|
||||
["tool:max_drop_level"] = { label = "Max drop Level" },
|
||||
-- ["tool:groupcaps:fleshy"] = { label = "??" },
|
||||
|
@ -6,7 +6,6 @@ return {
|
||||
["group:armor"] = { label = "Armor" },
|
||||
["group:armor:state"] = { label = "Armor state" },
|
||||
["group:armor:level"] = { label = "Armor level" },
|
||||
["group:armor:count"] = { label = false },
|
||||
["group:physics:jump"] = { label = "Jump high" },
|
||||
["group:physics:speed"] = { label = "Walking speed" },
|
||||
["group:physics:gravity"] = { label = "Gravity" },
|
||||
@ -22,11 +21,9 @@ return {
|
||||
|
||||
-- http://dev.minetest.net/Groups
|
||||
-- http://dev.minetest.net/Groups/Custom_groups
|
||||
["group"] = { label = false },
|
||||
["group:level"] = { label = "Valuable" },
|
||||
["group:oddly_breakable_by_hand"] = { label = "Oddly breakable" },
|
||||
["group:dig_immediate"] = { label = false },
|
||||
["group:dig_immediate:2"] = { label = "Fast diggable" },
|
||||
["group:dig_immediate"] = { label = "Fast diggable" },
|
||||
["group:dig_immediate:3"] = { label = "Immediate diggable" },
|
||||
["group:disable_jump"] = { label = "Sticky" },
|
||||
["group:bouncy"] = { label = "Bouncy" },
|
||||
@ -40,10 +37,9 @@ return {
|
||||
["group:choppy"] = { label = "Choppy" },
|
||||
["group:explody"] = { label = "Explosive" },
|
||||
["group:flammable"] = { label = "Flammable" },
|
||||
["group:not_in_creative_inventory"] = { label = false },
|
||||
["group:not_in_creative_inventory"] = { label = "Hidden" },
|
||||
|
||||
["group:tree"] = { label = "Tree" },
|
||||
["group:leafdecay"] = { label = false }, -- represented by group:leaves
|
||||
["group:flora"] = { label = "Flora" },
|
||||
["group:soil"] = { label = "Soil" },
|
||||
["group:soil:1"] = { label = "Basic soil" },
|
||||
@ -72,7 +68,6 @@ return {
|
||||
["group:panel"] = {label = "Panel"},
|
||||
["group:slope"] = {label = "Slope"},
|
||||
["group:customnode"] = {label = "Decorative node"},
|
||||
["group:customnode:default"] = {label = false},
|
||||
|
||||
["type:tool"] = { label = "Tools" },
|
||||
["type:node"] = { label = "Nodes" },
|
||||
@ -87,7 +82,6 @@ return {
|
||||
["light"] = { label = "Light source"},
|
||||
["ingredient"] = { label = "Crafted with"},
|
||||
|
||||
["tool"] = {label = false},
|
||||
["tool:full_punch_interval"] = { label = "Punch interval" },
|
||||
["tool:max_drop_level"] = { label = "Max drop level" },
|
||||
-- ["tool:groupcaps:fleshy"] = { label = "??" },
|
||||
|
@ -98,11 +98,7 @@ local cache = smart_inventory.cache
|
||||
table.insert(filtered_list, entry)
|
||||
else
|
||||
for _, cgroup in pairs(entry.citem.cgroups) do
|
||||
local prefix_end_pos = cgroup.name:find(":")
|
||||
if string.find(cgroup.name:lower(), search_string, prefix_end_pos) then
|
||||
table.insert(filtered_list, entry)
|
||||
break
|
||||
elseif string.find(cgroup.group_desc:lower(), search_string) then
|
||||
if cgroup.keyword and string.find(cgroup.keyword:lower(), search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
break
|
||||
end
|
||||
@ -129,7 +125,7 @@ end
|
||||
function ui_tools.get_tight_groups(cgroups)
|
||||
local out_list = {}
|
||||
for group1, groupdef1 in pairs(cgroups) do
|
||||
if not string.find(group1, "ingredient:") then
|
||||
if not string.find(group1, "ingredient:") and groupdef1.keyword then
|
||||
out_list[group1] = groupdef1
|
||||
for group2, groupdef2 in pairs(out_list) do
|
||||
if string.len(group1) > string.len(group2) and
|
||||
|
Loading…
x
Reference in New Issue
Block a user