HeavenStudioPlus/Assets/Scripts/LevelEditor/Timeline.cs

258 lines
8.8 KiB
C#
Raw Normal View History

2022-01-06 00:11:33 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
2022-01-08 16:42:48 +00:00
using Starpelly;
2022-01-06 00:11:33 +00:00
namespace RhythmHeavenMania.Editor
{
public class Timeline : MonoBehaviour
{
[Header("Song Positions")]
[SerializeField] private TMP_Text SongBeat;
[SerializeField] private TMP_Text SongPos;
[Header("Timeline Properties")]
private float lastBeatPos = 0;
private Vector2 lastMousePos;
2022-01-06 00:11:33 +00:00
[Header("Timeline Components")]
[SerializeField] private RectTransform TimelineSlider;
[SerializeField] private TMP_Text TimelinePlaybackBeat;
[SerializeField] private RectTransform TimelineContent;
[SerializeField] private RectTransform TimelineSongPosLineRef;
2022-01-09 23:35:55 +00:00
[SerializeField] private RectTransform TimelineEventObjRef;
private RectTransform TimelineSongPosLine;
2022-01-06 00:11:33 +00:00
2022-01-11 00:17:29 +00:00
public static Timeline instance { get; private set; }
2022-01-08 16:42:48 +00:00
2022-01-06 00:11:33 +00:00
#region Initializers
public void Init()
{
2022-01-11 00:17:29 +00:00
instance = this;
2022-01-08 16:42:48 +00:00
for (int i = 0; i < GameManager.instance.Beatmap.entities.Count; i++)
{
var entity = GameManager.instance.Beatmap.entities[i];
var e = GameManager.instance.Beatmap.entities[i];
2022-01-09 23:35:55 +00:00
EventCaller.GameAction gameAction = EventCaller.instance.GetGameAction(EventCaller.instance.GetMinigame(e.datamodel.Split(0)), e.datamodel.Split(1));
GameObject g = Instantiate(TimelineEventObjRef.gameObject, TimelineEventObjRef.parent);
2022-01-08 16:42:48 +00:00
g.transform.localPosition = new Vector3(e.beat, Mathp.Round2Nearest(Random.Range(0, -205.36f), 51.34f));
g.transform.GetChild(1).GetComponent<TMP_Text>().text = e.datamodel.Split('/')[1];
2022-01-09 23:35:55 +00:00
TimelineEventObj eventObj = g.GetComponent<TimelineEventObj>();
eventObj.Icon.sprite = Editor.GameIcon(e.datamodel.Split(0));
2022-01-08 16:42:48 +00:00
if (gameAction != null)
{
2022-01-09 23:35:55 +00:00
g.GetComponent<RectTransform>().sizeDelta = new Vector2(gameAction.defaultLength, g.GetComponent<RectTransform>().sizeDelta.y);
float length = gameAction.defaultLength;
eventObj.length = length;
2022-01-08 16:42:48 +00:00
}
g.SetActive(true);
2022-01-09 23:35:55 +00:00
entity.eventObj = g.GetComponent<TimelineEventObj>();
entity.track = (int)(g.transform.localPosition.y / 51.34f * -1);
2022-01-08 16:42:48 +00:00
}
2022-01-06 00:11:33 +00:00
}
#endregion
private void Update()
{
if (!Conductor.instance.isPlaying && !Conductor.instance.isPaused)
{
SongBeat.text = $"Beat {string.Format("{0:0.000}", TimelineSlider.localPosition.x)}";
SongPos.text = FormatTime(Conductor.instance.GetSongPosFromBeat(TimelineSlider.localPosition.x));
}
else
{
SongBeat.text = $"Beat {string.Format("{0:0.000}", Conductor.instance.songPositionInBeats)}";
SongPos.text = FormatTime(Conductor.instance.songPosition);
}
2022-01-06 00:11:33 +00:00
SliderControl();
2022-01-06 00:11:33 +00:00
if (Input.GetKeyDown(KeyCode.Space))
{
if (Input.GetKey(KeyCode.LeftShift))
{
2022-01-08 16:42:48 +00:00
PlayCheck(false);
2022-01-06 00:11:33 +00:00
}
else
{
2022-01-08 16:42:48 +00:00
PlayCheck(true);
2022-01-06 00:11:33 +00:00
}
}
lastBeatPos = Conductor.instance.songPositionInBeats;
2022-01-08 16:42:48 +00:00
if (Input.GetMouseButton(1) && !Conductor.instance.isPlaying)
{
RectTransformUtility.ScreenPointToLocalPointInRectangle(TimelineContent, Input.mousePosition, Editor.instance.EditorCamera, out lastMousePos);
2022-01-08 16:42:48 +00:00
TimelineSlider.localPosition = new Vector3(Mathp.Round2Nearest(lastMousePos.x + 0.12f, 0.25f), TimelineSlider.transform.localPosition.y);
Conductor.instance.SetBeat(TimelineSlider.transform.localPosition.x);
}
2022-01-06 00:11:33 +00:00
}
private void SliderControl()
{
TimelinePlaybackBeat.text = $"Beat {string.Format("{0:0.000}", TimelineSlider.localPosition.x)}";
if (TimelineSongPosLine != null)
2022-01-06 00:11:33 +00:00
{
TimelineSongPosLine.transform.localPosition = new Vector3(Conductor.instance.songPositionInBeats, TimelineSlider.transform.localPosition.y);
TimelineSongPosLine.transform.localScale = new Vector3(1f / TimelineContent.transform.localScale.x, TimelineSlider.transform.localScale.y, 1);
2022-01-06 00:11:33 +00:00
}
}
#region PlayChecks
public void PlayCheck(bool fromStart)
{
if (fromStart)
{
2022-01-08 16:42:48 +00:00
if (!Conductor.instance.isPlaying)
{
Play(false, TimelineSlider.transform.localPosition.x);
}
2022-01-06 00:11:33 +00:00
else
2022-01-08 16:42:48 +00:00
{
Stop(TimelineSlider.transform.localPosition.x);
}
2022-01-06 00:11:33 +00:00
}
else
{
if (!Conductor.instance.isPlaying)
2022-01-06 00:11:33 +00:00
{
2022-01-08 16:42:48 +00:00
Play(false, TimelineSongPosLine.transform.localPosition.x);
2022-01-06 00:11:33 +00:00
}
else
{
Pause();
}
}
}
2022-01-08 16:42:48 +00:00
public void Play(bool fromStart, float time)
2022-01-06 00:11:33 +00:00
{
2022-01-08 16:42:48 +00:00
// if (fromStart) Stop();
if (!Conductor.instance.isPaused)
{
TimelineSongPosLine = Instantiate(TimelineSongPosLineRef, TimelineSongPosLineRef.parent).GetComponent<RectTransform>();
TimelineSongPosLine.gameObject.SetActive(true);
}
2022-01-08 16:42:48 +00:00
Conductor.instance.Play(time);
2022-01-06 00:11:33 +00:00
}
public void Pause()
{
// isPaused = true;
Conductor.instance.Pause();
2022-01-06 00:11:33 +00:00
}
2022-01-08 16:42:48 +00:00
public void Stop(float time)
2022-01-06 00:11:33 +00:00
{
// isPaused = true;
// timelineSlider.value = 0;
if (TimelineSongPosLine != null)
Destroy(TimelineSongPosLine.gameObject);
2022-01-08 16:42:48 +00:00
Conductor.instance.Stop(time);
2022-01-06 00:11:33 +00:00
}
#endregion
#region Extras
private string FormatTime(float time)
{
int minutes = (int)time / 60;
int seconds = (int)time - 60 * minutes;
int milliseconds = (int)(1000 * (time - minutes * 60 - seconds));
return string.Format("{0:00}:{1:00}:{2:000}", minutes, seconds, milliseconds);
}
2022-01-11 00:17:29 +00:00
public bool CheckIfMouseInTimeline()
{
return (this.gameObject.activeSelf && RectTransformUtility.RectangleContainsScreenPoint(TimelineContent.transform.parent.gameObject.GetComponent<RectTransform>(), Input.mousePosition, Camera.main));
}
#endregion
#region Functions
public void AddEventObject(string eventName, bool dragNDrop = false)
{
GameObject g = Instantiate(TimelineEventObjRef.gameObject, TimelineEventObjRef.parent);
g.transform.localPosition = new Vector3(0, 0);
g.transform.GetChild(1).GetComponent<TMP_Text>().text = eventName.Split('/')[1];
TimelineEventObj eventObj = g.GetComponent<TimelineEventObj>();
eventObj.Icon.sprite = Editor.GameIcon(eventName.Split(0));
EventCaller.GameAction gameAction = EventCaller.instance.GetGameAction(EventCaller.instance.GetMinigame(eventName.Split(0)), eventName.Split(1));
if (gameAction != null)
{
g.GetComponent<RectTransform>().sizeDelta = new Vector2(gameAction.defaultLength, g.GetComponent<RectTransform>().sizeDelta.y);
float length = gameAction.defaultLength;
eventObj.length = length;
}
g.SetActive(true);
Beatmap.Entity entity = new Beatmap.Entity();
entity.datamodel = eventName;
entity.eventObj = eventObj;
GameManager.instance.Beatmap.entities.Add(entity);
GameManager.instance.SortEventsList();
g.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (dragNDrop)
{
eventObj.OnDown();
}
2022-01-12 03:29:27 +00:00
Editor.EventObjs.Add(eventObj);
2022-01-11 00:17:29 +00:00
// entity.eventObj = g.GetComponent<TimelineEventObj>();
// entity.track = (int)(g.transform.localPosition.y / 51.34f * -1);
}
2022-01-12 03:29:27 +00:00
public void DestroyEventObject(TimelineEventObj eventObj)
{
var e = GameManager.instance.Beatmap.entities.Find(c => c.eventObj == eventObj);
GameManager.instance.Beatmap.entities.Remove(e);
GameManager.instance.SortEventsList();
Destroy(eventObj.gameObject);
Editor.EventObjs.Remove(eventObj);
}
#endregion
#region Commands
public void Move()
{
}
public void Undo()
{
}
2022-01-06 00:11:33 +00:00
#endregion
}
}