History.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  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.config = editor.config;
  13. //Set editor-reference in Command
  14. Command( 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 === "SetScriptValueCommand" ) {
  36. // When the cmd.type is "SetScriptValueCommand" 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.config.getKey( 'project/history/stored' ) ) {
  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.undos = [];
  99. history.redos = [];
  100. if ( ! this.config.getKey( 'project/history/stored' ) ) {
  101. return history;
  102. }
  103. // Append Undos to History
  104. for ( var i = 0 ; i < this.undos.length; i ++ ) {
  105. if ( this.undos[ i ].hasOwnProperty( "json" ) ) {
  106. history.undos.push( this.undos[ i ].json );
  107. }
  108. }
  109. // Append Redos to History
  110. for ( var i = 0 ; i < this.redos.length; i ++ ) {
  111. if ( this.redos[ i ].hasOwnProperty( "json" ) ) {
  112. history.redos.push( this.redos[ i ].json );
  113. }
  114. }
  115. return history;
  116. },
  117. fromJSON: function ( json ) {
  118. if ( json === undefined ) return;
  119. for ( var i = 0; i < json.undos.length; i ++ ) {
  120. var cmdJSON = json.undos[ i ];
  121. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  122. cmd.json = cmdJSON;
  123. cmd.id = cmdJSON.id;
  124. cmd.name = cmdJSON.name;
  125. this.undos.push( cmd );
  126. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  127. }
  128. for ( var i = 0; i < json.redos.length; i ++ ) {
  129. var cmdJSON = json.redos[ i ];
  130. var cmd = new window[ cmdJSON.type ](); // creates a new object of type "json.type"
  131. cmd.json = cmdJSON;
  132. cmd.id = cmdJSON.id;
  133. cmd.name = cmdJSON.name;
  134. this.redos.push( cmd );
  135. this.idCounter = ( cmdJSON.id > this.idCounter ) ? cmdJSON.id : this.idCounter; // set last used idCounter
  136. }
  137. // Select the last executed undo-command
  138. this.editor.signals.historyChanged.dispatch( this.undos[ this.undos.length - 1 ] );
  139. },
  140. clear: function () {
  141. this.undos = [];
  142. this.redos = [];
  143. this.idCounter = 0;
  144. this.editor.signals.historyChanged.dispatch();
  145. },
  146. goToState: function ( id ) {
  147. if ( this.historyDisabled ) {
  148. alert( "Undo/Redo disabled while scene is playing." );
  149. return;
  150. }
  151. this.editor.signals.sceneGraphChanged.active = false;
  152. this.editor.signals.historyChanged.active = false;
  153. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  154. if ( cmd === undefined || id > cmd.id ) {
  155. cmd = this.redo();
  156. while ( cmd !== undefined && id > cmd.id ) {
  157. cmd = this.redo();
  158. }
  159. } else {
  160. while ( true ) {
  161. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  162. if ( cmd === undefined || id === cmd.id ) break;
  163. cmd = this.undo();
  164. }
  165. }
  166. this.editor.signals.sceneGraphChanged.active = true;
  167. this.editor.signals.historyChanged.active = true;
  168. this.editor.signals.sceneGraphChanged.dispatch();
  169. this.editor.signals.historyChanged.dispatch( cmd );
  170. },
  171. enableSerialization: function ( id ) {
  172. /**
  173. * because there might be commands in this.undos and this.redos
  174. * which have not been serialized with .toJSON() we go back
  175. * to the oldest command and redo one command after the other
  176. * while also calling .toJSON() on them.
  177. */
  178. this.goToState( - 1 );
  179. this.editor.signals.sceneGraphChanged.active = false;
  180. this.editor.signals.historyChanged.active = false;
  181. var cmd = this.redo();
  182. while ( cmd !== undefined ) {
  183. if ( ! cmd.hasOwnProperty( "json" ) ) {
  184. cmd.json = cmd.toJSON();
  185. }
  186. cmd = this.redo();
  187. }
  188. this.editor.signals.sceneGraphChanged.active = true;
  189. this.editor.signals.historyChanged.active = true;
  190. this.goToState( id );
  191. }
  192. };