History.js 4.8 KB

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