MeowmentDebugTool/Packages/com.bywaystudios.meowmentdebugtool/Runtime/SettingsModule.cs
2026-02-03 10:32:06 +08:00

227 lines
8.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace MeowmentDebugTool
{
/// <summary>
/// 设置模块 - 提供分辨率设置等功能
/// </summary>
public class SettingsModule : IDebugModule
{
#region
private GameObject settingsPage;
private TMP_InputField widthInputField;
private TMP_InputField heightInputField;
private Button applyResolutionButton;
private Button resetResolutionButton;
private TMP_Text currentResolutionText;
// Console Log缓存池设置
private TMP_InputField infoBufferInputField;
private TMP_InputField warningBufferInputField;
private TMP_InputField errorBufferInputField;
private TMP_InputField fatalBufferInputField;
private Button applyBufferButton;
private Button resetBufferButton;
private ConsoleModule consoleModule;
// 主窗口引用
private RectTransform mainWindow;
private Canvas canvas;
// 默认分辨率
private Vector2 defaultResolution = new Vector2(1080, 2340);
private Vector2 currentCustomResolution;
// 默认缓存池大小
private const int DEFAULT_BUFFER_SIZE = 50;
#endregion
#region
public SettingsModule(GameObject page, TMP_InputField widthInput, TMP_InputField heightInput,
Button applyButton, Button resetButton, TMP_Text resolutionText,
TMP_InputField infoBufferInput, TMP_InputField warningBufferInput,
TMP_InputField errorBufferInput, TMP_InputField fatalBufferInput,
Button applyBufferBtn, Button resetBufferBtn,
RectTransform mainWin, Canvas canvasRef)
{
settingsPage = page;
widthInputField = widthInput;
heightInputField = heightInput;
applyResolutionButton = applyButton;
resetResolutionButton = resetButton;
currentResolutionText = resolutionText;
infoBufferInputField = infoBufferInput;
warningBufferInputField = warningBufferInput;
errorBufferInputField = errorBufferInput;
fatalBufferInputField = fatalBufferInput;
applyBufferButton = applyBufferBtn;
resetBufferButton = resetBufferBtn;
mainWindow = mainWin;
canvas = canvasRef;
currentCustomResolution = defaultResolution;
}
#endregion
#region IDebugModule
public void Initialize()
{
Debug.Log("[SettingsModule] 初始化设置模块...");
if (applyResolutionButton != null)
applyResolutionButton.onClick.AddListener(ApplyCustomResolution);
if (resetResolutionButton != null)
resetResolutionButton.onClick.AddListener(ResetToDefaultResolution);
if (applyBufferButton != null)
applyBufferButton.onClick.AddListener(ApplyBufferSize);
if (resetBufferButton != null)
resetBufferButton.onClick.AddListener(ResetBufferSize);
// 应用默认分辨率
ApplyResolution(defaultResolution);
// 设置默认缓存池大小
ResetBufferSize();
}
public GameObject GetPage()
{
return settingsPage;
}
public string GetModuleName()
{
return "设置";
}
/// <summary>
/// 设置 Console Module 引用
/// </summary>
public void SetConsoleModule(ConsoleModule module)
{
consoleModule = module;
// 更新输入框显示当前值
if (consoleModule != null)
{
consoleModule.GetMaxLogLines(out int maxInfo, out int maxWarning, out int maxError, out int maxFatal);
if (infoBufferInputField != null)
infoBufferInputField.text = maxInfo.ToString();
if (warningBufferInputField != null)
warningBufferInputField.text = maxWarning.ToString();
if (errorBufferInputField != null)
errorBufferInputField.text = maxError.ToString();
if (fatalBufferInputField != null)
fatalBufferInputField.text = maxFatal.ToString();
}
}
#endregion
#region
private void ApplyCustomResolution()
{
if (widthInputField == null || heightInputField == null) return;
if (float.TryParse(widthInputField.text, out float width) &&
float.TryParse(heightInputField.text, out float height))
{
if (width > 0 && height > 0)
{
currentCustomResolution = new Vector2(width, height);
ApplyResolution(currentCustomResolution);
Debug.Log($"[SettingsModule] 已应用自定义分辨率: {width} x {height}");
}
else
{
Debug.LogWarning("[SettingsModule] 分辨率值必须大于0");
}
}
else
{
Debug.LogWarning("[SettingsModule] 无效的分辨率值");
}
}
private void ResetToDefaultResolution()
{
currentCustomResolution = defaultResolution;
ApplyResolution(defaultResolution);
if (widthInputField != null)
widthInputField.text = defaultResolution.x.ToString();
if (heightInputField != null)
heightInputField.text = defaultResolution.y.ToString();
Debug.Log($"[SettingsModule] 已重置为默认分辨率: {defaultResolution.x} x {defaultResolution.y}");
}
private void ApplyResolution(Vector2 resolution)
{
if (mainWindow != null)
{
mainWindow.sizeDelta = resolution;
}
if (currentResolutionText != null)
{
currentResolutionText.text = $"当前窗口尺寸: {resolution.x} x {resolution.y}";
}
// 不需要强制刷新Canvas会导致rebuild loop
// Canvas会自动在下一帧更新
}
private void ApplyBufferSize()
{
if (consoleModule == null)
{
Debug.LogWarning("[SettingsModule] ConsoleModule未设置无法应用缓存池大小");
return;
}
int maxInfo = DEFAULT_BUFFER_SIZE;
int maxWarning = DEFAULT_BUFFER_SIZE;
int maxError = DEFAULT_BUFFER_SIZE;
int maxFatal = DEFAULT_BUFFER_SIZE;
if (infoBufferInputField != null && int.TryParse(infoBufferInputField.text, out int info) && info > 0)
maxInfo = info;
if (warningBufferInputField != null && int.TryParse(warningBufferInputField.text, out int warning) && warning > 0)
maxWarning = warning;
if (errorBufferInputField != null && int.TryParse(errorBufferInputField.text, out int error) && error > 0)
maxError = error;
if (fatalBufferInputField != null && int.TryParse(fatalBufferInputField.text, out int fatal) && fatal > 0)
maxFatal = fatal;
consoleModule.SetMaxLogLines(maxInfo, maxWarning, maxError, maxFatal);
Debug.Log($"[SettingsModule] 已应用缓存池大小 - Info:{maxInfo}, Warning:{maxWarning}, Error:{maxError}, Fatal:{maxFatal}");
}
private void ResetBufferSize()
{
if (infoBufferInputField != null)
infoBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (warningBufferInputField != null)
warningBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (errorBufferInputField != null)
errorBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (fatalBufferInputField != null)
fatalBufferInputField.text = DEFAULT_BUFFER_SIZE.ToString();
if (consoleModule != null)
{
consoleModule.SetMaxLogLines(DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE, DEFAULT_BUFFER_SIZE);
Debug.Log($"[SettingsModule] 已重置缓存池大小为默认值: {DEFAULT_BUFFER_SIZE}");
}
}
#endregion
}
}