Your code works, but in certain situations the mouse did not detect me at the point. Or I didn't know how to implement it correctly. But it helped me to understand how the functions of Curve2D work a bit. So put this together, much longer but safe. There are details to be polished but nothing that an if cannot solve :-p, It is necessary to implement the control nodes (in and out). I leave it as an answer in case it serves someone.
extends Node2D
onready var curve = $Path2D.curve
var drag = false
var index = -1
enum Tools { SELECT, MOVE , DELETE, CREATE }
onready var current_tool = Tools.SELECT
func _input(event):
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.is_pressed():
drag = true
if current_tool == Tools.SELECT:
for p in curve.get_point_count():
if curve.get_point_position(p).distance_to(get_local_mouse_position()) < 10:
index = p
current_tool = Tools.MOVE
update()
if event is InputEventMouseMotion and drag == true:
if current_tool == Tools.MOVE and index > -1:
curve.set_point_position(index, curve.get_point_position(index) + event.relative)
update()
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and !event.is_pressed():
drag=false
#index = null
current_tool=Tools.SELECT
update()
if event is InputEventMouseButton and event.button_index == BUTTON_RIGHT and event.is_pressed():
current_tool=Tools.CREATE
curve.add_point(get_global_mouse_position())
update()
func _draw():
if curve.get_point_count() > 0:
draw_polyline(curve.get_baked_points(), Color.red, 2)
for i in curve.get_point_count():
var pos=curve.get_point_position(i)
draw_circle(pos, 5, Color.blue)
if (current_tool == Tools.SELECT or current_tool == Tools.MOVE) and (index > -1 and index < curve.get_point_count()):
draw_circle(curve.get_point_position(index), 7 , Color.aliceblue)
print(index)