Sidebar.Material.ConstantProperty.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { UIRow, UISelect, UIText } from './libs/ui.js';
  2. import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
  3. function SidebarMaterialConstantProperty( editor, property, name, options ) {
  4. const signals = editor.signals;
  5. const container = new UIRow();
  6. container.add( new UIText( name ).setWidth( '90px' ) );
  7. const constant = new UISelect().setOptions( options ).onChange( onChange );
  8. container.add( constant );
  9. let object = null;
  10. let material = null;
  11. function onChange() {
  12. const value = parseInt( constant.getValue() );
  13. if ( material[ property ] !== value ) {
  14. editor.execute( new SetMaterialValueCommand( editor, object, property, value, 0 /* TODO: currentMaterialSlot */ ) );
  15. }
  16. }
  17. function update() {
  18. if ( object === null ) return;
  19. if ( object.material === undefined ) return;
  20. material = object.material;
  21. if ( property in material ) {
  22. constant.setValue( material[ property ] );
  23. container.setDisplay( '' );
  24. } else {
  25. container.setDisplay( 'none' );
  26. }
  27. }
  28. //
  29. signals.objectSelected.add( function ( selected ) {
  30. object = selected;
  31. update();
  32. } );
  33. signals.materialChanged.add( update );
  34. return container;
  35. }
  36. export { SidebarMaterialConstantProperty };