1 module PixelPerfectEngine.concrete.eventChainSystem;
2 /**
3  * Defines an undoable event.
4  */
5 public interface UndoableEvent{
6 	public void redo();	///called both when a redo command is initialized or the event is added to the stack.
7 	public void undo();	///called when an undo command is initialized on the stack.
8 }
9 
10 /**
11  * Implements an undoable event list with automatic handling of undo/redo commands
12  */
13 public class UndoableStack{
14 	public UndoableEvent[] events;
15 	protected size_t currentPos, currentCap, maxLength;
16 
17 	public this(size_t maxElements) @safe pure nothrow{
18 		maxLength = maxElements;
19 		events.length = maxElements;
20 	}
21 	/**
22 	 * Adds an event to the top of the stack. If there are any undone events, they'll be lost. Bottom event is always lost.
23 	 */
24 	public void addToTop(UndoableEvent e){
25 		events = e ~ events[currentPos..$-1];
26 		events.length = maxLength;
27 		e.redo;
28 		currentPos = 0;
29 	}
30 	/**
31 	 * Undos top event.
32 	 */
33 	public void undo(){
34 		if(currentPos < events.length){
35 			if(events[currentPos]){
36 				events[currentPos].undo;
37 				currentPos++;
38 			}
39 		}
40 	}
41 	/**
42 	 * Redos top event.
43 	 */
44 	public void redo() {
45 		if(currentPos >= 0){
46 			if(events[currentPos]){
47 				currentPos--;
48 				events[currentPos].redo;
49 			}
50 		}
51 	}
52 	/**
53 	 * Returns the length of the current stack
54 	 */
55 	public size_t length() {
56 		return events.length;
57 	}
58 }