class_name CameraGimbal extends Node3D

@export var rotation_speed = (PI / 180) * 120
@export var default_zoom = 8

# is mouse captured by game permanently?
# (i.e. not just holding right click)
var locked = false
var mouse_velocity = Vector2.ZERO

var current_zoom = default_zoom
var zoom_min = 4
var zoom_max = 80
var zoom_speed = 20

func _lock():
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _unlock():
	Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	mouse_velocity = Vector2.ZERO

func lock():
	_lock()
	locked = true

# optional API function for nodes that want
# to unlock the mouse for a period of time
func unlock():
	_unlock()
	locked = false

func reset():
	basis = Basis()
	$InnerGimbal.basis = Basis()
	current_zoom = default_zoom

# y is before x because the outer gimbal is
# in charge of rotating the y axis
func rotate_camera(delta):
	var y = 0
	var x = 0

	if mouse_velocity.x > 5 or mouse_velocity.x < -5:
		y -= clamp(mouse_velocity.x / 12.5, -10, 10)
	if mouse_velocity.y > 5 or mouse_velocity.y < -5:
		x -= clamp(mouse_velocity.y / 12.5, -10, 10)

	rotate_object_local(Vector3.UP, y * rotation_speed * delta)
	$InnerGimbal.rotate_object_local(
		Vector3.RIGHT, x * rotation_speed * delta)
	$InnerGimbal.rotation.x = clamp($InnerGimbal.rotation.x, -0.8, 0.7)

func _input(event):
	if event is InputEventMouseMotion:
		mouse_velocity = event.relative
	elif event.is_action_pressed("cam_zoom_out"):
		current_zoom += 1
	elif event.is_action_pressed("cam_zoom_in"):
		current_zoom -= 1

	current_zoom = clamp(current_zoom, zoom_min, zoom_max)

func _process(delta):
	# rotate camera
	if locked:
		rotate_camera(delta)
	else:
		if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
			_lock()
			rotate_camera(delta)
		else:
			_unlock()

	# zoom in/out
	var pos = Vector3($InnerGimbal/Camera3D.position)
	pos.y = lerp(pos.y, float(current_zoom), zoom_speed * delta)
	pos.z = lerp(pos.z, float(current_zoom), zoom_speed * delta)

	$InnerGimbal/Camera3D.position = pos

	# reset camera
	if Input.is_action_pressed("cam_reset"):
		reset()

func _ready():
	lock()
