227 lines
6.4 KiB
C#
227 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogCanvasManager : Singleton<DialogCanvasManager>, IDialogView
|
|
{
|
|
#region 引用
|
|
|
|
private readonly Dictionary<DialogViewType, IDialogView> _dialogViewDict = new();
|
|
private IDialogView _curView;
|
|
private IDialogView _lastShowView;
|
|
|
|
public TempTextBox tempText;
|
|
|
|
#endregion
|
|
|
|
private readonly List<IUnRegister> _rmvList = new();
|
|
|
|
public override void OnSingletonInit()
|
|
{
|
|
InitEvent();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
// 默认对话框直接加载
|
|
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);
|
|
if (tempText != null)
|
|
{
|
|
RegisterDialogView(DialogViewType.Temp, tempText);
|
|
}
|
|
|
|
_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
|
|
|
|
#region 对话框相关处理
|
|
|
|
/// <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, string lineId, bool isAutoSkip = false)
|
|
{
|
|
// 根据DialogViewType确定在哪个对话框显示
|
|
if (character.dialogViewType == DialogViewType.Default)
|
|
{
|
|
if (_lastShowView != _curView)
|
|
{
|
|
_lastShowView?.HideDialog();
|
|
}
|
|
|
|
_lastShowView = _curView;
|
|
// 没有指定对话框,就用当前激活的
|
|
_curView.ShowLine(dialogLine, character, nextStep, lineId, isAutoSkip);
|
|
}
|
|
else if (_dialogViewDict.TryGetValue(character.dialogViewType, out var view))
|
|
{
|
|
if (_lastShowView != view)
|
|
{
|
|
_lastShowView?.HideDialog();
|
|
}
|
|
|
|
_lastShowView = view;
|
|
// 对于指定了对话框的,就在指定对话框显示
|
|
view.ShowLine(dialogLine, character, nextStep, lineId, 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 OnLocalizationChanged(string value)
|
|
{
|
|
}
|
|
|
|
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 对话回顾
|
|
|
|
private StringBuilder DialogRecord => UIManager.Instance.GetPanel<RecordPanel>().recordStr;
|
|
|
|
/// <summary>
|
|
/// 记录对话
|
|
/// </summary>
|
|
/// <param name="line">对话</param>
|
|
private void AddDialogueLine(DialogText line)
|
|
{
|
|
DialogRecord.Append(line.GetDialogText());
|
|
}
|
|
|
|
private void CleanDialogHistory()
|
|
{
|
|
DialogRecord.Clear();
|
|
}
|
|
|
|
public void CloseCanvas()
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
|
|
public void OpenCanvas()
|
|
{
|
|
gameObject.SetActive(true);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
public enum DialogViewType
|
|
{
|
|
Default,
|
|
Bubble,
|
|
OldBox,
|
|
Screen,
|
|
CenterText,
|
|
CenterOption,
|
|
Task,
|
|
Temp
|
|
}
|
|
} |