using System; using System.Collections; using System.Collections.Generic; using Kill.Bluetooth; using Kill.Bluetooth.Protocol; using Kill.Core; using Kill.Managers; using Kill.UI.Components; using UnityEngine; using UnityEngine.UI; namespace Kill.UI.Pages { public class LightSettingPage : MonoBehaviour { public Switch switchLight; public MutiSelectPage typeSelect; public Text textType; public MutiSelectPage powerSelect; public Text textPower; public Button backButton; public Button typeSelectButton; public Button powerSelectButton; private List typeKeyList { get; set; } = new List { "100112", "100113" }; private List powerKeyList { get; set; } = new List { "100114", "100115", "100116" }; private List typeStrList = new List(); private List powerStrList = new List(); // 当前设置值 private bool currentLightEnabled = false; private bool selectedLightEnabled = false; private FillLightType currentLightType = FillLightType.Infrared; private FillLightType selectedLightType = FillLightType.Infrared; private LightIntensity currentIntensity = LightIntensity.Medium; private LightIntensity selectedIntensity = LightIntensity.Medium; // 进入设置前的原始值(用于取消恢复) private bool originalLightEnabled = false; private FillLightType originalLightType = FillLightType.Infrared; private LightIntensity originalIntensity = LightIntensity.Medium; // 静态标志:是否有设置页面正在处理回调 private static bool _isSettingPageActive = false; void Start() { // 初始化语言文本 InitLanguageText(); // 绑定按钮事件 BindButtonEvents(); } void OnEnable() { // 确保语言文本已初始化 if (typeStrList.Count == 0 || powerStrList.Count == 0) { InitLanguageText(); } // 页面显示时初始化 InitLightSettingPage(); } /// /// 初始化语言文本 /// private void InitLanguageText() { typeStrList.Clear(); powerStrList.Clear(); foreach (var key in typeKeyList) { typeStrList.Add(LanguageManager.Instance.GetLanguage(key)); } foreach (var key in powerKeyList) { powerStrList.Add(LanguageManager.Instance.GetLanguage(key)); } } /// /// 绑定按钮事件 /// private void BindButtonEvents() { // 绑定返回按钮 if (backButton != null) { backButton.onClick.RemoveAllListeners(); backButton.onClick.AddListener(OnBackButtonClick); } // 绑定类型选择按钮 if (typeSelectButton != null) { typeSelectButton.onClick.RemoveAllListeners(); typeSelectButton.onClick.AddListener(OnClickTypeSelect); } // 绑定功率选择按钮 if (powerSelectButton != null) { powerSelectButton.onClick.RemoveAllListeners(); powerSelectButton.onClick.AddListener(OnClickPowerSelect); } } /// /// 初始化灯光设置页面 /// public void InitLightSettingPage() { Debug.Log("[LightSettingPage] 初始化灯光设置页面"); // 注册蓝牙回调(顶替主页) RegisterBluetoothCallbacks(); // 读取当前补光灯设置 ReadLightSettings(); } /// /// 注册蓝牙回调(顶替主页) /// private void RegisterBluetoothCallbacks() { if (BLECommunicationManager.Instance != null) { // 设置标志位,表示设置页面正在处理 _isSettingPageActive = true; // 订阅事件 BLECommunicationManager.Instance.OnFillLightControlReceived += OnFillLightControlReceived; Debug.Log("[LightSettingPage] 蓝牙回调已注册"); } } /// /// 注销蓝牙回调(恢复主页) /// private void UnregisterBluetoothCallbacks() { if (BLECommunicationManager.Instance != null) { // 取消订阅事件 BLECommunicationManager.Instance.OnFillLightControlReceived -= OnFillLightControlReceived; // 清除标志位 _isSettingPageActive = false; Debug.Log("[LightSettingPage] 蓝牙回调已注销"); } } /// /// 补光灯控制接收回调 /// private void OnFillLightControlReceived(FillLightControl control) { // 只有当前设置页面激活时才处理 if (!_isSettingPageActive) return; LoadingUI.Hide(); currentLightEnabled = control.Enable; selectedLightEnabled = currentLightEnabled; originalLightEnabled = currentLightEnabled; currentLightType = control.LightType; selectedLightType = currentLightType; originalLightType = currentLightType; currentIntensity = control.Intensity; selectedIntensity = currentIntensity; originalIntensity = currentIntensity; Debug.Log($"[LightSettingPage] 读取补光灯设置: 开关={currentLightEnabled}, 类型={currentLightType}, 强度={currentIntensity}"); // 更新UI显示 UpdateUI(); } /// /// 检查是否有设置页面正在处理回调 /// public static bool IsSettingPageActive() { return _isSettingPageActive; } /// /// 读取补光灯设置 /// private void ReadLightSettings() { #if UNITY_EDITOR // 编辑器模式下使用假数据 Editor_InitWithFakeData(); return; #endif // 显示Loading LoadingUI.Show(); // 读取补光灯控制 BLECommunicationManager.Instance?.ReadFillLightControl(); // 超时保护 StartCoroutine(ReadSettingsTimeout()); } #if UNITY_EDITOR /// /// 编辑器模式下使用假数据初始化 /// private void Editor_InitWithFakeData() { Debug.Log("[LightSettingPage] 编辑器模式:使用假数据初始化"); currentLightEnabled = true; selectedLightEnabled = true; currentLightType = FillLightType.White; selectedLightType = FillLightType.White; currentIntensity = LightIntensity.High; selectedIntensity = LightIntensity.High; UpdateUI(); } #endif /// /// 读取设置超时保护 /// private IEnumerator ReadSettingsTimeout() { yield return new WaitForSeconds(5f); LoadingUI.Hide(); } /// /// 更新UI显示 /// private void UpdateUI() { // 初始化开关 if (switchLight != null) { switchLight.Init(selectedLightEnabled, OnLightSwitchChanged); } // 更新类型文本 UpdateTypeText(); // 更新功率文本 UpdatePowerText(); } /// /// 更新类型文本 /// private void UpdateTypeText() { if (textType == null) return; int typeIndex = (int)selectedLightType; if (typeIndex >= 0 && typeIndex < typeStrList.Count) { textType.text = typeStrList[typeIndex]; Debug.Log($"[LightSettingPage] 更新类型文本: {typeStrList[typeIndex]}"); } else { Debug.LogError($"[LightSettingPage] 无效的类型索引: {typeIndex}"); } } /// /// 更新功率文本 /// private void UpdatePowerText() { if (textPower == null) return; int powerIndex = (int)selectedIntensity; if (powerIndex >= 0 && powerIndex < powerStrList.Count) { textPower.text = powerStrList[powerIndex]; } } /// /// 灯光开关状态改变回调 /// private void OnLightSwitchChanged(bool isOn) { Debug.Log($"[LightSettingPage] 灯光开关: {(isOn ? "开" : "关")}"); selectedLightEnabled = isOn; #if UNITY_EDITOR Debug.Log("[LightSettingPage] 编辑器模式:模拟设置灯光开关"); currentLightEnabled = selectedLightEnabled; return; #endif // 发送设置到设备 SendLightSettingToDevice(); } /// /// 打开类型选择页面 /// public void OnClickTypeSelect() { if (typeSelect == null) return; // 实例化选择页面 MutiSelectPage selectPage = Instantiate(typeSelect, transform); int currentIndex = (int)selectedLightType; // 设置选项文本 for (int i = 0; i < selectPage.options.Count && i < typeStrList.Count; i++) { Text optionText = selectPage.options[i].GetComponentInChildren(); if (optionText != null) { optionText.text = typeStrList[i]; } } selectPage.Init(currentIndex, (selectedIndex) => { Debug.Log($"[LightSettingPage] 选择类型: {selectedIndex}"); selectedLightType = (FillLightType)selectedIndex; UpdateTypeText(); #if UNITY_EDITOR Debug.Log("[LightSettingPage] 编辑器模式:模拟设置灯光类型"); currentLightType = selectedLightType; return; #endif // 发送设置到设备 SendLightSettingToDevice(); }); selectPage.gameObject.SetActive(true); } /// /// 打开功率选择页面 /// public void OnClickPowerSelect() { if (powerSelect == null) return; // 实例化选择页面 MutiSelectPage selectPage = Instantiate(powerSelect, transform); int currentIndex = (int)selectedIntensity; // 设置选项文本 for (int i = 0; i < selectPage.options.Count && i < powerStrList.Count; i++) { Text optionText = selectPage.options[i].GetComponentInChildren(); if (optionText != null) { optionText.text = powerStrList[i]; } } selectPage.Init(currentIndex, (selectedIndex) => { Debug.Log($"[LightSettingPage] 选择功率: {selectedIndex}"); selectedIntensity = (LightIntensity)selectedIndex; UpdatePowerText(); #if UNITY_EDITOR Debug.Log("[LightSettingPage] 编辑器模式:模拟设置灯光强度"); currentIntensity = selectedIntensity; return; #endif // 发送设置到设备 SendLightSettingToDevice(); }); selectPage.gameObject.SetActive(true); } /// /// 发送灯光设置到设备 /// private void SendLightSettingToDevice() { FillLightControl lightControl = new FillLightControl { Enable = selectedLightEnabled, LightType = selectedLightType, Intensity = selectedIntensity }; BLECommunicationManager.Instance?.WriteFillLightControl(lightControl, (success) => { Loom.QueueOnMainThread(() => { if (success) { Debug.Log($"[LightSettingPage] 灯光设置成功: 开关={selectedLightEnabled}, 类型={selectedLightType}, 强度={selectedIntensity}"); currentLightEnabled = selectedLightEnabled; currentLightType = selectedLightType; currentIntensity = selectedIntensity; } else { Debug.LogError("[LightSettingPage] 灯光设置失败"); // 设置失败,恢复UI显示 selectedLightEnabled = currentLightEnabled; selectedLightType = currentLightType; selectedIntensity = currentIntensity; UpdateUI(); } }); }); } /// /// 返回按钮点击事件 - 直接关闭页面 /// private void OnBackButtonClick() { Debug.Log("[LightSettingPage] 返回按钮点击,关闭页面"); ClosePage(); } /// /// 关闭页面 /// public void ClosePage() { LoadingUI.Hide(); // 注销蓝牙回调(恢复主页) UnregisterBluetoothCallbacks(); Destroy(gameObject); NotifyHomePageRefresh(); // 清除UIManager返回事件 UIManager.Instance?.ClearBackAction(); } /// /// 通知主页刷新 /// private void NotifyHomePageRefresh() { // 通知主页刷新灯光显示 HomePageCtrl.Instance?.RefreshLightControl(); } } }