biography-of-lijie/Assets/GameMain/Scripts/UI/View/AIChatEntryForm.cs

87 lines
2.2 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace UI
{
public sealed class AIChatEntryForm : UGuiForm
{
[SerializeField] private Button _entryButton;
[SerializeField] private TMP_Text _entryLabel;
[SerializeField] private string _openLabel = "问 AI";
[SerializeField] private string _closeLabel = "关闭 AI";
private AIChatEntryFormController _controller;
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
BindEntryButton();
if (!(userData is AIChatEntryFormContext context))
{
Log.Error("AIChatEntryFormContext is invalid.");
return;
}
RefreshUI(context);
}
protected override void OnClose(bool isShutdown, object userData)
{
UnbindEntryButton();
_controller = null;
base.OnClose(isShutdown, userData);
}
public void RefreshUI(AIChatEntryFormContext context)
{
if (context == null)
{
return;
}
_controller = context.Controller;
SetChatOpened(context.IsChatOpened);
}
public void SetChatOpened(bool isChatOpened)
{
if (_entryLabel == null)
{
return;
}
_entryLabel.text = isChatOpened ? _closeLabel : _openLabel;
}
public void OnEntryButtonClick()
{
_controller?.ToggleAIChatForm();
}
private void BindEntryButton()
{
if (_entryButton == null)
{
Log.Warning("AIChatEntryForm entry button is not assigned.");
return;
}
_entryButton.onClick.RemoveListener(OnEntryButtonClick);
_entryButton.onClick.AddListener(OnEntryButtonClick);
}
private void UnbindEntryButton()
{
if (_entryButton == null)
{
return;
}
_entryButton.onClick.RemoveListener(OnEntryButtonClick);
}
}
}