53 lines
1.4 KiB
GDScript
53 lines
1.4 KiB
GDScript
extends Camera2D
|
|
|
|
class_name WorldCamera
|
|
|
|
var last_drag_position : Vector2
|
|
|
|
var dragging = false
|
|
|
|
@export var zoom_constant : float = 1.1
|
|
|
|
@export var min_zoom : float = 0.3
|
|
@export var max_zoom : float = 3
|
|
|
|
@export var speed : float = 1000
|
|
|
|
func get_camera_rect() -> Rect2:
|
|
var pos = position
|
|
var size = get_viewport_rect().size / zoom
|
|
return Rect2(pos - size / 2, size)
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_MIDDLE:
|
|
dragging = event.pressed
|
|
last_drag_position = get_local_mouse_position()
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
|
zoom_out()
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
|
zoom_in()
|
|
|
|
func zoom_out():
|
|
if zoom.x / zoom_constant > min_zoom:
|
|
zoom /= zoom_constant
|
|
|
|
func zoom_in():
|
|
if zoom.x * zoom_constant < max_zoom:
|
|
zoom *= zoom_constant
|
|
|
|
func _process(delta: float) -> void:
|
|
if dragging:
|
|
var current_mouse_position = get_local_mouse_position()
|
|
position += (last_drag_position - current_mouse_position)
|
|
last_drag_position = current_mouse_position
|
|
|
|
if Input.is_action_pressed("camera_down"):
|
|
position.y += speed / zoom.y * delta
|
|
if Input.is_action_pressed("camera_up"):
|
|
position.y -= speed / zoom.y * delta
|
|
if Input.is_action_pressed("camera_left"):
|
|
position.x -= speed / zoom.x * delta
|
|
if Input.is_action_pressed("camera_right"):
|
|
position.x += speed / zoom.x * delta
|