450 lines
14 KiB
C#
450 lines
14 KiB
C#
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<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()
|
||
{
|
||
#if UNITY_EDITOR
|
||
// 编辑器模式下使用假数据
|
||
Editor_InitWithFakeData();
|
||
return;
|
||
#endif
|
||
|
||
// 显示Loading
|
||
LoadingUI.Show();
|
||
|
||
// 读取补光灯控制
|
||
BLECommunicationManager.Instance?.ReadFillLightControl();
|
||
|
||
}
|
||
|
||
#if UNITY_EDITOR
|
||
/// <summary>
|
||
/// 编辑器模式下使用假数据初始化
|
||
/// </summary>
|
||
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
|
||
|
||
|
||
|
||
/// <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>
|
||
private void OnLightSwitchChanged(bool isOn)
|
||
{
|
||
Debug.Log($"[LightSettingPage] 灯光开关: {(isOn ? "开" : "关")}");
|
||
selectedLightEnabled = isOn;
|
||
|
||
#if UNITY_EDITOR
|
||
Debug.Log("[LightSettingPage] 编辑器模式:模拟设置灯光开关");
|
||
currentLightEnabled = selectedLightEnabled;
|
||
return;
|
||
#endif
|
||
|
||
// 发送设置到设备
|
||
SendLightSettingToDevice();
|
||
}
|
||
|
||
/// <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];
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
/// <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];
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送灯光设置到设备
|
||
/// </summary>
|
||
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();
|
||
}
|
||
});
|
||
|
||
});
|
||
}
|
||
|
||
/// <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();
|
||
}
|
||
}
|
||
}
|