512 lines
17 KiB
C#
512 lines
17 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 SafetySettingPage : MonoBehaviour
|
||
{
|
||
public Switch visualDetectionSwitch;
|
||
public Text visualDetectionText;
|
||
/// <summary>
|
||
/// 灵敏度选择器 视觉检测和毫米波雷达通用
|
||
/// </summary>
|
||
public MutiSelectPage sensitivitySelect;
|
||
public Text MillimeterWaveRadarText;
|
||
public Button backBtn;
|
||
/// <summary>
|
||
/// 0视觉检测灵敏度选择 1毫米波雷达灵敏度选择 2试射测试
|
||
/// </summary>
|
||
public Button[] settingButtons;
|
||
|
||
// 灵敏度语言key
|
||
private List<string> sensitivityKeyList = new List<string> { "100114", "100115", "100116" };
|
||
private List<string> sensitivityStrList = new List<string>();
|
||
|
||
// 当前设置值
|
||
private bool currentVisualEnabled = false;
|
||
private bool selectedVisualEnabled = false;
|
||
private SensitivityLevel currentVisualSensitivity = SensitivityLevel.Medium;
|
||
private SensitivityLevel selectedVisualSensitivity = SensitivityLevel.Medium;
|
||
private SensitivityLevel currentWaveSensitivity = SensitivityLevel.Medium;
|
||
private SensitivityLevel selectedWaveSensitivity = SensitivityLevel.Medium;
|
||
|
||
// 进入设置前的原始值
|
||
private bool originalVisualEnabled = false;
|
||
private SensitivityLevel originalVisualSensitivity = SensitivityLevel.Medium;
|
||
private SensitivityLevel originalWaveSensitivity = SensitivityLevel.Medium;
|
||
|
||
// 静态标志:是否有设置页面正在处理回调
|
||
private static bool _isSettingPageActive = false;
|
||
|
||
void Start()
|
||
{
|
||
// 初始化语言文本
|
||
InitLanguageText();
|
||
// 绑定按钮事件
|
||
BindButtonEvents();
|
||
InitSafetySettingPage();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化语言文本
|
||
/// </summary>
|
||
private void InitLanguageText()
|
||
{
|
||
sensitivityStrList.Clear();
|
||
foreach (var key in sensitivityKeyList)
|
||
{
|
||
sensitivityStrList.Add(LanguageManager.Instance.GetLanguage(key));
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 绑定按钮事件
|
||
/// </summary>
|
||
private void BindButtonEvents()
|
||
{
|
||
// 绑定返回按钮
|
||
if (backBtn != null)
|
||
{
|
||
backBtn.onClick.RemoveAllListeners();
|
||
backBtn.onClick.AddListener(OnBackButtonClick);
|
||
}
|
||
|
||
// 绑定设置按钮
|
||
if (settingButtons != null && settingButtons.Length >= 3)
|
||
{
|
||
// 视觉检测灵敏度选择
|
||
if (settingButtons[0] != null)
|
||
{
|
||
settingButtons[0].onClick.RemoveAllListeners();
|
||
settingButtons[0].onClick.AddListener(OnClickVisualSensitivitySelect);
|
||
}
|
||
// 毫米波雷达灵敏度选择
|
||
if (settingButtons[1] != null)
|
||
{
|
||
settingButtons[1].onClick.RemoveAllListeners();
|
||
settingButtons[1].onClick.AddListener(OnClickWaveSensitivitySelect);
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化安全设置页面
|
||
/// </summary>
|
||
public void InitSafetySettingPage()
|
||
{
|
||
Debug.Log("[SafetySettingPage] 初始化安全设置页面");
|
||
|
||
// 注册蓝牙回调(顶替主页)
|
||
RegisterBluetoothCallbacks();
|
||
|
||
ReadSafetySettings();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册蓝牙回调(顶替主页)
|
||
/// </summary>
|
||
private void RegisterBluetoothCallbacks()
|
||
{
|
||
if (BLECommunicationManager.Instance != null)
|
||
{
|
||
// 设置标志位,表示设置页面正在处理
|
||
_isSettingPageActive = true;
|
||
|
||
// 订阅事件
|
||
BLECommunicationManager.Instance.OnVisualDetectionSettingReceived += OnVisualDetectionSettingReceived;
|
||
BLECommunicationManager.Instance.OnMillimeterWaveSettingReceived += OnMillimeterWaveSettingReceived;
|
||
|
||
Debug.Log("[SafetySettingPage] 蓝牙回调已注册");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注销蓝牙回调(恢复主页)
|
||
/// </summary>
|
||
private void UnregisterBluetoothCallbacks()
|
||
{
|
||
if (BLECommunicationManager.Instance != null)
|
||
{
|
||
// 取消订阅事件
|
||
BLECommunicationManager.Instance.OnVisualDetectionSettingReceived -= OnVisualDetectionSettingReceived;
|
||
BLECommunicationManager.Instance.OnMillimeterWaveSettingReceived -= OnMillimeterWaveSettingReceived;
|
||
|
||
// 清除标志位
|
||
_isSettingPageActive = false;
|
||
|
||
Debug.Log("[SafetySettingPage] 蓝牙回调已注销");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 视觉检测设置接收回调
|
||
/// </summary>
|
||
private void OnVisualDetectionSettingReceived(VisualDetectionSetting setting)
|
||
{
|
||
Loom.QueueOnMainThread(() =>
|
||
{
|
||
// 只有当前设置页面激活时才处理
|
||
if (!_isSettingPageActive) return;
|
||
|
||
currentVisualEnabled = setting.Enable;
|
||
selectedVisualEnabled = currentVisualEnabled;
|
||
originalVisualEnabled = currentVisualEnabled;
|
||
|
||
currentVisualSensitivity = setting.Sensitivity;
|
||
selectedVisualSensitivity = currentVisualSensitivity;
|
||
originalVisualSensitivity = currentVisualSensitivity;
|
||
|
||
Debug.Log($"[SafetySettingPage] 读取视觉检测设置: 开关={currentVisualEnabled}, 灵敏度={currentVisualSensitivity}");
|
||
|
||
// 然后读取毫米波雷达设置
|
||
BLECommunicationManager.Instance?.ReadMillimeterWaveSetting();
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 毫米波雷达设置接收回调
|
||
/// </summary>
|
||
private void OnMillimeterWaveSettingReceived(MillimeterWaveSetting setting)
|
||
{
|
||
Loom.QueueOnMainThread(() =>
|
||
{
|
||
// 只有当前设置页面激活时才处理
|
||
if (!_isSettingPageActive) return;
|
||
|
||
LoadingUI.Hide();
|
||
|
||
// 毫米波雷达保持开启,只读取灵敏度
|
||
currentWaveSensitivity = setting.Sensitivity;
|
||
selectedWaveSensitivity = currentWaveSensitivity;
|
||
originalWaveSensitivity = currentWaveSensitivity;
|
||
|
||
Debug.Log($"[SafetySettingPage] 读取毫米波雷达设置: 灵敏度={currentWaveSensitivity}");
|
||
|
||
UpdateUI();
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查是否有设置页面正在处理回调
|
||
/// </summary>
|
||
public static bool IsSettingPageActive()
|
||
{
|
||
return _isSettingPageActive;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 读取安全设置
|
||
/// </summary>
|
||
private void ReadSafetySettings()
|
||
{
|
||
#if UNITY_EDITOR
|
||
Editor_InitWithFakeData();
|
||
return;
|
||
#endif
|
||
|
||
LoadingUI.Show();
|
||
|
||
// 先读取视觉检测设置
|
||
BLECommunicationManager.Instance?.ReadVisualDetectionSetting();
|
||
}
|
||
|
||
#if UNITY_EDITOR
|
||
/// <summary>
|
||
/// 编辑器模式下使用假数据初始化
|
||
/// </summary>
|
||
private void Editor_InitWithFakeData()
|
||
{
|
||
Debug.Log("[SafetySettingPage] 编辑器模式:使用假数据初始化");
|
||
|
||
currentVisualEnabled = true;
|
||
selectedVisualEnabled = true;
|
||
currentVisualSensitivity = SensitivityLevel.High;
|
||
selectedVisualSensitivity = SensitivityLevel.High;
|
||
currentWaveSensitivity = SensitivityLevel.Medium;
|
||
selectedWaveSensitivity = SensitivityLevel.Medium;
|
||
|
||
UpdateUI();
|
||
}
|
||
#endif
|
||
|
||
|
||
/// <summary>
|
||
/// 更新UI显示
|
||
/// </summary>
|
||
private void UpdateUI()
|
||
{
|
||
// 初始化视觉检测开关
|
||
if (visualDetectionSwitch != null)
|
||
{
|
||
visualDetectionSwitch.Init(selectedVisualEnabled, OnVisualSwitchChanged);
|
||
}
|
||
|
||
// 更新视觉检测灵敏度文本
|
||
UpdateVisualDetectionSensitivityText();
|
||
|
||
// 更新毫米波雷达灵敏度文本
|
||
UpdateMillimeterWaveSensitivityText();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新视觉检测灵敏度文本
|
||
/// </summary>
|
||
private void UpdateVisualDetectionSensitivityText()
|
||
{
|
||
if (visualDetectionText == null) return;
|
||
|
||
visualDetectionText.text = GetSensitivityText(selectedVisualSensitivity);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新毫米波雷达灵敏度文本
|
||
/// </summary>
|
||
private void UpdateMillimeterWaveSensitivityText()
|
||
{
|
||
if (MillimeterWaveRadarText == null) return;
|
||
|
||
MillimeterWaveRadarText.text = GetSensitivityText(selectedWaveSensitivity);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取灵敏度显示文本
|
||
/// </summary>
|
||
private string GetSensitivityText(SensitivityLevel level)
|
||
{
|
||
int index = (int)level;
|
||
if (index >= 0 && index < sensitivityStrList.Count)
|
||
{
|
||
return sensitivityStrList[index];
|
||
}
|
||
return level.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 视觉检测开关状态改变回调
|
||
/// </summary>
|
||
private void OnVisualSwitchChanged(bool isOn)
|
||
{
|
||
Debug.Log($"[SafetySettingPage] 视觉检测开关: {(isOn ? "开" : "关")}");
|
||
selectedVisualEnabled = isOn;
|
||
|
||
#if UNITY_EDITOR
|
||
Debug.Log("[SafetySettingPage] 编辑器模式:模拟设置视觉检测开关");
|
||
currentVisualEnabled = selectedVisualEnabled;
|
||
return;
|
||
#endif
|
||
|
||
// 发送设置到设备
|
||
SendVisualDetectionSettingToDevice();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开视觉检测灵敏度选择页面
|
||
/// </summary>
|
||
public void OnClickVisualSensitivitySelect()
|
||
{
|
||
if (sensitivitySelect == null) return;
|
||
|
||
MutiSelectPage selectPage = Instantiate(sensitivitySelect, transform);
|
||
int currentIndex = (int)selectedVisualSensitivity;
|
||
|
||
// 设置选项文本
|
||
for (int i = 0; i < selectPage.options.Count && i < sensitivityStrList.Count; i++)
|
||
{
|
||
Text optionText = selectPage.options[i].GetComponentInChildren<Text>();
|
||
if (optionText != null)
|
||
{
|
||
optionText.text = sensitivityStrList[i];
|
||
}
|
||
}
|
||
|
||
selectPage.Init(currentIndex, (selectedIndex) =>
|
||
{
|
||
Debug.Log($"[SafetySettingPage] 选择视觉检测灵敏度: {selectedIndex}");
|
||
selectedVisualSensitivity = (SensitivityLevel)selectedIndex;
|
||
UpdateVisualDetectionSensitivityText();
|
||
|
||
#if UNITY_EDITOR
|
||
Debug.Log("[SafetySettingPage] 编辑器模式:模拟设置视觉检测灵敏度");
|
||
currentVisualSensitivity = selectedVisualSensitivity;
|
||
return;
|
||
#endif
|
||
|
||
SendVisualDetectionSettingToDevice();
|
||
});
|
||
selectPage.gameObject.SetActive(true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 打开毫米波雷达灵敏度选择页面
|
||
/// </summary>
|
||
public void OnClickWaveSensitivitySelect()
|
||
{
|
||
if (sensitivitySelect == null) return;
|
||
|
||
MutiSelectPage selectPage = Instantiate(sensitivitySelect, transform);
|
||
int currentIndex = (int)selectedWaveSensitivity;
|
||
|
||
// 设置选项文本
|
||
for (int i = 0; i < selectPage.options.Count && i < sensitivityStrList.Count; i++)
|
||
{
|
||
Text optionText = selectPage.options[i].GetComponentInChildren<Text>();
|
||
if (optionText != null)
|
||
{
|
||
optionText.text = sensitivityStrList[i];
|
||
}
|
||
}
|
||
|
||
selectPage.Init(currentIndex, (selectedIndex) =>
|
||
{
|
||
Debug.Log($"[SafetySettingPage] 选择毫米波雷达灵敏度: {selectedIndex}");
|
||
selectedWaveSensitivity = (SensitivityLevel)selectedIndex;
|
||
UpdateMillimeterWaveSensitivityText();
|
||
|
||
#if UNITY_EDITOR
|
||
Debug.Log("[SafetySettingPage] 编辑器模式:模拟设置毫米波雷达灵敏度");
|
||
currentWaveSensitivity = selectedWaveSensitivity;
|
||
return;
|
||
#endif
|
||
|
||
SendMillimeterWaveSettingToDevice();
|
||
});
|
||
selectPage.gameObject.SetActive(true);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 试射测试按钮点击
|
||
/// </summary>
|
||
public void OnClickFireTest()
|
||
{
|
||
Debug.Log("[SafetySettingPage] 点击试射测试按钮");
|
||
|
||
#if UNITY_EDITOR
|
||
Debug.Log("[SafetySettingPage] 编辑器模式:模拟试射测试");
|
||
return;
|
||
#endif
|
||
|
||
LoadingUI.Show();
|
||
BLECommunicationManager.Instance?.TriggerLaserTest((success) =>
|
||
{
|
||
LoadingUI.Hide();
|
||
if (success)
|
||
{
|
||
Debug.Log("[SafetySettingPage] 试射测试已触发");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("[SafetySettingPage] 试射测试触发失败");
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送视觉检测设置到设备
|
||
/// </summary>
|
||
private void SendVisualDetectionSettingToDevice()
|
||
{
|
||
VisualDetectionSetting setting = new VisualDetectionSetting
|
||
{
|
||
Enable = selectedVisualEnabled,
|
||
Sensitivity = selectedVisualSensitivity
|
||
};
|
||
|
||
BLECommunicationManager.Instance?.WriteVisualDetectionSetting(setting, (success) =>
|
||
{
|
||
Loom.QueueOnMainThread(() =>
|
||
{
|
||
if (success)
|
||
{
|
||
Debug.Log($"[SafetySettingPage] 视觉检测设置成功: 开关={selectedVisualEnabled}, 灵敏度={selectedVisualSensitivity}");
|
||
currentVisualEnabled = selectedVisualEnabled;
|
||
currentVisualSensitivity = selectedVisualSensitivity;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("[SafetySettingPage] 视觉检测设置失败");
|
||
selectedVisualEnabled = currentVisualEnabled;
|
||
selectedVisualSensitivity = currentVisualSensitivity;
|
||
UpdateVisualDetectionSensitivityText();
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 发送毫米波雷达设置到设备(保持开启,只设置灵敏度)
|
||
/// </summary>
|
||
private void SendMillimeterWaveSettingToDevice()
|
||
{
|
||
// 毫米波雷达保持开启,只修改灵敏度
|
||
MillimeterWaveSetting setting = new MillimeterWaveSetting
|
||
{
|
||
Enable = true, // 保持开启
|
||
Sensitivity = selectedWaveSensitivity,
|
||
SafeDistance = 10 // 默认安全距离1米(单位0.1米)
|
||
};
|
||
|
||
BLECommunicationManager.Instance?.WriteMillimeterWaveSetting(setting, (success) =>
|
||
{
|
||
Loom.QueueOnMainThread(() =>
|
||
{
|
||
if (success)
|
||
{
|
||
Debug.Log($"[SafetySettingPage] 毫米波雷达设置成功: 灵敏度={selectedWaveSensitivity}");
|
||
currentWaveSensitivity = selectedWaveSensitivity;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError("[SafetySettingPage] 毫米波雷达设置失败");
|
||
selectedWaveSensitivity = currentWaveSensitivity;
|
||
UpdateMillimeterWaveSensitivityText();
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回按钮点击事件 - 直接关闭页面
|
||
/// </summary>
|
||
private void OnBackButtonClick()
|
||
{
|
||
Debug.Log("[SafetySettingPage] 返回按钮点击,关闭页面");
|
||
ClosePage();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 关闭页面
|
||
/// </summary>
|
||
public void ClosePage()
|
||
{
|
||
LoadingUI.Hide();
|
||
// 注销蓝牙回调(恢复主页)
|
||
UnregisterBluetoothCallbacks();
|
||
|
||
Destroy(gameObject);
|
||
NotifyHomePageRefresh();
|
||
// 清除UIManager返回事件
|
||
UIManager.Instance?.ClearBackAction();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通知主页刷新
|
||
/// </summary>
|
||
private void NotifyHomePageRefresh()
|
||
{
|
||
// 通知主页刷新安全设置显示
|
||
BLECommunicationManager.Instance?.ReadVisualDetectionSetting();
|
||
BLECommunicationManager.Instance?.ReadMillimeterWaveSetting();
|
||
}
|
||
}
|
||
}
|