History.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * edited by dforrer on 20.07.15.
  4. */
  5. History = function ( editor ) {
  6. this.editor = editor;
  7. this.undos = [];
  8. this.redos = [];
  9. this.lastCmdTime = new Date();
  10. this.idCounter = 0;
  11. this.historyDisabled = false;
  12. //Set editor-reference in Cmd
  13. Cmd( editor );
  14. // signals
  15. var scope = this;
  16. this.editor.signals.startPlayer.add( function () {
  17. scope.historyDisabled = true;
  18. } );
  19. this.editor.signals.stopPlayer.add( function () {
  20. scope.historyDisabled = false;
  21. } );
  22. };
  23. History.prototype = {
  24. execute: function ( cmd, optionalName ) {
  25. var lastCmd = this.undos[ this.undos.length - 1 ];
  26. var timeDifference = new Date().getTime() - this.lastCmdTime.getTime();
  27. var isUpdatableCmd = lastCmd &&
  28. lastCmd.updatable &&
  29. cmd.updatable &&
  30. lastCmd.object === cmd.object &&
  31. lastCmd.type === cmd.type &&
  32. lastCmd.script === cmd.script &&
  33. lastCmd.attributeName === cmd.attributeName;
  34. if ( isUpdatableCmd && cmd.type === "CmdSetScriptValue" ) {
  35. // When the cmd.type is "CmdSetScriptValue" the timeDifference is ignored
  36. lastCmd.update( cmd );
  37. cmd = lastCmd;
  38. } else if ( isUpdatableCmd && timeDifference < 500 ) {
  39. lastCmd.update( cmd );
  40. cmd = lastCmd;
  41. } else {
  42. // the command is not updatable and is added as a new part of the history
  43. this.undos.push( cmd );
  44. cmd.id = ++this.idCounter;
  45. }
  46. cmd.name = optionalName !== undefined ? optionalName : cmd.name;
  47. cmd.execute();
  48. cmd.inMemory = true;
  49. cmd.json = cmd.toJSON(); // serialize cmd immediately after execution
  50. this.lastCmdTime = new Date();
  51. // clearing all the redo-commands
  52. this.redos = [];
  53. this.editor.signals.historyChanged.dispatch( cmd );
  54. },
  55. undo: function () {
  56. if ( this.historyDisabled ) {
  57. alert("Undo/Redo disabled while scene is playing.");
  58. return;
  59. }
  60. var cmd = undefined;
  61. if ( this.undos.length > 0 ) {
  62. var cmd = this.undos.pop();
  63. if ( cmd.inMemory === false ) {
  64. cmd.fromJSON( cmd.json );
  65. }
  66. }
  67. if ( cmd !== undefined ) {
  68. cmd.undo();
  69. this.redos.push( cmd );
  70. this.editor.signals.historyChanged.dispatch( cmd );
  71. }
  72. return cmd;
  73. },
  74. redo: function () {
  75. if ( this.historyDisabled ) {
  76. alert("Undo/Redo disabled while scene is playing.");
  77. return;
  78. }
  79. var cmd = undefined;
  80. if ( this.redos.length > 0 ) {
  81. var cmd = this.redos.pop();
  82. if ( cmd.inMemory === false ) {
  83. cmd.fromJSON( cmd.json );
  84. }
  85. }
  86. if ( cmd !== undefined ) {
  87. cmd.execute();
  88. this.undos.push( cmd );
  89. this.editor.signals.historyChanged.dispatch( cmd );
  90. }
  91. return cmd;
  92. },
  93. toJSON: function () {
  94. var history = {};
  95. // Append Undos to History
  96. var undos = [];
  97. for ( var i = 0 ; i < this.undos.length; i++ ) {
  98. var cmd = this.undos[ i ];
  99. undos.push( cmd.json );
  100. }
  101. history.undos = undos;
  102. // Append Redos to History
  103. var redos = [];
  104. for ( var i = 0 ; i < this.redos.length; i++ ) {
  105. var cmd = this.redos[ i ];
  106. redos.push( cmd.json );
  107. }
  108. history.redos = redos;
  109. return history;
  110. },
  111. fromJSON: function ( json ) {
  112. if ( json === undefined ) return;
  113. for ( var i = 0; i < json.undos.length ; i++ ) {
  114. var cmdJSON = json.undos[ i ];
  115. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  116. cmd.json = cmdJSON;
  117. this.undos.push( cmd );
  118. this.idCounter = cmdJSON.id > this.idCounter ? cmdJSON.id : this.idCounter; // set last used idCounter
  119. }
  120. for ( var i = 0; i < json.redos.length ; i++ ) {
  121. var cmdJSON = json.redos[ i ];
  122. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  123. cmd.json = cmdJSON;
  124. this.redos.push( cmd );
  125. this.idCounter = cmdJSON.id > this.idCounter ? cmdJSON.id : this.idCounter; // set last used idCounter
  126. }
  127. this.editor.signals.historyChanged.dispatch();
  128. },
  129. clear: function () {
  130. this.undos = [];
  131. this.redos = [];
  132. this.idCounter = 0;
  133. this.editor.signals.historyChanged.dispatch();
  134. },
  135. goToState: function ( id ) {
  136. if ( this.historyDisabled ) {
  137. alert("Undo/Redo disabled while scene is playing.");
  138. return;
  139. }
  140. this.editor.signals.sceneGraphChanged.active = false;
  141. this.editor.signals.historyChanged.active = false;
  142. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  143. if ( cmd === undefined || id > cmd.json.id ) {
  144. cmd = this.redo();
  145. while ( cmd !== undefined && id > cmd.json.id ) {
  146. cmd = this.redo();
  147. }
  148. } else {
  149. while ( true ) {
  150. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  151. if ( cmd === undefined || id === cmd.json.id ) break;
  152. cmd = this.undo();
  153. }
  154. }
  155. this.editor.signals.sceneGraphChanged.active = true;
  156. this.editor.signals.historyChanged.active = true;
  157. this.editor.signals.sceneGraphChanged.dispatch();
  158. this.editor.signals.historyChanged.dispatch( cmd );
  159. }
  160. };