ItemList.gd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. @tool
  2. extends ItemList
  3. const ITEMS_FOLDER = Path.ItemDataRsc
  4. var resources: Array[Resource] = []
  5. func _ready():
  6. if not PluginUtil.is_part_of_edited_scene(self):
  7. refresh()
  8. var inspector = EditorInterface.get_inspector()
  9. inspector.connect("property_edited", property_edited)
  10. inspector.get_edited_object()
  11. func property_edited(_property: String):
  12. # Only update the resource that changed
  13. var inspected_object = EditorInterface.get_inspector().get_edited_object()
  14. if inspected_object and inspected_object.resource_path.contains(ITEMS_FOLDER):
  15. for i in resources.size():
  16. var resource = resources[i]
  17. print(resource.resource_path, inspected_object.resource_path)
  18. if resource.resource_path == inspected_object.resource_path:
  19. resources[i] = ResourceLoader.load(resource.resource_path)
  20. showResources()
  21. return
  22. func refresh():
  23. load_resources()
  24. showResources()
  25. func filterResource(resource: Resource, rawQuery: String):
  26. var query = rawQuery.to_lower()
  27. var nameMatches = resource.name.to_lower().contains(query)
  28. var descriptionMatches = resource.description.to_lower().contains(query)
  29. print(resource.name, query, nameMatches, descriptionMatches)
  30. return nameMatches || descriptionMatches
  31. func showResources():
  32. var query = $"../HBoxContainer/LineEdit".text
  33. var filtered_resources: Array[Resource]
  34. if query.is_empty():
  35. filtered_resources = resources
  36. else:
  37. filtered_resources = resources.filter(func(r): return filterResource(r, query))
  38. clear()
  39. for resource in filtered_resources:
  40. var id = add_item(resource.name if resource.name else "[ Unconfigured Item ] " + resource.resource_path, resource.icon)
  41. set_item_tooltip(id, resource.description)
  42. set_item_metadata(id, resource.resource_path)
  43. func load_resources():
  44. var new_resources: Array[Resource] = []
  45. var dir = DirAccess.open(ITEMS_FOLDER)
  46. for file in dir.get_files():
  47. if file.ends_with(".tres"):
  48. new_resources.push_back(ResourceLoader.load(ITEMS_FOLDER + "/" + file))
  49. resources = new_resources
  50. func _on_item_clicked(index, at_position, mouse_button_index):
  51. var resource_path = get_item_metadata(index)
  52. EditorInterface.edit_resource(ResourceLoader.load(resource_path))
  53. func _on_line_edit_text_changed(new_text):
  54. showResources()
  55. #region create item
  56. func alert(message: String):
  57. $"../HBoxContainer2/AcceptDialog".dialog_text = message
  58. $"../HBoxContainer2/AcceptDialog".popup_centered()
  59. func _on_create_item_pressed():
  60. var textField: LineEdit = $"../HBoxContainer2/LineEdit"
  61. var text: String = textField.text
  62. if text.is_empty():
  63. alert("filename not set")
  64. return
  65. var filename: String
  66. if text.ends_with(".tres"):
  67. filename = text
  68. else:
  69. filename = text + ".tres"
  70. var item_type = $"../HBoxContainer2/ItemTypeSelector".get_value()
  71. var resource = item_type.new()
  72. var file_path = ITEMS_FOLDER + "/" + filename
  73. if FileAccess.file_exists(file_path):
  74. alert("Item exists already, choose a different filename")
  75. return
  76. var error = ResourceSaver.save(resource, file_path)
  77. if error != OK:
  78. printerr("error saving resource", file_path, error)
  79. alert("error saving resource, look in console for details")
  80. return
  81. EditorInterface.edit_resource(ResourceLoader.load(file_path))
  82. textField.text = ""
  83. refresh()
  84. $"../HBoxContainer/LineEdit".text = ""
  85. #endregion