using RR.Common.Settings; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; namespace RR.Common.UI { /// /// Биндер для кнопок в настройках. /// Позволяет назначать действия кнопки настроек прямо на объекты сцены. /// public class SettingsButtonBinder : MonoBehaviour { [Header("Binding")] [Tooltip("Ключ кнопки из SettingsDatabase")] [SerializeField] private string key; /// /// Ключ кнопки для диспетчера /// public string Key => key; [Header("UI Elements (опционально)")] [SerializeField] private Button button; [SerializeField] private TMP_Text label; [Header("Action")] [Tooltip("Действие, выполняемое при нажатии кнопки")] [SerializeField] private UnityEvent onButtonClick; private SettingEntry def; private bool isRegistered; private TextInputSettingsBinder textInputBinder; private void Awake() { Debug.Log($"[SettingsButtonBinder] Awake() called for key '{key}' on GameObject '{gameObject.name}'"); // Получаем ссылку на TextInputSettingsBinder если он есть textInputBinder = GetComponent(); if (textInputBinder != null) { Debug.Log($"[SettingsButtonBinder] Found TextInputSettingsBinder on GameObject '{gameObject.name}'"); } // НЕ регистрируемся в Awake - только при OnEnable } private void Start() { Debug.Log($"[SettingsButtonBinder] Start() called for key '{key}'"); if (string.IsNullOrWhiteSpace(key)) return; if (SettingsManager.Instance) { def = SettingsManager.Instance.Define(key); } // Установка текста кнопки если есть if (label && def != null) { string text = !string.IsNullOrEmpty(def.buttonText) ? def.buttonText : def.displayName; if (string.IsNullOrEmpty(text)) text = key; label.text = text; } // Привязка действия к локальной кнопке если есть if (button) { button.onClick.RemoveAllListeners(); button.onClick.AddListener(OnButtonClick); Debug.Log($"[SettingsButtonBinder] Attached OnButtonClick to button for key '{key}'"); } } private void OnEnable() { // Регистрируемся только когда объект активен RegisterIfNeeded(); } private void OnDisable() { // Отписываемся при деактивации if (isRegistered) { SettingsButtonDispatcher.Unregister(key, this); isRegistered = false; } } private void OnDestroy() { // Отписываемся только при уничтожении (на всякий случай) if (isRegistered) { SettingsButtonDispatcher.Unregister(key, this); isRegistered = false; } } private void RegisterIfNeeded() { if (isRegistered || string.IsNullOrWhiteSpace(key)) return; SettingsButtonDispatcher.Register(key, this); isRegistered = true; } private void OnButtonClick() { Invoke(); } /// /// Вызывается при нажатии кнопки (из диспетчера или напрямую) /// public void Invoke() { // Если есть TextInputSettingsBinder, вызываем его if (textInputBinder != null) { textInputBinder.OpenTextInput(); } // Вызываем обычные действия кнопки onButtonClick?.Invoke(); } } }