min_max_sliders.gd 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. extends GraphNode
  2. @export var min_gap: float = 0.5 # editable value in inspector for the minimum gap between min and max
  3. func _ready() -> void:
  4. var sliders := _get_all_hsliders(self) #finds all sliders
  5. #links sliders to this script
  6. for slider in sliders:
  7. slider.value_changed.connect(_on_slider_value_changed.bind(slider))
  8. func _get_all_hsliders(node: Node) -> Array:
  9. #moves through all children recusively to find nested sliders
  10. var result: Array = []
  11. for child in node.get_children():
  12. if child is HSlider:
  13. result.append(child)
  14. elif child.has_method("get_children"):
  15. result += _get_all_hsliders(child)
  16. return result
  17. func _on_slider_value_changed(value: float, changed_slider: HSlider) -> void:
  18. #checks if the slider moved has min or max meta data
  19. var is_min := changed_slider.has_meta("min")
  20. var is_max := changed_slider.has_meta("max")
  21. #if not exits function
  22. if not is_min and not is_max:
  23. return
  24. var sliders := _get_all_hsliders(self)
  25. for other_slider in sliders:
  26. if other_slider == changed_slider:
  27. continue
  28. if is_min and other_slider.has_meta("max"):
  29. var max_value: float = other_slider.value
  30. if changed_slider.value > max_value - min_gap:
  31. changed_slider.value = max_value - min_gap
  32. elif is_max and other_slider.has_meta("min"):
  33. var min_value: float = other_slider.value
  34. if changed_slider.value < min_value + min_gap:
  35. changed_slider.value = min_value + min_gap