Files
2026-02-21 16:45:37 +08:00

160 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using SRDebugger.Internal;
using SRDebugger.Services;
using SRF;
using SRF.UI.Layout;
using UnityEngine;
using UnityEngine.UI;
namespace SRDebugger.UI.Controls
{
public class ConsoleLogControl : SRMonoBehaviourEx
{
[SerializeField]
[RequiredField]
private VirtualVerticalLayoutGroup _consoleScrollLayoutGroup;
[SerializeField]
[RequiredField]
private ScrollRect _consoleScrollRect;
private bool _isDirty;
private bool _showErrors = true;
private bool _showInfo = true;
private bool _showWarnings = true;
public Action<ConsoleEntry> SelectedItemChanged;
private Vector2? _scrollPosition;
public bool ShowErrors
{
get
{
return _showErrors;
}
set
{
_showErrors = value;
SetIsDirty();
}
}
public bool ShowWarnings
{
get
{
return _showWarnings;
}
set
{
_showWarnings = value;
SetIsDirty();
}
}
public bool ShowInfo
{
get
{
return _showInfo;
}
set
{
_showInfo = value;
SetIsDirty();
}
}
public bool EnableSelection
{
get
{
return _consoleScrollLayoutGroup.EnableSelection;
}
set
{
_consoleScrollLayoutGroup.EnableSelection = value;
}
}
protected override void Awake()
{
base.Awake();
_consoleScrollLayoutGroup.SelectedItemChanged.AddListener(OnSelectedItemChanged);
Service.Console.Updated += ConsoleOnUpdated;
}
protected override void Start()
{
base.Start();
SetIsDirty();
}
protected override void OnDestroy()
{
if (Service.Console != null)
{
Service.Console.Updated -= ConsoleOnUpdated;
}
base.OnDestroy();
}
private void OnSelectedItemChanged(object arg0)
{
ConsoleEntry obj = arg0 as ConsoleEntry;
if (SelectedItemChanged != null)
{
SelectedItemChanged(obj);
}
}
protected override void Update()
{
base.Update();
if (_scrollPosition.HasValue)
{
_consoleScrollRect.normalizedPosition = _scrollPosition.Value;
_scrollPosition = null;
}
if (_isDirty)
{
Refresh();
}
}
private void Refresh()
{
if (_consoleScrollRect.normalizedPosition.y.ApproxZero())
{
_scrollPosition = _consoleScrollRect.normalizedPosition;
}
_consoleScrollLayoutGroup.ClearItems();
IList<ConsoleEntry> entries = Service.Console.Entries;
for (int i = 0; i < entries.Count; i++)
{
ConsoleEntry consoleEntry = entries[i];
if (((consoleEntry.LogType != LogType.Error && consoleEntry.LogType != LogType.Exception && consoleEntry.LogType != LogType.Assert) || ShowErrors) && (consoleEntry.LogType != LogType.Warning || ShowWarnings) && (consoleEntry.LogType != LogType.Log || ShowInfo))
{
_consoleScrollLayoutGroup.AddItem(consoleEntry);
}
}
_isDirty = false;
}
private void SetIsDirty()
{
_isDirty = true;
}
private void ConsoleOnUpdated(IConsoleService console)
{
SetIsDirty();
}
}
}