194 lines
7.6 KiB
C#
194 lines
7.6 KiB
C#
using System;
|
||
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;
|
||
Button Voice;
|
||
ItemInput userInput;
|
||
GameObject content;
|
||
GameObject inputContent;
|
||
GameObject MicrophoneImg;
|
||
GameObject btnsContent;
|
||
Action<bool> sendAction;
|
||
string tipTxt;
|
||
string stepName;
|
||
int score;
|
||
int id;
|
||
bool clickTip;
|
||
Coroutine coroutine;
|
||
public override void Init(IUIManager uictrl)
|
||
{
|
||
base.Init(uictrl);
|
||
tip = transform.FindFirst<Button>("Tip_Btn");
|
||
Send = transform.FindFirst<Button>("Send_Btn");
|
||
Voice = transform.FindFirst<Button>("Voice_Btn");
|
||
content = transform.FindFirst("Content");
|
||
inputContent = transform.FindFirst("InputContent");
|
||
MicrophoneImg = transform.FindFirst("MicrophoneImg");
|
||
btnsContent = transform.FindFirst("Btns");
|
||
userInput = transform.FindFirst<ItemInput>("EditorPanel");
|
||
UIBehaviourExtension.OnEventButtonEvent(Voice.gameObject, EventTriggerType.PointerDown, onMicrophoneDown);
|
||
UIBehaviourExtension.OnEventButtonEvent(Voice.gameObject, EventTriggerType.PointerUp, onMicrophoneUp);
|
||
userInput.onValidateInput += OnValidateInput;
|
||
Send.onClick.AddListener(Submit);
|
||
tip.onClick.AddListener(() =>
|
||
{
|
||
userInput.text = tipTxt;
|
||
clickTip = true;
|
||
});
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
public void Submit()
|
||
{
|
||
//<2F><>ֹ<EFBFBD><D6B9><EFBFBD>Ϳ<EFBFBD><CDBF>ַ<EFBFBD>
|
||
if (!string.IsNullOrEmpty(userInput.text) && !string.IsNullOrEmpty(tipTxt))
|
||
{
|
||
btnsContent.SetActive(false);
|
||
userInput.readOnly = true;
|
||
ChatAI.Instance.SendData("<22>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ڣ<EFBFBD>" + stepName + "<22><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>" + userInput.text + "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>жϣ<D0B6><CFA3><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>true<75><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>false", str =>
|
||
{
|
||
if (str.Contains("true"))
|
||
{
|
||
if (clickTip)
|
||
{
|
||
GameManager.Instance.kaoheManager.AddScore(0, id, null);
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = true });
|
||
sendAction?.Invoke(true);
|
||
}
|
||
else
|
||
{
|
||
GameManager.Instance.kaoheManager.AddScore(score, id, (s, t) =>
|
||
{
|
||
GameManager.Instance.uiManager.GetUI<ScorePanel>().SetScore(s.ToString(), t.ToString());
|
||
});
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = true });
|
||
sendAction?.Invoke(true);
|
||
}
|
||
}
|
||
else if (str.Contains("false"))
|
||
{
|
||
GameManager.Instance.kaoheManager.AddScore(0, id, null);
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = false });
|
||
if (MVC.GetModel<Main.GameModel>().modeType == ModeType.ShiXun)
|
||
{
|
||
Game.Instance.IEnumeratorManager.Run(0.1f, () =>
|
||
{
|
||
ChatUI.Instance.SendAINext("<22>û<EFBFBD><C3BB><EFBFBD><EFBFBD>ڣ<EFBFBD>" + stepName + "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD><D2BD>ܱ<EFBFBD><DCB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ𰸺<C4B4><F0B0B8BA><EFBFBD><EFBFBD><EFBFBD>", () =>
|
||
{
|
||
userInput.text = tipTxt;
|
||
sendAction?.Invoke(false);
|
||
});
|
||
});
|
||
}
|
||
else
|
||
{
|
||
sendAction?.Invoke(false);
|
||
}
|
||
}
|
||
else if (str.Contains("error") || string.IsNullOrEmpty(str))
|
||
{
|
||
GameManager.Instance.fsm.AddScore(0, id);
|
||
userInput.text = tipTxt;
|
||
sendAction?.Invoke(false);
|
||
}
|
||
});
|
||
}
|
||
if (!string.IsNullOrEmpty(userInput.text) && string.IsNullOrEmpty(tipTxt))
|
||
{
|
||
GameManager.Instance.fsm.AddScore(score, id);
|
||
Game.Instance.eventManager.Raise(new PlayTrueOrFalseEvent() { isTrue = true });
|
||
}
|
||
}
|
||
|
||
public void ShowSpeakPanel(string stepName, string tipTxt, Vector2 v, int delayShowTime, int id, int score, Action<bool> callBack)
|
||
{
|
||
coroutine = Game.Instance.IEnumeratorManager.Run((int)delayShowTime, () =>
|
||
{
|
||
this.id = id;
|
||
this.tipTxt = tipTxt;
|
||
this.score = score;
|
||
sendAction = callBack;
|
||
this.stepName = stepName;
|
||
userInput.readOnly = false;
|
||
SetActive(true);
|
||
btnsContent.SetActive(true);
|
||
userInput.GetComponent<RectTransform>().anchoredPosition = new Vector2(-157.5f, 90f);
|
||
content.GetComponent<RectTransform>().anchoredPosition = v;
|
||
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(tipTxt))
|
||
{
|
||
tip.gameObject.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
tip.gameObject.SetActive(true);
|
||
}
|
||
});
|
||
}
|
||
|
||
void onMicrophoneDown(BaseEventData down)
|
||
{
|
||
inputContent.SetActive(false);
|
||
MicrophoneImg.SetActive(true);
|
||
transform.FindFirst<Animator>("boxing").enabled = true;
|
||
ChatAI.Instance.StartRecord();
|
||
}
|
||
|
||
void onMicrophoneUp(BaseEventData up)
|
||
{
|
||
inputContent.SetActive(true);
|
||
MicrophoneImg.SetActive(false);
|
||
transform.FindFirst<Animator>("boxing").enabled = false;
|
||
ChatAI.Instance.StopRecord(str =>
|
||
{
|
||
userInput.text += str;
|
||
});
|
||
}
|
||
public override void Hide()
|
||
{
|
||
base.Hide();
|
||
Game.Instance.IEnumeratorManager.Stop(coroutine);
|
||
ChatUI.Instance.CloseChatTxt();
|
||
clickTip = false;
|
||
}
|
||
}
|
||
} |