SetMaterialValueCommand.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 number, string, boolean or object
  9. * @constructor
  10. */
  11. var SetMaterialValueCommand = function ( object, attributeName, newValue ) {
  12. Command.call( this );
  13. this.type = 'SetMaterialValueCommand';
  14. this.name = 'Set Material.' + attributeName;
  15. this.updatable = true;
  16. this.object = object;
  17. this.oldValue = ( object !== undefined ) ? object.material[ attributeName ] : undefined;
  18. this.newValue = newValue;
  19. this.attributeName = attributeName;
  20. };
  21. SetMaterialValueCommand.prototype = {
  22. execute: function () {
  23. this.object.material[ this.attributeName ] = this.newValue;
  24. this.object.material.needsUpdate = true;
  25. this.editor.signals.objectChanged.dispatch( this.object );
  26. this.editor.signals.materialChanged.dispatch( this.object.material );
  27. },
  28. undo: function () {
  29. this.object.material[ this.attributeName ] = this.oldValue;
  30. this.object.material.needsUpdate = true;
  31. this.editor.signals.objectChanged.dispatch( this.object );
  32. this.editor.signals.materialChanged.dispatch( this.object.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.attributeName = json.attributeName;
  48. this.oldValue = json.oldValue;
  49. this.newValue = json.newValue;
  50. this.object = this.editor.objectByUuid( json.objectUuid );
  51. }
  52. };