input_options.gd 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. @tool
  2. class_name ModToolInterfaceInputOptions
  3. extends ModToolInterfaceInput
  4. @export var input_options: Array[String]: set = set_input_options
  5. func set_input_options(new_options: Array[String]) -> void:
  6. input_options = new_options
  7. var input: OptionButton = get_node_or_null("%Input") as OptionButton
  8. if not input or new_options.is_empty(): return # node can't be found directly after reloading the plugin
  9. input.clear()
  10. for option in input_options:
  11. input.add_item(option)
  12. input.select(0)
  13. func get_input_value() -> int:
  14. return ($"%Input" as OptionButton).get_selected_id()
  15. func get_input_string() -> String:
  16. if get_input_value() == -1:
  17. return ""
  18. return input_options[get_input_value()]
  19. func validate(condition: bool) -> bool:
  20. # Check if input is required and empty
  21. if is_required and get_input_value() == -1:
  22. is_valid = false
  23. return false
  24. # Invalidate field if the condition is not met
  25. is_valid = condition
  26. return is_valid
  27. func _on_Input_item_selected(index: int) -> void:
  28. emit_signal("value_changed", get_input_string(), self)