210 lines
6.0 KiB
C#
210 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogCanvasManager : Singleton<DialogCanvasManager>, IDialogView
|
|
{
|
|
private const string QuickTalk = "Quick Talk";
|
|
|
|
#region 引用
|
|
|
|
private readonly Dictionary<DialogViewType, IDialogView> _dialogViewDict = new();
|
|
private IDialogView _curView;
|
|
private IDialogView _lastShowView;
|
|
private GameObject _quickTalkButton;
|
|
|
|
#endregion
|
|
|
|
private readonly List<IUnRegister> _rmvList = new();
|
|
|
|
public override void OnSingletonInit()
|
|
{
|
|
InitQuickTalkButton();
|
|
InitEvent();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_curView = _dialogViewDict[DialogViewType.OldBox];
|
|
}
|
|
|
|
private void InitEvent()
|
|
{
|
|
_rmvList.Add(
|
|
EnumEventSystem.Global.Register<DialogEventEnum, DialogText>(DialogEventEnum.LineStart,
|
|
AddDialogueLine));
|
|
_rmvList.Add(
|
|
EnumEventSystem.Global.Register<DialogEventEnum, DialogText>(DialogEventEnum.OptionSelected,
|
|
AddDialogueLine));
|
|
_rmvList.Add(EnumEventSystem.Global.Register(EventEnum.NextYarn, CleanDialogHistory));
|
|
}
|
|
|
|
public override void OnSingletonDestroy()
|
|
{
|
|
// 销毁前处理事件
|
|
_rmvList.ForEach(item => item.UnRegister());
|
|
}
|
|
|
|
#region 对话结束后隐藏
|
|
|
|
private void OnEnable()
|
|
{
|
|
DialogController.OnDialogueComplete += HideDialog;
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
DialogController.OnDialogueComplete -= HideDialog;
|
|
}
|
|
|
|
#endregion
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Q) && Input.GetKey(KeyCode.P))
|
|
{
|
|
_quickTalkButton.SetActive(!_quickTalkButton.activeSelf);
|
|
}
|
|
}
|
|
|
|
#region 对话框相关处理
|
|
|
|
private void InitQuickTalkButton()
|
|
{
|
|
_quickTalkButton = transform.Find(QuickTalk).gameObject;
|
|
_quickTalkButton.GetComponent<Button>().onClick.AddListener(SwitchQuickTalkMode);
|
|
}
|
|
|
|
private void SwitchQuickTalkMode()
|
|
{
|
|
DialogController.Instance.quickRunMode = !DialogController.Instance.quickRunMode;
|
|
_quickTalkButton.GetComponent<Image>().color =
|
|
DialogController.Instance.quickRunMode ? Color.gray : Color.white;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册对话框
|
|
/// </summary>
|
|
/// <param name="viewType"></param>
|
|
/// <param name="dialogView"></param>
|
|
public void RegisterDialogView(DialogViewType viewType, IDialogView dialogView)
|
|
{
|
|
_dialogViewDict.Add(viewType, dialogView);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注销对话框
|
|
/// </summary>
|
|
/// <param name="viewType"></param>
|
|
public void UnregisterDialogView(DialogViewType viewType)
|
|
{
|
|
_dialogViewDict.Remove(viewType);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 对话框接口
|
|
|
|
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, Action onTextShowed = null,
|
|
bool isAutoSkip = false)
|
|
{
|
|
// 根据DialogViewType确定在哪个对话框显示
|
|
if (character.dialogViewType == DialogViewType.Default)
|
|
{
|
|
_lastShowView = _curView;
|
|
// 没有指定对话框,就用当前激活的
|
|
_curView.ShowLine(dialogLine, character, nextStep, onTextShowed, isAutoSkip);
|
|
}
|
|
else if (_dialogViewDict.TryGetValue(character.dialogViewType, out var view))
|
|
{
|
|
_lastShowView = view;
|
|
// 对于指定了对话框的,就在指定对话框显示
|
|
view.ShowLine(dialogLine, character, nextStep, onTextShowed, isAutoSkip);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{character.dialogViewType.ToString()}类型对话框不存在");
|
|
}
|
|
}
|
|
|
|
public void ShowOptions(DialogOption[] dialogueOptions)
|
|
{
|
|
// 暂时全部使用Box做选择,后面应该会有其他选择框
|
|
_dialogViewDict[DialogViewType.OldBox].ShowOptions(dialogueOptions);
|
|
}
|
|
|
|
public void HideDialog()
|
|
{
|
|
_curView.HideDialog();
|
|
}
|
|
|
|
public bool TrySkipLine()
|
|
{
|
|
return _lastShowView.TrySkipLine();
|
|
}
|
|
|
|
public void NextStep()
|
|
{
|
|
_lastShowView.NextStep();
|
|
}
|
|
|
|
public void SwitchDialogView(DialogViewType dialogViewType)
|
|
{
|
|
if (_dialogViewDict.TryGetValue(dialogViewType, out var view))
|
|
{
|
|
_curView.HideDialog();
|
|
_curView = view;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{dialogViewType.ToString()}类型对话框不存在");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 对话回顾
|
|
|
|
[Header("对话回顾组件")] [SerializeField] private TMP_Text logText;
|
|
|
|
/// <summary>
|
|
/// 记录对话
|
|
/// </summary>
|
|
/// <param name="line">对话</param>
|
|
private void AddDialogueLine(DialogText line)
|
|
{
|
|
logText.text += line.GetDialogText();
|
|
}
|
|
|
|
private void CleanDialogHistory()
|
|
{
|
|
logText.text = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换对话回顾展示状态
|
|
/// </summary>
|
|
public void ControlDialogHistory()
|
|
{
|
|
var logObj = transform.Find("Dialogue Log").gameObject;
|
|
logObj.SetActive(!logObj.activeSelf);
|
|
EnumEventSystem.Global.Send(EventEnum.Menu, logObj.activeSelf);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public enum DialogViewType
|
|
{
|
|
Default,
|
|
Bubble,
|
|
OldBox,
|
|
Screen
|
|
}
|
|
} |