Sidebar.Material.RangeValueProperty.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 ).setClass( 'Label' ) );
  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 materialSlot = null;
  11. let material = null;
  12. function onChange() {
  13. if ( material[ property ][ isMin ? 0 : 1 ] !== number.getValue() ) {
  14. const minValue = isMin ? number.getValue() : material[ property ][ 0 ];
  15. const maxValue = isMin ? material[ property ][ 1 ] : number.getValue();
  16. editor.execute( new SetMaterialRangeCommand( editor, object, property, minValue, maxValue, materialSlot ) );
  17. }
  18. }
  19. function update( currentObject, currentMaterialSlot = 0 ) {
  20. object = currentObject;
  21. materialSlot = currentMaterialSlot;
  22. if ( object === null ) return;
  23. if ( object.material === undefined ) return;
  24. material = editor.getObjectMaterial( object, materialSlot );
  25. if ( property in material ) {
  26. number.setValue( material[ property ][ isMin ? 0 : 1 ] );
  27. container.setDisplay( '' );
  28. } else {
  29. container.setDisplay( 'none' );
  30. }
  31. }
  32. //
  33. signals.objectSelected.add( update );
  34. signals.materialChanged.add( update );
  35. return container;
  36. }
  37. export { SidebarMaterialRangeValueProperty };