76 lines
2.2 KiB
C#
76 lines
2.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class WaveSlider : MonoBehaviour
|
|
{
|
|
private OscilloscopeSystem _oscilloscopeSystem;
|
|
private OscilloscopeSystem OscilloscopeSystem
|
|
{
|
|
get
|
|
{
|
|
if (!_oscilloscopeSystem)
|
|
{
|
|
_oscilloscopeSystem = FixSystemCenter.SystemDic.Get<OscilloscopeSystem>();
|
|
}
|
|
|
|
return _oscilloscopeSystem;
|
|
}
|
|
}
|
|
|
|
private Transform _slider; // 滑块的 Transform
|
|
|
|
private Vector2 _startPos;
|
|
private Vector2 _endPos;
|
|
|
|
public float Value { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
InitComponentsRef();
|
|
}
|
|
|
|
private void InitComponentsRef()
|
|
{
|
|
_slider = transform.Find("Sliding Block");
|
|
var startTrans = transform.Find("Slider Start Pos");
|
|
var endTrans = transform.Find("Slider End Pos");
|
|
|
|
_startPos = new Vector2(startTrans.position.x, startTrans.position.y);
|
|
_endPos = new Vector2(endTrans.position.x, endTrans.position.y);
|
|
}
|
|
|
|
public void InitSlider()
|
|
{
|
|
_slider.position = new Vector3(_startPos.x, _startPos.y, _slider.position.z);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (OscilloscopeSystem.isScanning) return;
|
|
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
var clampedX = Mathf.Clamp(mousePos.x, _startPos.x, _endPos.x);
|
|
_slider.position = new Vector3(clampedX, _slider.position.y, _slider.position.z);
|
|
|
|
// 计算滑块在轨道上的比例
|
|
var curValue = (clampedX - _startPos.x) / (_endPos.x - _startPos.x);
|
|
UpdateValue(curValue);
|
|
}
|
|
}
|
|
|
|
private void UpdateValue(float curValue)
|
|
{
|
|
if (!Mathf.Approximately(curValue, Value))
|
|
{
|
|
// 如果相位有更新,就同步给波形
|
|
OscilloscopeSystem.AdjustPhaseShift(curValue);
|
|
Value = curValue;
|
|
}
|
|
}
|
|
}
|
|
}
|