Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 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>
  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. Logger.Instance.AddCmd(this); // add this
  57. Logger.Instance.OnCollect += OnCollect;
  58. }
  59. }
  60.  
  61. public override void Undo()
  62. {
  63. if (undoStack.Count > 0)
  64. {
  65. T current = GetLogValue();
  66. redoStack.Push(current);
  67.  
  68. T restore = undoStack.Pop();
  69. Restore(restore);
  70. }
  71. }
  72.  
  73. public override void Redo()
  74. {
  75. if (redoStack.Count > 0)
  76. {
  77. T current = GetLogValue();
  78. undoStack.Push(current);
  79.  
  80. T restore = redoStack.Pop();
  81. Restore(restore);
  82. }
  83. }
  84.  
  85. void OnCollect()
  86. {
  87. // called when operation finished (On Mouse Up)
  88. logged = false;
  89. Logger.Instance.OnCollect -= OnCollect;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement