killapp/Assets/Scripts/UI/Pages/HomePage/LightSettingPage.cs

450 lines
15 KiB
C#
Raw Permalink Normal View History

using System;
2026-06-17 15:42:55 +08:00
using System.Threading.Tasks;
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<string> typeKeyList { get; set; } = new List<string> { "100112", "100113" };
private List<string> powerKeyList { get; set; } = new List<string> { "100114", "100115", "100116" };
private List<string> typeStrList = new List<string>();
private List<string> powerStrList = new List<string>();
// 当前设置值
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();
}
/// <summary>
/// 初始化语言文本
/// </summary>
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));
}
}
/// <summary>
/// 绑定按钮事件
/// </summary>
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);
}
}
/// <summary>
/// 初始化灯光设置页面
/// </summary>
public void InitLightSettingPage()
{
Debug.Log("[LightSettingPage] 初始化灯光设置页面");
// 注册蓝牙回调(顶替主页)
RegisterBluetoothCallbacks();
// 读取当前补光灯设置
ReadLightSettings();
}
/// <summary>
/// 注册蓝牙回调(顶替主页)
/// </summary>
private void RegisterBluetoothCallbacks()
{
if (BLECommunicationManager.Instance != null)
{
// 设置标志位,表示设置页面正在处理
_isSettingPageActive = true;
// 订阅事件
BLECommunicationManager.Instance.OnFillLightControlReceived += OnFillLightControlReceived;
Debug.Log("[LightSettingPage] 蓝牙回调已注册");
}
}
/// <summary>
/// 注销蓝牙回调(恢复主页)
/// </summary>
private void UnregisterBluetoothCallbacks()
{
if (BLECommunicationManager.Instance != null)
{
// 取消订阅事件
BLECommunicationManager.Instance.OnFillLightControlReceived -= OnFillLightControlReceived;
// 清除标志位
_isSettingPageActive = false;
Debug.Log("[LightSettingPage] 蓝牙回调已注销");
}
}
/// <summary>
/// 补光灯控制接收回调
/// </summary>
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();
}
/// <summary>
/// 检查是否有设置页面正在处理回调
/// </summary>
public static bool IsSettingPageActive()
{
return _isSettingPageActive;
}
/// <summary>
/// 读取补光灯设置
/// </summary>
private void ReadLightSettings()
{
2026-06-17 15:42:55 +08:00
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
LoadSettingsFromConfig();
return;
}
// 显示Loading
LoadingUI.Show();
// 读取补光灯控制
BLECommunicationManager.Instance?.ReadFillLightControl();
}
2026-06-17 15:42:55 +08:00
private void LoadSettingsFromConfig()
{
2026-06-17 15:42:55 +08:00
var config = DataManager.Instance.deviceConfig;
currentLightEnabled = config?.fill_light_enable ?? false;
selectedLightEnabled = currentLightEnabled;
originalLightEnabled = currentLightEnabled;
currentLightType = DeviceConfig.ParseServerEnum(config?.fill_light_type, FillLightType.Infrared);
selectedLightType = currentLightType;
originalLightType = currentLightType;
currentIntensity = DeviceConfig.ParseServerEnum(config?.fill_light_intensity, LightIntensity.Medium);
selectedIntensity = currentIntensity;
originalIntensity = currentIntensity;
UpdateUI();
}
2026-05-18 08:42:33 +08:00
/// <summary>
/// 更新UI显示
/// </summary>
private void UpdateUI()
{
// 初始化开关
if (switchLight != null)
{
switchLight.Init(selectedLightEnabled, OnLightSwitchChanged);
}
// 更新类型文本
UpdateTypeText();
// 更新功率文本
UpdatePowerText();
}
/// <summary>
/// 更新类型文本
/// </summary>
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}");
}
}
/// <summary>
/// 更新功率文本
/// </summary>
private void UpdatePowerText()
{
if (textPower == null) return;
int powerIndex = (int)selectedIntensity;
if (powerIndex >= 0 && powerIndex < powerStrList.Count)
{
textPower.text = powerStrList[powerIndex];
}
}
/// <summary>
/// 灯光开关状态改变回调
/// </summary>
2026-06-17 15:42:55 +08:00
private async void OnLightSwitchChanged(bool isOn)
{
Debug.Log($"[LightSettingPage] 灯光开关: {(isOn ? "" : "")}");
selectedLightEnabled = isOn;
// 发送设置到设备
2026-06-17 15:42:55 +08:00
await SendLightSettingToDeviceAsync();
}
/// <summary>
/// 打开类型选择页面
/// </summary>
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<Text>();
if (optionText != null)
{
optionText.text = typeStrList[i];
}
}
2026-06-17 15:42:55 +08:00
selectPage.Init(currentIndex, async (selectedIndex) =>
{
Debug.Log($"[LightSettingPage] 选择类型: {selectedIndex}");
selectedLightType = (FillLightType)selectedIndex;
UpdateTypeText();
// 发送设置到设备
2026-06-17 15:42:55 +08:00
await SendLightSettingToDeviceAsync();
});
selectPage.gameObject.SetActive(true);
}
/// <summary>
/// 打开功率选择页面
/// </summary>
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<Text>();
if (optionText != null)
{
optionText.text = powerStrList[i];
}
}
2026-06-17 15:42:55 +08:00
selectPage.Init(currentIndex, async (selectedIndex) =>
{
Debug.Log($"[LightSettingPage] 选择功率: {selectedIndex}");
selectedIntensity = (LightIntensity)selectedIndex;
UpdatePowerText();
// 发送设置到设备
2026-06-17 15:42:55 +08:00
await SendLightSettingToDeviceAsync();
});
selectPage.gameObject.SetActive(true);
}
/// <summary>
/// 发送灯光设置到设备
/// </summary>
2026-06-17 15:42:55 +08:00
private async Task SendLightSettingToDeviceAsync()
{
FillLightControl lightControl = new FillLightControl
{
Enable = selectedLightEnabled,
LightType = selectedLightType,
Intensity = selectedIntensity
};
2026-06-17 15:42:55 +08:00
if (DataManager.Instance.hasWifi && !DataManager.Instance.hasBluetooth)
{
bool success = await HomePageCtrl.Instance.SendBleCommandByWifiAsync(
BLEConstants.CMD_FILL_LIGHT_CONTROL, lightControl.ToBytes());
if (success)
{
SyncLightConfigFromCurrent();
}
else
{
Debug.LogError("[LightSettingPage] 灯光设置失败");
}
}
else
{
BLECommunicationManager.Instance?.WriteFillLightControl(lightControl, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
Debug.Log($"[LightSettingPage] 灯光设置成功: 开关={selectedLightEnabled}, 类型={selectedLightType}, 强度={selectedIntensity}");
2026-06-17 15:42:55 +08:00
SyncLightConfigFromCurrent();
}
else
{
Debug.LogError("[LightSettingPage] 灯光设置失败");
// 设置失败恢复UI显示
selectedLightEnabled = currentLightEnabled;
selectedLightType = currentLightType;
selectedIntensity = currentIntensity;
UpdateUI();
}
});
});
2026-06-17 15:42:55 +08:00
}
}
private void SyncLightConfigFromCurrent()
{
var config = DataManager.Instance.deviceConfig;
config.fill_light_enable = selectedLightEnabled;
config.fill_light_type = DeviceConfig.ToServerString(selectedLightType);
config.fill_light_intensity = DeviceConfig.ToServerString(selectedIntensity);
}
/// <summary>
/// 返回按钮点击事件 - 直接关闭页面
/// </summary>
private void OnBackButtonClick()
{
Debug.Log("[LightSettingPage] 返回按钮点击,关闭页面");
ClosePage();
}
/// <summary>
/// 关闭页面
/// </summary>
public void ClosePage()
{
LoadingUI.Hide();
// 注销蓝牙回调(恢复主页)
UnregisterBluetoothCallbacks();
Destroy(gameObject);
NotifyHomePageRefresh();
// 清除UIManager返回事件
UIManager.Instance?.ClearBackAction();
}
/// <summary>
/// 通知主页刷新
/// </summary>
private void NotifyHomePageRefresh()
{
// 通知主页刷新灯光显示
HomePageCtrl.Instance?.RefreshLightControl();
}
}
}