SetMaterialColorCommand.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @author dforrer / https://github.com/dforrer
  3. * Developed as part of a project at University of Applied Sciences and Arts Northwestern Switzerland (www.fhnw.ch)
  4. */
  5. import { Command } from '../Command.js';
  6. /**
  7. * @param editor Editor
  8. * @param object THREE.Object3D
  9. * @param attributeName string
  10. * @param newValue integer representing a hex color value
  11. * @constructor
  12. */
  13. var SetMaterialColorCommand = function ( editor, object, attributeName, newValue, materialSlot ) {
  14. Command.call( this, editor );
  15. this.type = 'SetMaterialColorCommand';
  16. this.name = 'Set Material.' + attributeName;
  17. this.updatable = true;
  18. this.object = object;
  19. this.material = this.editor.getObjectMaterial( object, materialSlot );
  20. this.oldValue = ( this.material !== undefined ) ? this.material[ attributeName ].getHex() : undefined;
  21. this.newValue = newValue;
  22. this.attributeName = attributeName;
  23. };
  24. SetMaterialColorCommand.prototype = {
  25. execute: function () {
  26. this.material[ this.attributeName ].setHex( this.newValue );
  27. this.editor.signals.materialChanged.dispatch( this.material );
  28. },
  29. undo: function () {
  30. this.material[ this.attributeName ].setHex( this.oldValue );
  31. this.editor.signals.materialChanged.dispatch( this.material );
  32. },
  33. update: function ( cmd ) {
  34. this.newValue = cmd.newValue;
  35. },
  36. toJSON: function () {
  37. var output = Command.prototype.toJSON.call( this );
  38. output.objectUuid = this.object.uuid;
  39. output.attributeName = this.attributeName;
  40. output.oldValue = this.oldValue;
  41. output.newValue = this.newValue;
  42. return output;
  43. },
  44. fromJSON: function ( json ) {
  45. Command.prototype.fromJSON.call( this, 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. }
  51. };
  52. export { SetMaterialColorCommand };