100 lines
2.8 KiB
C#
100 lines
2.8 KiB
C#
using UnityEngine;
|
|
|
|
namespace AibisDream.FixSystem
|
|
{
|
|
public class WaveSlider : MonoBehaviour
|
|
{
|
|
private OscilloscopeSystem _oscilloscopeSystem;
|
|
|
|
private Transform _slider; // 滑块的 Transform
|
|
|
|
private Vector2 _startPos;
|
|
private Vector2 _endPos;
|
|
|
|
private float _value;
|
|
private bool _isDragging;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
InitComponentsRef();
|
|
}
|
|
|
|
private void InitComponentsRef()
|
|
{
|
|
_oscilloscopeSystem = transform.GetComponentInParent<OscilloscopeSystem>();
|
|
|
|
_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);
|
|
InitSlider();
|
|
}
|
|
|
|
public void InitSlider()
|
|
{
|
|
_slider.position = new Vector3(_startPos.x, _startPos.y, _slider.position.z);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (_oscilloscopeSystem.isScanning) return;
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
OnMouseButtonDown();
|
|
}
|
|
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
OnMouseButtonUp();
|
|
}
|
|
|
|
if (_isDragging)
|
|
{
|
|
OnDragging();
|
|
}
|
|
}
|
|
|
|
private void OnMouseButtonDown()
|
|
{
|
|
// 检测鼠标是否点击在滑块上
|
|
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
Collider2D hitCollider = Physics2D.OverlapPoint(mousePos);
|
|
|
|
if (hitCollider && hitCollider.transform == _slider)
|
|
{
|
|
_isDragging = true;
|
|
}
|
|
}
|
|
|
|
private void OnMouseButtonUp()
|
|
{
|
|
_isDragging = false;
|
|
}
|
|
|
|
private void OnDragging()
|
|
{
|
|
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
float 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;
|
|
}
|
|
}
|
|
}
|
|
}
|