No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimelineArea.gd 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. tool
  2. extends ScrollContainer
  3. var _drag_drop_indicator = null
  4. # store last attempts since godot sometimes misses drop events
  5. var _is_drag_receiving = false
  6. var _last_event_button_drop_attempt = ''
  7. var _mouse_exited = false
  8. func _ready():
  9. connect("mouse_entered", self, '_on_mouse_entered')
  10. connect("mouse_exited", self, '_on_mouse_exited')
  11. connect("gui_input", self, '_on_gui_input')
  12. func can_drop_data(position, data):
  13. if (data != null and data is Dictionary and data.has("source")):
  14. if (data["source"] == "EventButton"):
  15. # position drop indicator
  16. _set_indicator_position(position)
  17. _is_drag_receiving = true
  18. _last_event_button_drop_attempt = data["event_name"]
  19. return true
  20. _remove_drop_indicator()
  21. return false
  22. func cancel_drop():
  23. _is_drag_receiving = false
  24. _last_event_button_drop_attempt = ''
  25. _remove_drop_indicator()
  26. pass
  27. func drop_data(position, data):
  28. # todo, getting timeline like this is prone to fail someday
  29. var timeline_editor = get_parent()
  30. # add event
  31. if (data["source"] == "EventButton"):
  32. var piece = timeline_editor.create_event(data["event_name"])
  33. if (piece != null and _drag_drop_indicator != null):
  34. var parent = piece.get_parent()
  35. if (parent != null):
  36. parent.remove_child(piece)
  37. parent.add_child_below_node(_drag_drop_indicator, piece)
  38. timeline_editor.indent_events()
  39. # @todo _select_item seems to be a "private" function
  40. # maybe expose it as "public" or add a public helper function
  41. # to TimelineEditor.gd
  42. timeline_editor._select_item(piece)
  43. _is_drag_receiving = false
  44. _last_event_button_drop_attempt = ''
  45. _remove_drop_indicator()
  46. func _on_mouse_exited():
  47. _mouse_exited = true
  48. func _on_mouse_entered():
  49. _mouse_exited = false
  50. func _input(event):
  51. if (event is InputEventMouseButton and is_visible_in_tree() and event.button_index == BUTTON_LEFT):
  52. if (_mouse_exited and _is_drag_receiving):
  53. cancel_drop()
  54. func _on_gui_input(event):
  55. # godot sometimes misses drop events
  56. if (event is InputEventMouseButton and event.button_index == BUTTON_LEFT):
  57. if (_is_drag_receiving):
  58. if (_last_event_button_drop_attempt != ''):
  59. drop_data(Vector2.ZERO, { "source": "EventButton", "event_name": _last_event_button_drop_attempt} )
  60. _is_drag_receiving = false
  61. _remove_drop_indicator()
  62. pass
  63. func _create_drop_indicator():
  64. _remove_drop_indicator()
  65. var timeline = get_child(0)
  66. if (timeline == null):
  67. return
  68. var indicator = ColorRect.new()
  69. indicator.name = "DropIndicator"
  70. indicator.rect_size.y = 100
  71. indicator.rect_min_size.y = 100
  72. indicator.color = Color(0.35, 0.37, 0.44) # default editor light blue
  73. indicator.mouse_filter = MOUSE_FILTER_IGNORE
  74. # add indent node like the other scene nodes have
  75. var indent = Control.new()
  76. indent.rect_min_size.x = 25
  77. indent.visible = false
  78. indent.name = "Indent"
  79. indicator.add_child(indent)
  80. var label = Label.new()
  81. label.text = "Drop here"
  82. indicator.add_child(label)
  83. timeline.add_child(indicator)
  84. _drag_drop_indicator = indicator
  85. func _remove_drop_indicator():
  86. if (_drag_drop_indicator != null):
  87. _drag_drop_indicator.get_parent().remove_child(_drag_drop_indicator)
  88. _drag_drop_indicator.queue_free()
  89. _drag_drop_indicator = null
  90. func _set_indicator_position(position):
  91. var timeline = get_child(0)
  92. if (timeline == null):
  93. return
  94. if (_drag_drop_indicator == null):
  95. _create_drop_indicator()
  96. var highest_index = 0
  97. var index = 0
  98. for child in timeline.get_children():
  99. if child.get_local_mouse_position().y > 0 and index > highest_index:
  100. highest_index = index
  101. index += 1
  102. if (_drag_drop_indicator.is_inside_tree()):
  103. timeline.move_child(_drag_drop_indicator, max(0, highest_index))
  104. pass