Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Suleymanov |
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:
- Point-in and point-out handles visibility and control;
- Add point between the points, along with the curve (currently, it adds at mouse location, but not between the points);
- Authentic close curve (Path2d only puts the last point on top of the first point, but they are not attached which is a problem);
- 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)