Skip to content

Commit

Permalink
Updated DuplicateKeyFrame and SplitKeyFrame
Browse files Browse the repository at this point in the history
Updated DuplicateKeyFrame and SplitKeyFrame to account for grouped strokes. Previously it did not recognize grouped strokes, so when it was replicated to the new canvas, there was no key to the strokes, preventing users from selecting the stroke. When un-registered grouped strokes was selected, it will select the original grouped strokes in the previous canvas.

Refactored blocks of code in DuplicateKeyFrame and SplitKeyFrame onto ReplicateStrokes.
  • Loading branch information
JayMayo authored and andybak committed Jan 14, 2025
1 parent e0fe04a commit 66c1cd4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 54 deletions.
102 changes: 50 additions & 52 deletions Assets/Scripts/Animation/AnimationUI_Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -952,54 +952,72 @@ public void ExtendKeyFrame(int trackNum)
return index;
}

public (int, int) splitKeyFrame(int trackNum = -1, int frameNum = -1)
private void ReplicateStrokes(List<Stroke> newStrokes, CanvasScript newCanvas)
{
(int, int) index = (trackNum == -1 || frameNum == -1) ? GetCanvasLocation(App.Scene.ActiveCanvas) : (trackNum, frameNum);

CanvasScript newCanvas = App.Scene.AddCanvas();
CanvasScript oldCanvas = App.Scene.ActiveCanvas;

int frameLegnth = GetFrameLength(index.Item1, index.Item2);


int splittingIndex = FrameOn;
if (splittingIndex < index.Item2 || splittingIndex > index.Item2 + frameLegnth - 1) return (-1, -1);

List<Stroke> oldStrokes = SketchMemoryScript.m_Instance.GetMemoryList
.Where(x => x.Canvas == oldCanvas).ToList();

List<Stroke> newStrokes = oldStrokes.Select(stroke =>
SketchMemoryScript.m_Instance.DuplicateStroke(stroke, App.Scene.SelectionCanvas, null))
.ToList();
Dictionary<int, List<Stroke>> strokeGroups = new Dictionary<int, List<Stroke>>();

foreach (var stroke in newStrokes)
{
if (stroke.Group != SketchGroupTag.None)
{
if (strokeGroups.TryGetValue(stroke.Group.GetHashCode(), out List<Stroke> group))
{
group.Add(stroke);
}
else
{
strokeGroups[stroke.Group.GetHashCode()] = new List<Stroke> { stroke };
}
}

switch (stroke.m_Type)
{
case Stroke.Type.BrushStroke:
BaseBrushScript brushScript = stroke.m_Object.GetComponent<BaseBrushScript>();
if (brushScript)
{
BaseBrushScript brushScript = stroke.m_Object.GetComponent<BaseBrushScript>();
if (brushScript)
{
brushScript.HideBrush(false);
}
brushScript.HideBrush(false);
}
break;
case Stroke.Type.BatchedBrushStroke:
{
stroke.m_BatchSubset.m_ParentBatch.EnableSubset(stroke.m_BatchSubset);
}
stroke.m_BatchSubset.m_ParentBatch.EnableSubset(stroke.m_BatchSubset);
break;
default:
Debug.LogError("Unexpected: redo NotCreated duplicate stroke");
break;
}
TiltMeterScript.m_Instance.AdjustMeter(stroke, up: true);

stroke.SetParentKeepWorldPosition(newCanvas);
}

for (int f = splittingIndex; f < index.Item2 + frameLegnth; f++)
foreach (var sg in strokeGroups)
{
GroupManager.MoveStrokesToNewGroups(sg.Value,null);
}
}

