Jelajahi Sumber

Reimplemented Undo/Redo for Scripts-Editor

Issues #29, #28

Removed CmdSetScriptName and CmdSetScriptSource => merged into
CmdSetScriptValue
Daniel 10 tahun lalu
induk
melakukan
63ec9dbfa8

+ 1 - 2
editor/index.html

@@ -134,8 +134,7 @@
 		<script src="js/CmdMultiCmds.js"></script>
 		<script src="js/CmdAddScript.js"></script>
 		<script src="js/CmdRemoveScript.js"></script>
-		<script src="js/CmdSetScriptName.js"></script>
-		<script src="js/CmdSetScriptSource.js"></script>
+		<script src="js/CmdSetScriptValue.js"></script>
 		<script src="js/CmdSetMaterialValue.js"></script>
 		<script src="js/CmdSetScene.js"></script>
 

+ 0 - 66
editor/js/CmdSetScriptName.js

@@ -1,66 +0,0 @@
-/**
- * Created by Daniel on 21.07.15.
- */
-
-CmdSetScriptName = function ( object, script, name ) {
-
-	Cmd.call( this );
-
-	this.type = 'CmdSetScriptName';
-
-	this.object = object;
-	this.script = script;
-	this.oldName = script !== undefined ? script.name : undefined;
-	this.newName = name;
-	this.objectUuid = object !== undefined ? object.uuid : undefined;
-
-};
-
-CmdSetScriptName.prototype = {
-
-	execute: function () {
-
-		this.index = this.editor.scripts[ this.objectUuid ].indexOf( this.script );
-		this.script.name = this.newName;
-
-		this.editor.signals.scriptChanged.dispatch();
-		this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script );
-
-	},
-
-	undo: function () {
-
-		this.script.name = this.oldName;
-
-		this.editor.signals.scriptChanged.dispatch();
-		this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script );
-
-	},
-
-	toJSON: function () {
-
-		var output = Cmd.prototype.toJSON.call( this );
-
-		output.objectUuid = this.objectUuid;
-		output.index = this.index;
-		output.oldName = this.oldName;
-		output.newName = this.newName;
-
-		return output;
-
-	},
-
-	fromJSON: function ( json ) {
-
-		Cmd.prototype.fromJSON.call( this, json );
-
-		this.objectUuid = json.objectUuid;
-		this.index = json.index;
-		this.oldName = json.oldName;
-		this.newName = json.newName;
-		this.object = this.editor.objectByUuid( json.objectUuid );
-		this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
-
-	}
-
-};

+ 24 - 13
editor/js/CmdSetScriptSource.js → editor/js/CmdSetScriptValue.js

@@ -2,50 +2,60 @@
  * Created by Daniel on 21.07.15.
  */
 
