HeavenStudioPlus/Assets/Scripts/LevelEditor/TimelineEventObj.cs

224 lines
6.1 KiB
C#
Raw Normal View History

2022-01-09 23:35:55 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Starpelly;
using DG.Tweening;
namespace RhythmHeavenMania.Editor
{
public class TimelineEventObj : MonoBehaviour
{
private float startPosX;
private float startPosY;
2022-01-11 00:17:29 +00:00
public bool isDragging;
2022-01-09 23:35:55 +00:00
private Vector3 lastPos;
[Header("Components")]
[SerializeField] private RectTransform PosPreview;
[SerializeField] private RectTransform PosPreviewRef;
[SerializeField] public Image Icon;
[Header("Properties")]
private int enemyIndex;
public float length;
private bool eligibleToMove = false;
2022-01-12 03:29:27 +00:00
private bool lastVisible;
2022-01-13 03:59:54 +00:00
public bool selected;
public bool mouseHovering;
2022-01-12 03:29:27 +00:00
[Header("Colors")]
public Color NormalCol;
public Color SelectedCol;
public Color DeleteCol;
2022-01-09 23:35:55 +00:00
private void Update()
{
2022-01-13 03:59:54 +00:00
mouseHovering = RectTransformUtility.RectangleContainsScreenPoint(GetComponent<RectTransform>(), Input.mousePosition, Camera.main);
#region Optimizations
2022-01-12 03:29:27 +00:00
bool visible = GetComponent<RectTransform>().IsVisibleFrom(Camera.main);
if (visible != lastVisible)
{
for (int i = 0; i < this.transform.childCount; i++)
{
this.transform.GetChild(i).gameObject.SetActive(visible);
}
}
lastVisible = visible;
2022-01-13 03:59:54 +00:00
#endregion
if (selected)
{
SetColor(1);
}
else
{
SetColor(0);
}
2022-01-12 03:29:27 +00:00
2022-01-11 00:17:29 +00:00
if (Conductor.instance.NotStopped())
2022-01-09 23:35:55 +00:00
{
Cancel();
return;
}
enemyIndex = GameManager.instance.Beatmap.entities.FindIndex(a => a.eventObj == this);
2022-01-13 03:59:54 +00:00
if (Input.GetMouseButtonDown(0) && Timeline.instance.IsMouseAboveEvents())
{
if (selected)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
startPosX = mousePos.x - this.transform.position.x;
startPosY = mousePos.y - this.transform.position.y;
isDragging = true;
}
}
else if (Input.GetMouseButtonUp(0))
{
if (!mouseHovering && !isDragging && !BoxSelection.instance.selecting)
{
if (!Input.GetKey(KeyCode.LeftShift))
{
Selections.instance.Deselect(this);
}
}
OnUp();
}
if (isDragging && selected)
2022-01-09 23:35:55 +00:00
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
2022-01-11 00:17:29 +00:00
this.transform.position = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY - 0.40f, 0);
2022-01-12 03:29:27 +00:00
this.transform.localPosition = new Vector3(Mathf.Clamp(Mathp.Round2Nearest(this.transform.localPosition.x, 0.25f), 0, Mathf.Infinity), Mathf.Clamp(Mathp.Round2Nearest(this.transform.localPosition.y, 51.34f), -51.34f * 3, 0));
2022-01-09 23:35:55 +00:00
if (lastPos != transform.localPosition)
OnMove();
2022-01-11 00:17:29 +00:00
lastPos = this.transform.localPosition;
2022-01-09 23:35:55 +00:00
}
2022-01-11 00:17:29 +00:00
2022-01-12 03:29:27 +00:00
if (Input.GetKeyDown(KeyCode.Delete))
Timeline.instance.DestroyEventObject(this);
2022-01-13 03:59:54 +00:00
2022-01-09 23:35:55 +00:00
}
private void OnMove()
{
2022-01-11 00:17:29 +00:00
if (GameManager.instance.Beatmap.entities.FindAll(c => c.beat == this.transform.localPosition.x && c.track == (int)(this.transform.localPosition.y / 51.34f * -1)).Count > 0)
2022-01-09 23:35:55 +00:00
{
eligibleToMove = false;
}
else
{
eligibleToMove = true;
}
}
private void OnComplete()
{
var entity = GameManager.instance.Beatmap.entities[enemyIndex];
2022-01-11 00:17:29 +00:00
entity.beat = this.transform.localPosition.x;
2022-01-09 23:35:55 +00:00
GameManager.instance.SortEventsList();
2022-01-11 00:17:29 +00:00
entity.track = (int)(this.transform.localPosition.y / 51.34f) * -1;
2022-01-13 03:59:54 +00:00
}
#region ClickEvents
2022-01-09 23:35:55 +00:00
2022-01-13 03:59:54 +00:00
public void OnDown()
{
if (!selected)
{
if (Input.GetKey(KeyCode.LeftShift))
{
Selections.instance.ShiftClickSelect(this);
}
else
{
Selections.instance.ClickSelect(this);
}
// Selector.instance.Click(this);
}
}
public void OnUp()
{
if (selected)
{
isDragging = false;
if (eligibleToMove)
{
OnComplete();
}
Cancel();
}
2022-01-09 23:35:55 +00:00
}
private void Cancel()
{
eligibleToMove = false;
}
2022-01-13 03:59:54 +00:00
#endregion
#region Selection
public void Select()
2022-01-09 23:35:55 +00:00
{
2022-01-13 03:59:54 +00:00
selected = true;
2022-01-09 23:35:55 +00:00
}
2022-01-13 03:59:54 +00:00
public void DeSelect()
2022-01-09 23:35:55 +00:00
{
2022-01-13 03:59:54 +00:00
selected = false;
2022-01-09 23:35:55 +00:00
}
2022-01-12 03:29:27 +00:00
2022-01-13 03:59:54 +00:00
#endregion
#region Extra
2022-01-12 03:29:27 +00:00
public void SetColor(int type)
{
Color c = Color.white;
switch (type)
{
case 0:
c = NormalCol;
break;
case 1:
c = SelectedCol;
break;
case 2:
c = DeleteCol;
break;
}
transform.GetChild(0).GetComponent<Image>().color = c;
}
2022-01-13 03:59:54 +00:00
private void OnDestroy()
{
// better safety net than canada's healthcare system
GameManager.instance.Beatmap.entities.Remove(GameManager.instance.Beatmap.entities.Find(c => c.eventObj = this));
}
#endregion
2022-01-09 23:35:55 +00:00
}
}