84 lines
No EOL
2.5 KiB
GDScript
84 lines
No EOL
2.5 KiB
GDScript
class_name CameraController
|
|
extends Node2D
|
|
|
|
signal zoom_changed(zoom_level)
|
|
|
|
# Camera movement properties
|
|
@export var min_zoom: float = 0.5
|
|
@export var max_zoom: float = 2.0
|
|
@export var zoom_step: float = 0.1
|
|
@export var pan_speed: float = 1.0
|
|
|
|
var board_container: Node
|
|
var initial_board_position: Vector2
|
|
var initial_board_size: Vector2
|
|
|
|
var current_zoom: float = 1.0
|
|
var dragging: bool = false
|
|
var drag_start_position: Vector2
|
|
|
|
func _init(target_board: Node) -> void:
|
|
board_container = target_board
|
|
|
|
func _ready() -> void:
|
|
initial_board_position = board_container.position
|
|
if board_container.has_method("get_combined_minimum_size"):
|
|
initial_board_size = board_container.get_combined_minimum_size()
|
|
else:
|
|
initial_board_size = board_container.size
|
|
|
|
current_zoom = 1.0
|
|
board_container.scale = Vector2(current_zoom, current_zoom)
|
|
|
|
adjust_pivot()
|
|
|
|
func adjust_pivot() -> void:
|
|
var center = initial_board_size / 2
|
|
board_container.pivot_offset = center
|
|
|
|
func _input(event: InputEvent) -> void:
|
|
if event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
|
zoom_in(event.position)
|
|
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
|
zoom_out(event.position)
|
|
elif event.button_index == MOUSE_BUTTON_MIDDLE:
|
|
if event.pressed:
|
|
start_drag(event.position)
|
|
else:
|
|
end_drag()
|
|
|
|
if event is InputEventMouseMotion and dragging:
|
|
update_drag(event.position)
|
|
|
|
func zoom_in(mouse_pos: Vector2) -> void:
|
|
if current_zoom < max_zoom:
|
|
apply_zoom(current_zoom + zoom_step)
|
|
|
|
func zoom_out(mouse_pos: Vector2) -> void:
|
|
if current_zoom > min_zoom:
|
|
apply_zoom(current_zoom - zoom_step)
|
|
|
|
func apply_zoom(new_zoom: float) -> void:
|
|
current_zoom = new_zoom
|
|
board_container.scale = Vector2(current_zoom, current_zoom)
|
|
|
|
emit_signal("zoom_changed", current_zoom)
|
|
|
|
func start_drag(position: Vector2) -> void:
|
|
dragging = true
|
|
drag_start_position = position
|
|
|
|
func end_drag() -> void:
|
|
dragging = false
|
|
|
|
func update_drag(position: Vector2) -> void:
|
|
if dragging:
|
|
var delta = position - drag_start_position
|
|
board_container.position += delta * pan_speed
|
|
drag_start_position = position
|
|
|
|
func reset_view() -> void:
|
|
current_zoom = 1.0
|
|
board_container.scale = Vector2(current_zoom, current_zoom)
|
|
board_container.position = initial_board_position |