Viewport.Controls.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { UIPanel, UISelect } from './libs/ui.js';
  2. import { UIBoolean } from './libs/ui.three.js';
  3. function ViewportControls( editor ) {
  4. const signals = editor.signals;
  5. const strings = editor.strings;
  6. const container = new UIPanel();
  7. container.setPosition( 'absolute' );
  8. container.setRight( '10px' );
  9. container.setTop( '10px' );
  10. container.setColor( '#ffffff' );
  11. // grid
  12. const gridCheckbox = new UIBoolean( true, strings.getKey( 'viewport/controls/grid' ) );
  13. gridCheckbox.onChange( function () {
  14. signals.showGridChanged.dispatch( this.getValue() );
  15. } );
  16. container.add( gridCheckbox );
  17. // helpers
  18. const helpersCheckbox = new UIBoolean( true, strings.getKey( 'viewport/controls/helpers' ) );
  19. helpersCheckbox.onChange( function () {
  20. signals.showHelpersChanged.dispatch( this.getValue() );
  21. } );
  22. container.add( helpersCheckbox );
  23. // camera
  24. const cameraSelect = new UISelect();
  25. cameraSelect.setMarginLeft( '10px' );
  26. cameraSelect.setMarginRight( '10px' );
  27. cameraSelect.onChange( function () {
  28. editor.setViewportCamera( this.getValue() );
  29. } );
  30. container.add( cameraSelect );
  31. signals.cameraAdded.add( update );
  32. signals.cameraRemoved.add( update );
  33. // shading
  34. const shadingSelect = new UISelect();
  35. shadingSelect.setOptions( { 'realistic': 'realistic', 'solid': 'solid', 'normals': 'normals', 'wireframe': 'wireframe' } );
  36. shadingSelect.setValue( 'solid' );
  37. shadingSelect.onChange( function () {
  38. editor.setViewportShading( this.getValue() );
  39. } );
  40. container.add( shadingSelect );
  41. signals.editorCleared.add( function () {
  42. editor.setViewportCamera( editor.camera.uuid );
  43. shadingSelect.setValue( 'solid' );
  44. editor.setViewportShading( shadingSelect.getValue() );
  45. } );
  46. update();
  47. //
  48. function update() {
  49. const options = {};
  50. const cameras = editor.cameras;
  51. for ( const key in cameras ) {
  52. const camera = cameras[ key ];
  53. options[ camera.uuid ] = camera.name;
  54. }
  55. cameraSelect.setOptions( options );
  56. const selectedCamera = ( editor.viewportCamera.uuid in options )
  57. ? editor.viewportCamera
  58. : editor.camera;
  59. cameraSelect.setValue( selectedCamera.uuid );
  60. editor.setViewportCamera( selectedCamera.uuid );
  61. }
  62. return container;
  63. }
  64. export { ViewportControls };