History.js 4.9 KB

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