search_menu.gd 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. extends PopupPanel
  2. @onready var item_container: VBoxContainer = $VBoxContainer/ScrollContainer/ItemContainer
  3. @onready var scroll_container: ScrollContainer = $VBoxContainer/ScrollContainer
  4. @onready var search_bar = $VBoxContainer/SearchBar
  5. var node_data = {} #stores node data for each node to display in help popup
  6. signal make_node(command)
  7. func _ready() -> void:
  8. #parse json
  9. var file = FileAccess.open("res://scenes/main/process_help.json", FileAccess.READ)
  10. if file:
  11. var result = JSON.parse_string(file.get_as_text())
  12. if typeof(result) == TYPE_DICTIONARY:
  13. node_data = result
  14. else:
  15. push_error("Invalid JSON")
  16. #honestly not sure what of these is actually doing things
  17. item_container.custom_minimum_size.x = scroll_container.size.x
  18. scroll_container.size_flags_vertical = Control.SIZE_EXPAND_FILL
  19. scroll_container.set("theme_override_constants/maximum_height", 400)
  20. func _on_about_to_popup() -> void:
  21. display_items("") #populate menu when needed
  22. search_bar.clear()
  23. search_bar.grab_focus()
  24. func display_items(filter: String):
  25. # Remove all existing items from the VBoxContainer
  26. for child in item_container.get_children():
  27. child.queue_free()
  28. var filters = filter.to_lower().split(" ", false)
  29. for key in node_data.keys():
  30. var item = node_data[key]
  31. var title = item.get("title", "")
  32. #filter out output node
  33. if title == "Output File":
  34. continue
  35. var category = item.get("category", "")
  36. var subcategory = item.get("subcategory", "")
  37. var short_desc = item.get("short_description", "")
  38. var command = key.replace("_", " ")
  39. # Combine all searchable text into one lowercase string
  40. var searchable_text = "%s %s %s %s %s" % [title, short_desc, category, subcategory, key]
  41. searchable_text = searchable_text.to_lower()
  42. # If filter is not empty, skip non-matches populate all other buttons
  43. if filter != "":
  44. var match_all_words = true
  45. for word in filters:
  46. if word != "" and not searchable_text.findn(word) != -1:
  47. match_all_words = false
  48. break
  49. if not match_all_words:
  50. continue
  51. var btn = Button.new()
  52. btn.size_flags_horizontal = Control.SIZE_EXPAND_FILL #make buttons wide
  53. btn.alignment = 0 #left align text
  54. btn.clip_text = true #clip off labels that are too long
  55. btn.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS #and replace with ...
  56. if category.to_lower() == "pvoc": #format node names correctly, only show the category for PVOC
  57. btn.text = "%s %s: %s - %s" % [category.to_upper(), subcategory.to_pascal_case(), title, short_desc]
  58. elif title.to_lower() == "input file":
  59. btn.text = "%s - %s" % [title, short_desc]
  60. else:
  61. btn.text = "%s: %s - %s" % [subcategory.to_pascal_case(), title, short_desc]
  62. btn.connect("pressed", Callable(self, "_on_item_selected").bind(key)) #pass key (process name) when button is pressed
  63. #apply custom focus theme for keyboard naviagation
  64. var theme := Theme.new()
  65. var style_focus := StyleBoxFlat.new()
  66. style_focus.bg_color = Color.hex(0xffffff4a)
  67. theme.set_stylebox("focus", "Button", style_focus)
  68. btn.theme = theme
  69. item_container.add_child(btn)
  70. #resize menu within certain bounds #50
  71. await get_tree().process_frame
  72. if DisplayServer.screen_get_dpi(0) >= 144:
  73. self.size.y = min((item_container.size.y + search_bar.size.y + 12) * 2, 820) #i think this will scale for retina screens but might be wrong
  74. else:
  75. self.size.y = min(item_container.size.y + search_bar.size.y + 12, 410)
  76. #highlight first button
  77. _on_search_bar_editing_toggled(true)
  78. func _on_search_bar_text_changed(new_text: String) -> void:
  79. display_items(new_text)
  80. func _on_item_selected(key: String):
  81. self.hide()
  82. make_node.emit(key) # send out signal to main patch
  83. func _on_search_bar_text_submitted(new_text: String) -> void:
  84. var button = item_container.get_child(0)
  85. if button and button is Button:
  86. button.emit_signal("pressed")
  87. func _on_search_bar_editing_toggled(toggled_on: bool) -> void:
  88. #highlight first button when editing is toggled
  89. var button = item_container.get_child(0)
  90. if toggled_on:
  91. if button and button is Button:
  92. var base_stylebox = button.get_theme_stylebox("normal", "Button")
  93. var new_stylebox = base_stylebox.duplicate()
  94. new_stylebox.bg_color = Color.hex(0xffffff4a)
  95. button.add_theme_stylebox_override("normal", new_stylebox)
  96. #skip this button on tab navigation
  97. button.focus_mode = Control.FOCUS_CLICK
  98. else:
  99. if button and button is Button:
  100. button.remove_theme_stylebox_override("normal")