HeavenStudioPlus/Assets/Scripts/LevelEditor/Timeline/TempoTimelineObj.cs

106 lines
3.7 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
2022-01-29 02:26:34 +00:00
using DG.Tweening;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.Editor.Track
{
public class TempoTimelineObj : MonoBehaviour
{
[Header("Components")]
[SerializeField] private RectTransform rectTransform;
[SerializeField] private TMP_Text tempoTXT;
2022-01-29 02:26:34 +00:00
[SerializeField] private RectTransform raycastRect;
public Beatmap.TempoChange tempoChange;
2022-01-29 02:26:34 +00:00
private float startPosX;
private bool moving = false;
public bool hovering;
private float lastPosX;
private void Start()
{
rectTransform = GetComponent<RectTransform>();
tempoTXT = transform.GetChild(2).GetComponent<TMP_Text>();
UpdateTempo();
}
private void Update()
{
2022-01-29 02:26:34 +00:00
if (Timeline.instance.timelineState.tempoChange && !Conductor.instance.NotStopped())
{
2022-02-03 08:25:27 +00:00
if (RectTransformUtility.RectangleContainsScreenPoint(raycastRect, Input.mousePosition, Editor.instance.EditorCamera))
2022-01-29 02:26:34 +00:00
{
float newTempo = Input.mouseScrollDelta.y;
if (Input.GetKey(KeyCode.LeftShift))
newTempo *= 5f;
if (Input.GetKey(KeyCode.LeftControl))
newTempo /= 100f;
tempoChange.tempo += newTempo;
//make sure tempo is positive
if (tempoChange.tempo < 1)
tempoChange.tempo = 1;
2022-01-29 02:26:34 +00:00
if (Input.GetMouseButtonDown(0))
{
2022-02-03 08:25:27 +00:00
Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition);
2022-01-29 02:26:34 +00:00
startPosX = mousePos.x - transform.position.x;
moving = true;
lastPosX = transform.localPosition.x;
}
else if (Input.GetMouseButtonDown(1))
{
GameManager.instance.Beatmap.tempoChanges.Remove(tempoChange);
transform.parent.GetComponent<TempoTimeline>().tempoTimelineObjs.Remove(this);
2022-01-29 02:26:34 +00:00
Destroy(this.gameObject);
}
2022-01-29 02:26:34 +00:00
hovering = true;
}
else
{
hovering = false;
}
if (moving)
{
2022-02-03 08:25:27 +00:00
Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition);
2022-01-29 02:26:34 +00:00
transform.position = new Vector3(mousePos.x - startPosX, transform.position.y, 0);
transform.localPosition = new Vector3(Mathf.Clamp(Starpelly.Mathp.Round2Nearest(transform.localPosition.x, Timeline.SnapInterval()), 0, Mathf.Infinity), transform.localPosition.y);
2022-01-29 02:26:34 +00:00
}
if (Input.GetMouseButtonUp(0))
{
if (transform.parent.GetComponent<TempoTimeline>().tempoTimelineObjs.Find(c => c.gameObject.transform.localPosition.x == this.transform.localPosition.x && c != this) != null)
{
transform.localPosition = new Vector3(lastPosX, transform.localPosition.y);
}
else
{
tempoChange.beat = transform.localPosition.x;
}
moving = false;
lastPosX = transform.localPosition.x;
}
UpdateTempo();
}
}
private void UpdateTempo()
{
tempoTXT.text = $"{tempoChange.tempo} BPM";
Timeline.instance.FitToSong();
}
}
}