提交 63ec9dbf 编写于 作者: D Daniel

Reimplemented Undo/Redo for Scripts-Editor

Issues #29, #28

Removed CmdSetScriptName and CmdSetScriptSource => merged into
CmdSetScriptValue
上级 4cd5a7dd
......@@ -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>
......
/**
* 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 ];
}
};
......@@ -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;
......
......@@ -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;
......
......@@ -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;
};
......@@ -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 );
......
......@@ -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 ) {
......
......@@ -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>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册