Add duplicate

This commit is contained in:
Braedon Lewis 2023-09-27 02:04:01 -04:00
parent 6a6cf72574
commit 065a09d48f
5 changed files with 867 additions and 85 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -13,7 +13,7 @@ namespace HeavenStudio.Editor
/// <summary>
/// Are we currently drag selecting?
/// </summary>
public bool ActivelySelecting = false;
public bool ActivelySelecting { get; private set; } = false;
private Vector2 startPosition = Vector2.zero;
private Vector2 endPosition = Vector2.zero;
@ -70,6 +70,8 @@ namespace HeavenStudio.Editor
startPos = new Vector2(startPos.x, Mathf.Clamp(startPos.y, 0, Timeline.instance.LayerCount));
endPos = new Vector2(endPos.x, Mathf.Clamp(endPos.y, 0, Timeline.instance.LayerCount));
ActivelySelecting = true;
}
var start = new Vector2(Mathf.Min(startPos.x, endPos.x),
@ -81,6 +83,8 @@ namespace HeavenStudio.Editor
{
validClick = false;
boxGroup.DOFade(0.0f, 0.3f).SetEase(Ease.OutExpo);
ActivelySelecting = false;
return;
}

View file

@ -7,6 +7,7 @@ using Jukebox;
using HeavenStudio.Editor.Track;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine.Timeline;
namespace HeavenStudio.Editor.Commands
{
@ -128,6 +129,84 @@ namespace HeavenStudio.Editor.Commands
}
}
public class Duplicate : ICommand
{
public List<RiqEntity> dupEntityData = new();
private readonly List<System.Guid> placedEntityIDs = new();
public Duplicate(List<TimelineEventObj> original)
{
var entities = original.Select(c => c.entity).ToList();
foreach (var entity in entities)
{
dupEntityData.Add(entity.DeepCopy());
}
for (var i = 0; i < original.Count; i++)
{
placedEntityIDs.Add(Guid.NewGuid());
}
}
public void Execute()
{
var entities = new List<RiqEntity>();
foreach (var entity in dupEntityData)
{
entities.Add(entity.DeepCopy());
}
Selections.instance.DeselectAll();
for (var i = 0; i < entities.Count; i++)
{
var entity = entities[i];
entity.guid = placedEntityIDs[i];
GameManager.instance.Beatmap.Entities.Add(entity);
var marker = TimelineBlockManager.Instance.CreateEntity(entity);
Selections.instance.DragSelect(marker);
if (i == entities.Count - 1)
marker.BeginMoving(false);
}
GameManager.instance.SortEventsList();
}
public void Undo()
{
var deletedEntities = new List<RiqEntity>();
for (var i = 0; i < placedEntityIDs.Count; i++)
{
var placedEntityID = placedEntityIDs[i];
var createdEntity = GameManager.instance.Beatmap.Entities.Find(c => c.guid == placedEntityID);
if (createdEntity != null)
{
deletedEntities.Add(createdEntity);
if (TimelineBlockManager.Instance.EntityMarkers.ContainsKey(placedEntityID))
{
var marker = TimelineBlockManager.Instance.EntityMarkers[placedEntityID];
Selections.instance.Deselect(marker);
TimelineBlockManager.Instance.EntityMarkers.Remove(placedEntityID);
GameObject.Destroy(marker.gameObject);
}
GameManager.instance.Beatmap.Entities.Remove(createdEntity);
}
}
GameManager.instance.SortEventsList();
dupEntityData.Clear();
foreach (var entity in deletedEntities)
{
dupEntityData.Add(entity.DeepCopy());
}
}
}
public class Move : ICommand
{
private readonly List<Guid> entityIDs = new();
@ -178,36 +257,57 @@ namespace HeavenStudio.Editor.Commands
entity.beat = lastMove.beat[i];
entity["track"] = lastMove.layer[i];
Debug.Log(lastMove.beat[i]);
}
}
}
public class Duplicate : ICommand
public class Resize : ICommand
{
public string dupEntityJson;
private readonly List<System.Guid> placedEntityIDs = new();
public Guid entityId;
private EntityResize newResize;
private EntityResize lastResize;
public Duplicate(List<RiqEntity> original)
public struct EntityResize
{
dupEntityJson = JsonConvert.SerializeObject(original, Formatting.None, new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.None,
NullValueHandling = NullValueHandling.Include,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
});
public double beat;
public float length;
public EntityResize(double beat, float length)
{
this.beat = beat;
this.length = length;
}
}
public Resize(Guid entityId, double newBeat, float newLength)
{
this.entityId = entityId;
newResize = new EntityResize(newBeat, newLength);
}
public void Execute()
{
var entity = GameManager.instance.Beatmap.Entities.Find(c => c.guid == entityId);
lastResize = new EntityResize(entity.beat, entity.length);
entity.beat = newResize.beat;
entity.length = newResize.length;
if (TimelineBlockManager.Instance.EntityMarkers.ContainsKey(entityId))
TimelineBlockManager.Instance.EntityMarkers[entityId].SetWidthHeight();
}
public void Undo()
{
var entity = GameManager.instance.Beatmap.Entities.Find(c => c.guid == entityId);
entity.beat = lastResize.beat;
entity.length = lastResize.length;
if (TimelineBlockManager.Instance.EntityMarkers.ContainsKey(entityId))
TimelineBlockManager.Instance.EntityMarkers[entityId].SetWidthHeight();
}
}
}