-CmdSetScriptSource = function ( object, script, source, oldSource, cursorPosition ) {
+CmdSetScriptValue = function ( object, script, attributeName, newValue, cursorPosition ) {
 
 	Cmd.call( this );
 
-	this.type = 'CmdSetScriptSource';
+	this.type = 'CmdSetScriptValue';
+	this.updatable = true;
 
 	this.object = object;
 	this.script = script;
-	this.oldSource = oldSource !== undefined ? oldSource : undefined;
-	this.newSource = source;
+	this.attributeName = attributeName;
+	this.oldValue = script !== undefined ? script[ this.attributeName ] : undefined;
+	this.newValue = newValue;
 	this.objectUuid = object !== undefined ? object.uuid : undefined;
 	this.cursorPosition = cursorPosition; // Format {line: 2, ch: 3}
 
 };
 
-CmdSetScriptSource.prototype = {
+CmdSetScriptValue.prototype = {
 
 	execute: function () {
 
 		this.index = this.editor.scripts[ this.objectUuid ].indexOf( this.script );
-		this.script.source = this.newSource;
+		this.script[ this.attributeName ] = this.newValue;
 
-		this.editor.signals.scriptChanged.dispatch( this.script );
+		this.editor.signals.scriptChanged.dispatch();
 		this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
 
 	},
 
 	undo: function () {
 
-		this.script.source = this.oldSource;
+		this.script[ this.attributeName ] = this.oldValue;
 
-		this.editor.signals.scriptChanged.dispatch( this.script );
+		this.editor.signals.scriptChanged.dispatch();
 		this.editor.signals.refreshScriptEditor.dispatch( this.object, this.script, this.cursorPosition );
 
 	},
 
+	update: function ( cmd ) {
+
+		this.cursorPosition = cmd.cursorPosition;
+		this.newValue = cmd.newValue;
+
+	},
+
 	toJSON: function () {
 
 		var output = Cmd.prototype.toJSON.call( this );
 
 		output.objectUuid = this.objectUuid;
 		output.index = this.index;
-		output.oldSource = this.oldSource;
-		output.newSource = this.newSource;
+		output.attributeName = this.attributeName;
+		output.oldValue = this.oldValue;
+		output.newValue = this.newValue;
 		output.cursorPosition = this.cursorPosition;
 
 		return output;
@@ -58,8 +68,9 @@ CmdSetScriptSource.prototype = {
 
 		this.objectUuid = json.objectUuid;
 		this.index = json.index;
-		this.oldSource = json.oldSource;
-		this.newSource = json.newSource;
+		this.oldValue = json.oldValue;
+		this.newValue = json.newValue;
+		this.attributeName = json.attributeName;
 		this.object = this.editor.objectByUuid( json.objectUuid );
 		this.script = this.editor.scripts[ json.objectUuid ][ json.index ];
 		this.cursorPosition = json.cursorPosition;

+ 13 - 4
editor/js/History.js

@@ -20,13 +20,22 @@ History.prototype = {
 		var lastCmd = this.undos[ this.undos.length - 1 ];
 		var timeDifference = new Date().getTime() - this.lastCmdTime.getTime();
 
-		if ( lastCmd != null &&
+		var isScriptCmd = lastCmd &&
 			lastCmd.updatable &&
+			lastCmd.script !== undefined &&
 			lastCmd.object === cmd.object &&
-			lastCmd.type == cmd.type &&
-			timeDifference < 500 ) {
+			lastCmd.type === cmd.type &&
+			lastCmd.script === cmd.script &&
+			lastCmd.attributeName === cmd.attributeName;
+
+		var isUpdatableCmd = lastCmd &&
+			lastCmd.updatable &&
+			lastCmd.object === cmd.object &&
+			lastCmd.type === cmd.type &&
+			timeDifference < 500;
+
+		if ( isScriptCmd || isUpdatableCmd ) {
 
-			// command objects have the same type and are less than 0.5 second apart
 			lastCmd.update( cmd );
 			cmd = lastCmd;
 

+ 33 - 2
editor/js/Script.js

@@ -80,8 +80,11 @@ var Script = function ( editor ) {
 
 			if ( typeof( currentScript ) === 'object' ) {
 
-				currentScript.source = value;
-				signals.scriptChanged.dispatch( currentScript );
+				if ( value !== currentScript.source ) {
+
+					editor.execute( new CmdSetScriptValue( currentObject, currentScript, 'source', value, codemirror.getCursor() ) );
+
+				}
 				return;
 			}
 
@@ -390,6 +393,34 @@ var Script = function ( editor ) {
 
 	} );
 
+	signals.scriptRemoved.add( function ( script ) {
+
+		if ( currentScript === script ) {
+
+			container.setDisplay( 'none' );
+
+		}
+
+	} );
+
+	signals.refreshScriptEditor.add( function ( object, script, cursorPosition ) {
+
+		// Check if editor is being displayed
+		//...
+
+		if ( currentScript !== script ) return;
+
+		title.setValue( object.name + ' / ' + script.name );
+		codemirror.setValue( script.source );
+
+		if ( cursorPosition !== undefined ) {
+
+			codemirror.setCursor( cursorPosition );
+
+		}
+
+	} );
+
 	return container;
 
 };

+ 1 - 1
editor/js/Sidebar.Script.js

@@ -63,7 +63,7 @@ Sidebar.Script = function ( editor ) {
 					var name = new UI.Input( script.name ).setWidth( '130px' ).setFontSize( '12px' );
 					name.onChange( function () {
 
-						editor.execute( new CmdSetScriptName( editor.selected, script, this.getValue() ) );
+						editor.execute( new CmdSetScriptValue( editor.selected, script, 'name', this.getValue() ) );
 
 					} );
 					scriptsContainer.add( name );

+ 6 - 2
editor/js/Viewport.js

@@ -405,8 +405,12 @@ var Viewport = function ( editor ) {
 
 	signals.objectChanged.add( function ( object ) {
 
-		selectionBox.update( object );
-		transformControls.update();
+		if ( editor.selected === object ) {
+
+			selectionBox.update( object );
+			transformControls.update();
+
+		}
 
 		if ( object instanceof THREE.PerspectiveCamera ) {
 

+ 1 - 2
test/unit/unittests_editor.html

@@ -83,8 +83,7 @@
 <script src="../../editor/js/CmdSetPosition.js"></script>
 <script src="../../editor/js/CmdSetRotation.js"></script>
 <script src="../../editor/js/CmdSetScale.js"></script>
-<script src="../../editor/js/CmdSetScriptName.js"></script>
-<script src="../../editor/js/CmdSetScriptSource.js"></script>
+<script src="../../editor/js/CmdSetScriptValue.js"></script>
 <script src="../../editor/js/CmdSetUuid.js"></script>
 <script src="../../editor/js/CmdSetValue.js"></script>
 <script src="../../editor/js/CmdToggleBoolean.js"></script>