148 lines
3.9 KiB
C#
148 lines
3.9 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
/// <summary>
|
||
/// 报警记录面板
|
||
/// 显示系统报警历史记录,支持滚动查看
|
||
/// </summary>
|
||
public class AlarmRecordPanel : BasePanel
|
||
{
|
||
[Header("滚动列表")]
|
||
public ScrollableListView AlarmListView; // 新的滚动列表组件
|
||
// public RectTransform ContentRoot; // 保留兼容性
|
||
// public GameObject RecordItemTemplate; // 预制体:包含Index, Priority, Reason, Time四个TMP文本
|
||
|
||
[Header("操作按钮")]
|
||
public Button HomeButton;
|
||
public Button BackButton;
|
||
public Button RefreshButton; // 刷新按钮
|
||
public Button ClearButton; // 清空记录按钮
|
||
|
||
[Header("状态显示")]
|
||
public TextMeshProUGUI StatusText; // 状态文本
|
||
public TextMeshProUGUI RecordCountText; // 记录数量文本
|
||
|
||
public override void Init()
|
||
{
|
||
InitializeUI();
|
||
Populate();
|
||
}
|
||
|
||
private void InitializeUI()
|
||
{
|
||
if (HomeButton != null)
|
||
HomeButton.onClick.AddListener(() =>
|
||
{
|
||
ReturnToHome();
|
||
});
|
||
|
||
if (BackButton != null)
|
||
BackButton.onClick.AddListener(() => ClosePanel());
|
||
|
||
if (RefreshButton != null)
|
||
RefreshButton.onClick.AddListener(OnRefreshClicked);
|
||
|
||
if (ClearButton != null)
|
||
ClearButton.onClick.AddListener(OnClearClicked);
|
||
|
||
SetStatus("正在加载报警记录...", true);
|
||
}
|
||
|
||
|
||
|
||
private void SetStatus(string message, bool isSuccess)
|
||
{
|
||
if (StatusText != null)
|
||
{
|
||
StatusText.text = message;
|
||
StatusText.color = isSuccess ? Color.green : Color.red;
|
||
}
|
||
}
|
||
|
||
private void UpdateRecordCount(int count)
|
||
{
|
||
if (RecordCountText != null)
|
||
{
|
||
RecordCountText.text = $"共 {count} 条记录";
|
||
}
|
||
}
|
||
|
||
private void OnRefreshClicked()
|
||
{
|
||
Populate();
|
||
SetStatus("记录已刷新", true);
|
||
}
|
||
|
||
private void OnClearClicked()
|
||
{
|
||
// 显示确认对话框
|
||
ConfirmDialog.ShowDeleteConfirm(
|
||
"所有报警记录",
|
||
() => ClearAllRecords(),
|
||
() => Debug.Log("取消清空记录")
|
||
);
|
||
}
|
||
|
||
private void ClearAllRecords()
|
||
{
|
||
// 优先使用DCSAlarmManager清空记录
|
||
if (DCSAlarmManager.Instance != null)
|
||
{
|
||
DCSAlarmManager.Instance.ClearAlarmRecords();
|
||
}
|
||
|
||
// 清空UI显示
|
||
if (AlarmListView != null)
|
||
{
|
||
AlarmListView.ClearItems();
|
||
}
|
||
|
||
UpdateRecordCount(0);
|
||
SetStatus("记录已清空", true);
|
||
}
|
||
|
||
private void Populate()
|
||
{
|
||
var list = DCSAlarmManager.Instance.GetAlarmRecords(1000);
|
||
if (list == null)
|
||
{
|
||
SetStatus("无报警记录", false);
|
||
UpdateRecordCount(0);
|
||
return;
|
||
}
|
||
|
||
// 优先使用新的ScrollableListView
|
||
if (AlarmListView != null)
|
||
{
|
||
PopulateWithScrollableList(list);
|
||
}
|
||
|
||
UpdateRecordCount(list.Count);
|
||
SetStatus($"成功加载 {list.Count} 条记录", true);
|
||
}
|
||
|
||
private void PopulateWithScrollableList(IReadOnlyList<AlarmEvent> records)
|
||
{
|
||
AlarmListView.ClearItems();
|
||
|
||
for (int i = 0; i < records.Count; i++)
|
||
{
|
||
var evt = records[i];
|
||
var listItem = AlarmListView.AddItem(evt);
|
||
if (listItem != null)
|
||
{
|
||
var alarmItem = listItem.gameObject.GetComponent<AlarmRecordItem>();
|
||
alarmItem.Initialize(evt, i + 1);
|
||
}
|
||
}
|
||
|
||
// 滚动到底部显示最新记录
|
||
AlarmListView.ScrollToBottom();
|
||
}
|
||
|
||
}
|
||
|