Add new (key)frame.

Button add to stage fix.
getCharacters sync fix
This commit is contained in:
Jindra Petřík
2024-10-29 16:55:43 +01:00
parent 875b9c17d3
commit c43769410c
11 changed files with 604 additions and 113 deletions
@@ -16,10 +16,17 @@
*/
package com.jpexs.decompiler.flash.easygui;
import com.jpexs.decompiler.flash.ReadOnlyTagList;
import com.jpexs.decompiler.flash.tags.RemoveObject2Tag;
import com.jpexs.decompiler.flash.tags.ShowFrameTag;
import com.jpexs.decompiler.flash.tags.Tag;
import com.jpexs.decompiler.flash.tags.base.CharacterTag;
import com.jpexs.decompiler.flash.tags.base.MorphShapeTag;
import com.jpexs.decompiler.flash.tags.base.PlaceObjectTypeTag;
import com.jpexs.decompiler.flash.tags.base.RemoveTag;
import com.jpexs.decompiler.flash.timeline.DepthState;
import com.jpexs.decompiler.flash.timeline.Timeline;
import com.jpexs.decompiler.flash.timeline.Timelined;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
@@ -28,15 +35,22 @@ import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import org.pushingpixels.substance.internal.utils.SubstanceColorUtilities;
/**
@@ -44,8 +58,8 @@ import org.pushingpixels.substance.internal.utils.SubstanceColorUtilities;
*/
public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListener {
private final Timeline timeline;
private Timeline timeline;
public static final int FRAME_WIDTH = 8;
public static final int FRAME_HEIGHT = 18;
@@ -72,19 +86,23 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
public static final Color SELECTED_COLOR = new Color(0xff, 0x99, 0x99);
public static final Color SELECTED_BORDER_COLOR = new Color(0xcc, 0, 0);
//public static final Color SELECTED_COLOR = new Color(113, 174, 235);
public static final int BORDER_LINES_LENGTH = 2;
public static final float FONT_SIZE = 10.0f;
private final List<FrameSelectionListener> listeners = new ArrayList<>();
private final List<FrameSelectionListener> selectionListeners = new ArrayList<>();
private final List<Runnable> changeListeners = new ArrayList<>();
public Point cursor = null;
private int frame = 0;
private int depth = 0;
private final UndoManager undoManager;
private enum BlockType {
EMPTY, NORMAL, MOTION_TWEEN, SHAPE_TWEEN
@@ -101,7 +119,7 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
public static Color getBackgroundColor() {
return SystemColor.control;
}
public static Color getSelectedColor() {
return SystemColor.textHighlight;
}
@@ -115,26 +133,38 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
}
public void addFrameSelectionListener(FrameSelectionListener l) {
listeners.add(l);
selectionListeners.add(l);
}
public void removeFrameSelectionListener(FrameSelectionListener l) {
listeners.remove(l);
selectionListeners.remove(l);
}
public TimelineBodyPanel(Timeline timeline) {
public void addChangeListener(Runnable l) {
changeListeners.add(l);
}
this.timeline = timeline;
Dimension dim = new Dimension(FRAME_WIDTH * timeline.getFrameCount() + 1, FRAME_HEIGHT * (timeline.getMaxDepth() + 1));
setSize(dim);
setPreferredSize(dim);
public void removeChangeListener(Runnable l) {
changeListeners.remove(l);
}
private void fireChanged() {
for (Runnable l : changeListeners) {
l.run();
}
}
public TimelineBodyPanel(UndoManager undoManager) {
refresh();
addMouseListener(this);
addKeyListener(this);
setFocusable(true);
this.undoManager = undoManager;
}
@Override
protected void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
@@ -142,6 +172,9 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
g.setColor(getBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight());
if (timeline == null) {
return;
}
Rectangle clip = g.getClipBounds();
int frameWidth = FRAME_WIDTH;
int frameHeight = FRAME_HEIGHT;
@@ -180,22 +213,22 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
}
int awidth = g.getFontMetrics().stringWidth("a");
boolean firstAction = true;
boolean firstAction = true;
for (int f = start_f; f <= end_f || (firstAction && f <= max_f); f++) {
if (!timeline.getFrame(f).actions.isEmpty()) {
if (firstAction) {
drawBlock(g, getEmptyFrameColor(), 0, 0, f, BlockType.EMPTY);
}
}
int f2 = f + 1;
while(f2 <= max_f && timeline.getFrame(f2).actions.isEmpty()) {
while (f2 <= max_f && timeline.getFrame(f2).actions.isEmpty()) {
f2++;
}
drawBlock(g, getEmptyFrameColor(), 0, f, f2 - f, BlockType.EMPTY);
g.setColor(A_COLOR);
g.setFont(getFont().deriveFont(FONT_SIZE));
g.setFont(getFont().deriveFont(FONT_SIZE));
g.drawString("a", f * frameWidth + frameWidth / 2 - awidth / 2, frameHeight / 2);
firstAction = false;
firstAction = false;
}
}
@@ -337,7 +370,7 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
public void depthSelect(int depth) {
frameSelect(frame, depth);
}
public void frameSelect(int frame, int depth) {
if (cursor != null && cursor.x == frame && (cursor.y == depth || depth == -1)) {
return;
@@ -346,14 +379,19 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
depth = cursor.y;
}
cursor = new Point(frame, depth);
for (FrameSelectionListener l : listeners) {
l.frameSelected(frame, depth);
}
fireFrameSelected(frame, depth);
repaint();
this.frame = frame;
this.depth = depth;
scrollRectToVisible(getFrameBounds(frame, depth));
}
private void fireFrameSelected(int frame, int depth) {
for (FrameSelectionListener l : selectionListeners) {
l.frameSelected(frame, depth);
}
}
@Override
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
@@ -368,6 +406,342 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
}
frameSelect(p.x, p.y);
requestFocusInWindow();
if (SwingUtilities.isRightMouseButton(e)) {
JPopupMenu popupMenu = new JPopupMenu();
DepthState ds = timeline.getFrame(frame).layers.get(depth);
boolean thisEmpty = ds == null || ds.getCharacter() == null;
boolean previousEmpty = true;
boolean emptyDepth = true;
boolean somethingBefore = false;
boolean somethingAfter = false;
if (frame > 0) {
ds = timeline.getFrame(frame - 1).layers.get(depth);
previousEmpty = ds == null || ds.getCharacter() == null;
}
for (int f = frame - 1; f >= 0; f--) {
ds = timeline.getFrame(f).layers.get(depth);
boolean empty = ds == null || ds.getCharacter() == null;
if (!empty) {
somethingBefore = true;
break;
}
}
for (int f = frame + 1; f < timeline.getFrameCount(); f++) {
ds = timeline.getFrame(f).layers.get(depth);
boolean empty = ds == null || ds.getCharacter() == null;
if (!empty) {
somethingAfter = true;
break;
}
}
emptyDepth = thisEmpty && !somethingBefore && !somethingAfter;
JMenuItem addKeyFrameMenuItem = new JMenuItem("Add keyframe");
addKeyFrameMenuItem.addActionListener(this::addKeyFrame);
JMenuItem addKeyFrameEmptyBeforeMenuItem = new JMenuItem("Add keyframe with blank frames before");
addKeyFrameEmptyBeforeMenuItem.addActionListener(this::addKeyFrameEmptyBefore);
JMenuItem addFrameMenuItem = new JMenuItem("Add frame");
addFrameMenuItem.addActionListener(this::addFrame);
if (!thisEmpty) {
popupMenu.add(addKeyFrameMenuItem);
}
if (thisEmpty && previousEmpty && somethingBefore && !somethingAfter) {
popupMenu.add(addKeyFrameEmptyBeforeMenuItem);
}
if (!emptyDepth) {
popupMenu.add(addFrameMenuItem);
}
if (popupMenu.getComponentCount() > 0) {
popupMenu.show(this, e.getX(), e.getY());
}
}
}
private void addKeyFrame(ActionEvent e) {
if (timeline.getFrame(frame).layers.get(depth).key) {
return;
}
undoManager.doOperation(new TimelinedTagListDoableOperation(timeline.timelined) {
@Override
public void doOperation() {
super.doOperation();
Timelined timelined = timeline.timelined;
DepthState ds = timeline.getFrame(frame).layers.get(depth);
/*RemoveTag rm = new RemoveObject2Tag(timelined.getSwf());
rm.setDepth(depth);
rm.setTimelined(timelined);*/
PlaceObjectTypeTag place;
try {
place = (PlaceObjectTypeTag) ds.placeObjectTag.cloneTag();
} catch (InterruptedException | IOException ex) {
//should not happen
return;
}
place.setTimelined(timelined);
place.setPlaceFlagMove(true);
ShowFrameTag sf = timeline.getFrame(frame).showFrameTag;
int pos;
if (sf != null) {
pos = timelined.indexOfTag(sf);
} else {
pos = timelined.getTags().size();
}
//timelined.addTag(pos++, rm);
timelined.addTag(pos++, place);
timelined.resetTimeline();
timeline = timelined.getTimeline();
refresh();
fireChanged();
repaint();
}
@Override
public void undoOperation() {
super.undoOperation();
timeline = timeline.timelined.getTimeline();
refresh();
fireChanged();
repaint();
}
@Override
public String getDescription() {
return "Add key frame";
}
});
}
private void addKeyFrameEmptyBefore(ActionEvent e) {
undoManager.doOperation(new TimelinedTagListDoableOperation(timeline.timelined) {
@Override
public void doOperation() {
super.doOperation();
Timelined timelined = timeline.timelined;
DepthState ds = null;
for (int f = frame - 1; f >= 0; f--) {
ds = timeline.getFrame(f).layers.get(depth);
if (ds != null && ds.getCharacter() != null) {
break;
}
}
PlaceObjectTypeTag place;
try {
place = (PlaceObjectTypeTag) ds.placeObjectTag.cloneTag();
} catch (InterruptedException | IOException ex) {
//should not happen
return;
}
place.setTimelined(timelined);
place.setPlaceFlagMove(false);
ShowFrameTag sf = timeline.getFrame(frame).showFrameTag;
int pos;
if (sf != null) {
pos = timelined.indexOfTag(sf);
if (frame < timelined.getFrameCount() - 1) {
RemoveTag rm = new RemoveObject2Tag(timelined.getSwf());
rm.setTimelined(timelined);
rm.setDepth(depth);
timelined.addTag(pos + 1, rm);
}
} else {
pos = timelined.getTags().size();
}
timelined.addTag(pos++, place);
timelined.resetTimeline();
timeline = timelined.getTimeline();
refresh();
fireChanged();
repaint();
}
@Override
public void undoOperation() {
super.undoOperation();
timeline = timeline.timelined.getTimeline();
refresh();
fireChanged();
repaint();
}
@Override
public String getDescription() {
return "Add key frame";
}
});
}
private void addFrame(ActionEvent e) {
final int fframe = frame;
final int fdepth = depth;
undoManager.doOperation(new TimelinedTagListDoableOperation(timeline.timelined) {
@Override
public void doOperation() {
super.doOperation();
DepthState ds;
boolean somethingAfter = false;
for (int f = fframe + 1; f < timeline.getFrameCount(); f++) {
ds = timeline.getFrame(f).layers.get(fdepth);
boolean empty = ds == null || ds.getCharacter() == null;
if (!empty) {
somethingAfter = true;
break;
}
}
Timelined timelined = timeline.timelined;
for (int f = fframe; f >= 0; f--) {
ds = timeline.getFrame(f).layers.get(fdepth);
boolean empty = ds == null || ds.getCharacter() == null;
if (!empty || somethingAfter) {
int moveFrameCount = fframe - f;
if (moveFrameCount == 0) {
moveFrameCount = 1;
}
boolean frameAdded = false;
for (int mf = 0; mf < moveFrameCount; mf++) {
int pos = timelined.indexOfTag(timeline.getFrame(f).showFrameTag);
ReadOnlyTagList tags = timelined.getTags();
List<Tag> lastFrameDepthTags = new ArrayList<>();
int f2 = f + 1;
boolean endsWithRemove = false;
for (int i = pos + 1; i < tags.size(); i++) {
Tag t = tags.get(i);
if (t instanceof PlaceObjectTypeTag) {
PlaceObjectTypeTag po = (PlaceObjectTypeTag) t;
if (po.getDepth() == fdepth) {
lastFrameDepthTags.add(po);
timelined.removeTag(po);
endsWithRemove = false;
i--;
}
}
if (t instanceof RemoveTag) {
RemoveTag ro = (RemoveTag) t;
if (ro.getDepth() == fdepth) {
lastFrameDepthTags.add(ro);
timelined.removeTag(ro);
endsWithRemove = true;
i--;
}
}
if ((t instanceof ShowFrameTag) || (i == tags.size() - 1)) {
if (!(t instanceof ShowFrameTag) && !lastFrameDepthTags.isEmpty()) {
ShowFrameTag sf = new ShowFrameTag(timelined.getSwf());
sf.setTimelined(timelined);
timelined.addTag(sf);
i++;
}
boolean removeOnly = true;
for (Tag lt : lastFrameDepthTags) {
if (!(lt instanceof RemoveTag)) {
removeOnly = false;
break;
}
}
boolean onLastFrame = f2 == timelined.getFrameCount() - 1;
if (!(removeOnly && onLastFrame)) {
for (Tag lt : lastFrameDepthTags) {
i++;
timelined.addTag(i, lt);
}
}
//Add beyond current total frameCount
if (onLastFrame) {
if ((!removeOnly && !lastFrameDepthTags.isEmpty()) || !endsWithRemove) {
//Add removeTag to other layers
for (int d = 1; d <= timeline.maxDepth; d++) {
if (d == fdepth) {
continue;
}
ds = timeline.getFrame(f2).layers.get(d);
if (ds != null && ds.getCharacter() != null) {
RemoveTag rt = new RemoveObject2Tag(timelined.getSwf());
rt.setTimelined(timelined);
rt.setDepth(d);
timelined.addTag(i, rt);
i++;
}
}
frameAdded = true;
ShowFrameTag sf = new ShowFrameTag(timelined.getSwf());
sf.setTimelined(timelined);
timelined.addTag(sf);
i++;
timelined.setFrameCount(timelined.getFrameCount() + 1);
}
}
lastFrameDepthTags.clear();
f2++;
}
}
}
if (!frameAdded && fframe == timelined.getFrameCount() - 1) {
//Add removeTag to other layers
for (int d = 1; d <= timeline.maxDepth; d++) {
if (d == fdepth) {
continue;
}
ds = timeline.getFrame(fframe).layers.get(d);
if (ds != null && ds.getCharacter() != null) {
RemoveTag rt = new RemoveObject2Tag(timelined.getSwf());
rt.setTimelined(timelined);
rt.setDepth(d);
timelined.addTag(rt);
}
}
for (int mf = 0; mf < moveFrameCount; mf++) {
ShowFrameTag sf = new ShowFrameTag(timelined.getSwf());
sf.setTimelined(timelined);
timelined.addTag(sf);
}
}
break;
}
}
timelined.resetTimeline();
timelined.setFrameCount(timelined.getTimeline().getFrameCount());
timeline = timelined.getTimeline();
refresh();
fireChanged();
repaint();
}
@Override
public void undoOperation() {
super.undoOperation();
timeline = timeline.timelined.getTimeline();
refresh();
fireChanged();
repaint();
}
@Override
public String getDescription() {
return "Add frame";
}
});
}
@Override
@@ -418,10 +792,11 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
@Override
public void keyReleased(KeyEvent e) {
}
public Rectangle getDepthBounds(int depth) {
return getFrameBounds(frame, depth);
}
public Rectangle getFrameBounds(int frame, int depth) {
Rectangle rect = new Rectangle();
rect.width = FRAME_WIDTH;
@@ -430,11 +805,17 @@ public class TimelineBodyPanel extends JPanel implements MouseListener, KeyListe
rect.y = depth * FRAME_HEIGHT;
return rect;
}
public void refresh() {
Dimension dim = new Dimension(FRAME_WIDTH * timeline.getFrameCount() + 1, FRAME_HEIGHT * (timeline.getMaxDepth() + 1));
int frameCount = timeline == null ? 0 : timeline.getFrameCount();
int maxDepth = timeline == null ? 0 : timeline.getMaxDepth();
Dimension dim = new Dimension(FRAME_WIDTH * frameCount + 1, FRAME_HEIGHT * (maxDepth + 1));
setSize(dim);
setPreferredSize(dim);
}
}
public void setTimeline(Timeline timeline) {
this.timeline = timeline;
refresh();
}
}