using System.Text; using Common.Utility.CombatEvent; using Core.Language; using Excel2Json; using Fort23.UTool; using UnityEngine; using System; using Fort23.Core; namespace Fort23.Mono { [UIBinding(prefab = "DialoguePanel")] public partial class DialoguePanel : UIPanel { private int[] dialogueMessaga; private ShowDialogueEventData.MessageShowType messageShowType; private Action finish; private int index; private char[] _currShowMessage; private float _showTime = 0.05f; private float _currShowTime = 0; protected StringBuilder _sb = new StringBuilder(); private int _currShowIndex; private bool _isUpdate; private bool _isShowNextButton; private string[] showIconName; private EventConditionConfig eventConditionConfig; private bool _skipTyping; private bool _isShowingOptions; public static async void OpenDialoguePanel(int id, string[] icon, ShowDialogueEventData.MessageShowType messageShowType, Action finish) { DialoguePanel dialoguePanel = await UIManager.Instance.LoadAndOpenPanel(null, UILayer.Top); dialoguePanel.ShowPanel(id, icon, messageShowType, finish); } private void Init() { _skipTyping = false; _isShowingOptions = false; } protected override void AddEvent() { } protected override void DelEvent() { } public override void AddButtonEvent() { nextButton.onClick.AddListener(NextShow); } private void SkipTyping() { _skipTyping = true; if (_isUpdate) { _sb.Clear(); _sb.Append(_currShowMessage); message.text = _sb.ToString(); _currShowIndex = _currShowMessage.Length; _isUpdate = false; ShowNextIcon(); if (index >= dialogueMessaga.Length) { // 所有句子显示完成,检查是否有选项 if (eventConditionConfig.optionType == 1 && !_isShowingOptions) { ShowOptions(); } // else // { // // 无选项,关闭面板,返回 null // UIManager.Instance.HideUIUIPanel(this); // finish?.Invoke(null); // } // return; } } } private void NextShow() { if(_isShowingOptions) return; if (_isUpdate) { SkipTyping(); } else { StartShowMassge(); } } public void ShowPanel(int id, string[] icon, ShowDialogueEventData.MessageShowType messageShowType, Action finish) { eventConditionConfig = ConfigComponent.Instance.Get(id); showIconName = icon; this.dialogueMessaga = eventConditionConfig.LanID; this.messageShowType = messageShowType; this.finish = finish; index = 0; StaticUpdater.Instance.AddRenderUpdateCallBack(Update); StartShowMassge(); } private void StartShowMassge() { if (index >= dialogueMessaga.Length) { // 所有句子显示完成,检查是否有选项 if (eventConditionConfig.optionType == 1 && !_isShowingOptions) { ShowOptions(); } else { // 无选项,关闭面板,返回 null UIManager.Instance.HideUIUIPanel(this); finish?.Invoke(null); } return; } _isShowNextButton = false; nextIcon.SetActive(false); string m = LanguageManager.Instance.Text(dialogueMessaga[index]); if (showIconName != null && index < showIconName.Length) { icon.icon_name = showIconName[index]; } index++; switch (messageShowType) { case ShowDialogueEventData.MessageShowType.Default: message.text = m; ShowNextIcon(); break; case ShowDialogueEventData.MessageShowType.Verbatim: _sb.Clear(); _currShowMessage = m.ToCharArray(); _sb.Append(_currShowMessage[0]); message.text = _sb.ToString(); _isUpdate = true; _currShowIndex = 1; _skipTyping = false; break; } } private async void ShowOptions() { UIManager.Instance.DormancyAllGComponent(); _isShowingOptions = true; foreach (var op in eventConditionConfig.optionPara1) { // EventConditionConfig eventConditionConfig = ConfigComponent.Instance.Get(op); DialogueOptionWidget dialogueOptionWidget = await UIManager.Instance.CreateGComponent(null,OptionRoot); dialogueOptionWidget.CustomInit(op,SelectOption); } } private void SelectOption(DialogueOptionWidget obj) { int selectedOptionID = obj.eventConditionConfig.ID; UIManager.Instance.HideUIUIPanel(this); finish?.Invoke(selectedOptionID); } private void ShowNextIcon() { nextIcon.SetActive(true); _isShowNextButton = true; } public override void Hide() { base.Hide(); StaticUpdater.Instance.RemoveRenderUpdateCallBack(Update); } public void Update() { if (!_isUpdate) return; if (messageShowType == ShowDialogueEventData.MessageShowType.Verbatim) { if (_currShowIndex >= _currShowMessage.Length) { _isUpdate = false; StartShowMassge(); // ShowNextIcon(); return; } _currShowTime += Time.deltaTime; if (_currShowTime > _showTime) { _currShowTime -= _showTime; _sb.Append(_currShowMessage[_currShowIndex]); _currShowIndex++; message.text = _sb.ToString(); } } } public override void Close() { UIManager.Instance.DormancyAllGComponent(); _isShowingOptions = false; _skipTyping = false; base.Close(); } } }