View file

@ -7,6 +7,7 @@ using TMPro;
using UnityEngine.Timeline;
using System.Linq;
using System.Collections.Generic;
using HeavenStudio.Editor.Commands;
namespace HeavenStudio.Editor.Track
{
@ -53,9 +54,13 @@ namespace HeavenStudio.Editor.Track
private bool inResizeRegion;
private float resizingLeftBL;
private double lastResizeBeat = 0;
private float lastResizeLength = 0;
public bool isCreating;
private bool altWhenClicked = false;
private bool dragging = false;
private float initMoveX = 0.0f;
private float initMoveY = 0.0f;
@ -175,6 +180,7 @@ namespace HeavenStudio.Editor.Track
if (moving) moving = false;
entity.length = Mathf.Max(Timeline.instance.MousePos2BeatSnap - (float)entity.beat, Timeline.instance.snapInterval);
SetWidthHeight();
}
else if (resizingLeft)
@ -183,6 +189,7 @@ namespace HeavenStudio.Editor.Track
entity.beat = Mathf.Min(Timeline.instance.MousePos2BeatSnap, resizingLeftBL - Timeline.instance.snapInterval);
entity.length = Mathf.Max(resizingLeftBL - (float)entity.beat, Timeline.instance.snapInterval);
SetWidthHeight();
}
else
@ -215,6 +222,9 @@ namespace HeavenStudio.Editor.Track
GameManager.instance.SortEventsList();
TimelineBlockManager.Instance.SortMarkers();
}
altWhenClicked = false;
dragging = false;
}
if (moving)
@ -235,21 +245,11 @@ namespace HeavenStudio.Editor.Track
OnRightUp();
}
if (!BoxSelection.instance.ActivelySelecting)
if (resizing && selected || inResizeRegion && selected)
{
if (resizable)
Cursor.SetCursor(Timeline.instance.resizeCursor, new Vector2(14, 14), CursorMode.Auto);
}
// should consider adding this someday
// else if (moving && selected || mouseHovering && selected)
// {
// Cursor.SetCursor(Resources.Load<Texture2D>("Cursors/move"), new Vector2(8, 8), CursorMode.Auto);
// }
else
{
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
rectTransform.anchoredPosition = new Vector2((float)entity.beat * Timeline.instance.PixelsPerBeat, -(int)entity["track"] * Timeline.instance.LayerHeight());
@ -331,12 +331,16 @@ namespace HeavenStudio.Editor.Track
return;
}
if (dragging) return;
var entities = Selections.instance.eventsSelected;
if (entities.Count == 0)
{
entities = new() { this };
}
CommandManager.Instance.AddCommand(new Commands.Duplicate(entities));
dragging = true;
}
public void OnDown()
@ -403,46 +407,84 @@ namespace HeavenStudio.Editor.Track
public void ResizeEnter()
{
if (BoxSelection.instance.ActivelySelecting || !resizable || moving) return;
inResizeRegion = true;
Cursor.SetCursor(Timeline.instance.resizeCursor, new Vector2(14, 14), CursorMode.Auto);
}
public void ResizeExit()
{
inResizeRegion = false;
if (!resizing)
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
}
public void OnLeftDown()
{
if (BoxSelection.instance.ActivelySelecting) return;
if (resizable && selected)
{
ResetResize();
resizingLeft = true;
resizingLeftBL = (float)entity.beat + entity.length;
lastResizeBeat = entity.beat;
lastResizeLength = entity.length;
}
}
public void OnLeftUp()
{
if (resizable && selected)
if (resizable && resizingLeft)
{
ResetResize();
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
var b = entity.beat;
var l = entity.length;
entity.beat = lastResizeBeat;
entity.length = lastResizeLength;
CommandManager.Instance.AddCommand(new Commands.Resize(entity.guid, b, l));
}
}
public void OnRightDown()
{
if (BoxSelection.instance.ActivelySelecting) return;
if (resizable && selected)
{
ResetResize();
resizingRight = true;
lastResizeBeat = entity.beat;
lastResizeLength = entity.length;
}
}
public void OnRightUp()
{
if (resizable && selected)
if (resizable && resizingRight)
{
ResetResize();
Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto);
var b = entity.beat;
var l = entity.length;
entity.beat = lastResizeBeat;
entity.length = lastResizeLength;
CommandManager.Instance.AddCommand(new Commands.Resize(entity.guid, b, l));
}
}
@ -452,17 +494,6 @@ namespace HeavenStudio.Editor.Track
resizingRight = false;
}
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 Extra