killapp/Assets/Scripts/UI/Pages/DeviceInfoPage/VideoAndSoundSetting.cs

334 lines
11 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 VideoAndSoundSetting : MonoBehaviour
{
public Switch videoSwitch;
public Switch soundSwitch;
public MutiSelectPage videoLengthSelect;
public MutiSelectPage soundEffectSelect;
public Slider volumeSlider;
public Text videoLengthValueText;
public Text soundEffectValueText;
public Text volumeValueText;
// 视频时长选项3秒、5秒、10秒
private List<string> videoLengthKeyList = new List<string> { "100180", "100181", "100182" };
private List<string> videoLengthStrList = new List<string>();
private List<byte> videoLengthValueList = new List<byte> { 0x03, 0x05, 0x0A };
// 音效类型选项
private List<string> soundEffectKeyList = new List<string> { "100183", "100184", "100185", "100186", "100187" };
private List<string> soundEffectStrList = new List<string>();
private List<SoundEffectType> soundEffectValueList = new List<SoundEffectType>
{
SoundEffectType.Hello,
SoundEffectType.Goodbye,
SoundEffectType.Test,
SoundEffectType.BIU,
SoundEffectType.Main
};
private MultimediaControl multimediaSetting;
private Action callback;
public void Init(Action callback)
{
this.callback = callback;
InitLanguageText();
ReadMultimediaSettings();
UIManager.Instance.RegisterBackAction(Back);
volumeSlider.onValueChanged.RemoveAllListeners();
volumeSlider.onValueChanged.AddListener(OnVolumeChanged);
}
/// <summary>
/// 初始化语言文本
/// </summary>
private void InitLanguageText()
{
videoLengthStrList.Clear();
foreach (var key in videoLengthKeyList)
{
videoLengthStrList.Add(LanguageManager.Instance.GetLanguage(key));
}
soundEffectStrList.Clear();
foreach (var key in soundEffectKeyList)
{
soundEffectStrList.Add(LanguageManager.Instance.GetLanguage(key));
}
}
/// <summary>
/// 读取多媒体设置
/// </summary>
private void ReadMultimediaSettings()
{
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
MultimediaConfigFromServer();
return;
}
2026-05-18 08:42:33 +08:00
LoadingUI.Show();
BLECommunicationManager.Instance.ReadMultimediaControl((setting) =>
{
Loom.QueueOnMainThread(() =>
{
multimediaSetting = setting;
UpdateUI();
});
});
}
/// <summary>
/// 写入多媒体设置
/// </summary>
2026-06-17 15:42:55 +08:00
private async void WriteMultimediaSetting()
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_MULTIMEDIA_CONTROL, multimediaSetting.ToBytes());
if (success)
{
SyncMultimediaConfig();
UpdateUI();
}
else
{
ReadMultimediaSettings();
}
return;
}
2026-05-18 08:42:33 +08:00
LoadingUI.Show();
BLECommunicationManager.Instance.WriteMultimediaControl(multimediaSetting, (success) =>
{
Loom.QueueOnMainThread(() =>
{
if (success)
{
2026-06-17 15:42:55 +08:00
SyncMultimediaConfig();
2026-05-18 08:42:33 +08:00
UpdateUI();
}
else
{
ReadMultimediaSettings();
}
});
});
}
2026-06-17 15:42:55 +08:00
private void MultimediaConfigFromServer()
{
var config = DataManager.Instance.deviceConfig;
multimediaSetting = new MultimediaControl
{
VideoRecordEnable = config?.video_record_enable ?? false,
RecordDuration = config?.record_duration ?? 0x05,
SoundEnable = config?.sound_enable ?? true,
SoundType = DeviceConfig.ParseServerEnum(config?.sound_type, SoundEffectType.Hello),
Volume = config?.volume ?? 10
};
UpdateUI();
}
private void SyncMultimediaConfig()
{
var config = DataManager.Instance.deviceConfig;
config.video_record_enable = multimediaSetting.VideoRecordEnable;
config.record_duration = multimediaSetting.RecordDuration;
config.sound_enable = multimediaSetting.SoundEnable;
config.sound_type = DeviceConfig.ToServerString(multimediaSetting.SoundType);
config.volume = multimediaSetting.Volume;
}
2026-05-18 08:42:33 +08:00
/// <summary>
/// 更新UI显示
/// </summary>
private void UpdateUI()
{
// 更新视频录制开关
videoSwitch.Init(multimediaSetting.VideoRecordEnable, OnVideoSwitchChanged);
// 更新音效开关
soundSwitch.Init(multimediaSetting.SoundEnable, OnSoundSwitchChanged);
// 更新视频时长显示
int videoLengthIndex = GetVideoLengthIndexFromValue(multimediaSetting.RecordDuration);
if (videoLengthValueText != null && videoLengthIndex >= 0 && videoLengthIndex < videoLengthStrList.Count)
{
videoLengthValueText.text = videoLengthStrList[videoLengthIndex];
}
// 更新音效类型显示
int soundEffectIndex = GetSoundEffectIndexFromValue(multimediaSetting.SoundType);
if (soundEffectValueText != null && soundEffectIndex >= 0 && soundEffectIndex < soundEffectStrList.Count)
{
soundEffectValueText.text = soundEffectStrList[soundEffectIndex];
}
// 更新音量滑块
volumeSlider.value = multimediaSetting.Volume;
volumeSlider.onValueChanged.RemoveAllListeners();
volumeSlider.onValueChanged.AddListener(OnVolumeChanged);
if (volumeValueText != null)
{
volumeValueText.text = $"{multimediaSetting.Volume}";
}
LoadingUI.Hide();
UIManager.Instance.RegisterBackAction(Back);
}
/// <summary>
/// 视频录制开关变化
/// </summary>
private void OnVideoSwitchChanged(bool isOn)
{
multimediaSetting.VideoRecordEnable = isOn;
WriteMultimediaSetting();
}
/// <summary>
/// 音效开关变化
/// </summary>
private void OnSoundSwitchChanged(bool isOn)
{
multimediaSetting.SoundEnable = isOn;
WriteMultimediaSetting();
}
Coroutine volumeChangeCoroutine;
/// <summary>
/// 音量滑块变化
/// </summary>
private void OnVolumeChanged(float value)
{
byte volume = (byte)Mathf.Clamp(value, 0, 15);
multimediaSetting.Volume = volume;
if (volumeValueText != null)
{
volumeValueText.text = $"{volume}";
}
if (volumeChangeCoroutine != null)
{
StopCoroutine(volumeChangeCoroutine);
}
volumeChangeCoroutine = StartCoroutine(CountDownAndWriteMultimediaSetting(0.5f));
}
private IEnumerator CountDownAndWriteMultimediaSetting(float delay)
{
yield return new WaitForSeconds(delay);
WriteMultimediaSetting();
}
/// <summary>
/// 音量滑块拖动结束
/// </summary>
public void OnVolumeSliderEndDrag()
{
WriteMultimediaSetting();
}
/// <summary>
/// 点击视频时长设置
/// </summary>
public void ClickVideoLength()
{
MutiSelectPage m = Instantiate(videoLengthSelect.gameObject, transform).GetComponent<MutiSelectPage>();
int currentIndex = GetVideoLengthIndexFromValue(multimediaSetting.RecordDuration);
m.Init(currentIndex, OnVideoLengthSelected);
m.gameObject.SetActive(true);
UIManager.Instance.RegisterBackAction(m.Cancel);
}
/// <summary>
/// 视频时长选择回调
/// </summary>
private void OnVideoLengthSelected(int index)
{
if (index >= 0 && index < videoLengthValueList.Count)
{
multimediaSetting.RecordDuration = videoLengthValueList[index];
WriteMultimediaSetting();
}
}
/// <summary>
/// 点击音效类型设置
/// </summary>
public void ClickSoundEffect()
{
MutiSelectPage m = Instantiate(soundEffectSelect.gameObject, transform).GetComponent<MutiSelectPage>();
int currentIndex = GetSoundEffectIndexFromValue(multimediaSetting.SoundType);
m.Init(currentIndex, OnSoundEffectSelected);
m.gameObject.SetActive(true);
UIManager.Instance.RegisterBackAction(m.Cancel);
}
/// <summary>
/// 音效类型选择回调
/// </summary>
private void OnSoundEffectSelected(int index)
{
if (index >= 0 && index < soundEffectValueList.Count)
{
multimediaSetting.SoundType = soundEffectValueList[index];
WriteMultimediaSetting();
}
}
/// <summary>
/// 根据视频时长值获取选项索引
/// </summary>
private int GetVideoLengthIndexFromValue(byte value)
{
for (int i = 0; i < videoLengthValueList.Count; i++)
{
if (videoLengthValueList[i] == value)
{
return i;
}
}
// 默认值返回5秒(索引1)
return 1;
}
/// <summary>
/// 根据音效类型值获取选项索引
/// </summary>
private int GetSoundEffectIndexFromValue(SoundEffectType value)
{
for (int i = 0; i < soundEffectValueList.Count; i++)
{
if (soundEffectValueList[i] == value)
{
return i;
}
}
// 默认值返回Hello(索引0)
return 0;
}
public void Back()
{
callback?.Invoke();
Destroy(gameObject);
}
}
}