Files
2026-03-18 17:19:10 +07:00

83 lines
2.5 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster.Scripts
{
public class DebugCmdHistoryListItem : MonoBehaviour
{
[SerializeField] TMP_Text _label;
[SerializeField] Button _btnUse;
[SerializeField] Button _btnDelete;
private DlgConsole _console;
private DebugCmdHistoryStore.Entry _entry;
public void Bind(DlgConsole console, DebugCmdHistoryStore.Entry entry)
{
_console = console;
_entry = entry;
if (_label == null)
_label = GetComponentInChildren<TMP_Text>(true);
if (_btnUse == null)
_btnUse = GetComponentInChildren<Button>(true);
if (_btnDelete == null)
{
// Optional: if you have multiple buttons, assign _btnDelete in inspector.
// Auto-find by name as best-effort.
var buttons = GetComponentsInChildren<Button>(true);
for (int i = 0; i < buttons.Length; i++)
{
var b = buttons[i];
if (b != null && b.gameObject.name.IndexOf("delete", System.StringComparison.OrdinalIgnoreCase) >= 0)
{
_btnDelete = b;
break;
}
}
}
if (_label != null)
{
string cmd = entry.hasParam ? $"{entry.header} {entry.param}" : $"{entry.header}";
if (!string.IsNullOrWhiteSpace(entry.describe))
_label.text = $"{entry.describe} ({cmd})";
else
_label.text = cmd;
}
if (_btnUse != null)
{
_btnUse.onClick.RemoveListener(OnUseClicked);
_btnUse.onClick.AddListener(OnUseClicked);
}
if (_btnDelete != null)
{
_btnDelete.onClick.RemoveListener(OnDeleteClicked);
_btnDelete.onClick.AddListener(OnDeleteClicked);
}
}
private void OnUseClicked()
{
if (_console == null || _entry == null)
return;
_console.ApplyHistoryItemToInputs(_entry.header, _entry.param, _entry.hasParam, _entry.describe);
}
private void OnDeleteClicked()
{
if (_console == null || _entry == null)
return;
_console.RemoveHistoryItem(_entry.header, _entry.param, _entry.hasParam);
}
}
}