SetMaterialVectorCommand.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Command } from '../Command.js';
  2. class SetMaterialVectorCommand extends Command {
  3. constructor( editor, object = null, attributeName = '', newValue = null, materialSlot = - 1 ) {
  4. super( editor );
  5. this.type = 'SetMaterialVectorCommand';
  6. this.name = editor.strings.getKey( 'command/SetMaterialVector' ) + ': ' + attributeName;
  7. this.updatable = true;
  8. this.object = object;
  9. this.materialSlot = materialSlot;
  10. const material = ( object !== null ) ? editor.getObjectMaterial( object, materialSlot ) : null;
  11. this.oldValue = ( material !== null ) ? material[ attributeName ].toArray() : null;
  12. this.newValue = newValue;
  13. this.attributeName = attributeName;
  14. }
  15. execute() {
  16. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  17. material[ this.attributeName ].fromArray( this.newValue );
  18. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  19. }
  20. undo() {
  21. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  22. material[ this.attributeName ].fromArray( this.oldValue );
  23. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  24. }
  25. update( cmd ) {
  26. this.newValue = cmd.newValue;
  27. }
  28. toJSON() {
  29. const output = super.toJSON( this );
  30. output.objectUuid = this.object.uuid;
  31. output.attributeName = this.attributeName;
  32. output.oldValue = this.oldValue;
  33. output.newValue = this.newValue;
  34. output.materialSlot = this.materialSlot;
  35. return output;
  36. }
  37. fromJSON( json ) {
  38. super.fromJSON( json );
  39. this.object = this.editor.objectByUuid( json.objectUuid );
  40. this.attributeName = json.attributeName;
  41. this.oldValue = json.oldValue;
  42. this.newValue = json.newValue;
  43. this.materialSlot = json.materialSlot;
  44. }
  45. }
  46. export { SetMaterialVectorCommand };