viewport.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2018-2019 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 Autorenew from '@material-ui/icons/Autorenew';
  8. import BuildOutlined from '@material-ui/icons/BuildOutlined';
  9. import CameraOutlined from '@material-ui/icons/CameraOutlined';
  10. import Close from '@material-ui/icons/Close';
  11. import Crop from '@material-ui/icons/Crop';
  12. import Fullscreen from '@material-ui/icons/Fullscreen';
  13. import Tune from '@material-ui/icons/Tune';
  14. import * as React from 'react';
  15. import { resizeCanvas } from '../mol-canvas3d/util';
  16. import { PluginCommands } from '../mol-plugin/commands';
  17. import { PluginConfig } from '../mol-plugin/config';
  18. import { ParamDefinition as PD } from '../mol-util/param-definition';
  19. import { PluginUIComponent } from './base';
  20. import { ControlGroup, IconButton } from './controls/common';
  21. import { DownloadScreenshotControls } from './viewport/screenshot';
  22. import { SimpleSettingsControl } from './viewport/simple-settings';
  23. interface ViewportControlsState {
  24. isSettingsExpanded: boolean,
  25. isScreenshotExpanded: boolean
  26. }
  27. interface ViewportControlsProps {
  28. }
  29. export class ViewportControls extends PluginUIComponent<ViewportControlsProps, ViewportControlsState> {
  30. private allCollapsedState: ViewportControlsState = {
  31. isSettingsExpanded: false,
  32. isScreenshotExpanded: false
  33. };
  34. state = { ...this.allCollapsedState } as ViewportControlsState;
  35. resetCamera = () => {
  36. PluginCommands.Camera.Reset(this.plugin, {});
  37. }
  38. private toggle(panel: keyof ViewportControlsState) {
  39. return (e?: React.MouseEvent<HTMLButtonElement>) => {
  40. this.setState({ ...this.allCollapsedState, [panel]: !this.state[panel] });
  41. e?.currentTarget.blur();
  42. };
  43. }
  44. toggleSettingsExpanded = this.toggle('isSettingsExpanded');
  45. toggleScreenshotExpanded = this.toggle('isScreenshotExpanded');
  46. toggleControls = () => {
  47. PluginCommands.Layout.Update(this.plugin, { state: { showControls: !this.plugin.layout.state.showControls } });
  48. }
  49. toggleExpanded = () => {
  50. PluginCommands.Layout.Update(this.plugin, { state: { isExpanded: !this.plugin.layout.state.isExpanded } });
  51. }
  52. toggleSelectionMode = () => {
  53. this.plugin.selectionMode = !this.plugin.selectionMode;
  54. }
  55. setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  56. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { [p.name]: p.value } });
  57. }
  58. setLayout = (p: { param: PD.Base<any>, name: string, value: any }) => {
  59. PluginCommands.Layout.Update(this.plugin, { state: { [p.name]: p.value } });
  60. }
  61. screenshot = () => {
  62. this.plugin.helpers.viewportScreenshot?.download();
  63. }
  64. componentDidMount() {
  65. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate());
  66. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  67. this.subscribe(this.plugin.behaviors.interaction.selectionMode, () => this.forceUpdate());
  68. }
  69. icon(icon: React.FC, onClick: (e: React.MouseEvent<HTMLButtonElement>) => void, title: string, isOn = true) {
  70. return <IconButton svg={icon} toggleState={isOn} onClick={onClick} title={title} style={{ background: 'transparent' }} />;
  71. }
  72. onMouseMove = (e: React.MouseEvent) => {
  73. // ignore mouse moves when no button is held
  74. if (e.buttons === 0) e.stopPropagation();
  75. }
  76. render() {
  77. return <div className={'msp-viewport-controls'} onMouseMove={this.onMouseMove}>
  78. <div className='msp-viewport-controls-buttons'>
  79. <div>
  80. <div className='msp-semi-transparent-background' />
  81. {this.icon(Autorenew, this.resetCamera, 'Reset Camera')}
  82. </div>
  83. <div>
  84. <div className='msp-semi-transparent-background' />
  85. {this.icon(CameraOutlined, this.toggleScreenshotExpanded, 'Screenshot / State Snapshot', this.state.isScreenshotExpanded)}
  86. </div>
  87. <div>
  88. <div className='msp-semi-transparent-background' />
  89. {this.icon(BuildOutlined, this.toggleControls, 'Toggle Controls Panel', this.plugin.layout.state.showControls)}
  90. {this.plugin.config.get(PluginConfig.Viewport.ShowExpand) && this.icon(Fullscreen, this.toggleExpanded, 'Toggle Expanded Viewport', this.plugin.layout.state.isExpanded)}
  91. {this.icon(Tune, this.toggleSettingsExpanded, 'Settings / Controls Info', this.state.isSettingsExpanded)}
  92. </div>
  93. {this.plugin.config.get(PluginConfig.Viewport.ShowSelectionMode) && <div>
  94. <div className='msp-semi-transparent-background' />
  95. {this.icon(Crop, this.toggleSelectionMode, 'Toggle Selection Mode', this.plugin.behaviors.interaction.selectionMode.value)}
  96. </div>}
  97. </div>
  98. {this.state.isScreenshotExpanded && <div className='msp-viewport-controls-panel'>
  99. <ControlGroup header='Screenshot / State' title='Click to close.' initialExpanded={true} hideExpander={true} hideOffset={true} onHeaderClick={this.toggleScreenshotExpanded}
  100. topRightIcon={Close} noTopMargin childrenClassName='msp-viewport-controls-panel-controls'>
  101. <DownloadScreenshotControls close={this.toggleScreenshotExpanded} />
  102. </ControlGroup>
  103. </div>}
  104. {this.state.isSettingsExpanded && <div className='msp-viewport-controls-panel'>
  105. <ControlGroup header='Settings / Controls Info' title='Click to close.' initialExpanded={true} hideExpander={true} hideOffset={true} onHeaderClick={this.toggleSettingsExpanded}
  106. topRightIcon={Close} noTopMargin childrenClassName='msp-viewport-controls-panel-controls'>
  107. <SimpleSettingsControl />
  108. </ControlGroup>
  109. </div>}
  110. </div>;
  111. }
  112. }
  113. export const Logo = () =>
  114. <a className='msp-logo' href='https://molstar.org' target='_blank' />;
  115. interface ViewportState {
  116. noWebGl: boolean
  117. showLogo: boolean
  118. }
  119. export class Viewport extends PluginUIComponent<{ }, ViewportState> {
  120. private container = React.createRef<HTMLDivElement>();
  121. private canvas = React.createRef<HTMLCanvasElement>();
  122. state: ViewportState = {
  123. noWebGl: false,
  124. showLogo: true
  125. };
  126. private handleLogo = () => {
  127. this.setState({ showLogo: !this.plugin.canvas3d?.reprCount.value });
  128. }
  129. private handleResize = () => {
  130. const container = this.container.current;
  131. const canvas = this.canvas.current;
  132. if (container && canvas) {
  133. resizeCanvas(canvas, container);
  134. this.plugin.canvas3d!.handleResize();
  135. }
  136. }
  137. componentDidMount() {
  138. if (!this.canvas.current || !this.container.current || !this.plugin.initViewer(this.canvas.current!, this.container.current!)) {
  139. this.setState({ noWebGl: true });
  140. return;
  141. }
  142. this.handleLogo();
  143. this.handleResize();
  144. const canvas3d = this.plugin.canvas3d!;
  145. this.subscribe(canvas3d.reprCount, this.handleLogo);
  146. this.subscribe(canvas3d.input.resize, this.handleResize);
  147. this.subscribe(canvas3d.interaction.click, e => this.plugin.behaviors.interaction.click.next(e));
  148. this.subscribe(canvas3d.interaction.hover, e => this.plugin.behaviors.interaction.hover.next(e));
  149. this.subscribe(this.plugin.layout.events.updated, () => {
  150. setTimeout(this.handleResize, 50);
  151. });
  152. }
  153. componentWillUnmount() {
  154. if (super.componentWillUnmount) super.componentWillUnmount();
  155. // TODO viewer cleanup
  156. }
  157. renderMissing() {
  158. return <div className='msp-no-webgl'>
  159. <div>
  160. <p><b>WebGL does not seem to be available.</b></p>
  161. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps.</p>
  162. <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>
  163. </div>
  164. </div>;
  165. }
  166. render() {
  167. if (this.state.noWebGl) return this.renderMissing();
  168. return <div className='msp-viewport'>
  169. <div className='msp-viewport-host3d' ref={this.container}>
  170. <canvas ref={this.canvas} />
  171. </div>
  172. {this.state.showLogo && <Logo />}
  173. </div>;
  174. }
  175. }