Skip to content

Commit

Permalink
Multiple duplication of keyframes
Browse files Browse the repository at this point in the history
  • Loading branch information
mchorse committed Aug 19, 2020
1 parent 20530f7 commit f874478
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,16 @@ public void removeSelectedKeyframes()
/* Mouse input handling */

@Override
protected void duplicateKeyframe(Keyframe frame, GuiContext context, int mouseX, int mouseY)
protected void duplicateKeyframe(GuiContext context, int mouseX, int mouseY)
{
long offset = (long) this.fromGraphX(mouseX);
GuiSheet current = this.getCurrentSheet();
int index = current.channel.insert(offset, frame.value);
Keyframe created = current.channel.get(index);

current.selected.clear();
current.selected.add(index);
created.copy(frame);
created.tick = offset;
for (GuiSheet sheet : this.sheets)
{
sheet.duplicate(offset);
}

this.setKeyframe(this.getCurrent());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,16 +277,10 @@ public void selectByDuration(long duration)
/* Mouse input handling */

@Override
protected void duplicateKeyframe(Keyframe frame, GuiContext context, int mouseX, int mouseY)
protected void duplicateKeyframe(GuiContext context, int mouseX, int mouseY)
{
long offset = (long) this.fromGraphX(mouseX);
KeyframeChannel channel = this.sheet.channel;
Keyframe created = channel.get(channel.insert(offset, frame.value));

this.sheet.selected.clear();
this.sheet.selected.add(channel.getKeyframes().indexOf(created));
created.copy(frame);
created.tick = offset;
this.sheet.duplicate((long) this.fromGraphX(mouseX));
this.setKeyframe(this.getCurrent());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,7 @@ public boolean mouseClicked(GuiContext context)
/* Duplicate the keyframe */
if (GuiScreen.isAltKeyDown() && this.which == Selection.KEYFRAME)
{
Keyframe frame = this.getCurrent();

if (frame != null)
{
this.duplicateKeyframe(frame, context, mouseX, mouseY);
}
this.duplicateKeyframe(context, mouseX, mouseY);

return false;
}
Expand Down Expand Up @@ -220,8 +215,7 @@ else if (context.mouseButton == 2)
return false;
}

protected void duplicateKeyframe(Keyframe frame, GuiContext context, int mouseX, int mouseY)
{}
protected abstract void duplicateKeyframe(GuiContext context, int mouseX, int mouseY);

protected abstract boolean pickKeyframe(GuiContext context, int mouseX, int mouseY, boolean multi);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class GuiSheet
Expand Down Expand Up @@ -148,4 +149,45 @@ public void removeSelectedKeyframes()

this.clearSelection();
}

public void duplicate(long tick)
{
List<Keyframe> selected = new ArrayList<Keyframe>();
List<Keyframe> created = new ArrayList<Keyframe>();

long minTick = Integer.MAX_VALUE;

for (int index : this.selected)
{
Keyframe keyframe = this.channel.get(index);

if (keyframe != null)
{
selected.add(keyframe);
minTick = Math.min(keyframe.tick, minTick);
}
}

selected.sort(Comparator.comparingLong(a -> a.tick));

long diff = tick - minTick;

for (Keyframe keyframe : selected)
{
long fin = keyframe.tick + diff;
int index = this.channel.insert(fin, keyframe.value);
Keyframe current = this.channel.get(index);

current.copy(keyframe);
current.tick = fin;
created.add(current);
}

this.clearSelection();

for (Keyframe keyframe : created)
{
this.selected.add(this.channel.getKeyframes().indexOf(keyframe));
}
}
}

0 comments on commit f874478

Please sign in to comment.