viewport.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 { PluginUIComponent } from './base';
  9. import { PluginCommands } from '../../mol-plugin/command';
  10. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  11. import { ParameterControls } from './controls/parameters';
  12. import { Canvas3DParams } from '../../mol-canvas3d/canvas3d';
  13. import { PluginLayoutStateParams } from '../../mol-plugin/layout';
  14. import { ControlGroup, IconButton } from './controls/common';
  15. import { resizeCanvas } from '../../mol-canvas3d/util';
  16. interface ViewportState {
  17. noWebGl: boolean
  18. }
  19. export class ViewportControls extends PluginUIComponent<{}, { isSettingsExpanded: boolean }> {
  20. state = {
  21. isSettingsExpanded: false
  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. toggleControls = () => {
  31. PluginCommands.Layout.Update.dispatch(this.plugin, { state: { showControls: !this.plugin.layout.state.showControls } });
  32. }
  33. toggleExpanded = () => {
  34. PluginCommands.Layout.Update.dispatch(this.plugin, { state: { isExpanded: !this.plugin.layout.state.isExpanded } });
  35. }
  36. setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  37. PluginCommands.Canvas3D.SetSettings.dispatch(this.plugin, { settings: { [p.name]: p.value } });
  38. }
  39. setLayout = (p: { param: PD.Base<any>, name: string, value: any }) => {
  40. PluginCommands.Layout.Update.dispatch(this.plugin, { state: { [p.name]: p.value } });
  41. }
  42. componentDidMount() {
  43. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, e => {
  44. this.forceUpdate();
  45. });
  46. this.subscribe(this.plugin.layout.events.updated, () => {
  47. this.forceUpdate();
  48. });
  49. }
  50. icon(name: string, onClick: (e: React.MouseEvent<HTMLButtonElement>) => void, title: string, isOn = true) {
  51. return <IconButton icon={name} toggleState={isOn} onClick={onClick} title={title} />;
  52. }
  53. render() {
  54. return <div className={'msp-viewport-controls'}>
  55. <div className='msp-viewport-controls-buttons'>
  56. {this.icon('reset-scene', this.resetCamera, 'Reset Camera')}<br/>
  57. {this.icon('tools', this.toggleControls, 'Toggle Controls', this.plugin.layout.state.showControls)}<br/>
  58. {this.icon('expand-layout', this.toggleExpanded, 'Toggle Expanded', this.plugin.layout.state.isExpanded)}<br />
  59. {this.icon('settings', this.toggleSettingsExpanded, 'Settings', this.state.isSettingsExpanded)}<br/>
  60. </div>
  61. {this.state.isSettingsExpanded && <div className='msp-viewport-controls-scene-options'>
  62. <ControlGroup header='Layout' initialExpanded={true}>
  63. <ParameterControls params={PluginLayoutStateParams} values={this.plugin.layout.state} onChange={this.setLayout} />
  64. </ControlGroup>
  65. <ControlGroup header='Viewport' initialExpanded={true}>
  66. <ParameterControls params={Canvas3DParams} values={this.plugin.canvas3d.props} onChange={this.setSettings} />
  67. </ControlGroup>
  68. </div>}
  69. </div>
  70. }
  71. }
  72. export class Viewport extends PluginUIComponent<{ }, ViewportState> {
  73. private container = React.createRef<HTMLDivElement>();
  74. private canvas = React.createRef<HTMLCanvasElement>();
  75. state: ViewportState = {
  76. noWebGl: false
  77. };
  78. private handleResize = () => {
  79. const container = this.container.current;
  80. const canvas = this.canvas.current;
  81. if (container && canvas) {
  82. resizeCanvas(canvas, container);
  83. this.plugin.canvas3d.handleResize();
  84. }
  85. }
  86. componentDidMount() {
  87. if (!this.canvas.current || !this.container.current || !this.plugin.initViewer(this.canvas.current!, this.container.current!)) {
  88. this.setState({ noWebGl: true });
  89. }
  90. this.handleResize();
  91. const canvas3d = this.plugin.canvas3d;
  92. this.subscribe(canvas3d.input.resize, this.handleResize);
  93. this.subscribe(canvas3d.interaction.click, e => this.plugin.behaviors.interaction.click.next(e));
  94. this.subscribe(canvas3d.interaction.highlight, e => this.plugin.behaviors.interaction.highlight.next(e));
  95. this.subscribe(this.plugin.layout.events.updated, () => {
  96. setTimeout(this.handleResize, 50);
  97. });
  98. }
  99. componentWillUnmount() {
  100. if (super.componentWillUnmount) super.componentWillUnmount();
  101. // TODO viewer cleanup
  102. }
  103. renderMissing() {
  104. return <div>
  105. <div>
  106. <p><b>WebGL does not seem to be available.</b></p>
  107. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps.</p>
  108. <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>
  109. </div>
  110. </div>
  111. }
  112. render() {
  113. if (this.state.noWebGl) return this.renderMissing();
  114. return <div className='msp-viewport'>
  115. <div className='msp-viewport-host3d' ref={this.container}>
  116. <canvas ref={this.canvas} />
  117. </div>
  118. </div>;
  119. }
  120. }