History.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. console.log('Type: Undo ' + cmd.type );
  71. this.redos.push( cmd );
  72. this.editor.signals.historyChanged.dispatch( cmd );
  73. }
  74. return cmd;
  75. },
  76. redo: function () {
  77. if ( this.historyDisabled ) {
  78. alert("Undo/Redo disabled while scene is playing.");
  79. return;
  80. }
  81. var cmd = undefined;
  82. if ( this.redos.length > 0 ) {
  83. var cmd = this.redos.pop();
  84. if ( cmd.serialized ) {
  85. var json = cmd;
  86. cmd = new window[ json.type ](); // creates a new object of type "json.type"
  87. cmd.editor = this.editor;
  88. cmd.fromJSON( json );
  89. }
  90. }
  91. if ( cmd !== undefined ) {
  92. cmd.execute();
  93. console.log('Type: Redo ' + cmd.type );
  94. this.undos.push( cmd );
  95. this.editor.signals.historyChanged.dispatch( cmd );
  96. }
  97. return cmd;
  98. },
  99. toJSON: function () {
  100. var history = {};
  101. // Append Undos to History
  102. var undos = [];
  103. for ( var i = 0 ; i < this.undos.length; i++ ) {
  104. var cmd = this.undos[ i ];
  105. if ( cmd.serialized ) {
  106. undos.push( cmd ); // add without serializing
  107. } else {
  108. undos.push( cmd.toJSON() );
  109. }
  110. }
  111. history.undos = undos;
  112. // Append Redos to History
  113. var redos = [];
  114. for ( var i = 0 ; i < this.redos.length; i++ ) {
  115. var cmd = this.redos[ i ];
  116. if ( cmd.serialized ) {
  117. redos.push( cmd ); // add without serializing
  118. } else {
  119. redos.push( cmd.toJSON() );
  120. }
  121. }
  122. history.redos = redos;
  123. return history;
  124. },
  125. fromJSON: function ( json ) {
  126. if ( json === undefined ) return;
  127. for ( var i = 0; i < json.undos.length ; i++ ) {
  128. json.undos[ i ].serialized = true;
  129. this.undos.push( json.undos[ i ] );
  130. this.idCounter = json.undos[ i ].id > this.idCounter ? json.undos[ i ].id : this.idCounter; // set last used idCounter
  131. }
  132. for ( var i = 0; i < json.redos.length ; i++ ) {
  133. json.redos[ i ].serialized = true;
  134. this.redos.push( json.redos[ i ] );
  135. this.idCounter = json.redos[ i ].id > this.idCounter ? json.redos[ i ].id : this.idCounter; // set last used idCounter
  136. }
  137. this.editor.signals.historyChanged.dispatch();
  138. },
  139. clear: function () {
  140. this.undos = [];
  141. this.redos = [];
  142. this.idCounter = 0;
  143. this.editor.signals.historyChanged.dispatch();
  144. },
  145. goToState: function ( id ) {
  146. if ( this.historyDisabled ) {
  147. alert("Undo/Redo disabled while scene is playing.");
  148. return;
  149. }
  150. this.editor.signals.sceneGraphChanged.active = false;
  151. this.editor.signals.historyChanged.active = false;
  152. var cmd = this.undos.length > 0 ? this.undos[ this.undos.length - 1 ] : undefined; // next cmd to pop
  153. if ( cmd === undefined || id > cmd.id ) {
  154. cmd = this.redo();
  155. while ( id > cmd.id ) {
  156. cmd = this.redo();
  157. }
  158. } else {
  159. while ( true ) {
  160. cmd = this.undos[ this.undos.length - 1 ]; // next cmd to pop
  161. if ( cmd === undefined || id === cmd.id ) break;
  162. cmd = this.undo();
  163. }
  164. }
  165. this.editor.signals.sceneGraphChanged.active = true;
  166. this.editor.signals.historyChanged.active = true;
  167. this.editor.signals.sceneGraphChanged.dispatch();
  168. this.editor.signals.historyChanged.dispatch( cmd );
  169. }
  170. };
粤ICP备19079148号