Sidebar.Material.RangeValueProperty.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { UINumber, UIRow, UIText } from './libs/ui.js';
  2. import { SetMaterialRangeCommand } from './commands/SetMaterialRangeCommand.js';
  3. function SidebarMaterialRangeValueProperty( editor, property, name, isMin, range = [ - Infinity, Infinity ], precision = 2, step = 1, nudge = 0.01, unit = '' ) {
  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 ] ).setPrecision( precision ).setStep( step ).setNudge( nudge ).setUnit( unit ).onChange( onChange );
  8. container.add( number );
  9. let object = null;
  10. let material = null;
  11. function onChange() {
  12. if ( material[ property ][ isMin ? 0 : 1 ] !== number.getValue() ) {
  13. const minValue = isMin ? number.getValue() : material[ property ][ 0 ];
  14. const maxValue = isMin ? material[ property ][ 1 ] : number.getValue();
  15. editor.execute( new SetMaterialRangeCommand( editor, object, property, minValue, maxValue, 0 /* TODO: currentMaterialSlot */ ) );
  16. }
  17. }
  18. function update() {
  19. if ( object === null ) return;
  20. if ( object.material === undefined ) return;
  21. material = object.material;
  22. if ( property in material ) {
  23. number.setValue( material[ property ][ isMin ? 0 : 1 ] );
  24. container.setDisplay( '' );
  25. } else {
  26. container.setDisplay( 'none' );
  27. }
  28. }
  29. //
  30. signals.objectSelected.add( function ( selected ) {
  31. object = selected;
  32. update();
  33. } );
  34. signals.materialChanged.add( update );
  35. return container;
  36. }
  37. export { SidebarMaterialRangeValueProperty };