breakfilemaker.gd 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. extends Control
  2. const FIXED_POINT_COUNT := 2
  3. var points := [] # Stores Vector2 points
  4. var point_size := 10
  5. var dragged_point_index := -1
  6. signal automation_updated(values: Array)
  7. func _ready():
  8. set_process_unhandled_input(true)
  9. # these two are fixed: only Y-movable, not deletable
  10. var window = get_window().size
  11. points.append(Vector2(0, (window.y / 2) - 22))
  12. points.append(Vector2(window.x, (window.y / 2) - 22))
  13. func _unhandled_input(event):
  14. var pos = get_local_mouse_position()
  15. if event is InputEventMouseButton:
  16. # --- double-click: delete only if not fixed, otherwise add new --
  17. if event.button_index == MOUSE_BUTTON_LEFT and event.double_click:
  18. var idx = get_point_at_pos(pos)
  19. if idx >= FIXED_POINT_COUNT:
  20. points.remove_at(idx)
  21. elif idx == -1:
  22. points.append(pos)
  23. queue_redraw()
  24. # --- begin drag on press ---
  25. elif event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
  26. dragged_point_index = get_point_at_pos(pos)
  27. # --- end drag on release ---
  28. elif event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
  29. dragged_point_index = -1
  30. elif event is InputEventMouseMotion and dragged_point_index != -1:
  31. # if it’s one of the first two, constrain to Y only:
  32. if dragged_point_index < FIXED_POINT_COUNT:
  33. points[dragged_point_index].y = clamp(pos.y, 0, get_window().size.y -45)
  34. else:
  35. points[dragged_point_index].x = clamp(pos.x, 0, get_window().size.x)
  36. points[dragged_point_index].y = clamp(pos.y, 0, get_window().size.y - 45)
  37. queue_redraw()
  38. func _draw():
  39. var sorted = []
  40. sorted = points.duplicate()
  41. sorted.sort_custom(sort_points)
  42. for i in range(points.size() - 1):
  43. draw_dashed_line(sorted[i], sorted[i + 1], Color(0.1, 0.1, 0.1, 0.6), 2.0, 6.0, true, true)
  44. for point in points:
  45. draw_rect(Rect2(point.x - (point_size / 2), point.y - (point_size / 2), point_size, point_size), Color(0.1, 0.1, 0.1, 0.8))
  46. func sort_points(a, b):
  47. return a.x < b.x
  48. func get_point_at_pos(pos: Vector2) -> int:
  49. # find any point within radius + padding
  50. for i in range(points.size()):
  51. if points[i].distance_to(pos) <= point_size + 2:
  52. return i
  53. return -1
  54. func emit_automation_data():
  55. emit_signal("automation_updated", points)
  56. func _on_save_automation_button_down() -> void:
  57. emit_automation_data()
  58. func read_automation(stored_points: Array):
  59. points = stored_points.duplicate()
  60. func reset_automation():
  61. points = []
  62. var window = get_window().size
  63. points.append(Vector2(0, (window.y / 2) - 22))
  64. points.append(Vector2(window.x, (window.y / 2) - 22))