SetMaterialColorCommand.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Command } from '../Command.js';
  2. /**
  3. * @param editor Editor
  4. * @param object THREE.Object3D
  5. * @param attributeName string
  6. * @param newValue integer representing a hex color value
  7. * @constructor
  8. */
  9. class SetMaterialColorCommand extends Command {
  10. constructor( editor, object, attributeName, newValue, materialSlot ) {
  11. super( editor );
  12. this.type = 'SetMaterialColorCommand';
  13. this.name = `Set Material.${attributeName}`;
  14. this.updatable = true;
  15. this.object = object;
  16. this.materialSlot = materialSlot;
  17. this.material = ( this.object !== undefined ) ? this.editor.getObjectMaterial( object, materialSlot ) : undefined;
  18. this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ].getHex() : undefined;
  19. this.newValue = newValue;
  20. this.attributeName = attributeName;
  21. }
  22. execute() {
  23. this.material[ this.attributeName ].setHex( this.newValue );
  24. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  25. }
  26. undo() {
  27. this.material[ this.attributeName ].setHex( this.oldValue );
  28. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  29. }
  30. update( cmd ) {
  31. this.newValue = cmd.newValue;
  32. }
  33. toJSON() {
  34. const output = super.toJSON( this );
  35. output.objectUuid = this.object.uuid;
  36. output.attributeName = this.attributeName;
  37. output.oldValue = this.oldValue;
  38. output.newValue = this.newValue;
  39. return output;
  40. }
  41. fromJSON( json ) {
  42. super.fromJSON( json );
  43. this.object = this.editor.objectByUuid( json.objectUuid );
  44. this.attributeName = json.attributeName;
  45. this.oldValue = json.oldValue;
  46. this.newValue = json.newValue;
  47. }
  48. }
  49. export { SetMaterialColorCommand };