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 RGBlightSetting : MonoBehaviour { public Switch onSwitch; public ColorPicker colorPicker; public MutiSelectPage mode; private List modeKeyList { get; set; } = new List { "100167", "100168", "100169" }; private List modestrList = new List(); RGBControl setting; Action callback; public void Init(Action callback,RGBControl setting) { this.callback=callback; this.setting=setting; InitLanguageText(); //ReadLightSettings(); UpdateUI(); UIManager.Instance.RegisterBackAction(Back); } /// /// 初始化语言文本 /// private void InitLanguageText() { modestrList.Clear(); foreach (var key in modeKeyList) { modestrList.Add(LanguageManager.Instance.GetLanguage(key)); } } /// /// 读取RGB设置 /// private void ReadLightSettings() { if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth) { var config = DataManager.Instance.deviceConfig; this.setting = new RGBControl(); setting.Enable = config?.rgb_enable ?? false; setting.SetColor(new Color((config?.rgb_red ?? 0) / 255f, (config?.rgb_green ?? 0) / 255f, (config?.rgb_blue ?? 0) / 255f)); setting.Effect = DeviceConfig.ParseServerEnum(config?.rgb_effect, RGBEffectMode.Blinking); UpdateUI(); return; } // 显示Loading LoadingUI.Show(); // 读取rgb BLECommunicationManager.Instance.ReadRGBControl((setting) => { Loom.QueueOnMainThread(() => { this.setting = setting; UpdateUI(); }); }); } private async void SetRGBSetting() { if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth) { LoadingUI.Show(); bool success = await HomePageCtrl.Instance.SendBleCommandByWifiAsync( BLEConstants.CMD_RGB_CONTROL, setting.ToBytes()); if (success) { SyncRGBConfig(); UpdateUI(); } else { ReadLightSettings(); } return; } LoadingUI.Show(); BLECommunicationManager.Instance.WriteRGBControl(setting, (success) => { if (success) { SyncRGBConfig(); UpdateUI(); } else { ReadLightSettings(); } } ); } private void SyncRGBConfig() { var config = DataManager.Instance.deviceConfig; config.rgb_enable = setting.Enable; config.rgb_red = (byte)(setting.GetColor().r * 255); config.rgb_green = (byte)(setting.GetColor().g * 255); config.rgb_blue = (byte)(setting.GetColor().b * 255); config.rgb_effect = DeviceConfig.ToServerString(setting.Effect); } public Image lightColor; public Text modeValue; private void UpdateUI() { onSwitch.Init(setting.Enable,ClickSwitch); lightColor.color=setting.GetColor(); modeValue.text=modestrList[(int)setting.Effect]; LoadingUI.Hide(); UIManager.Instance.RegisterBackAction(Back); } public void ClickSwitch(bool ison) { setting.Enable=ison; SetRGBSetting(); } public void ClickColor() { ColorPicker p=Instantiate(colorPicker.gameObject,transform).GetComponent(); p.Init(setting.GetColor()); p.gameObject.SetActive(true); p.OnColorSelected+=SelectColor; UIManager.Instance.RegisterBackAction(p.Sure); } public void SelectColor(Color color) { setting.SetColor(color); SetRGBSetting(); } public void ClickMode() { MutiSelectPage m=Instantiate(mode.gameObject,transform).GetComponent(); m.Init((int)setting.Effect,SetMode); m.gameObject.SetActive(true); UIManager.Instance.RegisterBackAction(m.Cancel); } public void SetMode(int mode) { setting.Effect= (RGBEffectMode)mode; SetRGBSetting(); } public void Back() { callback?.Invoke(setting); Destroy(gameObject); } } }