SetMaterialColorCommand.js 1.8 KB

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