Files
aibis-dream/Assets/Scripts/FixSystem/Scope/OscilloscopeController.cs
T
2024-11-12 14:32:02 +08:00

308 lines
9.9 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using RainbowArt.CleanFlatUI;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using DG.Tweening;
using TMPro;
using UnityEditor.Localization.Plugins.XLIFF.V20;
using AibisDream;
using Yarn.Unity;
public class OscilloscopeController : MonoBehaviour
{
public WaveformVisualizer waveformVisualizer;
public LineRenderer scanLine;
//public SliderCircular phaseSlider;
public Slider phaseSlider;
public SliderCircular checkLine;
public Button DeepInButton;
private float phaseShiftMax = Mathf.PI * 1.5f;
private Plug plug;
private EyeTransitionManager transitionManager;
private bool setInitSliderValue = false;
private float scanSpeed = 2f;
public TMP_Text screenText;
private Socket currentSocket;
void Start()
{
scanLine.startWidth = 0.05f;
scanLine.endWidth = 0.05f;
scanLine.gameObject.SetActive(false);
transitionManager = FindObjectOfType<EyeTransitionManager>();
plug = FindObjectOfType<Plug>();
DeepInButton.GetComponent<Button>().onClick.AddListener(DeepIn);
screenText.text = "";
currentSocket = null;
if (plug != null)
{
plug.OnPlugInserted += HandlePlugInserted;
plug.OnPlugRemoved += HandlePlugRemoved;
}
}
private void HandlePlugInserted(Plug plug)
{
// 这里可以处理插入后的波形显示逻辑
Debug.Log("Plug inserted into the socket, starting waveform display.");
}
private void HandlePlugRemoved()
{
// 这里可以处理移除后的清除波形逻辑
screenText.text = "";
Debug.Log("Plug removed from the socket, stopping waveform display.");
}
public void DeepIn()
{
if (plug == null)
{
Debug.Log("未找到插头");
return;
}
currentSocket = plug.currentSocket;
if (currentSocket == null || currentSocket.targetData == null)
{
Debug.Log("未插入任何目标模块");
screenText.text = "未插入任何目标模块";
return;
}
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
string yarnString = "On" + trimmedString + "DeepIn";
if (currentSocket.WaitYarnCommand)
{
// DialogController.Instance.SetVariable("$ViewToDeepIn",trimmedString);
// DialogController.Instance.StartDialogNode("DeepInView");
}
else if (currentSocket.targetData.HasExternalError())
{
transitionManager.SetViewAvailability(trimmedString, true);
DeepInButton.interactable = false;
HandleDeepIn();
}
else
{
StartCoroutine(ScanForError(trimmedString)); // 启动扫描协程
}
}
private void DeepInexecution()
{
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
StartCoroutine(transitionManager.StartTransitionCoroutine(trimmedString));
DeepInButton.interactable = true;
phaseSlider.interactable = true;
//screenText.text="";
plug.Enable_plugInput();
}
private void HandleDeepIn()
{
if (currentSocket.notAlowedDeep)
{
screenText.text = "特殊保护,禁止深入模块";
DeepInButton.interactable = true;
phaseSlider.interactable = true;
//screenText.text="";
plug.Enable_plugInput();
}
else
{
screenText.text = "错误找到,开始深入准备";
}
string originalString = currentSocket.name;
string trimmedString = originalString.Replace("Socket", "");
string yarnString = "On" + trimmedString + "DeepIn";
if (DialogController.Instance.CheckIfNodeExistInCurrentYarnProject(yarnString))
{
DialogController.Instance.StartDialogNode(yarnString);
if (!currentSocket.notAlowedDeep)
{
DialogController.OnDialogueComplete += DeepInexecution;
}
}
else
{
if (!currentSocket.notAlowedDeep)
{
DeepInexecution();
}
}
}
void HandleExternalError()
{
scanLine.gameObject.SetActive(false);
phaseSlider.interactable = false;
waveformVisualizer.gameObject.SetActive(false);
screenText.text = "外部错误已关联,可以直接深入";
}
private IEnumerator ScanForError(string trimmedString)
{
phaseSlider.interactable = false;
plug.Disable_plugInput();
scanLine.gameObject.SetActive(true);
// 设置扫描线初始位置(左侧)
float startX = -waveformVisualizer.spriteWidth / 2;
float endX = waveformVisualizer.spriteWidth / 2;
float currentX = startX;
// 上下点的位置,x 相同,y 为正负数
Vector3 topPosition = new Vector3(currentX, waveformVisualizer.spriteHeight / 2, 0);
Vector3 bottomPosition = new Vector3(currentX, -waveformVisualizer.spriteHeight / 2, 0);
scanLine.SetPosition(0, topPosition);
scanLine.SetPosition(1, bottomPosition);
//float scanSpeed = waveformVisualizer.scanSpeed;
bool isErrorFound = false;
// 计算目标位置对应的 x 值
float spikeX =
(waveformVisualizer.SpikeIndex / (float)waveformVisualizer.samples) * waveformVisualizer.spriteWidth -
waveformVisualizer.spriteWidth / 2;
float previousX = currentX;
screenText.text = "搜寻中...";
// 从左到右扫描
while (currentX <= endX)
{
// 更新扫描线的位置,x 向右移动
currentX += Time.deltaTime * scanSpeed;
topPosition.x = currentX;
bottomPosition.x = currentX;
scanLine.SetPosition(0, topPosition);
scanLine.SetPosition(1, bottomPosition);
// 检查是否跨越了 spikeX
if (waveformVisualizer.isErrorFind && ((previousX < spikeX && currentX >= spikeX) ||
(previousX > spikeX && currentX <= spikeX)))
{
isErrorFound = true;
topPosition.x = spikeX;
bottomPosition.x = spikeX;
scanLine.SetPosition(0, topPosition);
scanLine.SetPosition(1, bottomPosition);
scanLine.startColor = Color.red;
scanLine.endColor = Color.red;
//Debug.Log("找到错误,可以开始深入");
//screenText.text="找到错误,可以开始深入";
yield return new WaitForSeconds(1f);
transitionManager.SetViewAvailability(trimmedString, true);
DeepInButton.interactable = false;
HandleDeepIn();
break;
}
previousX = currentX; // 更新 previousX 为当前的 currentX
yield return null; // 等待下一帧
}
if (!isErrorFound)
{
Debug.Log("未找到错误,无法深入");
screenText.text = "未找到错误,无法深入";
transitionManager.SetViewAvailability(trimmedString, false);
}
// 扫描完成,停留片刻后隐藏扫描线
yield return new WaitForSeconds(0.1f);
scanLine.gameObject.SetActive(false);
scanLine.startColor = Color.white;
scanLine.endColor = Color.white;
phaseSlider.interactable = true;
plug.Enable_plugInput();
//screenText.text="";
}
void Update()
{
if (plug != null)
{
var currentSocket = plug.currentSocket;
if (currentSocket != null && currentSocket.targetData != null)
{
var targetData = currentSocket.targetData;
if (targetData != null)
{
if (targetData.HasExternalError())
{
screenText.text = "外部错误已关联,可以直接深入";
return;
}
if (!setInitSliderValue)
{
setInitSliderValue = true;
phaseSlider.value = targetData.sliderValue;
}
waveformVisualizer.waveformType = targetData.waveformType;
waveformVisualizer.clarity = 1;
float phaseShift = 0f;
if (phaseSlider != null)
{
float sliderPhase = (phaseSlider.value / 8f) * phaseShiftMax;
phaseShift = sliderPhase + targetData.initialRandomPhase;
targetData.sliderValue = phaseSlider.value;
}
if (phaseSlider.value > 0)
{
waveformVisualizer.SetPhaseShift(phaseShift);
}
else
{
waveformVisualizer.SetPhaseShift(0);
}
waveformVisualizer.gameObject.SetActive(true);
if (!targetData.isCorrect)
{
waveformVisualizer.SetError(targetData);
}
}
}
else
{
ResetVisualizer();
}
}
}
void ResetVisualizer()
{
waveformVisualizer.waveformType = WaveformType.Noise;
waveformVisualizer.clarity = 0;
waveformVisualizer.ResetWaveform();
waveformVisualizer.gameObject.SetActive(true);
phaseSlider.value = 0;
waveformVisualizer.SetPhaseShift(0);
setInitSliderValue = false;
}
}