2022-01-13 03:59:54 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
2022-01-14 02:33:51 +00:00
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
using Starpelly;
|
2022-01-13 03:59:54 +00:00
|
|
|
|
2022-01-28 02:50:57 +00:00
|
|
|
using RhythmHeavenMania.Editor.Track;
|
|
|
|
|
2022-01-13 03:59:54 +00:00
|
|
|
namespace RhythmHeavenMania.Editor
|
|
|
|
{
|
|
|
|
public class BoxSelection : MonoBehaviour
|
|
|
|
{
|
|
|
|
[SerializeField] private RectTransform boxVisual;
|
|
|
|
private Rect selectionBox;
|
|
|
|
|
|
|
|
private Vector2 startPosition = Vector2.zero;
|
|
|
|
private Vector2 endPosition = Vector2.zero;
|
|
|
|
|
|
|
|
public bool selecting = false;
|
|
|
|
|
|
|
|
public static BoxSelection instance { get; private set; }
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
DrawVisual();
|
2022-01-14 02:33:51 +00:00
|
|
|
|
|
|
|
Color boxCol = EditorTheme.theme.properties.BoxSelectionCol.Hex2RGB();
|
|
|
|
boxVisual.GetComponent<Image>().color = new Color(boxCol.r, boxCol.g, boxCol.b, 0.3f);
|
2022-01-16 19:23:46 +00:00
|
|
|
boxVisual.transform.GetChild(0).GetComponent<Image>().color = EditorTheme.theme.properties.BoxSelectionOutlineCol.Hex2RGB();
|
2022-01-13 03:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
{
|
2022-01-17 23:54:25 +00:00
|
|
|
if (Selections.instance.eventsSelected.Count > 0 && Timeline.instance.InteractingWithEvents())
|
2022-01-13 03:59:54 +00:00
|
|
|
{
|
2022-01-17 23:54:25 +00:00
|
|
|
startPosition = Vector2.zero;
|
|
|
|
endPosition = Vector2.zero;
|
|
|
|
DrawVisual();
|
2022-01-13 03:59:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-17 02:31:49 +00:00
|
|
|
if (Conductor.instance.NotStopped())
|
|
|
|
{
|
|
|
|
startPosition = Vector2.zero;
|
|
|
|
endPosition = Vector2.zero;
|
|
|
|
DrawVisual();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-13 03:59:54 +00:00
|
|
|
// click
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
|
|
{
|
2022-01-16 19:23:46 +00:00
|
|
|
startPosition = MousePosition();
|
2022-01-13 03:59:54 +00:00
|
|
|
selectionBox = new Rect();
|
|
|
|
}
|
|
|
|
|
|
|
|
// dragging
|
|
|
|
if (Input.GetMouseButton(0))
|
|
|
|
{
|
2022-01-16 19:23:46 +00:00
|
|
|
endPosition = MousePosition();
|
2022-01-13 03:59:54 +00:00
|
|
|
DrawVisual();
|
|
|
|
DrawSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
// release click
|
|
|
|
if (Input.GetMouseButtonUp(0))
|
|
|
|
{
|
|
|
|
startPosition = Vector2.zero;
|
|
|
|
endPosition = Vector2.zero;
|
|
|
|
DrawVisual();
|
|
|
|
SelectEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
// selecting = (selectionBox.size != Vector2.zero); -- doesn't work really
|
|
|
|
|
|
|
|
// for real time selection just move SelectEvents() to here, but that breaks some shit. might fix soon idk --pelly
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DrawVisual()
|
|
|
|
{
|
|
|
|
Vector2 boxStart = startPosition;
|
|
|
|
Vector2 boxEnd = endPosition;
|
|
|
|
|
|
|
|
Vector2 boxCenter = (boxStart + boxEnd) / 2;
|
|
|
|
boxVisual.position = boxCenter;
|
|
|
|
|
|
|
|
Vector2 boxSize = new Vector2(Mathf.Abs(boxStart.x - boxEnd.x), Mathf.Abs(boxStart.y - boxEnd.y));
|
|
|
|
|
|
|
|
boxVisual.sizeDelta = boxSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DrawSelection()
|
|
|
|
{
|
|
|
|
// X
|
2022-01-16 19:23:46 +00:00
|
|
|
if (MousePosition().x < startPosition.x)
|
2022-01-13 03:59:54 +00:00
|
|
|
{
|
|
|
|
// dragging left
|
2022-01-16 19:23:46 +00:00
|
|
|
selectionBox.xMin = MousePosition().x;
|
2022-01-13 03:59:54 +00:00
|
|
|
selectionBox.xMax = startPosition.x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// dragging right
|
|
|
|
selectionBox.xMin = startPosition.x;
|
2022-01-16 19:23:46 +00:00
|
|
|
selectionBox.xMax = MousePosition().x;
|
2022-01-13 03:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Y
|
2022-01-16 19:23:46 +00:00
|
|
|
if (MousePosition().y < startPosition.y)
|
2022-01-13 03:59:54 +00:00
|
|
|
{
|
|
|
|
// dragging down
|
2022-01-16 19:23:46 +00:00
|
|
|
selectionBox.yMin = MousePosition().y;
|
2022-01-13 03:59:54 +00:00
|
|
|
selectionBox.yMax = startPosition.y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// dragging up
|
|
|
|
selectionBox.yMin = startPosition.y;
|
2022-01-16 19:23:46 +00:00
|
|
|
selectionBox.yMax = MousePosition().y;
|
2022-01-13 03:59:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SelectEvents()
|
|
|
|
{
|
|
|
|
int selected = 0;
|
|
|
|
|
2022-01-21 21:09:14 +00:00
|
|
|
// if (!Input.GetKeyDown(KeyCode.LeftShift)) Selections.instance.DeselectAll();
|
2022-01-13 03:59:54 +00:00
|
|
|
for (int i = 0; i < GameManager.instance.Beatmap.entities.Count; i++)
|
|
|
|
{
|
|
|
|
TimelineEventObj e = GameManager.instance.Beatmap.entities[i].eventObj;
|
|
|
|
|
2022-01-17 23:54:25 +00:00
|
|
|
|
2022-01-16 19:23:46 +00:00
|
|
|
if (selectionBox.Overlaps(GetWorldRect(e.GetComponent<RectTransform>())))
|
2022-01-13 03:59:54 +00:00
|
|
|
{
|
|
|
|
Selections.instance.DragSelect(e);
|
|
|
|
selected++;
|
2022-01-16 19:23:46 +00:00
|
|
|
}
|
2022-01-13 03:59:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
selecting = selected > 0;
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:23:46 +00:00
|
|
|
public Vector3 MousePosition()
|
2022-01-13 03:59:54 +00:00
|
|
|
{
|
2022-01-16 19:23:46 +00:00
|
|
|
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
|
|
|
return new Vector3(mousePos.x, mousePos.y, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Rect GetWorldRect(RectTransform rectTransform)
|
|
|
|
{
|
|
|
|
Vector3[] corners = new Vector3[4];
|
|
|
|
rectTransform.GetWorldCorners(corners);
|
|
|
|
// Get the bottom left corner.
|
|
|
|
Vector3 position = corners[0];
|
|
|
|
|
|
|
|
Vector2 size = new Vector2(
|
|
|
|
rectTransform.lossyScale.x * rectTransform.rect.size.x,
|
|
|
|
rectTransform.lossyScale.y * rectTransform.rect.size.y);
|
|
|
|
|
|
|
|
return new Rect(position, size);
|
2022-01-13 03:59:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|