HeavenStudio/Assets/Scripts/LevelEditor/TimelineEventObj.cs

372 lines
11 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;
2022-01-17 19:23:18 +00:00
private RectTransform rectTransform;
2022-01-09 23:35:55 +00:00
[Header("Components")]
[SerializeField] private RectTransform PosPreview;
[SerializeField] private RectTransform PosPreviewRef;
[SerializeField] public Image Icon;
[Header("Properties")]
private Beatmap.Entity entity;
2022-01-09 23:35:55 +00:00
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-17 19:23:18 +00:00
public bool resizable;
public bool resizing;
2022-01-12 03:29:27 +00:00
[Header("Colors")]
public Color NormalCol;
2022-01-09 23:35:55 +00:00
2022-01-17 19:23:18 +00:00
private void Start()
{
rectTransform = GetComponent<RectTransform>();
if (!resizable)
{
Destroy(transform.GetChild(6).gameObject);
2022-01-17 20:08:32 +00:00
Destroy(transform.GetChild(7).gameObject);
Destroy(transform.GetChild(1).gameObject);
2022-01-17 19:23:18 +00:00
}
}
2022-01-09 23:35:55 +00:00
private void Update()
{
entity = GameManager.instance.Beatmap.entities.Find(a => a.eventObj == this);
2022-01-17 19:23:18 +00:00
mouseHovering = RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Camera.main);
2022-01-13 03:59:54 +00:00
#region Optimizations
2022-01-12 03:29:27 +00:00
2022-01-17 20:08:32 +00:00
// problem with long objects but im lazy right now
bool visible = rectTransform.IsVisibleFrom(Camera.main);
2022-01-12 03:29:27 +00:00
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
SetColor(GetTrack());
2022-01-13 03:59:54 +00:00
if (selected)
{
if (Input.GetKeyDown(KeyCode.Delete))
{
Selections.instance.Deselect(this);
Timeline.instance.DestroyEventObject(entity);
}
2022-01-16 19:23:46 +00:00
transform.GetChild(3).gameObject.SetActive(true);
for (int i = 0; i < transform.GetChild(4).childCount; i++)
2022-01-17 19:23:18 +00:00
{
2022-01-16 19:23:46 +00:00
transform.GetChild(4).GetChild(i).GetComponent<Image>().color = Color.cyan;
2022-01-17 19:23:18 +00:00
}
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3[] v = new Vector3[4];
rectTransform.GetWorldCorners(v);
if (mouseHovering)
{
if (mousePos.x > transform.position.x && mousePos.x < transform.position.x + 0.1f)
{
}
else if (mousePos.x > v[3].x - 0.1f && mousePos.x < v[3].x)
{
}
}
2022-01-16 19:23:46 +00:00
}
else
{
transform.GetChild(3).gameObject.SetActive(false);
for (int i = 0; i < transform.GetChild(4).childCount; i++)
transform.GetChild(4).GetChild(i).GetComponent<Image>().color = new Color32(0, 0, 0, 51);
2022-01-13 03:59:54 +00:00
}
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;
}
2022-01-17 19:23:18 +00:00
if (!resizing)
2022-01-13 03:59:54 +00:00
{
2022-01-17 19:23:18 +00:00
if (Input.GetMouseButtonDown(0) && Timeline.instance.IsMouseAboveEvents())
2022-01-13 03:59:54 +00:00
{
2022-01-17 19:23:18 +00:00
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;
2022-01-13 03:59:54 +00:00
2022-01-17 19:23:18 +00:00
isDragging = true;
}
2022-01-13 03:59:54 +00:00
}
2022-01-17 19:23:18 +00:00
else if (Input.GetMouseButtonUp(0))
2022-01-13 03:59:54 +00:00
{
2022-01-17 19:23:18 +00:00
if (!mouseHovering && !isDragging && !BoxSelection.instance.selecting)
2022-01-13 03:59:54 +00:00
{
2022-01-17 19:23:18 +00:00
if (!Input.GetKey(KeyCode.LeftShift))
{
Selections.instance.Deselect(this);
}
2022-01-13 03:59:54 +00:00
}
2022-01-17 19:23:18 +00:00
OnUp();
2022-01-13 03:59:54 +00:00
}
2022-01-17 19:23:18 +00:00
if (isDragging && selected)
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.transform.position = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY - 0.40f, 0);
this.transform.localPosition = new Vector3(Mathf.Clamp(Mathp.Round2Nearest(this.transform.localPosition.x, 0.25f), 0, Mathf.Infinity), Timeline.instance.SnapToLayer(this.transform.localPosition.y));
2022-01-13 03:59:54 +00:00
2022-01-17 19:23:18 +00:00
if (lastPos != transform.localPosition)
OnMove();
lastPos = this.transform.localPosition;
}
2022-01-13 03:59:54 +00:00
}
2022-01-17 19:23:18 +00:00
}
#region ResizeEvents
public void DragEnter()
{
2022-01-17 20:08:32 +00:00
Cursor.SetCursor(Resources.Load<Texture2D>("Cursors/horizontal_resize"), new Vector2(8, 8), CursorMode.Auto);
2022-01-17 19:23:18 +00:00
}
public void DragExit()
{
2022-01-17 20:08:32 +00:00
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
2022-01-17 19:23:18 +00:00
}
public void OnLeftDown()
{
2022-01-17 20:08:32 +00:00
SetPivot(new Vector2(1, rectTransform.pivot.y));
resizing = true;
2022-01-17 19:23:18 +00:00
}
2022-01-09 23:35:55 +00:00
2022-01-17 19:23:18 +00:00
public void DragLeft()
{
if (!resizing) return;
Vector2 sizeDelta = rectTransform.sizeDelta;
Vector2 mousePos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, Camera.main, out mousePos);
2022-01-09 23:35:55 +00:00
2022-01-17 19:23:18 +00:00
sizeDelta = new Vector2(-mousePos.x + 0.1f, sizeDelta.y);
sizeDelta = new Vector2(Mathf.Clamp(sizeDelta.x, 0.25f, rectTransform.localPosition.x), sizeDelta.y);
rectTransform.sizeDelta = new Vector2(Mathp.Round2Nearest(sizeDelta.x, 0.25f), sizeDelta.y);
}
2022-01-09 23:35:55 +00:00
2022-01-17 19:23:18 +00:00
public void OnLeftUp()
{
SetPivot(new Vector2(0, rectTransform.pivot.y));
resizing = false;
OnComplete();
}
public void OnRightDown()
{
2022-01-17 20:08:32 +00:00
SetPivot(new Vector2(0, rectTransform.pivot.y));
resizing = true;
2022-01-17 19:23:18 +00:00
}
public void DragRight()
{
if (!resizing) return;
// if (!mouseHovering) return;
2022-01-11 00:17:29 +00:00
2022-01-17 19:23:18 +00:00
Vector2 sizeDelta = rectTransform.sizeDelta;
Vector2 mousePos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, Camera.main, out mousePos);
sizeDelta = new Vector2(mousePos.x, sizeDelta.y);
sizeDelta = new Vector2(Mathf.Clamp(sizeDelta.x, 0.25f, Mathf.Infinity), sizeDelta.y);
rectTransform.sizeDelta = new Vector2(Mathp.Round2Nearest(sizeDelta.x, 0.25f), sizeDelta.y);
2022-01-09 23:35:55 +00:00
}
2022-01-17 19:23:18 +00:00
public void OnRightUp()
{
resizing = false;
OnComplete();
}
private void SetPivot(Vector2 pivot)
{
if (rectTransform == null) return;
Vector2 size = rectTransform.rect.size;
Vector2 deltaPivot = rectTransform.pivot - pivot;
Vector3 deltaPosition = new Vector3(deltaPivot.x * size.x, deltaPivot.y * size.y);
rectTransform.pivot = pivot;
rectTransform.localPosition -= deltaPosition;
}
#endregion
#region OnEvents
2022-01-09 23:35:55 +00:00
private void OnMove()
{
2022-01-14 22:46:14 +00:00
if (GameManager.instance.Beatmap.entities.FindAll(c => c.beat == this.transform.localPosition.x && c.track == GetTrack()).Count > 0)
2022-01-09 23:35:55 +00:00
{
eligibleToMove = false;
}
else
{
eligibleToMove = true;
}
}
private void OnComplete()
{
2022-01-17 19:23:18 +00:00
entity.length = rectTransform.sizeDelta.x;
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();
entity.track = GetTrack();
2022-01-13 03:59:54 +00:00
}
2022-01-17 19:23:18 +00:00
#endregion
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 = EditorTheme.theme.properties.Layer1Col.Hex2RGB();
2022-01-12 03:29:27 +00:00
break;
case 1:
c = EditorTheme.theme.properties.Layer2Col.Hex2RGB();
2022-01-12 03:29:27 +00:00
break;
case 2:
c = EditorTheme.theme.properties.Layer3Col.Hex2RGB();
break;
case 3:
c = EditorTheme.theme.properties.Layer4Col.Hex2RGB();
2022-01-12 03:29:27 +00:00
break;
}
2022-01-14 22:46:14 +00:00
// c = new Color(c.r, c.g, c.b, 0.85f);
2022-01-12 03:29:27 +00:00
transform.GetChild(0).GetComponent<Image>().color = c;
2022-01-17 20:08:32 +00:00
if (resizable)
{
c = new Color(0, 0, 0, 0.35f);
transform.GetChild(1).GetChild(0).GetComponent<Image>().color = c;
transform.GetChild(1).GetChild(1).GetComponent<Image>().color = c;
transform.GetChild(1).GetChild(2).GetComponent<Image>().color = c;
}
2022-01-12 03:29:27 +00:00
}
2022-01-13 03:59:54 +00:00
public int GetTrack()
{
2022-01-14 22:46:14 +00:00
return (int)(this.transform.localPosition.y / Timeline.instance.LayerHeight()) * -1;
}
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));
2022-01-13 03:59:54 +00:00
}
#endregion
2022-01-09 23:35:55 +00:00
}
}