viewport.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import * as React from 'react'
  7. import { App } from '../app';
  8. import { MarkerAction } from 'mol-geo/geometry/marker-data';
  9. import { EmptyLoci, Loci, areLociEqual } from 'mol-model/loci';
  10. import { labelFirst } from 'mol-canvas3d/label';
  11. interface ViewportProps {
  12. app: App
  13. }
  14. interface ViewportState {
  15. noWebGl: boolean
  16. pickingInfo: string
  17. taskInfo: string
  18. }
  19. export class Viewport extends React.Component<ViewportProps, ViewportState> {
  20. private container: HTMLDivElement | null = null;
  21. private canvas: HTMLCanvasElement | null = null;
  22. state: ViewportState = {
  23. noWebGl: false,
  24. pickingInfo: '',
  25. taskInfo: ''
  26. };
  27. handleResize() {
  28. this.props.app.viewer.handleResize()
  29. }
  30. componentDidMount() {
  31. if (!this.canvas || !this.container || !this.props.app.initViewer(this.canvas, this.container)) {
  32. this.setState({ noWebGl: true });
  33. }
  34. this.handleResize()
  35. const viewer = this.props.app.viewer
  36. viewer.input.resize.subscribe(() => this.handleResize())
  37. let prevLoci: Loci = EmptyLoci
  38. viewer.input.move.subscribe(({x, y, inside, buttons}) => {
  39. if (!inside || buttons) return
  40. const p = viewer.identify(x, y)
  41. if (p) {
  42. const loci = viewer.getLoci(p)
  43. if (!areLociEqual(loci, prevLoci)) {
  44. viewer.mark(prevLoci, MarkerAction.RemoveHighlight)
  45. viewer.mark(loci, MarkerAction.Highlight)
  46. prevLoci = loci
  47. const label = labelFirst(loci)
  48. const pickingInfo = `${label}`
  49. this.setState({ pickingInfo })
  50. }
  51. }
  52. })
  53. this.props.app.taskCountChanged.subscribe(({ count, info }) => {
  54. this.setState({ taskInfo: count > 0 ? info : '' })
  55. })
  56. }
  57. componentWillUnmount() {
  58. if (super.componentWillUnmount) super.componentWillUnmount();
  59. // TODO viewer cleanup
  60. }
  61. renderMissing() {
  62. return <div>
  63. <div>
  64. <p><b>WebGL does not seem to be available.</b></p>
  65. <p>This can be caused by an outdated browser, graphics card driver issue, or bad weather. Sometimes, just restarting the browser helps.</p>
  66. <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>
  67. </div>
  68. </div>
  69. }
  70. render() {
  71. if (this.state.noWebGl) return this.renderMissing();
  72. return <div style={{ backgroundColor: 'rgb(0, 0, 0)', width: '100%', height: '100%'}}>
  73. <div ref={elm => this.container = elm} style={{width: '100%', height: '100%'}}>
  74. <canvas ref={elm => this.canvas = elm}></canvas>
  75. </div>
  76. <div
  77. style={{
  78. position: 'absolute',
  79. top: 10,
  80. left: 10,
  81. padding: 10,
  82. color: 'lightgrey',
  83. background: 'rgba(0, 0, 0, 0.2)'
  84. }}
  85. >
  86. {this.state.pickingInfo}
  87. </div>
  88. { this.state.taskInfo ?
  89. <div
  90. style={{
  91. position: 'absolute',
  92. top: 10,
  93. right: 10,
  94. padding: 10,
  95. color: 'lightgrey',
  96. background: 'rgba(0, 0, 0, 0.2)'
  97. }}
  98. >
  99. {this.state.taskInfo}
  100. </div>
  101. : '' }
  102. </div>;
  103. }
  104. }