1
0

Sidebar.Project.Materials.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { UIBreak, UIPanel, UIRow, UIText, UIListbox, UIButton } from './libs/ui.js';
  2. import { SetMaterialCommand } from './commands/SetMaterialCommand.js';
  3. function SidebarProjectMaterials( editor ) {
  4. const signals = editor.signals;
  5. const strings = editor.strings;
  6. const container = new UIPanel();
  7. const headerRow = new UIRow();
  8. headerRow.add( new UIText( strings.getKey( 'sidebar/project/materials' ).toUpperCase() ) );
  9. container.add( headerRow );
  10. const listbox = new UIListbox();
  11. container.add( listbox );
  12. container.add( new UIBreak() );
  13. const buttonsRow = new UIRow();
  14. container.add( buttonsRow );
  15. const assignMaterial = new UIButton( strings.getKey( 'sidebar/project/Assign' ) );
  16. assignMaterial.onClick( function () {
  17. const selectedObject = editor.selected;
  18. if ( selectedObject !== null ) {
  19. const oldMaterial = selectedObject.material;
  20. // only assing materials to objects with a material property (e.g. avoid assigning material to THREE.Group)
  21. if ( oldMaterial !== undefined ) {
  22. const material = editor.getMaterialById( parseInt( listbox.getValue() ) );
  23. if ( material !== undefined ) {
  24. editor.removeMaterial( oldMaterial );
  25. editor.execute( new SetMaterialCommand( editor, selectedObject, material ) );
  26. editor.addMaterial( material );
  27. }
  28. }
  29. }
  30. } );
  31. buttonsRow.add( assignMaterial );
  32. // Signals
  33. function refreshMaterialBrowserUI() {
  34. listbox.setItems( Object.values( editor.materials ) );
  35. }
  36. signals.objectSelected.add( function ( object ) {
  37. if ( object !== null ) {
  38. const index = Object.values( editor.materials ).indexOf( object.material );
  39. listbox.selectIndex( index );
  40. }
  41. } );
  42. signals.materialAdded.add( refreshMaterialBrowserUI );
  43. signals.materialChanged.add( refreshMaterialBrowserUI );
  44. signals.materialRemoved.add( refreshMaterialBrowserUI );
  45. return container;
  46. }
  47. export { SidebarProjectMaterials };