172 lines
5.6 KiB
C#
Raw Normal View History

2025-09-08 14:51:28 +08:00
using System;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using ZXKFramework;
2025-09-08 17:37:12 +08:00
namespace DongWuYiXue.DaoNiaoShu
2025-09-08 14:51:28 +08:00
{
public static class UIBehaviourExtension
{
public static void OnEventButtonEvent(this GameObject self, EventTriggerType type, UnityAction<BaseEventData> action)
{
EventTrigger.Entry entry = new EventTrigger.Entry { eventID = type };
var eventTrigger = self.GetComponent<EventTrigger>() ?? self.gameObject.AddComponent<EventTrigger>();
entry.callback.AddListener(action);
eventTrigger.triggers.Add(entry);
}
}
public class SpeakPanel : UIBase
{
public override string GroupName => "SpeakPanel";
public override string Name => "SpeakPanel";
Button tip;
Button Send;
string keyword;//<2F><>ȷ<EFBFBD>Ĺؼ<C4B9><D8BC><EFBFBD>
string answer;//<2F><><EFBFBD><EFBFBD>
ItemInput userInput;
GameObject inputContent;
public Action<int,string> sendAction;
int hidetime;
int showtime;
bool hasTip;
IUIManager uIManager;
Coroutine coroutine;
public override void Init(IUIManager uictrl)
{
base.Init(uictrl);
uIManager = uictrl;
tip = gameObject.FindFirst<Button>("YJSR_Btn");
Send = gameObject.FindFirst<Button>("Send_Btn");
userInput = gameObject.FindFirst<ItemInput>("EditorPanel");
userInput.gameObject.OnEventButtonEvent(EventTriggerType.PointerDown, args => {
uictrl.CloseUI<ArrowPanel>();
});
userInput.onValidateInput += OnValidateInput;
inputContent = gameObject.FindFirst("InputContent");
Send.onClick.AddListener(() =>
{
Submit();
});
tip.onClick.AddListener(() => {
hasTip = true;
uictrl.CloseUI<ArrowPanel>();
userInput.text = answer;
//userInput.onValueChanged.Invoke(userInput.text);
});
}
private char OnValidateInput(string text, int charIndex, char addedChar)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻س<CBBB><D8B3><EFBFBD>
if (addedChar == '\n' || addedChar == '\r')
{
// ִ<><D6B4><EFBFBD><EFBFBD>߼<EFBFBD>
Submit();
// <20><><EFBFBD>ؿ<EFBFBD><D8BF>ַ<EFBFBD><D6B7><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD>ӻ<EFBFBD><D3BB><EFBFBD>
return '\0';
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ
return addedChar;
}
//private void Update()
//{
// if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
// {
// if (Send.gameObject.activeSelf)
// {
// Submit();
// }
// }
//}
public void Submit()
{
uIManager.CloseUI<ArrowPanel>();
//<2F><>ֹ<EFBFBD><D6B9><EFBFBD>Ϳ<EFBFBD><CDBF>ַ<EFBFBD>
if (!string.IsNullOrEmpty(userInput.text))
{
Game.Instance.sound.StopBGM();
if (string.IsNullOrEmpty(answer))
{
StartCoroutine(TenSecond(1, userInput.text));
}
else
{
if (Regex.Replace(userInput.text, @"[\p{P}\p{S}]", "").Contains(keyword) )
{
if (!hasTip)
{
StartCoroutine(TenSecond(1, userInput.text));
}
else
{
StartCoroutine(TenSecond(2, userInput.text));
}
}
else
{
StartCoroutine(TenSecond(0, userInput.text));
}
}
Send.gameObject.SetActive(false);
tip.gameObject.SetActive(false);
}
}
public void SetTransformWorldPlacement(Vector3 TransformWorldPlacement)
{
inputContent.GetComponent<RectTransform>().anchoredPosition = TransformWorldPlacement;
}
public void ShowSpeakPanel(string answer, string keyWord, bool showtip, bool showsend, int showtime, int hidetime, Action<int,string> callBack)
{
this.hidetime = hidetime;
this.showtime = showtime;
hasTip = false;
coroutine = Game.Instance.IEnumeratorManager.Run(showtime, () => {
SetActive(true);
userInput.GetComponent<RectTransform>().anchoredPosition = new Vector2(-159f, 67.5f);
userInput.text = "";
// <20><>InputField<6C><64>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
userInput.Select();
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
userInput.ActivateInputField();
if (string.IsNullOrEmpty(answer))
{
tip.gameObject.SetActive(false);
}
else
{
tip.gameObject.SetActive(true);
}
Send.gameObject.SetActive(showsend);
sendAction = callBack;
this.answer = answer;
keyword = keyWord;
});
}
IEnumerator TenSecond(int i,string s)
{
yield return new WaitForSeconds(hidetime);
userInput.text = "";
SetActive(false);
sendAction?.Invoke(i,s);
}
public override void Hide()
{
base.Hide();
Game.Instance.IEnumeratorManager.Stop(coroutine);
}
}
}