viewport.tsx 7.7 KB

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