From 4df82e709a4bb46e7f51ebe9c228836cd4d16c6d Mon Sep 17 00:00:00 2001 From: minenice55 Date: Fri, 8 Jul 2022 12:50:23 -0400 Subject: [PATCH] duplicating entities can now be undone / redone --- Assets/Scenes/Editor.unity | 16 ++--- Assets/Scripts/LevelEditor/Commands/Block.cs | 34 +++++++++++ Assets/Scripts/LevelEditor/Editor.cs | 2 +- .../Scripts/LevelEditor/Timeline/Timeline.cs | 12 +++- .../LevelEditor/Timeline/TimelineEventObj.cs | 60 ++++++++++++------- 5 files changed, 91 insertions(+), 33 deletions(-) diff --git a/Assets/Scenes/Editor.unity b/Assets/Scenes/Editor.unity index 3c5d4710..1aa35332 100644 --- a/Assets/Scenes/Editor.unity +++ b/Assets/Scenes/Editor.unity @@ -2342,7 +2342,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 1, y: 0} - m_AnchoredPosition: {x: -289.15002, y: 2} + m_AnchoredPosition: {x: -289.15, y: 2} m_SizeDelta: {x: -578.29, y: 49.92} m_Pivot: {x: 0.5, y: 0} --- !u!114 &156962255 @@ -8869,7 +8869,7 @@ MonoBehaviour: m_HandleRect: {fileID: 704039020} m_Direction: 0 m_Value: 0 - m_Size: 0.032871284 + m_Size: 0.03287128 m_NumberOfSteps: 0 m_OnValueChanged: m_PersistentCalls: @@ -12488,8 +12488,8 @@ MonoBehaviour: m_TargetGraphic: {fileID: 1589389272} m_HandleRect: {fileID: 1589389271} m_Direction: 2 - m_Value: 1 - m_Size: 0.5293149 + m_Value: 1.0000002 + m_Size: 0.52891773 m_NumberOfSteps: 0 m_OnValueChanged: m_PersistentCalls: @@ -15074,7 +15074,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0.5} m_AnchorMax: {x: 1, y: 1} - m_AnchoredPosition: {x: -408.97992, y: -17} + m_AnchoredPosition: {x: -408.97998, y: -17} m_SizeDelta: {x: -817.96, y: -46.29} m_Pivot: {x: 0.5, y: 0.5} --- !u!1 &1121370303 @@ -15607,7 +15607,7 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0.5} m_AnchorMax: {x: 1, y: 0.5} - m_AnchoredPosition: {x: 0, y: 129.94681} + m_AnchoredPosition: {x: 0, y: 129.85045} m_SizeDelta: {x: 0, y: 491} m_Pivot: {x: 0.5, y: 1} --- !u!114 &1154875944 @@ -26349,8 +26349,8 @@ RectTransform: m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 0, y: -26.463135} - m_SizeDelta: {x: 1054, y: -115.3} + m_AnchoredPosition: {x: -0, y: -28.03} + m_SizeDelta: {x: 1054, y: -113.92} m_Pivot: {x: 0, y: 0.5} --- !u!114 &2066632681 MonoBehaviour: diff --git a/Assets/Scripts/LevelEditor/Commands/Block.cs b/Assets/Scripts/LevelEditor/Commands/Block.cs index 85ef0202..deb74105 100644 --- a/Assets/Scripts/LevelEditor/Commands/Block.cs +++ b/Assets/Scripts/LevelEditor/Commands/Block.cs @@ -171,4 +171,38 @@ namespace HeavenStudio.Editor.Commands } } } + + public class Duplicate : IAction + { + List eventObjs; + List copiedObjs; + + public Duplicate(List eventObjs) + { + this.eventObjs = eventObjs; + } + + public void Execute() + { + } + + public void Redo() + { + for (int i = 0; i < copiedObjs.Count; i++) + { + Beatmap.Entity e = copiedObjs[i].entity; + eventObjs[i] = Timeline.instance.AddEventObject(e.datamodel, false, new Vector3(e.beat, -e.track * Timeline.instance.LayerHeight()), e, true, e.eventObj.eventObjID); + } + } + + public void Undo() + { + copiedObjs = eventObjs; + for (int i = 0; i < eventObjs.Count; i++) + { + Selections.instance.Deselect(eventObjs[i]); + Timeline.instance.DestroyEventObject(eventObjs[i].entity); + } + } + } } \ No newline at end of file diff --git a/Assets/Scripts/LevelEditor/Editor.cs b/Assets/Scripts/LevelEditor/Editor.cs index b4d70b99..3f5a3280 100644 --- a/Assets/Scripts/LevelEditor/Editor.cs +++ b/Assets/Scripts/LevelEditor/Editor.cs @@ -199,7 +199,7 @@ namespace HeavenStudio.Editor for (int i = 0; i < selectedEvents.Count; i++) { - if (selectedEvents[i].isCreating == false) + if (!(selectedEvents[i].isCreating || selectedEvents[i].wasDuplicated)) { result.Add(selectedEvents[i]); } diff --git a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs index 35bd8120..5607bdef 100644 --- a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs +++ b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs @@ -611,14 +611,22 @@ namespace HeavenStudio.Editor.Track return eventObj; } - public TimelineEventObj CopyEventObject(Beatmap.Entity e) + private List duplicatedEventObjs = new List(); + public TimelineEventObj CopyEventObject(TimelineEventObj e) { - Beatmap.Entity clone = e.DeepCopy(); + Beatmap.Entity clone = e.entity.DeepCopy(); TimelineEventObj dup = AddEventObject(clone.datamodel, false, new Vector3(clone.beat, -clone.track * Timeline.instance.LayerHeight()), clone, true, RandomID()); + duplicatedEventObjs.Add(dup); return dup; } + public void FinalizeDuplicateEventStack() + { + CommandManager.instance.Execute(new Commands.Duplicate(duplicatedEventObjs)); + duplicatedEventObjs = new List(); + } + public void DestroyEventObject(Beatmap.Entity entity) { if (EventParameterManager.instance.entity == entity) diff --git a/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs b/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs index cbf557fd..c021c7f9 100644 --- a/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs +++ b/Assets/Scripts/LevelEditor/Timeline/TimelineEventObj.cs @@ -38,10 +38,10 @@ namespace HeavenStudio.Editor.Track public bool resizable; public bool resizing; public bool moving; + public bool wasDuplicated; private bool resizingLeft; private bool resizingRight; private bool inResizeRegion; - private bool wasDuplicated; public Vector2 lastMovePos; public bool isCreating; public string eventObjID; @@ -120,7 +120,10 @@ namespace HeavenStudio.Editor.Track selectedImage.gameObject.SetActive(true); for (int i = 0; i < outline.childCount; i++) { - outline.GetChild(i).GetComponent().color = Color.cyan; + if (moving) + outline.GetChild(i).GetComponent().color = Color.magenta; + else + outline.GetChild(i).GetComponent().color = Color.cyan; } } else @@ -153,29 +156,33 @@ namespace HeavenStudio.Editor.Track if (!resizing) { - if (Input.GetMouseButtonUp(0) && Timeline.instance.timelineState.selected) - { - if (Timeline.instance.eventObjs.FindAll(c => c.mouseHovering).Count == 0 && Timeline.instance.eventObjs.FindAll(c => c.moving).Count == 0 && !BoxSelection.instance.selecting && Timeline.instance.eventObjs.FindAll(c => c.resizing).Count == 0) - { - if (!Input.GetKey(KeyCode.LeftShift)) - { - // Selections.instance.Deselect(this); - } - } - - // OnUp(); - } - if (Timeline.instance.eventObjs.FindAll(c => c.moving).Count > 0 && selected) { - //duplicate the entity if holding alt or r-click - if ((!wasDuplicated) && (Input.GetKey(KeyCode.LeftAlt) || Input.GetMouseButton(1))) + Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition); + //duplicate the entity if holding alt or m-click + if ((!wasDuplicated) && (Input.GetKey(KeyCode.LeftAlt) || Input.GetMouseButton(2))) { - wasDuplicated = true; - var te = Timeline.instance.CopyEventObject(entity); + Selections.instance.Deselect(this); + this.wasDuplicated = false; + this.moving = false; + + var te = Timeline.instance.CopyEventObject(this); + + Selections.instance.DragSelect(te); + + te.wasDuplicated = true; + te.transform.localPosition = transform.localPosition; + te.lastPos_ = transform.localPosition; + + for (int i = 0; i < Timeline.instance.eventObjs.Count; i++) + { + Timeline.instance.eventObjs[i].startPosX = mousePos.x - Timeline.instance.eventObjs[i].transform.position.x; + Timeline.instance.eventObjs[i].startPosY = mousePos.y - Timeline.instance.eventObjs[i].transform.position.y; + } + + te.moving = true; } - Vector3 mousePos = Editor.instance.EditorCamera.ScreenToWorldPoint(Input.mousePosition); lastPos_ = transform.localPosition; @@ -231,7 +238,12 @@ namespace HeavenStudio.Editor.Track if (resizable) Cursor.SetCursor(Resources.Load("Cursors/horizontal_resize"), new Vector2(8, 8), CursorMode.Auto); } - else if (Timeline.instance.eventObjs.FindAll(c => c.inResizeRegion).Count == 0 && Timeline.instance.eventObjs.FindAll(c => c.resizing).Count == 0) + // should consider adding this someday + // else if (moving && selected || mouseHovering && selected) + // { + // Cursor.SetCursor(Resources.Load("Cursors/move"), new Vector2(8, 8), CursorMode.Auto); + // } + else { Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); } @@ -293,13 +305,17 @@ namespace HeavenStudio.Editor.Track if (selected && Timeline.instance.timelineState.selected) { + if (wasDuplicated) + { + Timeline.instance.FinalizeDuplicateEventStack(); + wasDuplicated = false; + } if (eligibleToMove) { OnComplete(true); } moving = false; - wasDuplicated = false; Cancel(); if (isCreating == true)