public (int, int) SplitKeyFrame(int trackNum = -1, int frameNum = -1)
{
(int, int) index = (trackNum == -1 || frameNum == -1) ? GetCanvasLocation(App.Scene.ActiveCanvas) : (trackNum, frameNum);

CanvasScript newCanvas = App.Scene.AddCanvas();
CanvasScript oldCanvas = App.Scene.ActiveCanvas;

int frameLength = GetFrameLength(index.Item1, index.Item2);

int splittingIndex = FrameOn;
if (splittingIndex < index.Item2 || splittingIndex > index.Item2 + frameLength - 1) return (-1, -1);

List<Stroke> oldStrokes = SketchMemoryScript.m_Instance.GetMemoryList
.Where(x => x.Canvas == oldCanvas).ToList();

List<Stroke> newStrokes = oldStrokes.Select(stroke =>
SketchMemoryScript.m_Instance.DuplicateStroke(stroke, App.Scene.SelectionCanvas, null))
.ToList();

ReplicateStrokes(newStrokes,newCanvas);

for (int f = splittingIndex; f < index.Item2 + frameLength; f++)
{
Frame addingFrame = NewFrame(newCanvas);
Timeline[index.Item1].Frames[f] = addingFrame;
Expand All @@ -1010,13 +1028,13 @@ public void ExtendKeyFrame(int trackNum)
return (index.Item1, splittingIndex);
}

public (int, int) duplicateKeyFrame(int trackNum = -1, int frameNum = -1)
public (int, int) DuplicateKeyFrame(int trackNum = -1, int frameNum = -1)
{
(int, int) index = (trackNum == -1 || frameNum == -1) ? GetCanvasLocation(App.Scene.ActiveCanvas) : (trackNum, frameNum);
CanvasScript newCanvas = App.Scene.AddCanvas();
CanvasScript oldCanvas = App.Scene.ActiveCanvas;

int frameLegnth = GetFrameLength(index.Item1, index.Item2);
int frameLength = GetFrameLength(index.Item1, index.Item2);
(int, int) nextIndex = GetFollowingFrameIndex(index.Item1, index.Item2);
List<Stroke> oldStrokes = SketchMemoryScript.m_Instance.GetMemoryList
.Where(x => x.Canvas == oldCanvas).ToList();
Expand All @@ -1025,29 +1043,9 @@ public void ExtendKeyFrame(int trackNum)
.Select(stroke => SketchMemoryScript.m_Instance.DuplicateStroke(
stroke, App.Scene.SelectionCanvas, null)).ToList();

foreach (var stroke in newStrokes)
{
switch (stroke.m_Type)
{
case Stroke.Type.BrushStroke:
BaseBrushScript brushScript = stroke.m_Object.GetComponent<BaseBrushScript>();
if (brushScript)
{
brushScript.HideBrush(false);
}
break;
case Stroke.Type.BatchedBrushStroke:
stroke.m_BatchSubset.m_ParentBatch.EnableSubset(stroke.m_BatchSubset);
break;
default:
Debug.LogError("Unexpected: redo NotCreated duplicate stroke");
break;
}
TiltMeterScript.m_Instance.AdjustMeter(stroke, up: true);
stroke.SetParentKeepWorldPosition(newCanvas);
}
ReplicateStrokes(newStrokes,newCanvas);

for (int f = 0; f < frameLegnth; f++)
for (int f = 0; f < frameLength; f++)
{
if (nextIndex.Item2 + f < Timeline[nextIndex.Item1].Frames.Count &&
!GetFrameFilled(nextIndex.Item1, nextIndex.Item2))
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Commands/DuplicateFrameCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public DuplicateFrameCommand()

protected override void OnRedo()
{
m_DuplicatingIndex = m_Manager.duplicateKeyFrame(m_TimelineLocation.Item1, m_TimelineLocation.Item2);
m_DuplicatingIndex = m_Manager.DuplicateKeyFrame(m_TimelineLocation.Item1, m_TimelineLocation.Item2);
}

protected override void OnUndo()
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Commands/SplitFrameCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SplitFrameCommand()

protected override void OnRedo()
{
m_SplittingIndex = m_Manager.splitKeyFrame(m_TimelineLocation.Item1, m_TimelineLocation.Item2);
m_SplittingIndex = m_Manager.SplitKeyFrame(m_TimelineLocation.Item1, m_TimelineLocation.Item2);
}

protected override void OnUndo()
Expand Down

0 comments on commit 66c1cd4

Please sign in to comment.