History.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. this.serializationEnabled = true;
  13. //Set editor-reference in Cmd
  14. Cmd( editor );
  15. // signals
  16. var scope = this;
  17. this.editor.signals.startPlayer.add( function () {
  18. scope.historyDisabled = true;
  19. } );
  20. this.editor.signals.stopPlayer.add( function () {
  21. scope.historyDisabled = false;
  22. } );
  23. };
  24. History.prototype = {
  25. execute: function ( cmd, optionalName ) {
  26. var lastCmd = this.undos[ this.undos.length - 1 ];
  27. var timeDifference = new Date().getTime() - this.lastCmdTime.getTime();
  28. var isUpdatableCmd = lastCmd &&
  29. lastCmd.updatable &&
  30. cmd.updatable &&
  31. lastCmd.object === cmd.object &&
  32. lastCmd.type === cmd.type &&
  33. lastCmd.script === cmd.script &&
  34. lastCmd.attributeName === cmd.attributeName;
  35. if ( isUpdatableCmd && cmd.type === "CmdSetScriptValue" ) {
  36. // When the cmd.type is "CmdSetScriptValue" the timeDifference is ignored
  37. lastCmd.update( cmd );
  38. cmd = lastCmd;
  39. } else if ( isUpdatableCmd && timeDifference < 500 ) {
  40. lastCmd.update( cmd );
  41. cmd = lastCmd;
  42. } else {
  43. // the command is not updatable and is added as a new part of the history
  44. this.undos.push( cmd );
  45. cmd.id = ++this.idCounter;
  46. }
  47. cmd.name = ( optionalName !== undefined ) ? optionalName : cmd.name;
  48. cmd.execute();
  49. cmd.inMemory = true;
  50. if ( this.serializationEnabled ) {
  51. cmd.json = cmd.toJSON(); // serialize the cmd immediately after execution and append the json to the cmd
  52. }
  53. this.lastCmdTime = new Date();
  54. // clearing all the redo-commands
  55. this.redos = [];
  56. this.editor.signals.historyChanged.dispatch( cmd );
  57. },
  58. undo: function () {
  59. if ( this.historyDisabled ) {
  60. alert("Undo/Redo disabled while scene is playing.");
  61. return;
  62. }
  63. var cmd = undefined;
  64. if ( this.undos.length > 0 ) {
  65. cmd = this.undos.pop();
  66. if ( cmd.inMemory === false ) {
  67. cmd.fromJSON( cmd.json );
  68. }
  69. }
  70. if ( cmd !== undefined ) {
  71. cmd.undo();
  72. this.redos.push( cmd );
  73. this.editor.signals.historyChanged.dispatch( cmd );
  74. }
  75. return cmd;
  76. },
  77. redo: function () {
  78. if ( this.historyDisabled ) {
  79. alert("Undo/Redo disabled while scene is playing.");
  80. return;
  81. }
  82. var cmd = undefined;
  83. if ( this.redos.length > 0 ) {
  84. cmd = this.redos.pop();
  85. if ( cmd.inMemory === false ) {
  86. cmd.fromJSON( cmd.json );
  87. }
  88. }
  89. if ( cmd !== undefined ) {
  90. cmd.execute();
  91. this.undos.push( cmd );
  92. this.editor.signals.historyChanged.dispatch( cmd );
  93. }
  94. return cmd;
  95. },
  96. toJSON: function () {
  97. var history = {};
  98. history.serializationEnabled = this.serializationEnabled;
  99. history.undos = [];
  100. history.redos = [];
  101. if ( !this.serializationEnabled ) {
  102. return history;
  103. }
  104. // Append Undos to History
  105. for ( var i = 0 ; i < this.undos.length; i++ ) {
  106. if ( this.undos[ i ].hasOwnProperty( "json" ) ) {
  107. history.undos.push( this.undos[ i ].json );
  108. }
  109. }
  110. // Append Redos to History
  111. for ( var i = 0 ; i < this.redos.length; i++ ) {
  112. if ( this.redos[ i ].hasOwnProperty( "json" ) ) {
  113. history.redos.push( this.redos[ i ].json );
  114. }
  115. }
  116. return history;
  117. },
  118. fromJSON: function ( json ) {
  119. if ( json === undefined ) return;
  120. this.serializationEnabled = json.serializationEnabled;
  121. for ( var i = 0; i < json.undos.length ; i++ ) {
  122. var cmdJSON = json.undos[ i ];
  123. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  124. cmd.json = cmdJSON;
  125. cmd.id = cmdJSON.id;
  126. cmd.name = cmdJSON.name;
  127. this.undos.push( cmd );
  128. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  129. }
  130. for ( var i = 0; i < json.redos.length ; i++ ) {
  131. var cmdJSON = json.redos[ i ];
  132. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  133. cmd.json = cmdJSON;
  134. cmd.id = cmdJSON.id;
  135. cmd.name = cmdJSON.name;
  136. this.redos.push( cmd );
  137. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  138. }
  139. // Select the last executed undo-command
  140. this.editor.signals.historyChanged.dispatch( this.undos[ this.undos.length - 1 ] );
  141. },
  142. clear: function () {
  143. this.undos = [];
  144. this.redos = [];
  145. this.idCounter = 0;
  146. this.editor.signals.historyChanged.dispatch();
  147. },
  148. goToState: function ( id ) {
  149. if ( this.historyDisabled ) {
  150. alert("Undo/Redo disabled while scene is playing.");
  151. return;
  152. }
  153. this.editor.signals.sceneGraphChanged.active = false;
  154. this.editor.signals.historyChanged.active = false;
  155. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  156. if ( cmd === undefined || id > cmd.id ) {
  157. cmd = this.redo();
  158. while ( cmd !== undefined && id > cmd.id ) {
  159. cmd = this.redo();
  160. }
  161. } else {
  162. while ( true ) {
  163. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  164. if ( cmd === undefined || id === cmd.id ) break;
  165. cmd = this.undo();
  166. }
  167. }
  168. this.editor.signals.sceneGraphChanged.active = true;
  169. this.editor.signals.historyChanged.active = true;
  170. this.editor.signals.sceneGraphChanged.dispatch();
  171. this.editor.signals.historyChanged.dispatch( cmd );
  172. },
  173. enableSerialization: function ( id ) {
  174. if ( this.serializationEnabled ) { return; }
  175. /**
  176. * because there might be commands in this.undos and this.redos
  177. * which have not been serialized with .toJSON() we go back
  178. * to the oldest command and redo one command after the other
  179. * while also calling .toJSON() on them.
  180. */
  181. this.goToState(-1);
  182. this.editor.signals.sceneGraphChanged.active = false;
  183. this.editor.signals.historyChanged.active = false;
  184. var cmd = this.redo();
  185. while ( cmd !== undefined ) {
  186. if ( !cmd.hasOwnProperty( "json" ) ) {
  187. cmd.json = cmd.toJSON();
  188. }
  189. cmd = this.redo();
  190. }
  191. this.editor.signals.sceneGraphChanged.active = true;
  192. this.editor.signals.historyChanged.active = true;
  193. this.serializationEnabled = true;
  194. this.goToState( id );
  195. },
  196. disableSerialization: function () {
  197. if ( !this.serializationEnabled ) { return; }
  198. this.serializationEnabled = false;
  199. this.editor.signals.historyChanged.dispatch();
  200. }
  201. };