brackeys-12/camera.gd

38 lines
1.1 KiB
GDScript3
Raw Normal View History

2024-09-17 20:37:19 +00:00
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()
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 = get_global_mouse_position()
else:
is_panning = false
# Handle panning movement
if is_panning and event is InputEventMouseMotion:
var mouse_movement = last_mouse_position - get_global_mouse_position()
global_position += mouse_movement * zoom.x # Adjust movement based on zoom level
last_mouse_position = get_global_mouse_position()