309 lines
10 KiB
C#
Raw Permalink Normal View History

2026-05-18 08:42:33 +08:00
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<string> dormancyKeyList = new List<string> { "100176", "100177", "100178", "100179", "100175" };
private List<string> dormancyStrList = new List<string>();
// 休眠时间值: 1分钟, 5分钟, 10分钟, 30分钟, 0(关闭)
private List<byte> dormancyValueList = new List<byte> { 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);
}
/// <summary>
/// 初始化语言文本
/// </summary>
private void InitLanguageText()
{
dormancyStrList.Clear();
foreach (var key in dormancyKeyList)
{
dormancyStrList.Add(LanguageManager.Instance.GetLanguage(key));
}
}
/// <summary>
/// 读取LCD设置
/// </summary>
private void ReadLCDSettings()
{
2026-06-17 15:42:55 +08:00
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
2026-05-18 08:42:33 +08:00
{
2026-06-17 15:42:55 +08:00
var config = DataManager.Instance.deviceConfig;
brightnessSetting = new LCDBrightnessSetting
{
AutoBrightness = config?.lcd_auto_brightness ?? false,
Brightness = config?.lcd_brightness ?? 80
};
sleepSetting = new LCDSleepSetting
{
Enable = config?.lcd_sleep_enable ?? true,
SleepTime = config?.lcd_sleep_time ?? 0x05
};
UpdateUI();
return;
}
2026-05-18 08:42:33 +08:00
LoadingUI.Show();
// 读取亮度设置
BLECommunicationManager.Instance.ReadLCDBrightnessSetting((brightness) =>
{
Loom.QueueOnMainThread(() =>
{
brightnessSetting = brightness;
// 读取休眠设置
BLECommunicationManager.Instance.ReadLCDSleepSetting((sleep) =>
{
Loom.QueueOnMainThread(() =>
{
sleepSetting = sleep;
UpdateUI();
});
});
});
});
}
/// <summary>
/// 写入LCD亮度设置
/// </summary>
2026-06-17 15:42:55 +08:00
private async void WriteBrightnessSetting()
2026-05-18 08:42:33 +08:00
{
2026-06-17 15:42:55 +08:00
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
LoadingUI.Show();
bool success = await HomePageCtrl.Instance.SendBleCommandByWifiAsync(
BLEConstants.CMD_LCD_BRIGHTNESS_SETTING, brightnessSetting.ToBytes());
if (success)
{
SyncBrightnessConfig();
UpdateUI();
}
else
{
ReadLCDSettings();
}
return;
}
2026-05-18 08:42:33 +08:00
LoadingUI.Show();
BLECommunicationManager.Instance.WriteLCDBrightnessSetting(brightnessSetting, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
UpdateUI();
}
else
{
// 写入失败,重新读取
ReadLCDSettings();
}
});
});
}
2026-06-17 15:42:55 +08:00
private void SyncBrightnessConfig()
{
var config = DataManager.Instance.deviceConfig;
config.lcd_auto_brightness = brightnessSetting.AutoBrightness;
config.lcd_brightness = brightnessSetting.Brightness;
}
2026-05-18 08:42:33 +08:00
/// <summary>
/// 写入LCD休眠设置
/// </summary>
2026-06-17 15:42:55 +08:00
private async void WriteSleepSetting()
2026-05-18 08:42:33 +08:00
{
2026-06-17 15:42:55 +08:00
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
LoadingUI.Show();
bool success = await HomePageCtrl.Instance.SendBleCommandByWifiAsync(
BLEConstants.CMD_LCD_SLEEP_SETTING, sleepSetting.ToBytes());
if (success)
{
SyncSleepConfig();
UpdateUI();
}
else
{
ReadLCDSettings();
}
return;
}
2026-05-18 08:42:33 +08:00
LoadingUI.Show();
BLECommunicationManager.Instance.WriteLCDSleepSetting(sleepSetting, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
2026-06-17 15:42:55 +08:00
SyncSleepConfig();
2026-05-18 08:42:33 +08:00
UpdateUI();
}
else
{
// 写入失败,重新读取
ReadLCDSettings();
}
});
});
}
2026-06-17 15:42:55 +08:00
private void SyncSleepConfig()
{
var config = DataManager.Instance.deviceConfig;
config.lcd_sleep_enable = sleepSetting.Enable;
config.lcd_sleep_time = sleepSetting.SleepTime;
}
2026-05-18 08:42:33 +08:00
/// <summary>
/// 更新UI显示
/// </summary>
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);
}
/// <summary>
/// 自动亮度开关变化
/// </summary>
private void OnAutoSwitchChanged(bool isOn)
{
brightnessSetting.AutoBrightness = isOn;
WriteBrightnessSetting();
}
Coroutine brightnessChangeCoroutine;
/// <summary>
/// 亮度滑块变化
/// </summary>
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();
}
/// <summary>
/// 亮度滑块拖动结束
/// </summary>
public void OnBrightnessSliderEndDrag()
{
WriteBrightnessSetting();
}
/// <summary>
/// 点击休眠设置
/// </summary>
public void ClickDormancy()
{
MutiSelectPage m = Instantiate(dormancy.gameObject, transform).GetComponent<MutiSelectPage>();
int currentIndex = GetDormancyIndexFromValue(sleepSetting.SleepTime);
m.Init(currentIndex, OnDormancySelected);
m.gameObject.SetActive(true);
UIManager.Instance.RegisterBackAction(m.Cancel);
}
/// <summary>
/// 休眠选项选择回调
/// </summary>
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();
}
}
/// <summary>
/// 根据休眠值获取选项索引
/// </summary>
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);
}
}
}