@tool
class_name VibrationComponent
extends Node

## A small helper node to quickly perform controller vibrations.


## If [code]false[/code], calling [method vibrate] will not start a controller vibration.
@export var enabled: bool = true:
	get = is_enabled,
	set = set_enabled
## [param duration] is the duration of the effect in seconds
## (a duration of [code]0[/code] will try to play the vibration indefinitely).[br]
## The vibration can be stopped early by calling [method stop_vibration].
@export var duration: float = 0.15:
	get = get_duration,
	set = set_duration
## The device index to perform the vibration on.
@export var device: int = 0:
	get = get_device,
	set = set_device
## The delay the vibration by this amount.
@export var delay: float = 0.0

@warning_ignore_start("unused_private_class_variable")
@export_tool_button("Test Vibration", "InputEventJoypadMotion")
var _editor_test_vibration: Callable = vibrate
@export_tool_button("Stop Vibration", "MissingNode")
var _editor_stop_test_vibration: Callable = stop_vibration
@warning_ignore_restore("unused_private_class_variable")

@export_group("Magnitude")
## [param weak_magnitude] is the strength of the weak motor
## (between [code]0[/code] and [code]1[/code]).
@export_range(0.0, 1.0) var weak_magnitude: float = 0.1:
	get = get_weak_magnitude,
	set = set_weak_magnitude
## [param strong_magnitude] is the strength of the strong motor
## (between [code]0[/code] and [code]1[/code]).
@export_range(0.0, 1.0) var strong_magnitude: float = 0.2:
	get = get_strong_magnitude,
	set = set_strong_magnitude
## Multiplies [member weak_magnitude] and [member strong_magnitude] by this amount.
@export var magnitude_multiplier: float = 1.0:
	get = get_magnitude_multiplier,
	set = set_magnitude_multiplier

var animated_weak_magnitude: float = 0.0:
	set(value):
		animated_weak_magnitude = clampf(value, 0.0, 1.0)
var animated_strong_magnitude: float = 0.0:
	set(value):
		animated_strong_magnitude = clampf(value, 0.0, 1.0)


func _process(delta: float) -> void:
	if not is_zero_approx(animated_weak_magnitude + animated_strong_magnitude):
		Input.start_joy_vibration.call_deferred(
			device, animated_weak_magnitude, animated_strong_magnitude, delta
		)
		animated_weak_magnitude = 0.0
		animated_strong_magnitude = 0.0


## Performs the controller vibration based on given parameters of the component.[br]
## [b]Info[/b]: The [member enabled] parameter must be [code]true[/code].
func vibrate() -> void:
	# Don't vibrate if the user has it turned off.
	if not Engine.is_editor_hint():
		if (
			not InputManager.using_controller
			#or not ProjectSettings.get_setting("game/input/controller_vibrations", true)
		):
			return

	if enabled:
		if delay > 0.0:
			await get_tree().create_timer(delay).timeout

		Input.start_joy_vibration(
				device,
				clampf(weak_magnitude * magnitude_multiplier, 0.0, 1.0),
				clampf(strong_magnitude * magnitude_multiplier, 0.0, 1.0),
				duration
		)


## Stops the vibration of the joypad, based on the [member device] parameter,
## started with [method vibrate].
func stop_vibration() -> void:
	Input.stop_joy_vibration(device)


func set_enabled(value: bool) -> void:
	enabled = value


func is_enabled() -> bool:
	return enabled


func set_device(device_id: int) -> void:
	device = device_id


func get_device() -> int:
	return device


func set_duration(length: float) -> void:
	duration = length


func get_duration() -> float:
	return duration


func set_magnitude_multiplier(multiplier_value: float) -> void:
	magnitude_multiplier = multiplier_value


func get_magnitude_multiplier() -> float:
	return magnitude_multiplier


func set_weak_magnitude(weak_value: float) -> void:
	weak_magnitude = weak_value


func get_weak_magnitude() -> float:
	return weak_magnitude


func set_strong_magnitude(strong_value: float) -> void:
	strong_magnitude = strong_value


func get_strong_magnitude() -> float:
	return strong_magnitude
