viewport.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. import { ParamDefinition as PD } from 'mol-util/param-definition';
  13. import { ParameterControls } from './controls/parameters';
  14. import { Canvas3DParams } from 'mol-canvas3d/canvas3d';
  15. interface ViewportState {
  16. noWebGl: boolean
  17. }
  18. export class ViewportControls extends PluginComponent {
  19. state = {
  20. isSettingsExpanded: false,
  21. settings: PD.getDefaultValues(Canvas3DParams)
  22. }
  23. resetCamera = () => {
  24. PluginCommands.Camera.Reset.dispatch(this.plugin, {});
  25. }
  26. toggleSettingsExpanded = (e: React.MouseEvent<HTMLButtonElement>) => {
  27. this.setState({ isSettingsExpanded: !this.state.isSettingsExpanded });
  28. e.currentTarget.blur();
  29. }
  30. // hideSettings = () => {
  31. // this.setState({ isSettingsExpanded: false });
  32. // }
  33. setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  34. PluginCommands.Canvas3D.SetSettings.dispatch(this.plugin, { settings: { [p.name]: p.value } });
  35. }
  36. componentDidMount() {
  37. if (this.plugin.canvas3d) {
  38. this.setState({ settings: this.plugin.canvas3d.props });
  39. }
  40. this.subscribe(this.plugin.events.canvad3d.settingsUpdated, e => {
  41. this.setState({ settings: this.plugin.canvas3d.props });
  42. });
  43. }
  44. render() {
  45. // TODO: show some icons dimmed etc..
  46. return <div className={'msp-viewport-controls'}>
  47. <div className='msp-viewport-controls-buttons'>
  48. <button className='msp-btn msp-btn-link' onClick={this.toggleSettingsExpanded}><span className='msp-icon msp-icon-settings'/></button>
  49. <button className='msp-btn msp-btn-link' title='Reset Camera' onClick={this.resetCamera}><span className='msp-icon msp-icon-reset-scene'/></button>
  50. </div>
  51. {this.state.isSettingsExpanded &&
  52. <div className='msp-viewport-controls-scene-options'>
  53. <ParameterControls params={Canvas3DParams} values={this.state.settings} onChange={this.setSettings} />
  54. </div>}
  55. </div>
  56. }
  57. }
  58. export class Viewport extends PluginComponent<{ }, ViewportState> {
  59. private container: HTMLDivElement | null = null;
  60. private canvas: HTMLCanvasElement | null = null;
  61. state: ViewportState = {
  62. noWebGl: false
  63. };
  64. private handleResize = () => {
  65. this.plugin.canvas3d.handleResize();
  66. }
  67. componentDidMount() {
  68. if (!this.canvas || !this.container || !this.plugin.initViewer(this.canvas, this.container)) {
  69. this.setState({ noWebGl: true });
  70. }
  71. this.handleResize();
  72. const canvas3d = this.plugin.canvas3d;
  73. this.subscribe(canvas3d.input.resize, this.handleResize);
  74. const idHelper = new Canvas3dIdentifyHelper(this.plugin, 15);
  75. this.subscribe(canvas3d.input.move, ({x, y, inside, buttons}) => {
  76. if (!inside || buttons) { return; }
  77. idHelper.move(x, y);
  78. });
  79. this.subscribe(canvas3d.input.leave, () => {
  80. idHelper.leave();
  81. });
  82. this.subscribe(canvas3d.input.click, ({x, y, buttons}) => {
  83. if (buttons !== ButtonsType.Flag.Primary) return;
  84. idHelper.select(x, y);
  85. });
  86. }
  87. componentWillUnmount() {
  88. if (super.componentWillUnmount) super.componentWillUnmount();
  89. // TODO viewer cleanup
  90. }
  91. renderMissing() {
  92. return <div>
  93. <div>
  94. <p><b>WebGL does not seem to be available.</b></p>
  95. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps.</p>
  96. <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>
  97. </div>
  98. </div>
  99. }
  100. render() {
  101. if (this.state.noWebGl) return this.renderMissing();
  102. return <div className='msp-viewport'>
  103. <div className='msp-viewport-host3d' ref={elm => this.container = elm}>
  104. <canvas ref={elm => {
  105. if (!!this.canvas && this.canvas !== elm) console.warn('changed viewport canvas')
  106. this.canvas = elm
  107. }} />
  108. </div>
  109. </div>;
  110. }
  111. }