viewport.tsx 8.4 KB

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