SetMaterialColorCommand.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 = null, attributeName = '', newValue = null, materialSlot = - 1 ) {
  11. super( editor );
  12. this.type = 'SetMaterialColorCommand';
  13. this.name = editor.strings.getKey( 'command/SetMaterialColor' ) + ': ' + attributeName;
  14. this.updatable = true;
  15. this.object = object;
  16. this.materialSlot = materialSlot;
  17. const material = ( object !== null ) ? editor.getObjectMaterial( object, materialSlot ) : null;
  18. this.oldValue = ( material !== null ) ? material[ attributeName ].getHex() : null;
  19. this.newValue = newValue;
  20. this.attributeName = attributeName;
  21. }
  22. execute() {
  23. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  24. material[ this.attributeName ].setHex( this.newValue );
  25. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  26. }
  27. undo() {
  28. const material = this.editor.getObjectMaterial( this.object, this.materialSlot );
  29. material[ this.attributeName ].setHex( this.oldValue );
  30. this.editor.signals.materialChanged.dispatch( this.object, this.materialSlot );
  31. }
  32. update( cmd ) {
  33. this.newValue = cmd.newValue;
  34. }
  35. toJSON() {
  36. const output = super.toJSON( this );
  37. output.objectUuid = this.object.uuid;
  38. output.attributeName = this.attributeName;
  39. output.oldValue = this.oldValue;
  40. output.newValue = this.newValue;
  41. output.materialSlot = this.materialSlot;
  42. return output;
  43. }
  44. fromJSON( json ) {
  45. super.fromJSON( json );
  46. this.object = this.editor.objectByUuid( json.objectUuid );
  47. this.attributeName = json.attributeName;
  48. this.oldValue = json.oldValue;
  49. this.newValue = json.newValue;
  50. this.materialSlot = json.materialSlot;
  51. }
  52. }
  53. export { SetMaterialColorCommand };