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.UI; using UnityEngine; using System; using Unity.VisualScripting; namespace Kill.UI.Pages { public class LCDSetting : MonoBehaviour { public Switch autoSwitch; public MutiSelectPage dormancy; public Slider brightnessSlider; public Text brightnessValueText; public Text dormancyValueText; // 休眠选项对应的语言键值和实际值 // 索引0=1分钟, 1=5分钟, 2=10分钟, 3=30分钟, 4=关闭休眠 private List dormancyKeyList = new List { "100176", "100177", "100178", "100179", "100175" }; private List dormancyStrList = new List(); // 休眠时间值: 1分钟, 5分钟, 10分钟, 30分钟, 0(关闭) private List dormancyValueList = new List { 0x01, 0x05, 0x0A, 0x1E, 0x00 }; private LCDBrightnessSetting brightnessSetting; private LCDSleepSetting sleepSetting; private Action callback; public void Init(Action callback) { this.callback = callback; InitLanguageText(); ReadLCDSettings(); UIManager.Instance.RegisterBackAction(Back); } /// /// 初始化语言文本 /// private void InitLanguageText() { dormancyStrList.Clear(); foreach (var key in dormancyKeyList) { dormancyStrList.Add(LanguageManager.Instance.GetLanguage(key)); } } /// /// 读取LCD设置 /// private void ReadLCDSettings() { #if UNITY_EDITOR // 编辑器模式下使用默认值 brightnessSetting = new LCDBrightnessSetting { AutoBrightness = false, Brightness = 80 }; sleepSetting = new LCDSleepSetting { Enable = true, SleepTime = 0x05 // 5分钟 }; UpdateUI(); return; #endif LoadingUI.Show(); // 读取亮度设置 BLECommunicationManager.Instance.ReadLCDBrightnessSetting((brightness) => { Loom.QueueOnMainThread(() => { brightnessSetting = brightness; // 读取休眠设置 BLECommunicationManager.Instance.ReadLCDSleepSetting((sleep) => { Loom.QueueOnMainThread(() => { sleepSetting = sleep; UpdateUI(); }); }); }); }); } /// /// 写入LCD亮度设置 /// private void WriteBrightnessSetting() { #if UNITY_EDITOR UpdateUI(); return; #endif LoadingUI.Show(); BLECommunicationManager.Instance.WriteLCDBrightnessSetting(brightnessSetting, (success) => { Loom.QueueOnMainThread(() => { if (success) { UpdateUI(); } else { // 写入失败,重新读取 ReadLCDSettings(); } }); }); } /// /// 写入LCD休眠设置 /// private void WriteSleepSetting() { #if UNITY_EDITOR UpdateUI(); return; #endif LoadingUI.Show(); BLECommunicationManager.Instance.WriteLCDSleepSetting(sleepSetting, (success) => { Loom.QueueOnMainThread(() => { if (success) { UpdateUI(); } else { // 写入失败,重新读取 ReadLCDSettings(); } }); }); } /// /// 更新UI显示 /// private void UpdateUI() { // 更新自动亮度开关 autoSwitch.Init(brightnessSetting.AutoBrightness, OnAutoSwitchChanged); // 更新亮度滑块 brightnessSlider.value = brightnessSetting.Brightness; brightnessSlider.onValueChanged.RemoveAllListeners(); brightnessSlider.onValueChanged.AddListener(OnBrightnessChanged); if (brightnessValueText != null) { brightnessValueText.text = $"{brightnessSetting.Brightness}%"; } // 更新休眠显示 int dormancyIndex = GetDormancyIndexFromValue(sleepSetting.SleepTime); if (dormancyValueText != null && dormancyIndex >= 0 && dormancyIndex < dormancyStrList.Count) { dormancyValueText.text = dormancyStrList[dormancyIndex]; } LoadingUI.Hide(); UIManager.Instance.RegisterBackAction(Back); } /// /// 自动亮度开关变化 /// private void OnAutoSwitchChanged(bool isOn) { brightnessSetting.AutoBrightness = isOn; WriteBrightnessSetting(); } Coroutine brightnessChangeCoroutine; /// /// 亮度滑块变化 /// private void OnBrightnessChanged(float value) { byte brightness = (byte)Mathf.Clamp(value, 10, 100); brightnessSetting.Brightness = brightness; if (brightnessValueText != null) { brightnessValueText.text = $"{brightness}%"; } if (brightnessChangeCoroutine != null) { StopCoroutine(brightnessChangeCoroutine); } brightnessChangeCoroutine = StartCoroutine(CountDownAndWriteMultimediaSetting(0.5f)); } private IEnumerator CountDownAndWriteMultimediaSetting(float delay) { yield return new WaitForSeconds(delay); OnBrightnessSliderEndDrag(); } /// /// 亮度滑块拖动结束 /// public void OnBrightnessSliderEndDrag() { WriteBrightnessSetting(); } /// /// 点击休眠设置 /// public void ClickDormancy() { MutiSelectPage m = Instantiate(dormancy.gameObject, transform).GetComponent(); int currentIndex = GetDormancyIndexFromValue(sleepSetting.SleepTime); m.Init(currentIndex, OnDormancySelected); m.gameObject.SetActive(true); UIManager.Instance.RegisterBackAction(m.Cancel); } /// /// 休眠选项选择回调 /// private void OnDormancySelected(int index) { if (index >= 0 && index < dormancyValueList.Count) { byte sleepValue = dormancyValueList[index]; // 索引4是关闭休眠(值为0),其他是开启休眠 sleepSetting.Enable = (index != 4); sleepSetting.SleepTime = sleepValue; WriteSleepSetting(); } } /// /// 根据休眠值获取选项索引 /// private int GetDormancyIndexFromValue(byte value) { for (int i = 0; i < dormancyValueList.Count; i++) { if (dormancyValueList[i] == value) { return i; } } // 默认值返回5分钟(索引1) return 1; } public void Back() { callback?.Invoke(); Destroy(gameObject); } } }