si-m1-face-package/Samples/SI-M1 Face Demo/UI/Common/FaceModuleDemoBootstrap.cs

174 lines
9.5 KiB
C#

using Aisu.SIM1.Face.Core;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Aisu.SIM1.Face.UI.Common
{
/// <summary>
/// Demo 场景运行时构建器。
/// </summary>
public sealed class FaceModuleDemoBootstrap : MonoBehaviour
{
#region Unity API
/// <summary>
/// 执行 Awake 方法。
/// </summary>
private void Awake()
{
Build();
}
#endregion
/// <summary>
/// 构建完整 UGUI 页面。
/// </summary>
public void Build()
{
if (FindObjectOfType<EventSystem>() == null)
{
var eventSystem = new GameObject("EventSystem");
eventSystem.AddComponent<EventSystem>();
eventSystem.AddComponent<StandaloneInputModule>();
}
var font = Resources.GetBuiltinResource<Font>("Arial.ttf");
var builder = new FaceModuleUiBuilder(font);
var manager = FindObjectOfType<FaceModuleManager>() ?? new GameObject("FaceModuleManager").AddComponent<FaceModuleManager>();
var canvasGo = FaceModuleUiBuilder.Create("Canvas", transform);
var canvas = canvasGo.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvasGo.AddComponent<GraphicRaycaster>();
var scaler = canvasGo.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(1920, 1080);
var safe = FaceModuleUiBuilder.Create("SafeArea", canvasGo.transform);
FaceModuleUiBuilder.Stretch(safe.GetComponent<RectTransform>());
var safeLayout = safe.AddComponent<VerticalLayoutGroup>();
safeLayout.padding = new RectOffset(16, 16, 16, 16);
safeLayout.spacing = 12;
safeLayout.childControlWidth = true;
safeLayout.childForceExpandWidth = true;
var top = FaceModuleUiBuilder.Create("TopStatusBar", safe.transform);
top.AddComponent<Image>().color = new Color(0.9f, 0.94f, 0.97f);
var topLayoutElement = top.AddComponent<LayoutElement>();
topLayoutElement.preferredHeight = 54;
var topLayout = top.AddComponent<HorizontalLayoutGroup>();
topLayout.padding = new RectOffset(12, 12, 8, 8);
topLayout.spacing = 20;
var statusBar = top.AddComponent<FaceModuleStatusBar>();
statusBar.Bind(
builder.Text(top.transform, "未连接", 20),
builder.Text(top.transform, "未 READY", 20),
builder.Text(top.transform, "状态: Invalid", 20),
builder.Text(top.transform, "命令:", 20),
builder.Text(top.transform, "错误:", 20));
var body = FaceModuleUiBuilder.Create("Body", safe.transform);
var bodyLayoutElement = body.AddComponent<LayoutElement>();
bodyLayoutElement.flexibleHeight = 1;
var bodyLayout = body.AddComponent<HorizontalLayoutGroup>();
bodyLayout.spacing = 12;
bodyLayout.childControlHeight = true;
bodyLayout.childForceExpandHeight = true;
var navigation = FaceModuleUiBuilder.Create("NavigationPanel", body.transform).AddComponent<FaceModuleNavigationPanel>();
navigation.gameObject.AddComponent<Image>().color = new Color(0.86f, 0.9f, 0.94f);
var navLayoutElement = navigation.gameObject.AddComponent<LayoutElement>();
navLayoutElement.preferredWidth = 240;
var navLayout = navigation.gameObject.AddComponent<VerticalLayoutGroup>();
navLayout.padding = new RectOffset(10, 10, 10, 10);
navLayout.spacing = 8;
var pageContainer = FaceModuleUiBuilder.Create("PageContainer", body.transform);
pageContainer.AddComponent<Image>().color = new Color(0.98f, 0.99f, 1f);
pageContainer.AddComponent<LayoutElement>().flexibleWidth = 1;
var bottom = FaceModuleUiBuilder.Create("BottomLogPanel", safe.transform);
bottom.AddComponent<Image>().color = new Color(0.08f, 0.09f, 0.1f);
bottom.AddComponent<LayoutElement>().preferredHeight = 220;
var bottomLayout = bottom.AddComponent<VerticalLayoutGroup>();
bottomLayout.padding = new RectOffset(8, 8, 8, 8);
bottomLayout.spacing = 6;
var logPanel = bottom.AddComponent<FaceModuleLogPanel>();
var autoScroll = builder.Toggle(bottom.transform, "自动滚动", true);
var scroll = builder.ScrollView(bottom.transform, out var logText);
logText.color = Color.white;
logPanel.Bind(logText, scroll, autoScroll);
var logButtons = builder.Row(bottom.transform);
builder.Button(logButtons.transform, "清空日志", logPanel.Clear);
builder.Button(logButtons.transform, "保存日志", logPanel.Save);
var loadingGo = FaceModuleUiBuilder.Create("LoadingView", canvasGo.transform);
loadingGo.AddComponent<Image>().color = new Color(0, 0, 0, 0.25f);
FaceModuleUiBuilder.Stretch(loadingGo.GetComponent<RectTransform>());
var loadingLayout = loadingGo.AddComponent<VerticalLayoutGroup>();
loadingLayout.childAlignment = TextAnchor.MiddleCenter;
var loading = loadingGo.AddComponent<FaceModuleLoadingView>();
loading.Bind(builder.Text(loadingGo.transform, "处理中...", 28, TextAnchor.MiddleCenter), builder.Slider(loadingGo.transform));
var dialogGo = FaceModuleUiBuilder.Create("ConfirmDialog", canvasGo.transform);
dialogGo.AddComponent<Image>().color = new Color(0, 0, 0, 0.35f);
FaceModuleUiBuilder.Stretch(dialogGo.GetComponent<RectTransform>());
var dialogLayout = dialogGo.AddComponent<VerticalLayoutGroup>();
dialogLayout.childAlignment = TextAnchor.MiddleCenter;
dialogLayout.spacing = 12;
var dialog = dialogGo.AddComponent<FaceModuleConfirmDialog>();
var dialogText = builder.Text(dialogGo.transform, "确认操作?", 26, TextAnchor.MiddleCenter);
var dialogButtons = builder.Row(dialogGo.transform);
var confirmButton = builder.Button(dialogButtons.transform, "确认", () => { });
var cancelButton = builder.Button(dialogButtons.transform, "取消", () => { });
dialog.Bind(dialogText, confirmButton, cancelButton);
var mainPanel = canvasGo.AddComponent<FaceModuleMainPanel>();
mainPanel.Bind(statusBar, navigation, logPanel, loading);
BuildPages(pageContainer.transform, builder, mainPanel);
mainPanel.Init(manager);
mainPanel.ShowPage("Connect");
}
/// <summary>
/// 构建 Demo 功能页面。
/// </summary>
private void BuildPages(Transform pageContainer, FaceModuleUiBuilder builder, FaceModuleMainPanel mainPanel)
{
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceConnectPage>(pageContainer, builder, mainPanel, "Connect", "连接");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceProtocolDebugPage>(pageContainer, builder, mainPanel, "ProtocolDebug", "协议调试");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceVerifyPage>(pageContainer, builder, mainPanel, "Verify", "人脸验证");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceEnrollPage>(pageContainer, builder, mainPanel, "Enroll", "人脸录入");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceUserManagePage>(pageContainer, builder, mainPanel, "UserManage", "用户管理");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceQrCodePage>(pageContainer, builder, mainPanel, "QrCode", "二维码");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceImagePage>(pageContainer, builder, mainPanel, "Image", "图片抓拍");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FacePhotoEnrollPage>(pageContainer, builder, mainPanel, "PhotoEnroll", "照片注册");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceFeaturePage>(pageContainer, builder, mainPanel, "Feature", "特征管理");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceUvcSettingsPage>(pageContainer, builder, mainPanel, "Uvc", "UVC 设置");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceAdvancedSettingsPage>(pageContainer, builder, mainPanel, "Advanced", "高级设置");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceEncryptionPage>(pageContainer, builder, mainPanel, "Encryption", "加密");
AddPage<global::Aisu.SIM1.Face.UI.Pages.FaceFirmwarePage>(pageContainer, builder, mainPanel, "Firmware", "固件升级");
}
/// <summary>
/// 添加 Demo 功能页面。
/// </summary>
private void AddPage<T>(Transform parent, FaceModuleUiBuilder builder, FaceModuleMainPanel mainPanel, string pageName, string label) where T : FaceModulePageBase
{
var pageGo = FaceModuleUiBuilder.Create(pageName + "Page", parent);
FaceModuleUiBuilder.Stretch(pageGo.GetComponent<RectTransform>());
var layout = pageGo.AddComponent<VerticalLayoutGroup>();
layout.padding = new RectOffset(16, 16, 16, 16);
layout.spacing = 10;
layout.childControlWidth = true;
layout.childForceExpandWidth = true;
var page = pageGo.AddComponent<T>();
page.Build(builder);
mainPanel.RegisterPage(pageName, label, page, builder);
}
}
}