42 lines
1 KiB
GDScript
42 lines
1 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 = 0.3
|
|
@export var max_zoom = 3
|
|
|
|
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
|