mirror of
https://github.com/RHeavenStudio/HeavenStudio.git
synced 2025-06-12 11:27:39 +02:00
bare bones selection system
This commit is contained in:
144
Assets/Scripts/LevelEditor/BoxSelection.cs
Normal file
144
Assets/Scripts/LevelEditor/BoxSelection.cs
Normal file
@ -0,0 +1,144 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Selections.instance.eventsSelected.Count > 0 && Timeline.instance.IsEventsDragging())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// click
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
startPosition = Input.mousePosition;
|
||||
selectionBox = new Rect();
|
||||
}
|
||||
|
||||
// dragging
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
endPosition = Input.mousePosition;
|
||||
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
|
||||
if (Input.mousePosition.x < startPosition.x)
|
||||
{
|
||||
// dragging left
|
||||
selectionBox.xMin = Input.mousePosition.x;
|
||||
selectionBox.xMax = startPosition.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
// dragging right
|
||||
selectionBox.xMin = startPosition.x;
|
||||
selectionBox.xMax = Input.mousePosition.x;
|
||||
}
|
||||
|
||||
// Y
|
||||
if (Input.mousePosition.y < startPosition.y)
|
||||
{
|
||||
// dragging down
|
||||
selectionBox.yMin = Input.mousePosition.y;
|
||||
selectionBox.yMax = startPosition.y;
|
||||
}
|
||||
else
|
||||
{
|
||||
// dragging up
|
||||
selectionBox.yMin = startPosition.y;
|
||||
selectionBox.yMax = Input.mousePosition.y;
|
||||
}
|
||||
}
|
||||
|
||||
private void SelectEvents()
|
||||
{
|
||||
int selected = 0;
|
||||
|
||||
for (int i = 0; i < GameManager.instance.Beatmap.entities.Count; i++)
|
||||
{
|
||||
TimelineEventObj e = GameManager.instance.Beatmap.entities[i].eventObj;
|
||||
if (selectionBox.Contains(Camera.main.WorldToScreenPoint(e.transform.position)))
|
||||
{
|
||||
print(RectTransformToScreenSpace(e.GetComponent<RectTransform>()));
|
||||
Selections.instance.DragSelect(e);
|
||||
selected++;
|
||||
}
|
||||
|
||||
// I'm trying this fix this dammit
|
||||
/*if (selectionBox.Overlaps(RectTransformToScreenSpace(e.GetComponent<RectTransform>())))
|
||||
{
|
||||
print(RectTransformToScreenSpace(e.GetComponent<RectTransform>()));
|
||||
print(selectionBox);
|
||||
Selections.instance.DragSelect(e);
|
||||
selected++;
|
||||
}*/
|
||||
}
|
||||
|
||||
selecting = selected > 0;
|
||||
}
|
||||
|
||||
public Rect RectTransformToScreenSpace(RectTransform transform)
|
||||
{
|
||||
Vector2 sizeTemp = Vector2.Scale(transform.rect.size, transform.localScale);
|
||||
Vector2 size = new Vector2(sizeTemp.x * 100, sizeTemp.y);
|
||||
return new Rect((Vector2)Camera.main.WorldToScreenPoint(transform.position) - (size * 0.5f), size);
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/LevelEditor/BoxSelection.cs.meta
Normal file
11
Assets/Scripts/LevelEditor/BoxSelection.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4cad4c81175643243b3e0c481b8acfb6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
67
Assets/Scripts/LevelEditor/Selections.cs
Normal file
67
Assets/Scripts/LevelEditor/Selections.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RhythmHeavenMania.Editor
|
||||
{
|
||||
public class Selections : MonoBehaviour
|
||||
{
|
||||
public List<TimelineEventObj> eventsSelected = new List<TimelineEventObj>();
|
||||
|
||||
public static Selections instance { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
|
||||
public void ClickSelect(TimelineEventObj eventToAdd)
|
||||
{
|
||||
DeselectAll();
|
||||
eventsSelected.Add(eventToAdd);
|
||||
eventToAdd.Select();
|
||||
}
|
||||
|
||||
public void ShiftClickSelect(TimelineEventObj eventToAdd)
|
||||
{
|
||||
if (!eventsSelected.Contains(eventToAdd))
|
||||
{
|
||||
eventsSelected.Add(eventToAdd);
|
||||
eventToAdd.Select();
|
||||
}
|
||||
/*else
|
||||
{
|
||||
eventsSelected.Remove(eventToAdd);
|
||||
eventToAdd.DeSelect();
|
||||
}*/
|
||||
}
|
||||
|
||||
public void DragSelect(TimelineEventObj eventToAdd)
|
||||
{
|
||||
if (!eventsSelected.Contains(eventToAdd))
|
||||
{
|
||||
eventsSelected.Add(eventToAdd);
|
||||
eventToAdd.Select();
|
||||
}
|
||||
}
|
||||
|
||||
public void DeselectAll()
|
||||
{
|
||||
for (int i = 0; i < eventsSelected.Count; i++)
|
||||
{
|
||||
eventsSelected[i].DeSelect();
|
||||
}
|
||||
|
||||
eventsSelected.Clear();
|
||||
}
|
||||
|
||||
public void Deselect(TimelineEventObj eventToDeselect)
|
||||
{
|
||||
if (eventsSelected.Contains(eventToDeselect))
|
||||
{
|
||||
eventsSelected.Remove(eventToDeselect);
|
||||
eventToDeselect.DeSelect();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/LevelEditor/Selections.cs.meta
Normal file
11
Assets/Scripts/LevelEditor/Selections.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e2b2a61b5fc830d4e906f0f059365bac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
52
Assets/Scripts/LevelEditor/Selector.cs
Normal file
52
Assets/Scripts/LevelEditor/Selector.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RhythmHeavenMania.Editor
|
||||
{
|
||||
public class Selector : MonoBehaviour
|
||||
{
|
||||
private bool clicked;
|
||||
|
||||
public static Selector instance { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
/*if (Input.GetMouseButtonUp(0))
|
||||
{
|
||||
if (!Timeline.instance.IsDraggingEvent())
|
||||
{
|
||||
if (clicked == false)
|
||||
{
|
||||
if (!Input.GetKey(KeyCode.LeftShift))
|
||||
{
|
||||
print('a');
|
||||
Selections.instance.DeselectAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clicked = false;*/
|
||||
}
|
||||
|
||||
public void Click(TimelineEventObj eventObj)
|
||||
{
|
||||
/*if (Input.GetKey(KeyCode.LeftShift))
|
||||
{
|
||||
Selections.instance.ShiftClickSelect(eventObj);
|
||||
}
|
||||
else
|
||||
{
|
||||
Selections.instance.ClickSelect(eventObj);
|
||||
}
|
||||
|
||||
clicked = true;*/
|
||||
}
|
||||
}
|
||||
}
|
11
Assets/Scripts/LevelEditor/Selector.cs.meta
Normal file
11
Assets/Scripts/LevelEditor/Selector.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b47ca28d69cd18e429a3548426c65351
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -17,6 +17,8 @@ namespace RhythmHeavenMania.Editor
|
||||
[Header("Timeline Properties")]
|
||||
private float lastBeatPos = 0;
|
||||
private Vector2 lastMousePos;
|
||||
public List<TimelineEventObj> eventObjs = new List<TimelineEventObj>();
|
||||
private bool lastFrameDrag;
|
||||
|
||||
[Header("Timeline Components")]
|
||||
[SerializeField] private RectTransform TimelineSlider;
|
||||
@ -36,7 +38,7 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
for (int i = 0; i < GameManager.instance.Beatmap.entities.Count; i++)
|
||||
{
|
||||
var entity = GameManager.instance.Beatmap.entities[i];
|
||||
/*var entity = GameManager.instance.Beatmap.entities[i];
|
||||
var e = GameManager.instance.Beatmap.entities[i];
|
||||
|
||||
EventCaller.GameAction gameAction = EventCaller.instance.GetGameAction(EventCaller.instance.GetMinigame(e.datamodel.Split(0)), e.datamodel.Split(1));
|
||||
@ -57,7 +59,12 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
g.SetActive(true);
|
||||
entity.eventObj = g.GetComponent<TimelineEventObj>();
|
||||
entity.track = (int)(g.transform.localPosition.y / 51.34f * -1);
|
||||
entity.track = (int)(g.transform.localPosition.y / 51.34f * -1);*/
|
||||
|
||||
var entity = GameManager.instance.Beatmap.entities[i];
|
||||
var e = GameManager.instance.Beatmap.entities[i];
|
||||
|
||||
AddEventObject(e.datamodel, false, new Vector3(e.beat, Mathp.Round2Nearest(Random.Range(0, -205.36f), 51.34f)), i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -190,10 +197,10 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
#region Functions
|
||||
|
||||
public void AddEventObject(string eventName, bool dragNDrop = false)
|
||||
public void AddEventObject(string eventName, bool dragNDrop = false, Vector3 pos = new Vector3(), int entityId = 0)
|
||||
{
|
||||
GameObject g = Instantiate(TimelineEventObjRef.gameObject, TimelineEventObjRef.parent);
|
||||
g.transform.localPosition = new Vector3(0, 0);
|
||||
g.transform.localPosition = pos;
|
||||
g.transform.GetChild(1).GetComponent<TMP_Text>().text = eventName.Split('/')[1];
|
||||
|
||||
TimelineEventObj eventObj = g.GetComponent<TimelineEventObj>();
|
||||
@ -210,24 +217,27 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
g.SetActive(true);
|
||||
|
||||
Beatmap.Entity entity = new Beatmap.Entity();
|
||||
entity.datamodel = eventName;
|
||||
entity.eventObj = eventObj;
|
||||
var entity = GameManager.instance.Beatmap.entities[entityId];
|
||||
var e = GameManager.instance.Beatmap.entities[entityId];
|
||||
|
||||
GameManager.instance.Beatmap.entities.Add(entity);
|
||||
GameManager.instance.SortEventsList();
|
||||
|
||||
g.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
entity.eventObj = g.GetComponent<TimelineEventObj>();
|
||||
entity.track = (int)(g.transform.localPosition.y / 51.34f * -1);
|
||||
|
||||
if (dragNDrop)
|
||||
{
|
||||
g.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
eventObj.OnDown();
|
||||
|
||||
Beatmap.Entity en = new Beatmap.Entity();
|
||||
en.datamodel = eventName;
|
||||
en.eventObj = eventObj;
|
||||
|
||||
GameManager.instance.Beatmap.entities.Add(en);
|
||||
GameManager.instance.SortEventsList();
|
||||
}
|
||||
|
||||
Editor.EventObjs.Add(eventObj);
|
||||
|
||||
// entity.eventObj = g.GetComponent<TimelineEventObj>();
|
||||
// entity.track = (int)(g.transform.localPosition.y / 51.34f * -1);
|
||||
eventObjs.Add(eventObj);
|
||||
}
|
||||
|
||||
public void DestroyEventObject(TimelineEventObj eventObj)
|
||||
@ -240,6 +250,16 @@ namespace RhythmHeavenMania.Editor
|
||||
Editor.EventObjs.Remove(eventObj);
|
||||
}
|
||||
|
||||
public bool IsMouseAboveEvents()
|
||||
{
|
||||
return Timeline.instance.eventObjs.FindAll(c => c.mouseHovering == true).Count > 0;
|
||||
}
|
||||
|
||||
public bool IsEventsDragging()
|
||||
{
|
||||
return Timeline.instance.eventObjs.FindAll(c => c.isDragging == true).Count > 0;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Commands
|
||||
|
@ -26,6 +26,8 @@ namespace RhythmHeavenMania.Editor
|
||||
public float length;
|
||||
private bool eligibleToMove = false;
|
||||
private bool lastVisible;
|
||||
public bool selected;
|
||||
public bool mouseHovering;
|
||||
|
||||
[Header("Colors")]
|
||||
public Color NormalCol;
|
||||
@ -34,7 +36,9 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Optimizations
|
||||
mouseHovering = RectTransformUtility.RectangleContainsScreenPoint(GetComponent<RectTransform>(), Input.mousePosition, Camera.main);
|
||||
|
||||
#region Optimizations
|
||||
|
||||
bool visible = GetComponent<RectTransform>().IsVisibleFrom(Camera.main);
|
||||
|
||||
@ -48,7 +52,16 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
lastVisible = visible;
|
||||
|
||||
// -------------
|
||||
#endregion
|
||||
|
||||
if (selected)
|
||||
{
|
||||
SetColor(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetColor(0);
|
||||
}
|
||||
|
||||
if (Conductor.instance.NotStopped())
|
||||
{
|
||||
@ -58,7 +71,33 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
enemyIndex = GameManager.instance.Beatmap.entities.FindIndex(a => a.eventObj == this);
|
||||
|
||||
if (isDragging == true)
|
||||
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)
|
||||
{
|
||||
Vector3 mousePos;
|
||||
mousePos = Input.mousePosition;
|
||||
@ -71,31 +110,21 @@ namespace RhythmHeavenMania.Editor
|
||||
OnMove();
|
||||
|
||||
lastPos = this.transform.localPosition;
|
||||
|
||||
SetColor(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetColor(0);
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonUp(0))
|
||||
OnUp();
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.Delete))
|
||||
Timeline.instance.DestroyEventObject(this);
|
||||
|
||||
}
|
||||
|
||||
private void OnMove()
|
||||
{
|
||||
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)
|
||||
{
|
||||
// PosPreview.GetComponent<Image>().color = Color.red;
|
||||
eligibleToMove = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// PosPreview.GetComponent<Image>().color = Color.yellow;
|
||||
eligibleToMove = true;
|
||||
}
|
||||
}
|
||||
@ -106,42 +135,65 @@ namespace RhythmHeavenMania.Editor
|
||||
entity.beat = this.transform.localPosition.x;
|
||||
GameManager.instance.SortEventsList();
|
||||
entity.track = (int)(this.transform.localPosition.y / 51.34f) * -1;
|
||||
|
||||
// this.transform.localPosition = this.transform.localPosition;
|
||||
// transform.DOLocalMove(PosPreview.transform.localPosition, 0.15f).SetEase(Ease.OutExpo);
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
{
|
||||
if (PosPreview) Destroy(PosPreview.gameObject);
|
||||
eligibleToMove = false;
|
||||
}
|
||||
#region ClickEvents
|
||||
|
||||
public void OnDown()
|
||||
{
|
||||
Vector3 mousePos;
|
||||
if (!selected)
|
||||
{
|
||||
if (Input.GetKey(KeyCode.LeftShift))
|
||||
{
|
||||
Selections.instance.ShiftClickSelect(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
Selections.instance.ClickSelect(this);
|
||||
}
|
||||
|
||||
/*PosPreview = Instantiate(PosPreviewRef, PosPreviewRef.transform.parent);
|
||||
PosPreview.sizeDelta = new Vector2(100 * transform.GetComponent<RectTransform>().sizeDelta.x, transform.GetComponent<RectTransform>().sizeDelta.y);
|
||||
PosPreview.transform.localPosition = this.transform.localPosition;
|
||||
PosPreview.GetComponent<Image>().enabled = true;
|
||||
PosPreview.GetComponent<Image>().color = Color.yellow;*/
|
||||
|
||||
mousePos = Input.mousePosition;
|
||||
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
|
||||
startPosX = mousePos.x - this.transform.position.x;
|
||||
startPosY = mousePos.y - this.transform.position.y;
|
||||
isDragging = true;
|
||||
// Selector.instance.Click(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnUp()
|
||||
{
|
||||
isDragging = false;
|
||||
if (selected)
|
||||
{
|
||||
isDragging = false;
|
||||
|
||||
if (eligibleToMove) OnComplete();
|
||||
Cancel();
|
||||
if (eligibleToMove)
|
||||
{
|
||||
OnComplete();
|
||||
}
|
||||
|
||||
Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
{
|
||||
eligibleToMove = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Selection
|
||||
|
||||
public void Select()
|
||||
{
|
||||
selected = true;
|
||||
}
|
||||
|
||||
public void DeSelect()
|
||||
{
|
||||
selected = false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Extra
|
||||
|
||||
public void SetColor(int type)
|
||||
{
|
||||
Color c = Color.white;
|
||||
@ -160,5 +212,13 @@ namespace RhythmHeavenMania.Editor
|
||||
|
||||
transform.GetChild(0).GetComponent<Image>().color = c;
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user