Преглед изворни кода

Editor: Added rudimentary undo/redo system.

Mr.doob пре 10 година
родитељ
комит
2bf0d0b675
4 измењених фајлова са 115 додато и 0 уклоњено
  1. 1 0
      editor/index.html
  2. 3 0
      editor/js/Editor.js
  3. 83 0
      editor/js/History.js
  4. 28 0
      editor/js/Menubar.Edit.js

+ 1 - 0
editor/index.html

@@ -83,6 +83,7 @@
 
 		<script src="js/Editor.js"></script>
 		<script src="js/Config.js"></script>
+		<script src="js/History.js"></script>
 		<script src="js/Loader.js"></script>
 		<script src="js/Menubar.js"></script>
 		<script src="js/Menubar.File.js"></script>

+ 3 - 0
editor/js/Editor.js

@@ -70,6 +70,7 @@ var Editor = function () {
 	};
 
 	this.config = new Config();
+	this.history = new History( this );
 	this.storage = new Storage();
 	this.loader = new Loader( this );
 
@@ -411,6 +412,8 @@ Editor.prototype = {
 
 	clear: function () {
 
+		this.history.clear();
+
 		this.camera.position.set( 500, 250, 500 );
 		this.camera.lookAt( new THREE.Vector3() );
 

+ 83 - 0
editor/js/History.js

@@ -0,0 +1,83 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+var History = function ( editor ) {
+
+	this.array = [];
+	this.arrayLength = -1;
+
+	this.current = -1;
+	this.isRecording = true;
+
+	//
+
+	var scope = this;
+	var signals = editor.signals;
+
+	signals.objectAdded.add( function ( object ) {
+
+		if ( scope.isRecording === false ) return;
+
+		scope.add(
+			function () {
+				editor.removeObject( object );
+				editor.select( null );
+			},
+			function () {
+				editor.addObject( object );
+				editor.select( object );
+			}
+		);
+
+	} );
+
+};
+
+History.prototype = {
+
+	add: function ( undo, redo ) {
+
+		this.current ++;
+
+		this.array[ this.current ] = { undo: undo, redo: redo };
+		this.arrayLength = this.current;
+
+	},
+
+	undo: function () {
+
+		if ( this.current < 0 ) return;
+
+		this.isRecording = false;
+
+		this.array[ this.current ].undo();
+		this.current --;
+
+		this.isRecording = true;
+
+	},
+
+	redo: function () {
+
+		if ( this.current === this.arrayLength ) return;
+
+		this.isRecording = false;
+
+		this.current ++;
+		this.array[ this.current ].redo();
+
+		this.isRecording = true;
+
+	},
+
+	clear: function () {
+
+		this.array = [];
+		this.arrayLength = -1;
+
+		this.current = -1;
+
+	}
+
+};

+ 28 - 0
editor/js/Menubar.Edit.js

@@ -16,6 +16,34 @@ Menubar.Edit = function ( editor ) {
 	options.setClass( 'options' );
 	container.add( options );
 
+	// Undo
+
+	var option = new UI.Panel();
+	option.setClass( 'option' );
+	option.setTextContent( 'Undo' );
+	option.onClick( function () {
+
+		editor.history.undo();
+
+	} );
+	options.add( option );
+
+	// Redo
+
+	var option = new UI.Panel();
+	option.setClass( 'option' );
+	option.setTextContent( 'Redo' );
+	option.onClick( function () {
+
+		editor.history.redo();
+
+	} );
+	options.add( option );
+
+	// ---
+
+	options.add( new UI.HorizontalRule() );
+
 	// Clone
 
 	var option = new UI.Panel();