forked from MattStultz/extrusiongen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IKRS.UndoHistory.js
123 lines (78 loc) · 3.44 KB
/
IKRS.UndoHistory.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* @author Ikaros Kappler
* @date 2013-09-12
* @version 1.0.0
**/
IKRS.UndoHistory = function( initialValue,
capacity
) {
if( !initialValue )
throw "Error: initial value must not be null.";
if( !initialValue.clone || typeof initialValue.clone != "function" )
throw "Error: initial value has no clone() function!";
if( !capacity )
capacity = 32;
this._undoCapacity = capacity;
this._undoHistory = [ this._undoCapacity ];
//this._undoHistory[0] = initialValue.clone(); // One history entry into the past :)
this._undoHistory[0] = initialValue; // The current path to work on
this._undoFront = 0;
this._undoLength = 1;
this._undoPointer = 0;
}
IKRS.UndoHistory.prototype = new IKRS.Object();
IKRS.UndoHistory.prototype.constructor = IKRS.UndoHistory;
IKRS.UndoHistory.prototype.getCurrentState = function() {
//window.alert( JSON.stringify(this._undoHistory) );
return this._undoHistory[ (this._undoFront + this._undoPointer) % this._undoCapacity ];
}
IKRS.UndoHistory.prototype.createHistoryEntry = function() {
//window.alert( JSON.stringify(this) );
// Clone current state
var newItem = this.getCurrentState().clone();
// Add to end of undo-history
//this._undoPointer = (this._undoPointer + 1.0) % this._undoCapacity;
if( this._undoPointer < this._undoCapacity ) {
this._undoPointer = this._undoPointer + 1;
} else {
this._undoFront = (this._undoFront + 1) % this._undoCapacity;
}
// Undo length reached? (is in present)
if( this._undoPointer < this._undoLength ) {
// Cut off the trailing elements
if( this._undoPointer < this._undoCapacity )
this._undoLength = this._undoPointer+1;
else
this._undoLength = this._undoCapacity;
} else { // if( this._undoPointer >= this._undoLength ) {
this._undoLength = Math.min( this._undoLength+1,
this._undoCapacity
);
}
//window.alert( "undoLength=" + this._undoLength + ", undoFront=" + this._undoFront + ", undoPointer=" + this._undoPointer + ", undoCapacity=" + this._undoCapacity + ", newItem=" + newItem );
this._undoHistory[ (this._undoFront + this._undoPointer) % this._undoCapacity ] = newItem;
}
IKRS.UndoHistory.prototype.undo = function() {
//window.alert( "_undoPointer=" + this._undoPointer );
// No history entries available?
if( this._undoPointer <= 0 )
return false;
this._undoPointer -= 1;
// Fetch history entry?
// var path = this._undoHistory[ (this._undoFront + this._undoPointer) % this._undoCapacity ];
//window.alert( "currentPath=" + JSON.stringify(this.bezierPath) + ",\n oldPath=" + JSON.stringify(path) + ",\n equal=" + this.bezierPath.equals(path) );
return true;
}
/*
IKRS.UndoHistory.prototype.redo = function() {
if( this._undoPointer+1 >= this._undoLength )
return false;
this._undoPointer = this._undoPointer + 1;
var path = this._undoHistory[ (this._undoFront + this._undoPointer) % this._undoCapacity ];
//window.alert( "currentPath=" + JSON.stringify(this.bezierPath) + ",\n oldPath=" + JSON.stringify(path) + ",\n equal=" + this.bezierPath.equals(path) );
return true;
}
*/
IKRS.UndoHistory.prototype._toString = function() {
return "[IKRS.UndoHistory]={ front=" + this._undoFront + ", length=" + this._undoLength + ", pointer=" + this._undoPointer + "}";
}