266 lines
8.4 KiB
C#
266 lines
8.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
//using System.Diagnostics;
|
|
using AibisDream;
|
|
using MeadowGames.UINodeConnect4;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
using RainbowArt.CleanFlatUI;
|
|
using UnityEngine.UI;
|
|
using DG.Tweening;
|
|
using MeadowGames.UINodeConnect4.GraphicRenderer;
|
|
using System;
|
|
|
|
public class ClueBaordNew : MonoBehaviour
|
|
{
|
|
|
|
private GraphManager gm;
|
|
public List<GameObject> nodeLists;
|
|
public Notification ClueBoardNotification;
|
|
private Connection currentConnection;
|
|
|
|
private List<Connection> connectionList;
|
|
|
|
private bool isTriggerDialogue=true;
|
|
|
|
public ScrollRect scrollRect; // ScrollView的ScrollRect组件
|
|
public RectTransform contentRectTransform; // Content的RectTransform
|
|
public float padding = 10f; // 节点的额外间隔
|
|
public float scrollDuration = 1.5f; // 滚动动画持续时间
|
|
public float scaleDuration = 1f; // 缩放动画持续时间
|
|
void Start()
|
|
{
|
|
gm=GetComponent<GraphManager>();
|
|
gm.OnConnectionAdded += CheckConnection;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
[YarnCommand("ScrollToNode")]
|
|
public IEnumerator ScrollToNode(string nodeName)
|
|
{
|
|
if (scrollRect == null || contentRectTransform == null)
|
|
{
|
|
Debug.LogError("ScrollRect or ContentRectTransform is not assigned!");
|
|
yield break;
|
|
}
|
|
|
|
// 获取目标节点
|
|
Transform targetNode = GetNodeGameObject(nodeName)?.transform;
|
|
if (targetNode == null)
|
|
{
|
|
Debug.LogWarning($"Node {nodeName} not found!");
|
|
yield break;
|
|
}
|
|
|
|
RectTransform targetRectTransform = targetNode.GetComponent<RectTransform>();
|
|
if (targetRectTransform == null)
|
|
{
|
|
Debug.LogWarning($"Node {nodeName} does not have a RectTransform!");
|
|
yield break;
|
|
}
|
|
|
|
// **计算目标 Content 位置**
|
|
Vector3[] viewportCorners = new Vector3[4];
|
|
scrollRect.viewport.GetWorldCorners(viewportCorners);
|
|
|
|
Vector3[] contentCorners = new Vector3[4];
|
|
contentRectTransform.GetWorldCorners(contentCorners);
|
|
|
|
Vector3 targetWorldPosition = targetRectTransform.position;
|
|
Vector2 viewportCenter = (viewportCorners[0] + viewportCorners[2]) / 2f;
|
|
Vector2 offset = (Vector2)targetWorldPosition - viewportCenter;
|
|
|
|
Vector2 targetContentPos = contentRectTransform.anchoredPosition - offset;
|
|
|
|
// **动画处理**
|
|
float zoomScale = 1.5f; // 放大倍数
|
|
Sequence scrollSequence = DOTween.Sequence();
|
|
|
|
// 平滑移动 Content
|
|
scrollSequence.Append(contentRectTransform.DOAnchorPos(targetContentPos, scrollDuration).SetEase(Ease.InOutQuad));
|
|
|
|
// 同时缩放目标节点
|
|
scrollSequence.Join(targetRectTransform.DOScale(Vector3.one * zoomScale, scaleDuration).SetEase(Ease.OutBack));
|
|
|
|
// 动画完成后恢复缩放
|
|
scrollSequence.Append(targetRectTransform.DOScale(Vector3.one, scaleDuration).SetEase(Ease.OutBack));
|
|
|
|
yield return scrollSequence.WaitForCompletion();
|
|
}
|
|
|
|
[YarnCommand("SetNodeState")]
|
|
public IEnumerator ActiveNodeToBoard(string nodeID,bool isactive,bool skipAnimation=false)
|
|
{
|
|
GameObject targetNode=nodeLists.Find(node => node.name == nodeID);
|
|
if (targetNode == null)
|
|
{
|
|
Debug.LogWarning($"Node {nodeID} not found!");
|
|
yield break;
|
|
}
|
|
targetNode.gameObject.SetActive(isactive);
|
|
|
|
if(skipAnimation)
|
|
{
|
|
yield break;
|
|
}
|
|
RectTransform targetRectTransform = targetNode.GetComponent<RectTransform>();
|
|
if (targetRectTransform == null)
|
|
{
|
|
Debug.LogWarning($"Node {nodeID} does not have a RectTransform!");
|
|
yield break;
|
|
}
|
|
targetRectTransform.DOScale(Vector3.one * 1.5f, scaleDuration).SetEase(Ease.OutBack);
|
|
|
|
// 等待动画完成
|
|
yield return new WaitForSeconds(scrollDuration);
|
|
|
|
// 恢复目标节点缩放
|
|
targetRectTransform.DOScale(Vector3.one, scaleDuration).SetEase(Ease.OutBack);
|
|
}
|
|
[YarnCommand("NodeInit")]
|
|
public void NodeInit(string nodeID,bool isactive)
|
|
{
|
|
GameObject targetNode=nodeLists.Find(node => node.name == nodeID);
|
|
if (targetNode == null)
|
|
{
|
|
Debug.LogWarning($"Node {nodeID} not found!");
|
|
return;
|
|
}
|
|
targetNode.gameObject.SetActive(isactive);
|
|
}
|
|
// [YarnCommand("SetNodeState")]
|
|
// public void ActiveNodeToBoard(string nodeID,bool isactive)
|
|
// {
|
|
// nodeLists.Find(node => node.name == nodeID).gameObject.SetActive(isactive);
|
|
// }
|
|
[YarnCommand("ClearAllNode")]
|
|
public void ClearAllNode()
|
|
{
|
|
for (int i = gm.localConnections.Count - 1; i >= 0; i--)
|
|
{
|
|
gm.localConnections[i].Remove();
|
|
}
|
|
|
|
for (int i = nodeLists.Count - 1; i >= 0; i--)
|
|
{
|
|
nodeLists[i].SetActive(false);
|
|
}
|
|
|
|
// foreach(Connection line in gm.localConnections)
|
|
// {
|
|
// line.Remove();
|
|
// }
|
|
// foreach(GameObject node in nodeLists)
|
|
// {
|
|
// node.SetActive(false);
|
|
// }
|
|
}
|
|
[YarnCommand("CreateConnection")]
|
|
public void CreateConnection(string StartNode,string EndNode)
|
|
{
|
|
Port start= GetNode(StartNode).ports[1];
|
|
Port end= GetNode(EndNode).ports[0];
|
|
Connection newConnection=Connection.NewConnection(start,end);
|
|
newConnection.DisableClick=false;
|
|
newConnection.EnableDrag=false;
|
|
newConnection.EnableHover=false;
|
|
newConnection.line.color=Color.red;
|
|
}
|
|
public void ExitClueBoard()
|
|
{
|
|
DialogController.Instance.StartDialogNode("退出ClueBoard");
|
|
}
|
|
|
|
public Node GetNode(string nodeID)
|
|
{
|
|
return gm.localNodes.Find(node => node.ID == nodeID);
|
|
}
|
|
|
|
public GameObject GetNodeGameObject(string nodeID)
|
|
{
|
|
return nodeLists.Find(node => node.name == nodeID);
|
|
}
|
|
|
|
[YarnCommand("SetIsTriggerDialogueWhenConnect")]
|
|
public void SetIsTriggerDialogueWhenConnect(bool isTrigger)
|
|
{
|
|
isTriggerDialogue=isTrigger;
|
|
}
|
|
|
|
[YarnCommand("ChangeConnectionVisual")]
|
|
public IEnumerator ChangeConnectionVisual(string startNode,float speed,string color)
|
|
{
|
|
Connection Targetconnection=gm.localConnections.Find(connection => connection.port0.node.ID == startNode);
|
|
if (Targetconnection == null)
|
|
{
|
|
Debug.LogWarning($"Connection start with {startNode} not found!");
|
|
yield break;
|
|
}
|
|
Targetconnection.line.animation.isActive=true;
|
|
Targetconnection.line.animation.shape=Shape.Type.Square;
|
|
Targetconnection.line.animation.speed=speed;
|
|
Targetconnection.line.animation.size=30;
|
|
switch (color)
|
|
{
|
|
case "Blue":
|
|
Targetconnection.line.animation.color=Color.blue;
|
|
break;
|
|
}
|
|
Targetconnection.line.animation.DrawLineAnimation(gm.lineRenderer,Targetconnection.line);
|
|
|
|
}
|
|
public void CheckConnection(Connection connection)
|
|
{
|
|
currentConnection=connection;
|
|
StorageSystem.Instance?.SetValue("$StartNode",connection.port0.node.ID);
|
|
StorageSystem.Instance?.SetValue("$EndNode",connection.port1.node.ID);
|
|
if(isTriggerDialogue)
|
|
{
|
|
DialogController.Instance.StartDialogNode("检查ClueBoard");
|
|
|
|
}
|
|
}
|
|
[YarnCommand("ConnectionCantBreak")]
|
|
public void SetConnectionProperties()
|
|
{
|
|
if (currentConnection == null)
|
|
{
|
|
Debug.LogWarning("Connection not found.");
|
|
return;
|
|
}
|
|
|
|
currentConnection.DisableClick=false;
|
|
currentConnection.EnableDrag=false;
|
|
currentConnection.EnableHover=false;
|
|
currentConnection.line.color=Color.red;
|
|
|
|
}
|
|
// [YarnCommand("AddNodeConnection")]
|
|
// public void CreateConnection(string starNode,int startPort,string endNode,int endPort)
|
|
// {
|
|
// Node start= GetNode(starNode);
|
|
// Node end= GetNode(endNode);
|
|
// Connection newConnection=start.ports[startPort].ConnectTo(end.ports[endPort]);
|
|
// newConnection.DisableClick=false;
|
|
// newConnection.EnableDrag=false;
|
|
// gm.UnselectAllElements();
|
|
// newConnection.DisableClick=false;
|
|
// newConnection.EnableDrag=false;
|
|
// newConnection.EnableHover=false;
|
|
// newConnection.line.color=Color.green;
|
|
// //currentConnection=null;
|
|
// }
|
|
[YarnCommand("EnableQuestion")]
|
|
public void EnableQuestion(string nodeID)
|
|
{
|
|
GetNode(nodeID).transform.GetChild(2).gameObject.SetActive(true);
|
|
}
|
|
|
|
}
|
|
|
|
|