Simple Undo/Redo functionality
package {
/**
* ...
* @author FCI
* @version v1.11
*/
public class UndoRedo {
private var _length:uint;
private var undoAction:Boolean;
private var redoAction:Boolean;
public var enableUndo:Boolean;
public var enableRedo:Boolean;
public var history:Array = [];
public var index:int = 0;
public var id:int = 0;
public function UndoRedo(length:uint = 10) {
this._length = length;
}
public function record(action:Array):void {
if(history.length == _length && history.length - 1 == index){
history.splice(0, 1);
history.push(action);
}else if (index > 0 && index < history.length - 1 || (id == -1 && history.length > 0)) {
history.splice(index);
history.push(action);
} else {
history.push(action);
}
index = id = history.length - 1;
redoAction = false;
undoAction = true;
checkEnabler();
}
public function undo():Array {
if (!undoAction) {
index--;
if (index < 0) {
index = 0;
}
}
id--;
if (id < -1)
id = -1;
checkEnabler();
// FOR REPEAT LAST STEP
redoAction = true;
undoAction = false;
return history[index];
}
public function redo():Array {
if (!redoAction) {
index++;
if (index > history.length - 1) {
index--;
}
}
id++;
if (id > history.length - 1)
id = history.length - 1;
checkEnabler();
// FOR REPEAT LAST STEP
redoAction = false;
undoAction = true;
return history[index];
}
private function checkEnabler():void {
if(history.length > 0){
if (id > -1) {
enableUndo = true;
}else {
enableUndo = false;
}
if (id < history.length - 1) {
enableRedo = true;
}else {
enableRedo = false;
}
}
}
public function clear():void {
history.length = 0;
index = id = 0;
enableUndo = false;
enableRedo = false;
checkEnabler();
}
public function get length():uint {
return _length;
}
}
}