made gdscript godot 4.3 to c#

extends Camera2D

@export var SPEED: float = 20.0
@export var pan_speed: float = 1000.0
@export var zoom_in_limit: Vector2 = Vector2(0.5, 0.5)
@export var zoom_out_limit: Vector2 = Vector2(2, 2)
@export var edge_margin: int = 20
@export var interpolation_speed: float = 25.0

var mousePos: Vector2 = Vector2()
var mousePosGlobal: Vector2 = Vector2()
var start: Vector2 = Vector2()
var startV: Vector2 = Vector2()
var end: Vector2 = Vector2()
var endV: Vector2 = Vector2()
var isDragging: bool = false
var target_position: Vector2 = Vector2()

@onready var box: Panel = get_node("../UI/Panel")
@onready var selection_box: Area2D = get_node("../Unit/Area2D")  # Invisible box for selection
@onready var global_script = Global  # Reference to the autoload script directly

func _ready():
	target_position = position

func _process(delta):
	_handle_panning(delta)
	_handle_zooming()  # Reintegrate zoom functionality
	process_select_area()
	position = lerp(position, target_position, interpolation_speed * delta)

func process_select_area() -> void:
	if Input.is_action_just_pressed("select_unit"):
		start = mousePosGlobal
		startV = mousePos
		isDragging = true
		global_script.is_box_selecting = true  # Flag to track rectangle selection

	if isDragging:
		end = mousePosGlobal
		endV = mousePos
		draw_area()

	if Input.is_action_just_released("select_unit"):
		if startV.distance_to(mousePos) > 20:
			end = mousePosGlobal
			endV = mousePos
			isDragging = false
			draw_area(false)
			global_script._on_area_selected(start, end)
		else:
			end = start
			isDragging = false
			draw_area(false)
			global_script.check_unit_selection(mousePosGlobal)
		global_script.is_box_selecting = false  # End rectangle selection

func _input(event: InputEvent) -> void:
	if event is InputEventMouse:
		mousePos = event.position
		mousePosGlobal = get_global_mouse_position()

func draw_area(_s: bool = true) -> void:
	box.size = Vector2(abs(startV.x - endV.x), abs(startV.y - endV.y))
	var pos = Vector2()
	pos.x = min(startV.x, endV.x)
	pos.y = min(startV.y, endV.y)
	box.position = pos
	box.size *= int(_s)

func _handle_panning(delta):
	var move_dir = Vector2()
	var mouse_pos = get_viewport().get_mouse_position()
	var viewport_size = get_viewport().get_visible_rect().size

	if mouse_pos.x <= edge_margin:
		move_dir.x -= 1
	elif mouse_pos.x >= viewport_size.x - edge_margin:
		move_dir.x += 1

	if mouse_pos.y <= edge_margin:
		move_dir.y -= 1
	elif mouse_pos.y >= viewport_size.y - edge_margin:
		move_dir.y += 1

	if Input.is_action_pressed("ui_left") or Input.is_action_pressed("ui_pan_left"):
		move_dir.x -= 1
	if Input.is_action_pressed("ui_right") or Input.is_action_pressed("ui_pan_right"):
		move_dir.x += 1
	if Input.is_action_pressed("ui_up") or Input.is_action_pressed("ui_pan_up"):
		move_dir.y -= 1
	if Input.is_action_pressed("ui_down") or Input.is_action_pressed("ui_pan_down"):
		move_dir.y += 1

	target_position += move_dir * pan_speed * delta

func _handle_zooming():
	if Input.is_action_just_pressed("ui_zoom_in"):
		zoom /= 1.1
		if zoom.x < zoom_in_limit.x:
			zoom = zoom_in_limit

	if Input.is_action_just_pressed("ui_zoom_out"):
		zoom *= 1.1
		if zoom.x > zoom_out_limit.x:
			zoom = zoom_out_limit

func check_unit_selection(global_mouse_position: Vector2) -> void:
	selection_box.global_position = global_mouse_position
	selection_box.call_deferred("select_units_in_box")