Skip to content

Commit

Permalink
Improve sorting and naming of options in the AddNode popup
Browse files Browse the repository at this point in the history
This is done by assigning a "result_quality" score to each item based on how it matched. In the Add Node Panel this is used to sort the results. E.g. results that START with the filter are rated higher then those that just contain it.
  • Loading branch information
Jowan-Spooner committed Oct 24, 2024
1 parent 5bbe7f5 commit fe83283
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 23 deletions.
44 changes: 42 additions & 2 deletions material_maker/tools/library_manager/library.gd
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,54 @@ func get_item(lib_name : String):
return { name=i.tree_item, item=i, icon=library_icons[i.tree_item] }
return null

## Returns the items of this library that match the filter.
## Also assigns each returned item a score of how well it matched.
func get_items(filter : String, disabled_sections : Array, aliased_items : Array) -> Array:
var array : Array = []
filter = filter.to_lower().strip_edges()
for i in library_items:
if filter == "" or i.tree_item.to_lower().find(filter) != -1 or aliased_items.find(i.tree_item) != -1:
var include := true
var result_quality := 1.0
if filter:
include = false
if i.display_name.to_lower().begins_with(filter) or " "+filter in i.display_name.to_lower():
include = true

if not include:
include = true
result_quality = 0.8
for word in filter.split(" "):
if not word in i.display_name.to_lower():
include = false

if (not include) and i.has("name"):
include = true
result_quality = 0.8
for word in filter.split(" "):
if not word in i.name.to_lower():
include = false

if not include:
result_quality = 0.6
for alias_dict in aliased_items:
for key in alias_dict:
if key == i.tree_item and filter in alias_dict[key]:
if alias_dict[key].begins_with(filter) or ","+filter in alias_dict[key]:
result_quality = 0.9
include = true

if not include:
include = true
result_quality = 0.4
for word in filter.split(" "):
if not word in i.tree_item.to_lower():
include = false

if include:
var slash_pos = i.tree_item.find("/")
var section_name = i.tree_item.left(slash_pos) if slash_pos != -1 else i.tree_item
if disabled_sections.find(section_name) == -1:
array.push_back({ name=i.tree_item, item=i, icon=library_icons[i.tree_item] })
array.push_back({ name=i.tree_item, item=i, icon=library_icons[i.tree_item], quality=result_quality})
return array

func generate_node_sections(node_sections : Dictionary) -> void:
Expand Down
22 changes: 13 additions & 9 deletions material_maker/tools/library_manager/library_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,22 @@ func get_item(item_name : String):
return item
return null

func get_items(filter : String, sorted = false) -> Array:
var array : Array = []
var aliased_items = []
for al in [ base_item_aliases, user_item_aliases ]:
for a in al.keys():
if al[a].find(filter) != -1 and aliased_items.find(a) == -1:
aliased_items.push_back(a)
func get_items(filter : String, sorted := false) -> Array:
var array: Array = []
var aliased_items := [base_item_aliases, user_item_aliases]

for li in get_child_count():
var l = get_child(li)
if disabled_libraries.find(l.library_path) == -1:
for i in l.get_items(filter, disabled_sections, aliased_items):
i.library_index = li
array.push_back(i)

if sorted:
var sorted_array : Array = []
var sorted_array: Array = []
for i in array:
var u1 = item_usage[i.name] if item_usage.has(i.name) else 0
var inserted = false
var inserted := false
for p in sorted_array.size():
var i2 = sorted_array[p]
var u2 = item_usage[i2.name] if item_usage.has(i2.name) else 0
Expand All @@ -120,8 +118,14 @@ func get_items(filter : String, sorted = false) -> Array:
if !inserted:
sorted_array.push_back(i)
array = sorted_array
var idx := 0
for item in array:
item["idx"] = idx
idx += 1

return array


func save_library_list() -> void:
var library_list = []
for i in range(2, get_child_count()):
Expand Down
30 changes: 18 additions & 12 deletions material_maker/windows/add_node_popup/add_node_popup.gd
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func show_popup(node_name : String = "", slot : int = -1, slot_type : int = -1,
b.enable()
if filter.text != "":
filter.text = ""
update_list(filter.text)
update_list(filter.text)
filter.grab_focus()


Expand Down Expand Up @@ -163,7 +163,9 @@ func update_list(filter_text : String = "") -> void:

%List.clear()
var idx := 0
for i in library_manager.get_items(filter_text, true):
var items: Array = library_manager.get_items(filter_text, true)
items.sort_custom(func(a,b): return a.idx < b.idx if a.quality == b.quality else a.quality > b.quality)
for i in items:
var obj = i.item
if not obj.has("type"):
continue
Expand All @@ -172,14 +174,18 @@ func update_list(filter_text : String = "") -> void:
var section = obj.tree_item.get_slice("/", 0)
var color : Color = get_node("/root/MainWindow/NodeLibraryManager").get_section_color(section)
color = color.lerp(get_theme_color("font_color", "Label"), 0.5)

%List.add_item(obj.display_name, i.icon)
#print(i)
var _name = obj.display_name
_name = obj.tree_item# + "("+str(i.quality)+")" + " ("+str(i.idx)+")"
#if obj.has("shortdesc")

%List.add_item(_name, i.icon)
%List.set_item_custom_fg_color(idx, color)
%List.set_item_metadata(idx, i)
%List.set_item_tooltip_enabled(idx, false)

idx += 1

%List.select(0)
%List.ensure_current_is_visible()

Expand All @@ -192,14 +198,19 @@ func _unhandled_input(event) -> void:
func _on_filter_gui_input(event: InputEvent) -> void:
if event.is_action("ui_down"):
%List.grab_focus()
%List.select(1)
%List.select(0)


func _on_list_gui_input(event: InputEvent) -> void:
if event.is_action("ui_up"):
if not %List.item_count or %List.is_selected(0):
%Filter.grab_focus()


if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
var idx: int = %List.get_item_at_position(%List.get_local_mouse_position(), true)
if idx != -1:
_on_list_item_activated(idx)


func get_list_drag_data(m_position):
var data = %List.get_item_metadata(%List.get_item_at_position(m_position))
Expand All @@ -210,11 +221,6 @@ func get_list_drag_data(m_position):
return data.item.tree_item



func _on_list_item_clicked(index: int, at_position:= Vector2(), mouse_button_index:= 0) -> void:
pass


func _on_list_item_activated(index: int) -> void:
var data = %List.get_item_metadata(index)
add_node(data.item)
Expand Down

0 comments on commit fe83283

Please sign in to comment.