-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrawing.tscn
76 lines (59 loc) · 2.07 KB
/
Drawing.tscn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
[gd_scene load_steps=2 format=3 uid="uid://r5o6c5lg4s1i"]
[sub_resource type="GDScript" id="GDScript_kr0f6"]
script/source = "extends CanvasLayer
var drawing = false
var path = PackedVector2Array()
var zoom_speed = 0.1
var max_zoom_out = 0.5 # Set your desired maximum zoom-out value
var base_line_width = 5 # Set your desired base line width
var camera_2d: Camera2D = null
func _ready():
set_process_input(true)
camera_2d = $Camera2D
func _input(event):
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT:
drawing = event.pressed
if drawing:
path.clear()
path.append(_get_world_mouse_position(event.position))
else:
# Finish drawing
path.append(_get_world_mouse_position(event.position))
draw_path()
elif event is InputEventMouseMotion and drawing:
path.append(_get_world_mouse_position(event.position))
draw_path()
elif event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_MIDDLE:
# Middle mouse button to pan the camera
if event.pressed:
camera_2d.set_drag_enabled(true)
else:
camera_2d.set_drag_enabled(false)
elif event is InputEventKey:
if event.keycode == KEY_I:
print(\"Zoom in\")
# Zoom in
camera_2d.zoom /= (1.0 - zoom_speed)
elif event.keycode == KEY_O:
print(\"Zoom out\")
# Zoom out
camera_2d.zoom *= 1.0 - zoom_speed
# Ensure a minimum zoom level
camera_2d.zoom = max(camera_2d.zoom, max_zoom_out)
func draw_path():
var line = Line2D.new()
# Adjust the line width inversely proportional to the camera zoom
line.width = base_line_width / camera_2d.zoom
line.points = path
add_child(line)
func _get_world_mouse_position(original_position):
# Convert the mouse position to world coordinates using the camera
var ray_origin = camera_2d.project_ray_origin(original_position)
var ray_direction = camera_2d.project_ray_normal(original_position)
var intersection = ray_origin + ray_direction * camera_2d.zfar
return intersection
"
[node name="Node2D" type="CanvasLayer"]
script = SubResource("GDScript_kr0f6")
[node name="Camera2D" type="Camera2D" parent="."]