SetMaterialColorCommand.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  6. * @param object THREE.Object3D
  7. * @param attributeName string
  8. * @param newValue integer representing a hex color value
  9. * @constructor
  10. */
  11. var SetMaterialColorCommand = function ( object, attributeName, newValue, slot ) {
  12. Command.call( this );
  13. this.type = 'SetMaterialColorCommand';
  14. this.name = 'Set Material.' + attributeName;
  15. this.updatable = true;
  16. this.object = object;
  17. this.attributeName = attributeName;
  18. this.slot = slot;
  19. var material = this.editor.getObjectMaterial( this.object, this.slot );
  20. this.oldValue = ( material !== undefined ) ? material[ this.attributeName ].getHex() : undefined;
  21. this.newValue = newValue;
  22. };
  23. SetMaterialColorCommand.prototype = {
  24. execute: function () {
  25. var material = this.editor.getObjectMaterial( this.object, this.slot )
  26. material[ this.attributeName ].setHex( this.newValue );
  27. this.editor.signals.materialChanged.dispatch( material );
  28. },
  29. undo: function () {
  30. var material = this.editor.getObjectMaterial( this.object, this.slot )
  31. material[ this.attributeName ].setHex( this.oldValue );
  32. this.editor.signals.materialChanged.dispatch( material );
  33. },
  34. update: function ( cmd ) {
  35. this.newValue = cmd.newValue;
  36. },
  37. toJSON: function () {
  38. var output = Command.prototype.toJSON.call( this );
  39. output.objectUuid = this.object.uuid;
  40. output.attributeName = this.attributeName;
  41. output.oldValue = this.oldValue;
  42. output.newValue = this.newValue;
  43. return output;
  44. },
  45. fromJSON: function ( json ) {
  46. Command.prototype.fromJSON.call( this, json );
  47. this.object = this.editor.objectByUuid( json.objectUuid );
  48. this.attributeName = json.attributeName;
  49. this.oldValue = json.oldValue;
  50. this.newValue = json.newValue;
  51. }
  52. };