JHRGB/Camera2D.gd
2023-12-28 12:10:07 +00:00

40 lines
1.2 KiB
GDScript

extends Camera2D
var last_mouse_position = Vector2.ZERO
func _ready():
set_process_input(true)
func _input(event):
print(event.as_text())
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_RIGHT and event.pressed:
last_mouse_position = get_global_mouse_position()
elif event.button_index == MOUSE_BUTTON_RIGHT and not event.pressed:
last_mouse_position = Vector2.ZERO
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN and event.pressed:
zoom_towards_mouse(Vector2(0.9, 0.9))
elif event.button_index == MOUSE_BUTTON_WHEEL_UP and event.pressed:
zoom_towards_mouse(Vector2(1.1, 1.1))
elif event is InputEventMouseMotion:
if last_mouse_position != Vector2.ZERO:
var mouse_motion = event.relative
var drag_offset = mouse_motion / zoom
position -= drag_offset
last_mouse_position = get_global_mouse_position()
# Macbook touchpad
elif event is InputEventPanGesture:
position+=event.delta
elif event is InputEventMagnifyGesture:
zoom_towards_mouse(Vector2(event.factor,event.factor))
func zoom_towards_mouse(zoom_factor):
var previous_mouse_position := get_local_mouse_position()
zoom *= zoom_factor
var diff = previous_mouse_position - get_local_mouse_position()
position += diff