SetColorCommand.js 1.5 KB

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