Skip to content

Commit

Permalink
Editor: Moved Materials panel to its own file.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Feb 7, 2021
1 parent 794af18 commit 76ac160
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 86 deletions.
82 changes: 82 additions & 0 deletions editor/js/Sidebar.Project.Materials.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { UIBreak, UIPanel, UIRow, UIText, UIListbox, UIButton } from './libs/ui.js';

import { SetMaterialCommand } from './commands/SetMaterialCommand.js';

function SidebarProjectMaterials( editor ) {

var signals = editor.signals;
var strings = editor.strings;

var container = new UIPanel();

var headerRow = new UIRow();
headerRow.add( new UIText( strings.getKey( 'sidebar/project/materials' ).toUpperCase() ) );

container.add( headerRow );

var listbox = new UIListbox();
container.add( listbox );

container.add( new UIBreak() );

var buttonsRow = new UIRow();
container.add( buttonsRow );

var assignMaterial = new UIButton().setLabel( strings.getKey( 'sidebar/project/Assign' ) );
assignMaterial.onClick( function () {

var selectedObject = editor.selected;

if ( selectedObject !== null ) {

var oldMaterial = selectedObject.material;

// only assing materials to objects with a material property (e.g. avoid assigning material to THREE.Group)

if ( oldMaterial !== undefined ) {

var material = editor.getMaterialById( parseInt( listbox.getValue() ) );

if ( material !== undefined ) {

editor.removeMaterial( oldMaterial );
editor.execute( new SetMaterialCommand( editor, selectedObject, material ) );
editor.addMaterial( material );

}

}

}

} );
buttonsRow.add( assignMaterial );

// Signals

function refreshMaterialBrowserUI() {

listbox.setItems( Object.values( editor.materials ) );

}

signals.objectSelected.add( function ( object ) {

if ( object !== null ) {

var index = Object.values( editor.materials ).indexOf( object.material );
listbox.selectIndex( index );

}

} );

signals.materialAdded.add( refreshMaterialBrowserUI );
signals.materialChanged.add( refreshMaterialBrowserUI );
signals.materialRemoved.add( refreshMaterialBrowserUI );

return container;

}

export { SidebarProjectMaterials };
90 changes: 4 additions & 86 deletions editor/js/Sidebar.Project.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { UIPanel, UIRow, UIInput, UICheckbox, UIText, UIListbox, UISpan, UIButton } from './libs/ui.js';
import { UIPanel, UIRow, UIInput, UICheckbox, UIText, UISpan } from './libs/ui.js';

import { SidebarProjectMaterials } from './Sidebar.Project.Materials.js';
import { SidebarProjectRenderer } from './Sidebar.Project.Renderer.js';

import { SetMaterialCommand } from './commands/SetMaterialCommand.js';

function SidebarProject( editor ) {

var config = editor.config;
var signals = editor.signals;
var strings = editor.strings;

var container = new UISpan();
Expand Down Expand Up @@ -60,91 +58,11 @@ function SidebarProject( editor ) {

projectsettings.add( vrRow );

// Renderer
//

container.add( new SidebarProjectMaterials( editor ) );
container.add( new SidebarProjectRenderer( editor ) );

// Materials

var materials = new UIPanel();

var headerRow = new UIRow();
headerRow.add( new UIText( strings.getKey( 'sidebar/project/materials' ).toUpperCase() ) );

materials.add( headerRow );

var listbox = new UIListbox();
materials.add( listbox );

var buttonsRow = new UIRow();
buttonsRow.setPadding( '10px 0px' );
materials.add( buttonsRow );

/*
var addButton = new UI.Button().setLabel( 'Add' ).setMarginRight( '5px' );
addButton.onClick( function () {
editor.addMaterial( new THREE.MeshStandardMaterial() );
} );
buttonsRow.add( addButton );
*/

var assignMaterial = new UIButton().setLabel( strings.getKey( 'sidebar/project/Assign' ) ).setMargin( '0px 5px' );
assignMaterial.onClick( function () {

var selectedObject = editor.selected;

if ( selectedObject !== null ) {

var oldMaterial = selectedObject.material;

// only assing materials to objects with a material property (e.g. avoid assigning material to THREE.Group)

if ( oldMaterial !== undefined ) {

var material = editor.getMaterialById( parseInt( listbox.getValue() ) );

if ( material !== undefined ) {

editor.removeMaterial( oldMaterial );
editor.execute( new SetMaterialCommand( editor, selectedObject, material ) );
editor.addMaterial( material );

}

}

}

} );
buttonsRow.add( assignMaterial );

container.add( materials );

// signals

signals.objectSelected.add( function ( object ) {

if ( object !== null ) {

var index = Object.values( editor.materials ).indexOf( object.material );
listbox.selectIndex( index );

}

} );

signals.materialAdded.add( refreshMaterialBrowserUI );
signals.materialChanged.add( refreshMaterialBrowserUI );
signals.materialRemoved.add( refreshMaterialBrowserUI );

function refreshMaterialBrowserUI() {

listbox.setItems( Object.values( editor.materials ) );

}

return container;

}
Expand Down
1 change: 1 addition & 0 deletions editor/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ const assets = [
'./js/Sidebar.js',
'./js/Sidebar.Scene.js',
'./js/Sidebar.Project.js',
'./js/Sidebar.Project.Materials.js',
'./js/Sidebar.Project.Renderer.js',
'./js/Sidebar.Settings.js',
'./js/Sidebar.Settings.Shortcuts.js',
Expand Down

0 comments on commit 76ac160

Please sign in to comment.