viewport.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * Copyright (c) 2018-2023 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. * @author Adam Midlik <midlik@gmail.com>
  7. */
  8. import * as React from 'react';
  9. import { throttleTime } from 'rxjs';
  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 { Button, ControlGroup, IconButton } from './controls/common';
  15. import { AutorenewSvg, BuildOutlinedSvg, CameraOutlinedSvg, CloseSvg, FullscreenSvg, TuneSvg } from './controls/icons';
  16. import { ToggleSelectionModeButton } from './structure/selection';
  17. import { ViewportCanvas } from './viewport/canvas';
  18. import { DownloadScreenshotControls } from './viewport/screenshot';
  19. import { SimpleSettingsControl } from './viewport/simple-settings';
  20. interface ViewportControlsState {
  21. isSettingsExpanded: boolean,
  22. isScreenshotExpanded: boolean,
  23. isCameraResetEnabled: boolean
  24. }
  25. interface ViewportControlsProps {
  26. }
  27. export class ViewportControls extends PluginUIComponent<ViewportControlsProps, ViewportControlsState> {
  28. private allCollapsedState = {
  29. isSettingsExpanded: false,
  30. isScreenshotExpanded: false,
  31. };
  32. state: ViewportControlsState = {
  33. ...this.allCollapsedState,
  34. isCameraResetEnabled: true,
  35. };
  36. resetCamera = () => {
  37. PluginCommands.Camera.Reset(this.plugin, {});
  38. };
  39. private toggle(panel: keyof ViewportControlsState) {
  40. return (e?: React.MouseEvent<HTMLButtonElement>) => {
  41. this.setState(old => ({ ...old, ...this.allCollapsedState, [panel]: !this.state[panel] }));
  42. e?.currentTarget.blur();
  43. };
  44. }
  45. toggleSettingsExpanded = this.toggle('isSettingsExpanded');
  46. toggleScreenshotExpanded = this.toggle('isScreenshotExpanded');
  47. toggleControls = () => {
  48. PluginCommands.Layout.Update(this.plugin, { state: { showControls: !this.plugin.layout.state.showControls } });
  49. };
  50. toggleExpanded = () => {
  51. PluginCommands.Layout.Update(this.plugin, { state: { isExpanded: !this.plugin.layout.state.isExpanded } });
  52. };
  53. setSettings = (p: { param: PD.Base<any>, name: string, value: any }) => {
  54. PluginCommands.Canvas3D.SetSettings(this.plugin, { settings: { [p.name]: p.value } });
  55. };
  56. setLayout = (p: { param: PD.Base<any>, name: string, value: any }) => {
  57. PluginCommands.Layout.Update(this.plugin, { state: { [p.name]: p.value } });
  58. };
  59. screenshot = () => {
  60. this.plugin.helpers.viewportScreenshot?.download();
  61. };
  62. enableCameraReset = (enable: boolean) => {
  63. this.setState(old => ({ ...old, isCameraResetEnabled: enable }));
  64. };
  65. componentDidMount() {
  66. this.subscribe(this.plugin.events.canvas3d.settingsUpdated, () => this.forceUpdate());
  67. this.subscribe(this.plugin.layout.events.updated, () => this.forceUpdate());
  68. if (this.plugin.canvas3d) {
  69. this.subscribe(
  70. this.plugin.canvas3d.camera.stateChanged.pipe(throttleTime(500, undefined, { leading: true, trailing: true })),
  71. snapshot => this.enableCameraReset(snapshot.radius !== 0 && snapshot.radiusMax !== 0)
  72. );
  73. }
  74. }
  75. icon(icon: React.FC, onClick: (e: React.MouseEvent<HTMLButtonElement>) => void, title: string, isOn = true) {
  76. return <IconButton svg={icon} toggleState={isOn} onClick={onClick} title={title} style={{ background: 'transparent' }} />;
  77. }
  78. render() {
  79. return <div className={'msp-viewport-controls'}>
  80. <div className='msp-viewport-controls-buttons'>
  81. <div className='msp-hover-box-wrapper'>
  82. <div className='msp-semi-transparent-background' />
  83. {this.icon(AutorenewSvg, this.resetCamera, 'Reset Zoom')}
  84. <div className='msp-hover-box-body'>
  85. <div className='msp-flex-column'>
  86. <div className='msp-flex-row'>
  87. <Button onClick={() => this.resetCamera()} disabled={!this.state.isCameraResetEnabled} title='Set camera zoom to fit the visible scene into view'>
  88. Reset Zoom
  89. </Button>
  90. </div>
  91. <div className='msp-flex-row'>
  92. <Button onClick={() => PluginCommands.Camera.OrientAxes(this.plugin)} disabled={!this.state.isCameraResetEnabled} title='Align principal component axes of the loaded structures to the screen axes (“lay flat”)'>
  93. Orient Axes
  94. </Button>
  95. </div>
  96. <div className='msp-flex-row'>
  97. <Button onClick={() => PluginCommands.Camera.ResetAxes(this.plugin)} disabled={!this.state.isCameraResetEnabled} title='Align Cartesian axes to the screen axes'>
  98. Reset Axes
  99. </Button>
  100. </div>
  101. </div>
  102. </div>
  103. <div className='msp-hover-box-spacer'></div>
  104. </div>
  105. <div>
  106. <div className='msp-semi-transparent-background' />
  107. {this.icon(CameraOutlinedSvg, this.toggleScreenshotExpanded, 'Screenshot / State Snapshot', this.state.isScreenshotExpanded)}
  108. </div>
  109. <div>
  110. <div className='msp-semi-transparent-background' />
  111. {this.plugin.config.get(PluginConfig.Viewport.ShowControls) && this.icon(BuildOutlinedSvg, this.toggleControls, 'Toggle Controls Panel', this.plugin.layout.state.showControls)}
  112. {this.plugin.config.get(PluginConfig.Viewport.ShowExpand) && this.icon(FullscreenSvg, this.toggleExpanded, 'Toggle Expanded Viewport', this.plugin.layout.state.isExpanded)}
  113. {this.plugin.config.get(PluginConfig.Viewport.ShowSettings) && this.icon(TuneSvg, this.toggleSettingsExpanded, 'Settings / Controls Info', this.state.isSettingsExpanded)}
  114. </div>
  115. {this.plugin.config.get(PluginConfig.Viewport.ShowSelectionMode) && <div>
  116. <div className='msp-semi-transparent-background' />
  117. <ToggleSelectionModeButton />
  118. </div>}
  119. </div>
  120. {this.state.isScreenshotExpanded && <div className='msp-viewport-controls-panel'>
  121. <ControlGroup header='Screenshot / State' title='Click to close.' initialExpanded={true} hideExpander={true} hideOffset={true} onHeaderClick={this.toggleScreenshotExpanded}
  122. topRightIcon={CloseSvg} noTopMargin childrenClassName='msp-viewport-controls-panel-controls'>
  123. <DownloadScreenshotControls close={this.toggleScreenshotExpanded} />
  124. </ControlGroup>
  125. </div>}
  126. {this.state.isSettingsExpanded && <div className='msp-viewport-controls-panel'>
  127. <ControlGroup header='Settings / Controls Info' title='Click to close.' initialExpanded={true} hideExpander={true} hideOffset={true} onHeaderClick={this.toggleSettingsExpanded}
  128. topRightIcon={CloseSvg} noTopMargin childrenClassName='msp-viewport-controls-panel-controls'>
  129. <SimpleSettingsControl />
  130. </ControlGroup>
  131. </div>}
  132. </div>;
  133. }
  134. }
  135. export const Logo = () =>
  136. <a className='msp-logo' href='https://molstar.org' target='_blank' />;
  137. export const Viewport = () => <ViewportCanvas logo={Logo} />;