2025-09-08 17:37:12 +08:00

172 lines
5.6 KiB
C#

using System;
using System.Collections;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using ZXKFramework;
namespace DongWuYiXue.DaoNiaoShu
{
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;//ÕýÈ·µÄ¹Ø¼ü×Ö
string answer;//´ð°¸
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)
{
// ¼ì²âÊÇ·ñ°´ÏÂÁ˻سµ¼ü
if (addedChar == '\n' || addedChar == '\r')
{
// Ö´ÐÐÌá½»Âß¼­
Submit();
// ·µ»Ø¿Õ×Ö·û±íʾ²»Ìí¼Ó»»ÐÐ
return '\0';
}
// ÆäËû×Ö·ûÕý³£ÏÔʾ
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>();
//·ÀÖ¹·¢ËÍ¿Õ×Ö·û
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 = "";
// ÈÃInputField»ñÈ¡½¹µã
userInput.Select();
// ÏÔʾÊäÈë¹â±ê
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);
}
}
}