1 module PixelPerfectEngine.concrete.eventChainSystem; 2 /** 3 * Defines an undoable event. 4 */ 5 public interface UndoableEvent{ 6 public abstract void redo(); ///called both when a redo command is initialized or the event is added to the stack. 7 public abstract 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 private UndoableEvent[] events; 15 private size_t currentPos; 16 17 public this(size_t maxElements){ 18 events.length = maxElements; 19 } 20 /** 21 * Adds an event to the top of the stack. If there are any undone events, they'll be lost. Bottom event is also lost. 22 */ 23 public void addToTop(UndoableEvent e){ 24 if(currentPos){ 25 events = events[currentPos..$]; 26 } 27 e.redo; 28 for(int i = events.length - 1 ; i > 0 ; i--){ 29 events[i] = events[i - 1]; 30 } 31 events[0] = e; 32 } 33 /** 34 * Undos top event. 35 */ 36 public void undo(){ 37 if(currentPos < events.length){ 38 if(events[currentPos]){ 39 events[currentPos].undo; 40 currentPos++; 41 } 42 } 43 } 44 /** 45 * Redos top event. 46 */ 47 public void redo(){ 48 if(currentPos > 0){ 49 if(events[currentPos]){ 50 currentPos--; 51 events[currentPos].redo; 52 } 53 } 54 } 55 }