SetMaterialColorCommand.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.material = ( this.object !== undefined ) ? this.editor.getObjectMaterial( object, materialSlot ) : undefined;
  17. this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ].getHex() : undefined;
  18. this.newValue = newValue;
  19. this.attributeName = attributeName;
  20. }
  21. execute() {
  22. this.material[ this.attributeName ].setHex( this.newValue );
  23. this.editor.signals.materialChanged.dispatch( this.material );
  24. }
  25. undo() {
  26. this.material[ this.attributeName ].setHex( this.oldValue );
  27. this.editor.signals.materialChanged.dispatch( this.material );
  28. }
  29. update( cmd ) {
  30. this.newValue = cmd.newValue;
  31. }
  32. toJSON() {
  33. const output = super.toJSON( this );
  34. output.objectUuid = this.object.uuid;
  35. output.attributeName = this.attributeName;
  36. output.oldValue = this.oldValue;
  37. output.newValue = this.newValue;
  38. return output;
  39. }
  40. fromJSON( json ) {
  41. super.fromJSON( json );
  42. this.object = this.editor.objectByUuid( json.objectUuid );
  43. this.attributeName = json.attributeName;
  44. this.oldValue = json.oldValue;
  45. this.newValue = json.newValue;
  46. }
  47. }
  48. export { SetMaterialColorCommand };