1 module eventChainSystem.eventChain;
2 
3 import std.algorithm.mutation;
4 
5 import eventChainSystem.undoable;
6 
7 public class EventChainSystem{
8 	protected UndoableEvent[] eventList;
9 	protected int topEvent, maxEventNumber;
10 	public this(int maxEventNumber){
11 		this.maxEventNumber = maxEventNumber;
12 		topEvent = -1;
13 	}
14 	/**
15 	 * Undos the top event in the list indicated by the internal variable, topEvent. If topEvent equals -1, it does nothing.
16 	 */
17 	public void undoTopEvent(){
18 		if(topEvent >= 0){
19 			eventList[topEvent].undo();
20 			topEvent--;
21 		}
22 	}
23 	/**
24 	 * Redos the last undone event in the list indicated by the internal variable, topEvent. If topEvent equals the number of current events, it does nothing.
25 	 */
26 	public void redoTopEvent(){
27 		if(topEvent < eventList.length){
28 			eventList[topEvent].redo();
29 			topEvent++;
30 		}
31 	}
32 	/**
33 	 * Undos the event indicated by i in the list.
34 	 */
35 	public void undoEventAtGivenPosition(int i){
36 		if(i < eventList.length)
37 			eventList[i].undo();
38 	}
39 	/**
40 	 * Redos the event indicated by i in the list.
41 	 */
42 	public void redoEventAtGivenPosition(int i){
43 		if(i < eventList.length)
44 			eventList[i].redo();
45 	}
46 	/**
47 	 * Appends an event on the top. If the maximum event number is reached, it deletes the oldest one.
48 	 */
49 	public void appendEvent(UndoableEvent e){
50 		if(eventList.length < maxEventNumber){
51 			eventList ~= e;
52 			topEvent++;
53 		}else{
54 			eventList = eventList[1..eventList.length] ~ e;
55 		}
56 		/*if(position < eventList.length){
57 			swapAt(eventList, eventList.length-1, position);
58 		}*/
59 	}
60 	/**
61 	 * Sets the maximum number of events that can be stored.
62 	 */
63 	public void setLength(int i){
64 		maxEventNumber = i;
65 	}
66 	/**
67 	 * Gets the maximum number of events that can be stored.
68 	 */
69 	public int getMaxLength(){
70 		return maxEventNumber;
71 	}
72 	/**
73 	 * Sets the current number of events that are stored.
74 	 */
75 	public int getCurrentLength(){
76 		return eventList.length;
77 	}
78 	/**
79 	 * Removes the event stored at position indicated by i.
80 	 */
81 	public void removeEvent(int i){
82 		eventList = remove(eventList, i);
83 	}
84 }