0 votes

Dear friends in Godot, here I put together a script (thanks to kind Godot community) that enables Path2D visibility and control while in the game. Now one can see the curve, add, delete, and control the points. Though, some functionalities are missing. In case you want to add value to the script below or rewrite it top-down to encompass all functionalities for those in need, here I mention what exactly are missing:

1) Point-in and point-out handles visibility and control;
2) Add point between the points, along with the curve (currently, it adds at mouse location, but not between the points);
3) Authentic close curve (Path2d only puts the last point on top of the first point, but they are not attached which is a problem);
4) Feel free to add more features to bezier

extends Path2D

# VARIABLES
var pathColor = Color.blue
var pointColor = Color.red
var drag = false
var index = 0
enum tools {SELECT, MOVE, DELETE, CREATE}
onready var currentTool = tools.SELECT

# MOUSE INPUT FUNCTION
func _input(event):
if Input.is_action_just_pressed("MLB"):
    drag = true
    if currentTool == tools.SELECT:
        for p in curve.get_point_count():
            if curve.get_point_position(p).distance_to(get_local_mouse_position()) < 10:
                index = p
                currentTool = tools.MOVE   
                update()    

# MOVE
if event is InputEventMouseMotion and drag == true:
    if currentTool == tools.MOVE and index > -1:
        curve.set_point_position(index, curve.get_point_position(index) + event.relative)
        update()

# SELECT
if Input.is_action_just_released("MouseLeftButton"):
    drag = false
    currentTool = tools.SELECT
    update()

# CREATE
if Input.is_action_just_pressed("MouseMiddleButton"):
    currentTool = tools.CREATE
    curve.add_point(get_local_mouse_position(), Vector2(0,0), Vector2(0,0), -1)
    update()  

# DELETE
if Input.is_action_just_pressed("MouseRightButton"):
    currentTool = tools.SELECT
    curve.remove_point (index) 
    update() 


# DRAW FUNCTIONS
func _draw():

if curve.get_point_count() > 0:
    draw_polyline(curve.get_baked_points(), pathColor, 2)
for p in curve.get_point_count():
    var pos=curve.get_point_position(p) 
    draw_circle(pos, 5, pathColor)

if (currentTool == tools.SELECT or currentTool == tools.MOVE):       
    draw_circle(curve.get_point_position(index), 8 , pointColor)
in Engine by (307 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.