viewport.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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', this.plugin.layout.state.showControls)}
  90. {this.plugin.config.get(PluginConfig.Viewport.ShowExpand) && this.icon(Fullscreen, this.toggleExpanded, 'Toggle Expanded', 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' 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' 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. <div className='msp-logo'>
  115. <div>
  116. <div>
  117. <div />
  118. <div className='msp-logo-image' />
  119. </div>
  120. </div>
  121. </div>;
  122. interface ViewportState {
  123. noWebGl: boolean
  124. showLogo: boolean
  125. }
  126. export class Viewport extends PluginUIComponent<{ }, ViewportState> {
  127. private container = React.createRef<HTMLDivElement>();
  128. private canvas = React.createRef<HTMLCanvasElement>();
  129. state: ViewportState = {
  130. noWebGl: false,
  131. showLogo: true
  132. };
  133. private handleLogo = () => {
  134. this.setState({ showLogo: !this.plugin.canvas3d?.reprCount.value });
  135. }
  136. private handleResize = () => {
  137. const container = this.container.current;
  138. const canvas = this.canvas.current;
  139. if (container && canvas) {
  140. resizeCanvas(canvas, container);
  141. this.plugin.canvas3d!.handleResize();
  142. }
  143. }
  144. componentDidMount() {
  145. if (!this.canvas.current || !this.container.current || !this.plugin.initViewer(this.canvas.current!, this.container.current!)) {
  146. this.setState({ noWebGl: true });
  147. return;
  148. }
  149. this.handleLogo();
  150. this.handleResize();
  151. const canvas3d = this.plugin.canvas3d!;
  152. this.subscribe(canvas3d.reprCount, this.handleLogo);
  153. this.subscribe(canvas3d.input.resize, this.handleResize);
  154. this.subscribe(canvas3d.interaction.click, e => this.plugin.behaviors.interaction.click.next(e));
  155. this.subscribe(canvas3d.interaction.hover, e => this.plugin.behaviors.interaction.hover.next(e));
  156. this.subscribe(this.plugin.layout.events.updated, () => {
  157. setTimeout(this.handleResize, 50);
  158. });
  159. }
  160. componentWillUnmount() {
  161. if (super.componentWillUnmount) super.componentWillUnmount();
  162. // TODO viewer cleanup
  163. }
  164. renderMissing() {
  165. return <div className='msp-no-webgl'>
  166. <div>
  167. <p><b>WebGL does not seem to be available.</b></p>
  168. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps.</p>
  169. <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>
  170. </div>
  171. </div>;
  172. }
  173. render() {
  174. if (this.state.noWebGl) return this.renderMissing();
  175. return <div className='msp-viewport'>
  176. <div className='msp-viewport-host3d' ref={this.container}>
  177. <canvas ref={this.canvas} />
  178. </div>
  179. {this.state.showLogo && <Logo />}
  180. </div>;
  181. }
  182. }