46 lines
1.3 KiB
GDScript
46 lines
1.3 KiB
GDScript
extends Camera2D
|
|
|
|
# Variables to control zoom limits and speed
|
|
var zoom_min = 0.5
|
|
var zoom_max = 2.0
|
|
var zoom_speed = 0.1
|
|
|
|
# Variables for panning
|
|
var is_panning = false
|
|
var last_mouse_position = Vector2()
|
|
var pan_speed = 1000
|
|
|
|
@onready var camera_target = position
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
position = position.move_toward(camera_target, delta * pan_speed)
|
|
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseButton:
|
|
# Zoom in (scroll up)
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
|
zoom -= Vector2(zoom_speed, zoom_speed)
|
|
# Zoom out (scroll down)
|
|
elif event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
|
zoom += Vector2(zoom_speed, zoom_speed)
|
|
|
|
# Clamp the zoom value to stay within zoom_min and zoom_max
|
|
zoom.x = clamp(zoom.x, zoom_min, zoom_max)
|
|
zoom.y = clamp(zoom.y, zoom_min, zoom_max)
|
|
|
|
# Handle panning with right click
|
|
if event.button_index == MOUSE_BUTTON_RIGHT:
|
|
if event.pressed:
|
|
is_panning = true
|
|
last_mouse_position = Cursor.mouse_control.position
|
|
else:
|
|
is_panning = false
|
|
|
|
# Handle panning movement
|
|
if is_panning and event is InputEventMouseMotion:
|
|
var mouse_movement = last_mouse_position - Cursor.mouse_control.position
|
|
camera_target += mouse_movement / zoom.x
|
|
#global_position += mouse_movement * zoom.x # Adjust movement based on zoom level
|
|
last_mouse_position = Cursor.mouse_control.position
|