viewport.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import * as React from 'react';
  8. import { ButtonsType } from 'mol-util/input/input-observer';
  9. import { Canvas3dIdentifyHelper } from 'mol-plugin/util/canvas3d-identify';
  10. import { PluginComponent } from './base';
  11. import { PluginCommands } from 'mol-plugin/command';
  12. interface ViewportState {
  13. noWebGl: boolean
  14. }
  15. export class ViewportControls extends PluginComponent {
  16. resetCamera = () => {
  17. PluginCommands.Camera.Reset.dispatch(this.plugin, {});
  18. }
  19. render() {
  20. return <div style={{ position: 'absolute', right: '10px', top: '10px', height: '100%', color: 'white' }}>
  21. <button onClick={this.resetCamera}>Reset Camera</button>
  22. </div>
  23. }
  24. }
  25. export class Viewport extends PluginComponent<{ }, ViewportState> {
  26. private container: HTMLDivElement | null = null;
  27. private canvas: HTMLCanvasElement | null = null;
  28. state: ViewportState = {
  29. noWebGl: false
  30. };
  31. private handleResize = () => {
  32. this.plugin.canvas3d.handleResize();
  33. }
  34. componentDidMount() {
  35. if (!this.canvas || !this.container || !this.plugin.initViewer(this.canvas, this.container)) {
  36. this.setState({ noWebGl: true });
  37. }
  38. this.handleResize();
  39. const canvas3d = this.plugin.canvas3d;
  40. this.subscribe(canvas3d.input.resize, this.handleResize);
  41. const idHelper = new Canvas3dIdentifyHelper(this.plugin, 15);
  42. this.subscribe(canvas3d.input.move, ({x, y, inside, buttons}) => {
  43. if (!inside || buttons) { return; }
  44. idHelper.move(x, y);
  45. });
  46. this.subscribe(canvas3d.input.leave, () => {
  47. idHelper.leave();
  48. });
  49. this.subscribe(canvas3d.input.click, ({x, y, buttons}) => {
  50. if (buttons !== ButtonsType.Flag.Primary) return;
  51. idHelper.select(x, y);
  52. });
  53. }
  54. componentWillUnmount() {
  55. if (super.componentWillUnmount) super.componentWillUnmount();
  56. // TODO viewer cleanup
  57. }
  58. renderMissing() {
  59. return <div>
  60. <div>
  61. <p><b>WebGL does not seem to be available.</b></p>
  62. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps.</p>
  63. <p>For a list of supported browsers, refer to <a href='http://caniuse.com/#feat=webgl' target='_blank'>http://caniuse.com/#feat=webgl</a>.</p>
  64. </div>
  65. </div>
  66. }
  67. render() {
  68. if (this.state.noWebGl) return this.renderMissing();
  69. return <div className='msp-viewport'>
  70. <div className='msp-viewport-host3d' ref={elm => this.container = elm}>
  71. <canvas ref={elm => this.canvas = elm}></canvas>
  72. </div>
  73. </div>;
  74. }
  75. }