Files
2026-05-26 16:15:54 +08:00

589 lines
20 KiB
C#

using asap.core;
using DG.Tweening;
using Game;
using GameCore;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using TMPro;
using UniRx;
using UnityEngine;
using UnityEngine.UI;
public class IsShowGuidance
{
public bool isStop = false;
public bool isShow = false;
public string curIndexDes = "";
public int curIndex;
public GroupName curGroupName;
public FingerType fingerType;
public TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
}
public class GuidancePanel : MonoBehaviour
{
public Image mask;
public Button maskBg;
public Button btn_skip;
public UIButtonDouble btn_next;
public RectTransform Hollowed;
public GameObject subCircleMask;
public GameObject subRectMask;
public Button btn_next_all;
public Transform finger;
Animator animator;
public GameObject npc_chat1;
public GameObject npc_chat2;
public GameObject chat;
public TMP_Text text_chat1;
public TMP_Text text_chat2;
public TMP_Text text_chat3;
public RectTransform arrow_down;
public RectTransform arrow_up;
public RectTransform arrow_left;
public RectTransform arrow_right;
public Transform TargetNode;
public GameObject hideGo;
public float clickDelayed;
float duration;
int clickCount;
float height;
AssetReferenceAudioVoiceClip data;
public static GuidancePanel Instance;
IDisposable disposable;
IDisposable disposable1;
GuidanceDefine curDefine;
IsShowGuidance isShowGuidance;
bool isDown;
float startTimer;
float endTimer;
bool isStop;
Canvas TopRoot;
GraphicRaycaster graphicRaycaster;
List<Canvas> canvasList = new List<Canvas>();
List<GraphicRaycaster> raycastResults = new List<GraphicRaycaster>();
Coroutine coroutine;
GuideDataCenter guideDataCenter;
private void Awake()
{
guideDataCenter = GContext.container.Resolve<GuideDataCenter>();
Hollowed = transform.Find("Hollowed").GetComponent<RectTransform>();
mask = transform.Find("mask").GetComponent<Image>();
maskBg = transform.Find("TopRoot/maskBg").GetComponent<Button>();
btn_skip = transform.Find("TopRoot/btn_skip").GetComponent<Button>();
btn_next = transform.Find("TopRoot/btn_next").GetComponent<UIButtonDouble>();
subCircleMask = Hollowed.transform.Find("Circle").gameObject;
subRectMask = Hollowed.transform.Find("Rect").gameObject;
btn_next_all = transform.Find("TopRoot/btn_next_all").GetComponent<Button>();
btn_next.ExecuteCount = 2;
TopRoot = transform.Find("TopRoot").GetComponent<Canvas>();
graphicRaycaster = TopRoot.GetComponent<GraphicRaycaster>();
finger = transform.Find("TopRoot/finger");
animator = finger.GetComponent<Animator>();
npc_chat1 = transform.Find("TopRoot/npc_chat1").gameObject;
npc_chat2 = transform.Find("TopRoot/npc_chat2").gameObject;
chat = transform.Find("TopRoot/chat").gameObject;
text_chat1 = npc_chat1.transform.Find("bg/text_info").GetComponent<TMP_Text>();
text_chat2 = npc_chat2.transform.Find("text_info").GetComponent<TMP_Text>();
text_chat3 = chat.transform.Find("bg/text_info").GetComponent<TMP_Text>();
arrow_down = chat.transform.Find("bg/arrow_down").GetComponent<RectTransform>();
arrow_up = chat.transform.Find("bg/arrow_up").GetComponent<RectTransform>();
arrow_left = chat.transform.Find("bg/arrow_left").GetComponent<RectTransform>();
arrow_right = chat.transform.Find("bg/arrow_right").GetComponent<RectTransform>();
Vector2 sizeDelta = UIManager.Instance.RectTrans.sizeDelta;
height = sizeDelta.y;
duration = guideDataCenter.guidanceDefineSetting.duration;
}
private void Start()
{
Instance = this;
btn_next.onUp.AddListener(OnUp);
btn_next.onDown.AddListener(OnDown);
btn_next.onClick.AddListener(Next);
btn_next_all.onClick.AddListener(Next);
maskBg.onClick.AddListener(AddClickCount);
btn_skip.onClick.AddListener(SkipNext);
}
void OnUp()
{
isDown = false;
endTimer = Time.time - startTimer;
}
private void OnDown()
{
if (curDefine.completeType == CompleteType.Down)
{
btn_next.OnPointerUp(btn_next.PointerEventData);
AddIndex(false);
return;
}
isDown = true;
startTimer = Time.time;
GuidanceDefine guidanceDefine = curDefine;
if (coroutine != null)
{
StopCoroutine(coroutine);
}
coroutine = StartCoroutine(OnDownIng(guidanceDefine));
}
IEnumerator OnDownIng(GuidanceDefine guidanceDefine)
{
yield return new WaitForSeconds(curDefine.holdTime);
if (isDown && curDefine.fingerType == FingerType.Hold && guidanceDefine == curDefine)
{
OnUp();
AddIndex(false);
}
}
void AddClickCount()
{
clickCount++;
if (clickCount >= guideDataCenter.guidanceDefineSetting.MaxClickNum)
{
btn_skip.gameObject.SetActive(true);
}
}
public void OnEnable()
{
guideDataCenter = GContext.container.Resolve<GuideDataCenter>();
curDefine = guideDataCenter.curDefine;
transform.SetAsLastSibling();
clickCount = 0;
btn_skip.gameObject.SetActive(false);
finger.gameObject.SetActive(false);
btn_next.gameObject.SetActive(false);
btn_next_all.gameObject.SetActive(false);
arrow_down.gameObject.SetActive(false);
arrow_up.gameObject.SetActive(false);
arrow_left.gameObject.SetActive(false);
arrow_right.gameObject.SetActive(false);
string CurPanelName = guideDataCenter.CurPanelName;
if (string.IsNullOrEmpty(CurPanelName))
{
CurPanelName = curDefine.panelName;
}
Transform panel = UIManager.Instance.UICanvas.transform.Find(CurPanelName);
if (panel == null)
{
panel = UIManager.Instance.UICanvas.transform.Find(CurPanelName + "(Clone)");
}
if (panel != null)
{
hideGo = null;
string path;
if (!string.IsNullOrEmpty(curDefine.hideRootPath))
{
path = curDefine.hideRootPath;
if (path.Contains(curDefine.hideRootPath + "/"))
{
path = path.Substring(path.IndexOf('/') + 1);
}
hideGo = panel.transform.Find(path).gameObject;
}
if (hideGo != null)
{
hideGo.SetActive(false);
}
if (!string.IsNullOrEmpty(curDefine.showRootPath))
{
path = curDefine.showRootPath;
if (path.Contains(curDefine.showRootPath + "/"))
{
path = path.Substring(path.IndexOf('/') + 1);
}
GameObject showGo = panel.transform.Find(path).gameObject;
if (showGo != null)
{
showGo.SetActive(true);
}
}
}
isStop = false;
StopAllCoroutines();
StartCoroutine(SetMask(curDefine, panel));
StartCoroutine(SetTextBox(curDefine, panel));
if (curDefine.isFinger)
{
StartCoroutine(ShowFinger(panel));
}
clickDelayed = 0.1f;
if (curDefine.isDelayedClick)
{
clickDelayed = curDefine.clickDelayed;
if (clickDelayed < curDefine.fingerDelayed)
{
clickDelayed = curDefine.fingerDelayed;
}
if (clickDelayed < curDefine.maskDelayed)
{
clickDelayed = curDefine.maskDelayed;
}
if (clickDelayed < curDefine.textDelayed)
{
clickDelayed = curDefine.textDelayed;
}
}
StartCoroutine(ShowBtnNext(panel));
StartCoroutine(ShowSkip());
disposable = GContext.OnEvent<IsShowGuidance>().Subscribe(IsShowGuidance);
disposable1 = GContext.OnEvent<GetUIAsyncEvent>().Subscribe(OnGetUIAsyncEvent);
}
void ShowHighlightedNode(Transform panel)
{
if (curDefine.highlightedNode != null && curDefine.highlightedNode.Length > 0)
{
string path;
for (int i = 0; i < curDefine.highlightedNode.Length; i++)
{
path = curDefine.highlightedNode[i];
if (path.Contains(curDefine.panelName + "/"))
{
path = path.Substring(path.IndexOf('/') + 1);
}
var node = panel.transform.Find(path);
if (node != null)
{
Canvas canvas = node.gameObject.AddComponent<Canvas>();
canvas.overrideSorting = true;
canvas.sortingOrder = TopRoot.sortingOrder - 1;
canvas.additionalShaderChannels = AdditionalCanvasShaderChannels.TexCoord1;
canvasList.Add(canvas);
GraphicRaycaster raycast = node.gameObject.AddComponent<GraphicRaycaster>();
raycastResults.Add(raycast);
}
}
}
}
void OnGetUIAsyncEvent(GetUIAsyncEvent data)
{
if (data.show && data.uIType != UITypes.RewardPopupPanel.Name)
{
transform.SetAsLastSibling();
}
}
void IsShowGuidance(IsShowGuidance data)
{
isShowGuidance = data;
isShowGuidance.isShow = true;
isShowGuidance.isStop = isStop;
isShowGuidance.curIndexDes = curDefine.Description;
isShowGuidance.curIndex = guideDataCenter.curSaveData.guideSubIndex;
isShowGuidance.curGroupName = guideDataCenter.curGroupDefine.groupName;
isShowGuidance.fingerType = curDefine.fingerType;
}
private void OnDisable()
{
foreach (var item in raycastResults)
{
DestroyImmediate(item);
}
raycastResults.Clear();
for (int i = 0; i < canvasList.Count; i++)
{
DestroyImmediate(canvasList[i]);
}
canvasList.Clear();
if (isShowGuidance != null)
{
isShowGuidance.tcs.SetResult(true);
isShowGuidance = null;
}
if (disposable != null)
{
disposable.Dispose();
disposable = null;
}
if (disposable1 != null)
{
disposable1.Dispose();
disposable1 = null;
}
}
IEnumerator ShowSkip()
{
yield return new WaitForSeconds(GContext.container.Resolve<GuideDataCenter>().guidanceDefineSetting.ShowSkipTimer);
btn_skip.gameObject.SetActive(true);
}
IEnumerator ShowBtnNext(Transform panel)
{
GuidanceDefine guidanceDefine = curDefine;
yield return new WaitForSeconds(clickDelayed);
isStop = true;
if (curDefine.maskType != MaskType.None)
{
if (curDefine.completeType == CompleteType.ClickSpace)
{
btn_next_all.gameObject.SetActive(true);
}
else
{
Vector2 vector2 = new Vector2(curDefine.maskRadius, curDefine.maskRadius);
if (!string.IsNullOrEmpty(curDefine.buttonPath))
{
string path = curDefine.buttonPath;
if (path.Contains(curDefine.panelName + "/"))
{
path = path.Substring(path.IndexOf('/') + 1);
}
TargetNode = panel.transform.Find(path);
}
else
{
TargetNode = null;
}
if (TargetNode != null)
{
btn_next.transform.position = TargetNode.position;
var rect = TargetNode.GetComponent<RectTransform>();
vector2 = rect.rect.size;
}
else
{
btn_next.transform.localPosition = Vector3.zero;
}
btn_next.GetComponent<RectTransform>().sizeDelta = vector2;
btn_next.gameObject.SetActive(true);
}
}
else
{
btn_next.transform.localPosition = Vector3.zero;
btn_next.GetComponent<RectTransform>().sizeDelta = new Vector2(height, height);
btn_next.gameObject.SetActive(true);
}
if (guidanceDefine.isOutHide)
{
yield return new WaitForSeconds(guidanceDefine.outHideDelayed);
if (guidanceDefine == curDefine)
{
OnUp();
AddIndex(false);
}
}
}
IEnumerator ShowFinger(Transform panel)
{
yield return new WaitForSeconds(curDefine.fingerDelayed);
if (!string.IsNullOrEmpty(curDefine.buttonPath))
{
string path = curDefine.buttonPath;
if (path.Contains(curDefine.panelName + "/"))
{
path = path.Substring(path.IndexOf('/') + 1);
}
TargetNode = panel.transform.Find(path);
}
else
{
TargetNode = null;
}
if (TargetNode != null)
{
finger.position = TargetNode.position;
}
else
{
finger.localPosition = Vector3.zero;
}
finger.gameObject.SetActive(true);
switch (curDefine.fingerType)
{
case FingerType.Hold:
animator.Play("finger_loop2");
break;
case FingerType.Slid:
animator.Play("finger_loop3");
break;
default:
animator.Play("finger_loop");
break;
}
finger.localEulerAngles = Vector3.forward * curDefine.fingerRotate;
}
IEnumerator SetMask(GuidanceDefine guide, Transform panel)
{
Hollowed.gameObject.SetActive(guide.maskType == MaskType.Black);
subCircleMask.gameObject.SetActive(curDefine.hollowedType == HollowedType.Circle);
subRectMask.gameObject.SetActive(curDefine.hollowedType == HollowedType.Rect);
if (guide.maskType == MaskType.Black)
{
Vector2 vector2 = new Vector2(curDefine.maskRadius, curDefine.maskRadius);
Hollowed.transform.localPosition = Vector3.zero;
Hollowed.sizeDelta = new Vector2(height * 2, height * 2);
yield return new WaitForSeconds(curDefine.maskDelayed);
if (!string.IsNullOrEmpty(curDefine.buttonPath))
{
string path = curDefine.buttonPath;
if (path.Contains(curDefine.panelName + "/"))
{
path = path.Substring(path.IndexOf('/') + 1);
}
TargetNode = panel.transform.Find(path);
}
else
{
TargetNode = null;
}
if (TargetNode != null)
{
var rect = TargetNode.GetComponent<RectTransform>().rect.size;
if (guide.hollowedType == HollowedType.Rect)
{
vector2 = new Vector2(rect.x + 70, rect.y + 70) - vector2;
if (vector2.y < 0)
{
vector2.y = 1;
}
if (vector2.x < 0)
{
vector2.x = 1;
}
}
}
else
{
Hollowed.sizeDelta = new Vector2(height, height);
}
if (TargetNode != null)
{
Hollowed.transform.position = TargetNode.position;
}
Hollowed.DOSizeDelta(vector2, duration);
isStop = true;
}
mask.gameObject.SetActive(guide.maskType == MaskType.Black);
if (panel != null)
{
ShowHighlightedNode(panel);
}
}
IEnumerator SetTextBox(GuidanceDefine guide, Transform panel)
{
npc_chat1.SetActive(false);
npc_chat2.SetActive(false);
chat.SetActive(false);
GameObject skip = null;
yield return new WaitForSeconds(guide.textDelayed);
Rect rect = transform.GetComponent<RectTransform>().rect;
float height = rect.height;
var pos = guide.pos;
pos.y *= height / 2340;
Transform anchor = null;
if (!string.IsNullOrEmpty(guide.anchorPath) && panel != null)
{
string path = guide.anchorPath;
if (path.Contains(curDefine.panelName + "/"))
{
path = path.Substring(path.IndexOf('/') + 1);
}
anchor = panel.transform.Find(path);
}
if (guide.textBoxType == TextBoxType.Head)
{
text_chat1.text = LocalizationMgr.GetText(guide.textContent);
if (anchor != null)
{
var position = anchor.position;
position.x = npc_chat1.transform.position.x;
position.z = npc_chat1.transform.position.z;
npc_chat1.transform.position = position;
npc_chat1.GetComponent<RectTransform>().anchoredPosition += guide.pos;
}
else
{
npc_chat1.GetComponent<RectTransform>().anchoredPosition = pos;
}
npc_chat1.SetActive(true);
skip = npc_chat1.transform.Find("bg/skip").gameObject;
}
else if (guide.textBoxType == TextBoxType.All)
{
text_chat2.text = LocalizationMgr.GetText(guide.textContent);
npc_chat2.SetActive(true);
skip = npc_chat2.transform.Find("skip").gameObject;
}
else if (guide.textBoxType == TextBoxType.NoHead || guide.textBoxType == TextBoxType.NoArrow)
{
text_chat3.text = LocalizationMgr.GetText(guide.textContent);
if (anchor != null)
{
var position = anchor.position;
position.x = npc_chat1.transform.position.x;
position.z = npc_chat1.transform.position.z;
chat.transform.position = position;
chat.GetComponent<RectTransform>().anchoredPosition += guide.pos;
}
else
{
chat.GetComponent<RectTransform>().anchoredPosition = pos;
}
chat.SetActive(true);
skip = chat.transform.Find("skip").gameObject;
}
else if (guide.textBoxType == TextBoxType.Popup)
{
GContext.container.Resolve<GuideDataCenter>().ShowGuidancePopupPanel(guide.popupNode);
}
if (guide.assetReference != null && guide.assetReference.RuntimeKeyIsValid())
{
data = new AssetReferenceAudioVoiceClip(guide.assetReference);
GContext.Publish(data);
}
if (skip != null)
{
skip.SetActive(false);
}
}
void SkipNext()
{
AddIndex(true);
}
void Next()
{
if (curDefine.fingerType == FingerType.Hold && curDefine.holdTime > endTimer - 0.001f || curDefine.fingerType == FingerType.Slid || curDefine.completeType == CompleteType.Down)
{
return;
}
AddIndex(false);
}
void AddIndex(bool is_pass)
{
if (hideGo != null && curDefine.endAutoShow)
{
hideGo.SetActive(true);
}
if (data != null && data.Sound != null)
{
GContext.Publish(new FadeOutEvent(data.Sound));
}
GContext.container.Resolve<GuideDataCenter>().AddIndex(is_pass);
}
private void OnDestroy()
{
Instance = null;
}
#if UNITY_EDITOR
private void Update()
{
if (Input.GetKeyDown(KeyCode.F1))
{
GContext.container.Resolve<GuideDataCenter>().GMClearGuide();
UIManager.Instance.DestroyUI(UITypes.GuidancePanel);
}
}
#endif
}