extends Control
class_name MovableControl

@export_category("Logic")
@export var move_target: BaseWidgetNode

var is_hovering: bool
var is_grab: bool
var mouse_down_pos: Vector2

func _ready() -> void:
	mouse_entered.connect(func(): is_hovering = true)
	mouse_exited.connect(func(): is_hovering = false)
	
func _process(delta: float) -> void:
	#print(is_hovering)
	if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and is_hovering and not is_grab:
		mouse_down_pos = get_viewport().get_mouse_position()
		is_grab = true
	
	if is_grab and not Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
		is_grab = false
		
	if is_grab:
		var current_mouse_pos: Vector2 = get_global_mouse_position()
		var offset: Vector2 = current_mouse_pos - mouse_down_pos
		move_target.position += offset
		mouse_down_pos = current_mouse_pos
