SetMaterialColorCommand.js 1.7 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. function SetMaterialColorCommand( editor, object, attributeName, newValue, materialSlot ) {
  10. Command.call( this, editor );
  11. this.type = 'SetMaterialColorCommand';
  12. this.name = 'Set Material.' + attributeName;
  13. this.updatable = true;
  14. this.object = object;
  15. this.material = this.editor.getObjectMaterial( object, materialSlot );
  16. this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ].getHex() : undefined;
  17. this.newValue = newValue;
  18. this.attributeName = attributeName;
  19. }
  20. SetMaterialColorCommand.prototype = {
  21. execute: function () {
  22. this.material[ this.attributeName ].setHex( this.newValue );
  23. this.editor.signals.materialChanged.dispatch( this.material );
  24. },
  25. undo: function () {
  26. this.material[ this.attributeName ].setHex( this.oldValue );
  27. this.editor.signals.materialChanged.dispatch( this.material );
  28. },
  29. update: function ( cmd ) {
  30. this.newValue = cmd.newValue;
  31. },
  32. toJSON: function () {
  33. var output = Command.prototype.toJSON.call( 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: function ( json ) {
  41. Command.prototype.fromJSON.call( this, 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 };