-
Notifications
You must be signed in to change notification settings - Fork 0
/
UndoableEdit.pde
87 lines (61 loc) · 2.4 KB
/
UndoableEdit.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.UndoableEditSupport;
import javax.swing.undo.AbstractUndoableEdit;
public class UndoableEdit extends AbstractUndoableEdit {
ArrayList<PropagateTarget> savedTargets;
ArrayList<color[]> savedTargetsPixelsForUndo = new ArrayList<color[]>();
ArrayList<color[]> savedTargetsPixelsForRedo = new ArrayList<color[]>();
public UndoableEdit (PGraphics buffer) {
this(new PropagateTarget(buffer));
}
public void end() {
finalize();
undoManager.addEdit(this);
}
public UndoableEdit (PropagateTarget savedTarget) {
ArrayList<PropagateTarget> savedTargets = new ArrayList<PropagateTarget>();
savedTargets.add(savedTarget);
this.savedTargets = savedTargets;
extractUndoRedoInformation(savedTargetsPixelsForUndo);
}
public UndoableEdit (ArrayList<PropagateTarget> savedTargets) {
this.savedTargets = savedTargets;
extractUndoRedoInformation(savedTargetsPixelsForUndo);
}
public void extractUndoRedoInformation(ArrayList<color[]>savedTargetsPixels) {
for (PropagateTarget target : savedTargets) {
target.buffer.loadPixels();
color[] targetBufferPixels = new color[target.buffer.pixels.length];
System.arraycopy(target.buffer.pixels, 0, targetBufferPixels, 0, target.buffer.pixels.length);
savedTargetsPixels.add(targetBufferPixels);
}
}
public void undo() throws CannotUndoException {
super.undo();
// println("undo");
restoreFromUndoRedoInformation(savedTargetsPixelsForUndo);
}
public void redo() throws CannotRedoException {
super.redo();
// println("redo");
restoreFromUndoRedoInformation(savedTargetsPixelsForRedo);
}
public void restoreFromUndoRedoInformation(ArrayList<color[]> savedTargetsPixels) {
for (int i=0; i< savedTargets.size(); i++) {
PropagateTarget target = savedTargets.get(i);
target.buffer.loadPixels();
color[] savedTargetPixels = savedTargetsPixels.get(i);
System.arraycopy(savedTargetPixels, 0, target.buffer.pixels, 0, target.buffer.pixels.length);
target.buffer.updatePixels();
}
}
public void updateBoundingBox(int start_x, int start_y, int final_x, int final_y) {
}
public void finalize() {
extractUndoRedoInformation(savedTargetsPixelsForRedo);
}
public String getPresentationName() {
return "";
}
}