Last active
June 10, 2023 13:02
-
-
Save SamSuleymanov/27924888017b72c5ac82c2ac47e13bd1 to your computer and use it in GitHub Desktop.
Contribution to GODOT community. If you want to make Path2D visible and controllable in play/game mode, attach this script to the root node. Don't forget to check Emulate Touch From Mouse in Project settings / Pointing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#NOTE!!! | |
#If you are using Godot 4+, replace "onready" with "@onready", "red" with "RED" and "update()" with "queue_redraw()" | |
extends Node2D | |
onready var path = get_node("Path2D") | |
var idx = 0 | |
var c = Color.red | |
func _input(event): | |
if event is InputEventScreenTouch: | |
#SELECT POINTS | |
for p in path.curve.get_point_count(): | |
if path.curve.get_point_position(p).distance_to(event.position) < 40: | |
idx = p | |
update() | |
#SELECT HANDLES | |
var a = path.curve.get_point_position(p) - path.curve.get_point_out(p) | |
var b = path.curve.get_point_position(p) - path.curve.get_point_in(p) | |
if a.distance_to(event.position) < 40: | |
idx = p | |
if b.distance_to(event.position) < 40: | |
idx = p | |
if event is InputEventScreenDrag: | |
#DRAG POINTS | |
for p in path.curve.get_point_count(): | |
if path.curve.get_point_position(p).distance_to(event.position) < 40 : | |
path.curve.set_point_position(idx, path.curve.get_point_position(idx) + event.relative) | |
update() | |
#DRAG HANDLES | |
var a = path.curve.get_point_position(p) - path.curve.get_point_out(p) | |
var b = path.curve.get_point_position(p) - path.curve.get_point_in(p) | |
var d = path.curve.get_point_position(p) - event.position | |
if a.distance_to(event.position) < 40: | |
path.curve.set_point_in(idx, -d + event.relative) | |
path.curve.set_point_out(idx, d + event.relative) | |
update() | |
if b.distance_to(event.position) < 40: | |
path.curve.set_point_out(idx, -d + event.relative) | |
path.curve.set_point_in(idx, d + event.relative) | |
update() | |
#DRAW | |
func _draw(): | |
draw_polyline(path.curve.get_baked_points(), c, 1, true) | |
for p in path.curve.get_point_count(): | |
if p != 0 and p != path.curve.get_point_count() - 1: | |
draw_circle(path.curve.get_point_position(p), 8, c) | |
draw_circle(path.curve.get_point_position(p) - path.curve.get_point_in(p) * -1, 5, c) | |
draw_circle(path.curve.get_point_position(p) - path.curve.get_point_out(p) * -1, 5, c) | |
var a = path.curve.get_point_position(p) - path.curve.get_point_in(p) * -1 | |
var b = path.curve.get_point_position(p) - path.curve.get_point_out(p) * -1 | |
var cp = path.curve.get_point_position(p) | |
draw_polyline(PoolVector2Array([a, cp]), c, 1, true) | |
draw_polyline(PoolVector2Array([b, cp]), c, 1, true) | |
update() | |
#NOTE!!! | |
#If you are using Godot 4+, replace "onready" with "@onready", "red" with "RED" and "update()" with "queue_redraw()" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment