Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
undo/redo in edit dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
mwe-ontec committed Feb 29, 2020
1 parent a947cc4 commit f64e916
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 18 deletions.
Binary file modified out/artifacts/PhotonFileValidator.jar
Binary file not shown.
137 changes: 120 additions & 17 deletions src/photon/application/dialogs/EditDialog.java
Expand Up @@ -25,6 +25,7 @@
package photon.application.dialogs;

import photon.application.MainForm;
import photon.application.utilities.MainUtils;
import photon.file.PhotonFile;
import photon.file.parts.PhotonDot;
import photon.file.parts.PhotonFileLayer;
Expand All @@ -34,10 +35,7 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Stack;
import java.util.*;

public class EditDialog extends JDialog {

Expand All @@ -59,18 +57,45 @@ public class EditDialog extends JDialog {
private int layerNo;
private int layerX;
private int layerY;
private HashSet<PhotonDot> dots;
private Set<EditDot> dots;
private Stack<Set<EditDot>> operations;
private Stack<Set<EditDot>> undoneOperations;

private boolean mirrored;
private PhotonDot cursorDot;

private MouseAdapter currentEditModeHandler;

private static class EditDot extends PhotonDot {
private Color color;

public EditDot(PhotonDot dot, Color color) {
this(dot.x, dot.y, color);
}

public EditDot(int x, int y, Color color) {
super(x, y);
this.color = color;
}


@Override
public String toString() {
final StringBuilder sb = new StringBuilder("EditDot{");
sb.append("x=").append(x);
sb.append(", y=").append(y);
sb.append(", color=").append(color);
sb.append('}');
return sb.toString();
}
}

private class ModePencilHandler extends MouseAdapter {
PhotonDot lastCell;

@Override
public void mousePressed(MouseEvent e) {
saveState();
handleCellChange(e);
}

Expand All @@ -82,7 +107,7 @@ public void mouseDragged(MouseEvent e) {
@Override
public void mouseReleased(MouseEvent e) {
lastCell = null;
if (e.getButton() == MouseEvent.BUTTON2) {
if (e.getButton() == MouseEvent.BUTTON2) {
flood(getPosition(e));
}
}
Expand All @@ -108,6 +133,8 @@ private void flood(PhotonDot origin) {
return;
}

saveState();

int count = 0;

// do we fill or erase?
Expand Down Expand Up @@ -160,40 +187,60 @@ private void editPixel(PhotonDot cell, boolean on) {
boolean isOriginalOn = original != PhotonLayer.OFF;
Color originalColor = getOriginalColor(cell);

PhotonDot dot = new PhotonDot(layerY + cell.y, layerX + cell.x);
EditDot dot = new EditDot(layerY + cell.y, layerX + cell.x, originalColor);

if (dots.contains(dot)) {
dots.remove(dot);
}

Color color = originalColor;
if (on) {
if (!isOriginalOn) {
color = Color.cyan;
dot.color = Color.cyan;
dots.add(dot);
}
} else {
if (isOriginalOn) {
color = Color.darkGray;
dot.color = Color.darkGray;
dots.add(dot);
}
}

((PhotonEditPanel) editArea).drawDot(cell.x, cell.y, layer, color);
((PhotonEditPanel) editArea).drawDot(cell.x, cell.y, layer, dot.color);
}

;

private class ModeSwapHandler extends MouseAdapter {
private PhotonDot pressedDot;
private Rectangle rect;

@Override
public void mousePressed(MouseEvent e) {
saveState();
pressedDot = getPosition(e);
}

@Override
public void mouseDragged(MouseEvent e) {
if (pressedDot != null) {

clearRect();

PhotonDot currentDot = getPosition(e);
int x1 = Integer.min(pressedDot.x, currentDot.x);
int x2 = Integer.max(pressedDot.x, currentDot.x);
int y1 = Integer.min(pressedDot.y, currentDot.y);
int y2 = Integer.max(pressedDot.y, currentDot.y);

rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
((PhotonEditPanel) editArea).drawRect(rect, Color.cyan);
editArea.repaint();
}
}

@Override
public void mouseReleased(MouseEvent e) {
clearRect();
PhotonDot releasedDot = getPosition(e);
if (pressedDot != null && releasedDot != null) {

Expand All @@ -211,6 +258,14 @@ public void mouseReleased(MouseEvent e) {
}
}
}

private void clearRect() {
if (rect != null) {
((PhotonEditPanel) editArea).drawRect(rect, Color.decode("#999999"));
rect = null;
}
}

}

;
Expand All @@ -221,6 +276,7 @@ private class ModeRectHandler extends MouseAdapter {

@Override
public void mousePressed(MouseEvent e) {
saveState();
pressedDot = getPosition(e);
}

Expand Down Expand Up @@ -249,6 +305,7 @@ private void clearRect() {
rect = null;
}
}

@Override
public void mouseReleased(MouseEvent e) {
clearRect();
Expand Down Expand Up @@ -339,6 +396,20 @@ public void actionPerformed(ActionEvent e) {
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

contentPane.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
undo();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_Z, MainUtils.getSystemDefaultModifierMask()), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

contentPane.registerKeyboardAction(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
redo();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_Z, MainUtils.getSystemDefaultModifierMask() | KeyEvent.SHIFT_DOWN_MASK), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

setEditMode(EditMode.pencil);

editArea.addMouseMotionListener(new MouseMotionAdapter() {
Expand All @@ -349,6 +420,39 @@ public void mouseMoved(MouseEvent e) {
});
}

private void saveState() {
Set<EditDot> state = new HashSet<>(dots);
operations.push(state);
}

private void undo() {
if (!operations.empty()) {
Set<EditDot> lastState = new HashSet<>(operations.pop());
undoneOperations.push(new HashSet<>(dots));
dots = new HashSet<>(lastState);

redraw();
}
}

private void redo() {
if (!undoneOperations.empty()) {
Set<EditDot> nextState = undoneOperations.pop();
operations.push(new HashSet<>(dots));
dots = new HashSet<>(nextState);

redraw();
}
}

private void redraw() {
((PhotonEditPanel) editArea).drawLayer(layerX, layerY, layer);
for (EditDot dot : dots) {
((PhotonEditPanel) editArea).drawDot(dot.y - layerX, dot.x - layerY, layer, dot.color);
}
editArea.repaint();
}

private void setEditMode(EditMode newEditMode) {
editArea.removeMouseListener(currentEditModeHandler);
editArea.removeMouseMotionListener(currentEditModeHandler);
Expand Down Expand Up @@ -405,8 +509,9 @@ private Color isSet(int x, int y) {
}

} else {
dots.add(dot);
return result ? Color.darkGray : Color.cyan;
Color color = result ? Color.darkGray : Color.cyan;
dots.add(new EditDot(dot, color));
return color;
}
}

Expand Down Expand Up @@ -495,6 +600,8 @@ private void onCancel() {

public void setInformation(PhotonFile photonFile, int layerNo, int mouseX, int mouseY) {
this.dots = new HashSet<>();
operations = new Stack<>();
undoneOperations = new Stack<>();
this.layerNo = layerNo;
this.photonFile = photonFile;
this.fileLayer = photonFile.getLayer(layerNo);
Expand Down Expand Up @@ -550,10 +657,6 @@ private boolean isValid(PhotonDot dot) {
if (dot.x >= 75) return false;
if (dot.y < 0) return false;
if (dot.y >= 45) return false;
if (layerX + dot.x < 0) return false;
if (layerX + dot.x >= photonFile.getWidth()) return false;
if (layerY + dot.y < 0) return false;
if (layerY + dot.y >= photonFile.getHeight()) return false;
return true;
}

Expand Down
10 changes: 9 additions & 1 deletion src/photon/application/utilities/MainUtils.java
Expand Up @@ -187,5 +187,13 @@ public static void boot() {
}
}


public static int getSystemDefaultModifierMask() {
int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
if (mask == Event.META_MASK) {
return KeyEvent.META_DOWN_MASK;
} else if (mask == Event.ALT_MASK) {
return KeyEvent.ALT_DOWN_MASK;
}
return KeyEvent.CTRL_DOWN_MASK;
}
}

0 comments on commit f64e916

Please sign in to comment.