History.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author dforrer / https://github.com/dforrer
  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 the cmd immediately after execution and append the json to the cmd
  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. 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. 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. undos.push( this.undos[ i ].json );
  99. }
  100. history.undos = undos;
  101. // Append Redos to History
  102. var redos = [];
  103. for ( var i = 0 ; i < this.redos.length; i++ ) {
  104. redos.push( this.redos[ i ].json );
  105. }
  106. history.redos = redos;
  107. return history;
  108. },
  109. fromJSON: function ( json ) {
  110. if ( json === undefined ) return;
  111. for ( var i = 0; i < json.undos.length ; i++ ) {
  112. var cmdJSON = json.undos[ i ];
  113. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  114. cmd.json = cmdJSON;
  115. this.undos.push( cmd );
  116. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  117. }
  118. for ( var i = 0; i < json.redos.length ; i++ ) {
  119. var cmdJSON = json.redos[ i ];
  120. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  121. cmd.json = cmdJSON;
  122. this.redos.push( cmd );
  123. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  124. }
  125. // Select the last executed undo-command
  126. this.editor.signals.historyChanged.dispatch( this.undos[ this.undos.length - 1 ] );
  127. },
  128. clear: function () {
  129. this.undos = [];
  130. this.redos = [];
  131. this.idCounter = 0;
  132. this.editor.signals.historyChanged.dispatch();
  133. },
  134. goToState: function ( id ) {
  135. if ( this.historyDisabled ) {
  136. alert("Undo/Redo disabled while scene is playing.");
  137. return;
  138. }
  139. this.editor.signals.sceneGraphChanged.active = false;
  140. this.editor.signals.historyChanged.active = false;
  141. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  142. if ( cmd === undefined || id > cmd.json.id ) {
  143. cmd = this.redo();
  144. while ( cmd !== undefined && id > cmd.json.id ) {
  145. cmd = this.redo();
  146. }
  147. } else {
  148. while ( true ) {
  149. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  150. if ( cmd === undefined || id === cmd.json.id ) break;
  151. cmd = this.undo();
  152. }
  153. }
  154. this.editor.signals.sceneGraphChanged.active = true;
  155. this.editor.signals.historyChanged.active = true;
  156. this.editor.signals.sceneGraphChanged.dispatch();
  157. this.editor.signals.historyChanged.dispatch( cmd );
  158. }
  159. };