Sidebar.Material.NumberProperty.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ] ) {
  4. const signals = editor.signals;
  5. const container = new UIRow();
  6. container.add( new UIText( name ).setWidth( '90px' ) );
  7. const number = new UINumber().setWidth( '60px' ).setRange( range[ 0 ], range[ 1 ] ).onChange( onChange );
  8. container.add( number );
  9. let object = null;
  10. let material = null;
  11. function onChange() {
  12. if ( material[ property ] !== number.getValue() ) {
  13. editor.execute( new SetMaterialValueCommand( editor, object, property, number.getValue(), 0 /* TODO: currentMaterialSlot */ ) );
  14. }
  15. }
  16. function update() {
  17. if ( object === null ) return;
  18. if ( object.material === undefined ) return;
  19. material = object.material;
  20. if ( property in material ) {
  21. number.setValue( material[ property ] );
  22. container.setDisplay( '' );
  23. } else {
  24. container.setDisplay( 'none' );
  25. }
  26. }
  27. //
  28. signals.objectSelected.add( function ( selected ) {
  29. object = selected;
  30. update();
  31. } );
  32. signals.materialChanged.add( update );
  33. return container;
  34. }
  35. export { SidebarMaterialNumberProperty };