-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMemento.pde
65 lines (56 loc) · 1.66 KB
/
Memento.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
/**
* Petter - vector-graphic-based pattern generator.
* http://www.lafkon.net/petter/
* Copyright (C) 2015 LAFKON/Benjamin Stephan
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/
import java.util.LinkedList;
public class Memento {
private int capacity;
private ControlP5 gui;
private LinkedList <String>deque;
private int index = 0;
public Memento(ControlP5 gui, int capacity) {
this.gui = gui;
this.capacity = capacity;
deque = new LinkedList<String>();
}
public void undo() {
if(index < deque.size()-1) {
index++;
gui.getProperties().getSnapshot(deque.get(index));
}
}
public void redo() {
if(index > 0) {
index--;
gui.getProperties().getSnapshot(deque.get(index));
}
}
public void setUndoStep() {
//when not on head of undo-list, remove head-elements first
if(index != 0) {
for(int i = 0; i<index ; i++) {
deque.pollFirst();
}
index = 0;
}
String id = str(millis());
if(deque.size() < capacity) {
deque.offerFirst(id);
gui.getProperties().setSnapshot(id);
} else {
deque.pollLast(); //remove and
setUndoStep(); //try again
}
//println(deque);
}
}