-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editor: Creates stub command during restore #28398
Conversation
I think it's better to actually update the commands. I appreciate your effort but this PR feels like we work around the actual issue (which eventually remains unsolved). E.g. import { Command } from '../Command.js';
import { ObjectLoader } from 'three';
/**
* @param editor Editor
* @param object THREE.Object3D
* @constructor
*/
class RemoveObjectCommand extends Command {
constructor( editor, object = null ) {
super( editor );
this.type = 'RemoveObjectCommand';
if ( object !== null ) {
this.name = editor.strings.getKey( 'command/RemoveObject' ) + ': ' + object.name;
this.object = object;
this.parent = ( object !== undefined ) ? object.parent : undefined;
if ( this.parent !== undefined ) {
this.index = this.parent.children.indexOf( this.object );
}
}
}
execute() {
this.editor.removeObject( this.object );
this.editor.deselect();
}
undo() {
this.editor.addObject( this.object, this.parent, this.index );
this.editor.select( this.object );
}
toJSON() {
const output = super.toJSON( this );
output.object = this.object.toJSON();
output.index = this.index;
output.parentUuid = this.parent.uuid;
return output;
}
fromJSON( json ) {
super.fromJSON( json );
this.parent = this.editor.objectByUuid( json.parentUuid );
if ( this.parent === undefined ) {
this.parent = this.editor.scene;
}
this.index = json.index;
this.object = this.editor.objectByUuid( json.object.object.uuid );
if ( this.object === undefined ) {
const loader = new ObjectLoader();
this.object = loader.parse( json.object );
}
}
}
export { RemoveObjectCommand }; Notice how |
Thanks,
Your approach will make the constructor hard to reason about, for example SetMaterialMapCommand ...
... which has several parameters needed to be validated, so it will get rejected by @mrdoob for the same reason of using |
It is better to design command constructor that all mandatory parameters are indeed required, instead of making them optional just for satisfying the case of 'restoring', or else anyone trying to learn the current system would be confused, note that commands may have real optional parameters, e.g. SetPositionCommand. Rather, we should do the opposite, let the consumer, |
I'm afraid this is against the way we implement |
If the 'curve classes' is referring to: three.js/src/extras/core/CurvePath.js Line 244 in de367cb
then I can tell why 'curve classes' works and why it's not suitable for our case.... If we look at ctors of all curves carefully, we'll find that the only operator in used is assign However, in commands, as simple as SetColorCommand, there's not only assigning value, but also accessing property, calling function etc... which we've to guard them or else it will throw when constructing at runtime:
i.e. If we're using command ctor to construct a placeholder command, then its inevitable that we need if/else to guard those operations, my initial attempt was using
Good luck :D |
Related: #28345 ( issue 1 )
This PR solved issue 1 in #28345, by creating a stub command when commands get restored from json, in this way we bypassed the constructor. This approach is enlightened by #28360 (comment) :D
can close #28360