Sidebar.Material.ConstantProperty.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 ).setClass( 'Label' ) );
  7. const constant = new UISelect().setOptions( options ).onChange( onChange );
  8. container.add( constant );
  9. let object = null;
  10. let materialSlot = null;
  11. let material = null;
  12. function onChange() {
  13. const value = parseInt( constant.getValue() );
  14. if ( material[ property ] !== value ) {
  15. editor.execute( new SetMaterialValueCommand( editor, object, property, value, materialSlot ) );
  16. }
  17. }
  18. function update( currentObject, currentMaterialSlot = 0 ) {
  19. object = currentObject;
  20. materialSlot = currentMaterialSlot;
  21. if ( object === null ) return;
  22. if ( object.material === undefined ) return;
  23. material = editor.getObjectMaterial( object, materialSlot );
  24. if ( property in material ) {
  25. constant.setValue( material[ property ] );
  26. container.setDisplay( '' );
  27. } else {
  28. container.setDisplay( 'none' );
  29. }
  30. }
  31. //
  32. signals.objectSelected.add( update );
  33. signals.materialChanged.add( update );
  34. return container;
  35. }
  36. export { SidebarMaterialConstantProperty };