From a93ba74d623bb154cb41af81e1e85c54a19e1109 Mon Sep 17 00:00:00 2001 From: studiben Date: Sun, 19 Nov 2023 15:10:31 +0100 Subject: [PATCH] Create Finger drawing In this version, I added a new _input function that checks for InputEventScreenTouch events. The handle_touch function is then called to manage touch input. The handle_mouse_button and handle_mouse_motion functions remain for handling mouse input. Signed-off-by: studiben --- Finger drawing | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Finger drawing diff --git a/Finger drawing b/Finger drawing new file mode 100644 index 0000000..aa487b7 --- /dev/null +++ b/Finger drawing @@ -0,0 +1,54 @@ +extends CanvasLayer + +var drawing = false +var path = PackedVector2Array() + +func _ready(): + set_process_input(true) + +func _input(event): + if event is InputEventMouseButton: + handle_mouse_button(event) + elif event is InputEventScreenTouch: + handle_touch(event) + elif event is InputEventMouseMotion: + handle_mouse_motion(event) + +func handle_mouse_button(event): + if event.button_index == MOUSE_BUTTON_LEFT: + drawing = event.pressed + if drawing: + path.clear() + path.append(event.position) + else: + # Finish drawing + path.append(event.position) + draw_path() + +func handle_touch(event): + if event.pressed: + drawing = true + path.clear() + path.append(event.position) + else: + drawing = false + path.append(event.position) + draw_path() + +func handle_mouse_motion(event): + if drawing: + path.append(event.position) + draw_path() + +func draw_path(): + # Remove any existing Line2D nodes + while get_child_count() > 0: + var child = get_child(0) + if child is Line2D: + remove_child(child) + + var line = Line2D.new() + line.width = 5 + line.points = path + add_child(line) +