239 lines
7.1 KiB
C#
239 lines
7.1 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();
|
|
|
|
// 默认对话框直接加载
|
|
var box = transform.Find("Dialog Box").GetComponent<IDialogView>();
|
|
RegisterDialogView(DialogViewType.OldBox, box);
|
|
var centerText = transform.Find("Center Text").GetComponent<IDialogView>();
|
|
RegisterDialogView(DialogViewType.CenterText, centerText);
|
|
var centerOption = transform.Find("Center Option").GetComponent<IDialogView>();
|
|
RegisterDialogView(DialogViewType.CenterOption, centerOption);
|
|
}
|
|
|
|
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, bool isAutoSkip = false)
|
|
{
|
|
// 根据DialogViewType确定在哪个对话框显示
|
|
if (character.dialogViewType == DialogViewType.Default)
|
|
{
|
|
if (_lastShowView != _curView)
|
|
{
|
|
_lastShowView?.HideDialog();
|
|
}
|
|
|
|
_lastShowView = _curView;
|
|
// 没有指定对话框,就用当前激活的
|
|
_curView.ShowLine(dialogLine, character, nextStep, isAutoSkip);
|
|
}
|
|
else if (_dialogViewDict.TryGetValue(character.dialogViewType, out var view))
|
|
{
|
|
if (_lastShowView != view)
|
|
{
|
|
_lastShowView?.HideDialog();
|
|
}
|
|
|
|
_lastShowView = view;
|
|
// 对于指定了对话框的,就在指定对话框显示
|
|
view.ShowLine(dialogLine, character, nextStep, isAutoSkip);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"{character.dialogViewType.ToString()}类型对话框不存在");
|
|
}
|
|
}
|
|
|
|
public void ShowOptions(DialogOption[] dialogueOptions)
|
|
{
|
|
var viewType = dialogueOptions[0].Character.dialogViewType;
|
|
// 根据状况选择不同的选择框
|
|
if (viewType == DialogViewType.Default)
|
|
{
|
|
_dialogViewDict[DialogViewType.OldBox].ShowOptions(dialogueOptions);
|
|
}
|
|
else
|
|
{
|
|
_dialogViewDict[viewType].ShowOptions(dialogueOptions);
|
|
}
|
|
}
|
|
|
|
public void HideDialog()
|
|
{
|
|
_curView.HideDialog();
|
|
}
|
|
|
|
public bool TrySkipLine(bool isForceSkip = false)
|
|
{
|
|
return _lastShowView != null && _lastShowView.TrySkipLine(isForceSkip);
|
|
}
|
|
|
|
public void NextStep()
|
|
{
|
|
_lastShowView?.NextStep();
|
|
}
|
|
|
|
public void SwitchDialogView(DialogViewType dialogViewType)
|
|
{
|
|
if (_dialogViewDict.TryGetValue(dialogViewType, out var view))
|
|
{
|
|
_curView.HideDialog();
|
|
_lastShowView?.HideDialog();
|
|
_lastShowView = null;
|
|
_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,
|
|
CenterText,
|
|
CenterOption
|
|
}
|
|
} |