@tool
extends RemoteTransform2D
class_name SingleComponentRemotor

@export var observed_remote_path: NodePath:
	set(value):
		remote_path = value
		_update_remotor_name()
	get:
		return remote_path

var _observed_node: Node = null

func _ready():
	_update_remotor_name()

func _update_remotor_name():
	if not Engine.is_editor_hint():
		return

	# 清理旧连接
	if is_instance_valid(_observed_node):
		_observed_node.disconnect("renamed", Callable(self, "_on_observed_node_renamed"))

	if remote_path.is_empty():
		name = "Remotor[NONE]"
		_observed_node = null
		return

	var remote_node := get_node_or_null(remote_path)
	if remote_node:
		name = "Remotor[%s]" % remote_node.name
		_observed_node = remote_node
		if not _observed_node.is_connected("renamed", Callable(self, "_on_observed_node_renamed")):
			_observed_node.connect("renamed", Callable(self, "_on_observed_node_renamed"))
	else:
		name = "Remotor[INVALID]"
		_observed_node = null

func _on_observed_node_renamed():
	if _observed_node:
		name = "Remotor[%s]" % _observed_node.name
