Sidebar.Material.NumberProperty.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { UINumber, UIRow, UIText } from './libs/ui.js';
  2. import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
  3. function SidebarMaterialNumberProperty( editor, property, name, range = [ - Infinity, Infinity ], precision = 2 ) {
  4. const signals = editor.signals;
  5. const container = new UIRow();
  6. container.add( new UIText( name ).setClass( 'Label' ) );
  7. const number = new UINumber().setWidth( '60px' ).setRange( range[ 0 ], range[ 1 ] ).setPrecision( precision ).onChange( onChange );
  8. container.add( number );
  9. let object = null;
  10. let materialSlot = null;
  11. let material = null;
  12. function onChange() {
  13. if ( material[ property ] !== number.getValue() ) {
  14. editor.execute( new SetMaterialValueCommand( editor, object, property, number.getValue(), materialSlot ) );
  15. }
  16. }
  17. function update( currentObject, currentMaterialSlot = 0 ) {
  18. object = currentObject;
  19. materialSlot = currentMaterialSlot;
  20. if ( object === null ) return;
  21. if ( object.material === undefined ) return;
  22. material = editor.getObjectMaterial( object, materialSlot );
  23. if ( property in material ) {
  24. number.setValue( material[ property ] );
  25. container.setDisplay( '' );
  26. } else {
  27. container.setDisplay( 'none' );
  28. }
  29. }
  30. //
  31. signals.objectSelected.add( update );
  32. signals.materialChanged.add( update );
  33. return container;
  34. }
  35. export { SidebarMaterialNumberProperty };