viewport.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 { PluginCommands } from '../mol-plugin/commands';
  9. import { PluginConfig } from '../mol-plugin/config';
  10. import { ParamDefinition as PD } from '../mol-util/param-definition';
  11. import { PluginUIComponent } from './base';
  12. import { ControlGroup, IconButton } from './controls/common';
  13. import { AutorenewSvg, BuildOutlinedSvg, CameraOutlinedSvg, CloseSvg, FullscreenSvg, TuneSvg } from './controls/icons';
  14. import { ToggleSelectionModeButton } from './structure/selection';
  15. import { ViewportCanvas } from './viewport/canvas';
  16. import { DownloadScreenshotControls } from './viewport/screenshot';
  17. import { SimpleSettingsControl } from './viewport/simple-settings';
  18. interface ViewportControlsState {
  19. isSettingsExpanded: boolean,
  20. isScreenshotExpanded: boolean
  21. }
  22. interface ViewportControlsProps {
  23. }
  24. export class ViewportControls extends PluginUIComponent<ViewportControlsProps, ViewportControlsState> {
  25. private allCollapsedState: ViewportControlsState = {
  26. isSettingsExpanded: false,
  27. isScreenshotExpanded: false
  28. };
  29. state = { ...this.allCollapsedState } as ViewportControlsState;
  30. resetCamera = () => {
  31. PluginCommands.Camera.Reset(this.plugin, {});
  32. };
  33. private toggle(panel: keyof ViewportControlsState) {
  34. return (e?: React.MouseEvent<HTMLButtonElement>) => {
  35. this.setState({ ...this.allCollapsedState, [panel]: !this.state[panel] });
  36. e?.currentTarget.blur();
  37. };
  38. }
  39. toggleSettingsExpanded = this.toggle('isSettingsExpanded');
  40. toggleScreenshotExpanded = this.toggle('isScreenshotExpanded');
  41. toggleControls = () => {
  42. PluginCommands.Layout.Update(this.plugin, { state: { showControls: !this.plugin.layout.state.showControls } });
  43. };
  44. toggleExpanded = () => {
  45. PluginCommands.Layout.Update(this.plugin, { state: { isExpanded: !this.plugin.layout.state.isExpanded } });
  46. };
  47. setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  48. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { [p.name]: p.value } });
  49. };
  50. setLayout = (p: { param: PD.Base<any>, name: string, value: any }) => {
  51. PluginCommands.Layout.Update(this.plugin, { state: { [p.name]: p.value } });
  52. };
  53. screenshot = () => {
  54. this.plugin.helpers.viewportScreenshot?.download();
  55. };
  56. componentDidMount() {
  57. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate());
  58. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  59. }
  60. icon(icon: React.FC, onClick: (e: React.MouseEvent<HTMLButtonElement>) => void, title: string, isOn = true) {
  61. return <IconButton svg={icon} toggleState={isOn} onClick={onClick} title={title} style={{ background: 'transparent' }} />;
  62. }
  63. onMouseMove = (e: React.MouseEvent) => {
  64. // ignore mouse moves when no button is held
  65. if (e.buttons === 0) e.stopPropagation();
  66. };
  67. render() {
  68. return <div className={'msp-viewport-controls'} onMouseMove={this.onMouseMove}>
  69. <div className='msp-viewport-controls-buttons'>
  70. <div>
  71. <div className='msp-semi-transparent-background' />
  72. {this.icon(AutorenewSvg, this.resetCamera, 'Reset Camera')}
  73. </div>
  74. <div>
  75. <div className='msp-semi-transparent-background' />
  76. {this.icon(CameraOutlinedSvg, this.toggleScreenshotExpanded, 'Screenshot / State Snapshot', this.state.isScreenshotExpanded)}
  77. </div>
  78. <div>
  79. <div className='msp-semi-transparent-background' />
  80. {this.plugin.config.get(PluginConfig.Viewport.ShowControls) && this.icon(BuildOutlinedSvg, this.toggleControls, 'Toggle Controls Panel', this.plugin.layout.state.showControls)}
  81. {this.plugin.config.get(PluginConfig.Viewport.ShowExpand) && this.icon(FullscreenSvg, this.toggleExpanded, 'Toggle Expanded Viewport', this.plugin.layout.state.isExpanded)}
  82. {this.plugin.config.get(PluginConfig.Viewport.ShowSettings) && this.icon(TuneSvg, this.toggleSettingsExpanded, 'Settings / Controls Info', this.state.isSettingsExpanded)}
  83. </div>
  84. {this.plugin.config.get(PluginConfig.Viewport.ShowSelectionMode) && <div>
  85. <div className='msp-semi-transparent-background' />
  86. <ToggleSelectionModeButton />
  87. </div>}
  88. </div>
  89. {this.state.isScreenshotExpanded && <div className='msp-viewport-controls-panel'>
  90. <ControlGroup header='Screenshot / State' title='Click to close.' initialExpanded={true} hideExpander={true} hideOffset={true} onHeaderClick={this.toggleScreenshotExpanded}
  91. topRightIcon={CloseSvg} noTopMargin childrenClassName='msp-viewport-controls-panel-controls'>
  92. <DownloadScreenshotControls close={this.toggleScreenshotExpanded} />
  93. </ControlGroup>
  94. </div>}
  95. {this.state.isSettingsExpanded && <div className='msp-viewport-controls-panel'>
  96. <ControlGroup header='Settings / Controls Info' title='Click to close.' initialExpanded={true} hideExpander={true} hideOffset={true} onHeaderClick={this.toggleSettingsExpanded}
  97. topRightIcon={CloseSvg} noTopMargin childrenClassName='msp-viewport-controls-panel-controls'>
  98. <SimpleSettingsControl />
  99. </ControlGroup>
  100. </div>}
  101. </div>;
  102. }
  103. }
  104. export const Logo = () =>
  105. <a className='msp-logo' href='https://molstar.org' target='_blank' />;
  106. export const Viewport = () => <ViewportCanvas logo={Logo} />;