using System;
using System.Collections.Generic;
using System.IO;
using Aisu.SIM1.Face.Core;
using Aisu.SIM1.Face.Protocol;
using UnityEngine;
using UnityEngine.UI;
namespace Aisu.SIM1.Face.UI.Common
{
///
/// FaceModule UI 公共状态。
///
public sealed class FaceModuleUiState
{
// 公开数据字段。
public bool IsBusy;
// 公开数据字段。
public string CurrentPage;
// 公开数据字段。
public string LastResult;
// 公开数据字段。
public float Progress;
}
///
/// 所有功能页面的基类。
///
public abstract class FaceModulePageBase : MonoBehaviour
{
// 内部状态字段。
protected FaceModuleManager m_manager;
// 内部状态字段。
protected FaceModuleLogPanel m_logPanel;
// 内部状态字段。
protected FaceModuleLoadingView m_loadingView;
// 内部状态字段。
protected Text m_resultText;
///
/// 初始化页面。
///
public virtual void Init(FaceModuleManager manager, FaceModuleLogPanel logPanel, FaceModuleLoadingView loadingView)
{
m_manager = manager;
m_logPanel = logPanel;
m_loadingView = loadingView;
}
///
/// 页面显示。
///
public virtual void Show()
{
gameObject.SetActive(true);
}
///
/// 页面隐藏。
///
public virtual void Hide()
{
gameObject.SetActive(false);
}
///
/// 由代码生成页面内容。
///
public virtual void Build(FaceModuleUiBuilder builder)
{
}
///
/// 执行 RequireConnected 方法。
///
protected bool RequireConnected()
{
if (m_manager != null && m_manager.IsOpen)
{
return true;
}
SetResult("模组未连接");
return false;
}
///
/// 执行 RequireReady 方法。
///
protected bool RequireReady()
{
if (RequireConnected() && m_manager.IsReady)
{
return true;
}
SetResult("模组未 READY");
return false;
}
///
/// 执行 SetResult 方法。
///
protected void SetResult(string message)
{
if (m_resultText != null)
{
m_resultText.text = message;
}
m_logPanel?.Info(message);
}
///
/// 执行 SetError 方法。
///
protected void SetError(Exception exception)
{
SetResult(exception.Message);
m_logPanel?.Error(exception.Message);
Debug.LogException(exception);
}
///
/// 执行 ReadByte 方法。
///
protected byte ReadByte(InputField input, byte fallback)
{
return input != null && byte.TryParse(input.text, out var value) ? value : fallback;
}
///
/// 执行 ReadInt 方法。
///
protected int ReadInt(InputField input, int fallback)
{
return input != null && int.TryParse(input.text, out var value) ? value : fallback;
}
///
/// 执行 ReadText 方法。
///
protected string ReadText(InputField input)
{
return input != null ? input.text : string.Empty;
}
///
/// 执行 RunWithButton 方法。
///
protected void RunWithButton(Button button, Func action)
{
if (button != null)
{
button.interactable = false;
}
RunAsync(async () =>
{
try
{
await action();
}
finally
{
if (button != null)
{
button.interactable = true;
}
}
});
}
///
/// 执行 RunAsync 方法。
///
protected async void RunAsync(Func action)
{
try
{
m_loadingView?.Show("处理中...");
await action();
}
catch (Exception ex)
{
SetError(ex);
}
finally
{
m_loadingView?.Hide();
}
}
}
///
/// 顶部状态栏。
///
public sealed class FaceModuleStatusBar : MonoBehaviour
{
// Inspector 绑定字段。
[SerializeField] private Text m_connectionText;
// Inspector 绑定字段。
[SerializeField] private Text m_readyText;
// Inspector 绑定字段。
[SerializeField] private Text m_statusText;
// Inspector 绑定字段。
[SerializeField] private Text m_commandText;
// Inspector 绑定字段。
[SerializeField] private Text m_errorText;
// 内部状态字段。
private FaceModuleManager m_manager;
private Action m_errorHandler; // Manager 错误事件回调。
#region Unity API
///
/// 执行 Update 方法。
///
private void Update()
{
Refresh();
}
#endregion
///
/// 初始化状态栏。
///
public void Init(FaceModuleManager manager)
{
if (m_manager != null && m_errorHandler != null)
{
m_manager.OnError -= m_errorHandler;
}
m_manager = manager;
if (m_manager != null)
{
m_errorHandler = (_, message) =>
{
if (m_errorText != null)
{
m_errorText.text = "错误: " + message;
}
};
m_manager.OnError += m_errorHandler;
}
Refresh();
}
private void OnDestroy()
{
if (m_manager != null && m_errorHandler != null)
{
m_manager.OnError -= m_errorHandler;
}
}
///
/// 绑定状态栏文本。
///
public void Bind(Text connectionText, Text readyText, Text statusText, Text commandText, Text errorText)
{
m_connectionText = connectionText;
m_readyText = readyText;
m_statusText = statusText;
m_commandText = commandText;
m_errorText = errorText;
}
///
/// 刷新显示。
///
public void Refresh()
{
if (m_manager == null)
{
return;
}
if (m_connectionText != null) m_connectionText.text = m_manager.IsReady ? "硬件已连接" : (m_manager.IsOpen ? "端口已打开" : "未连接");
if (m_readyText != null) m_readyText.text = m_manager.IsReady ? "READY" : "未 READY";
if (m_statusText != null) m_statusText.text = "状态: " + m_manager.CurrentStatus;
if (m_commandText != null) m_commandText.text = "命令: " + m_manager.CurrentCommand;
if (m_errorText != null && !string.IsNullOrEmpty(m_manager.LastError)) m_errorText.text = "错误: " + m_manager.LastError;
}
}
///
/// 左侧导航栏。
///
public sealed class FaceModuleNavigationPanel : MonoBehaviour
{
private readonly List