31 lines
1.0 KiB
GDScript
31 lines
1.0 KiB
GDScript
extends Camera2D
|
|
|
|
var last_mouse_position = Vector2.ZERO
|
|
|
|
func _ready():
|
|
set_process_input(true)
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_MIDDLE and event.pressed:
|
|
last_mouse_position = get_global_mouse_position()
|
|
elif event.button_index == MOUSE_BUTTON_MIDDLE 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()
|
|
|
|
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
|