Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. public class Face
  2. {
  3. List<Vertex> vertices;
  4. }
  5.  
  6. public class Vertex : ValueLog<Vertex>
  7. {
  8. Vector3 _position;
  9. public Vector3 Position
  10. {
  11. set { Log(); _position = value; }
  12. get { return _position; }
  13. }
  14.  
  15. public Vertex(Vector3 position)
  16. {
  17. _position = position;
  18. }
  19.  
  20. protected override void Restore(Vertex value)
  21. {
  22. _position = value._position;
  23. }
  24.  
  25. protected override Vertex GetLogValue()
  26. {
  27. return new Vertex(_position);
  28. }
  29. }
  30.  
  31. public abstract class ValueLog<T> : ICmd
  32. {
  33. Stack<T> undoStack;
  34. Stack<T> redoStack;
  35.  
  36. bool logged = false;
  37.  
  38. public ValueLog()
  39. {
  40. undoStack = new Stack<T>();
  41. redoStack = new Stack<T>();
  42. }
  43.  
  44. protected abstract void Restore(T value);
  45. protected abstract T GetLogValue();
  46.  
  47. protected void Log()
  48. {
  49. if (!logged && !Logger.Instance.logPaused)
  50. {
  51. logged = true;
  52.  
  53. redoStack.Clear();
  54. undoStack.Push(GetLogValue());
  55.  
  56. // add log operation to a list of operations that happen at once
  57. Logger.Instance.AddCmd(this);
  58.  
  59. Logger.Instance.OnCollect += OnCollect;
  60. }
  61. }
  62.  
  63. public override void Undo()
  64. {
  65. if (undoStack.Count > 0)
  66. {
  67. T current = GetLogValue();
  68. redoStack.Push(current);
  69.  
  70. T restore = undoStack.Pop();
  71. Restore(restore);
  72. }
  73. }
  74.  
  75. public override void Redo()
  76. {
  77. if (redoStack.Count > 0)
  78. {
  79. T current = GetLogValue();
  80. undoStack.Push(current);
  81.  
  82. T restore = redoStack.Pop();
  83. Restore(restore);
  84. }
  85. }
  86.  
  87. void OnCollect()
  88. {
  89. // called when operation finished (On Mouse Up)
  90. logged = false;
  91. Logger.Instance.OnCollect -= OnCollect;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement