interaction-events.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { PickingId } from '../../mol-geo/geometry/picking';
  8. import { Representation } from '../../mol-repr/representation';
  9. import { InputObserver, ModifiersKeys, ButtonsType } from '../../mol-util/input/input-observer';
  10. import { RxEventHelper } from '../../mol-util/rx-event-helper';
  11. import { Vec2, Vec3 } from '../../mol-math/linear-algebra';
  12. import { Camera } from '../camera';
  13. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  14. import { Bond } from '../../mol-model/structure';
  15. type Canvas3D = import('../canvas3d').Canvas3D
  16. type HoverEvent = import('../canvas3d').Canvas3D.HoverEvent
  17. type DragEvent = import('../canvas3d').Canvas3D.DragEvent
  18. type ClickEvent = import('../canvas3d').Canvas3D.ClickEvent
  19. const enum InputEvent { Move, Click, Drag }
  20. const tmpPosA = Vec3();
  21. const tmpPos = Vec3();
  22. const tmpNorm = Vec3();
  23. export const Canvas3dInteractionHelperParams = {
  24. maxFps: PD.Numeric(30, { min: 10, max: 60, step: 10 }),
  25. preferAtomPixelPadding: PD.Numeric(3, { min: 0, max: 20, step: 1 }, { description: 'Number of extra pixels at which to prefer atoms over bonds.' }),
  26. };
  27. export type Canvas3dInteractionHelperParams = typeof Canvas3dInteractionHelperParams
  28. export type Canvas3dInteractionHelperProps = PD.Values<Canvas3dInteractionHelperParams>
  29. export class Canvas3dInteractionHelper {
  30. private ev = RxEventHelper.create();
  31. readonly events = {
  32. hover: this.ev<HoverEvent>(),
  33. drag: this.ev<DragEvent>(),
  34. click: this.ev<ClickEvent>(),
  35. };
  36. private startX = -1;
  37. private startY = -1;
  38. private endX = -1;
  39. private endY = -1;
  40. private id: PickingId | undefined = void 0;
  41. private position: Vec3 | undefined = void 0;
  42. private currentIdentifyT = 0;
  43. private isInteracting = false;
  44. private prevLoci: Representation.Loci = Representation.Loci.Empty;
  45. private prevT = 0;
  46. private inside = false;
  47. private buttons: ButtonsType = ButtonsType.create(0);
  48. private button: ButtonsType.Flag = ButtonsType.create(0);
  49. private modifiers: ModifiersKeys = ModifiersKeys.None;
  50. readonly props: Canvas3dInteractionHelperProps;
  51. setProps(props: Partial<Canvas3dInteractionHelperProps>) {
  52. Object.assign(this.props, props);
  53. }
  54. private identify(e: InputEvent, t: number) {
  55. const xyChanged = this.startX !== this.endX || this.startY !== this.endY;
  56. if (e === InputEvent.Drag) {
  57. if (xyChanged && !this.outsideViewport(this.startX, this.startY)) {
  58. this.events.drag.next({ current: this.prevLoci, buttons: this.buttons, button: this.button, modifiers: this.modifiers, pageStart: Vec2.create(this.startX, this.startY), pageEnd: Vec2.create(this.endX, this.endY) });
  59. this.startX = this.endX;
  60. this.startY = this.endY;
  61. }
  62. return;
  63. }
  64. if (xyChanged) {
  65. const pickData = this.canvasIdentify(this.endX, this.endY);
  66. this.id = pickData?.id;
  67. this.position = pickData?.position;
  68. this.startX = this.endX;
  69. this.startY = this.endY;
  70. }
  71. if (e === InputEvent.Click) {
  72. const loci = this.getLoci(this.id, this.position);
  73. this.events.click.next({ current: loci, buttons: this.buttons, button: this.button, modifiers: this.modifiers, page: Vec2.create(this.endX, this.endY), position: this.position });
  74. this.prevLoci = loci;
  75. return;
  76. }
  77. if (!this.inside || this.currentIdentifyT !== t || !xyChanged || this.outsideViewport(this.endX, this.endY)) return;
  78. const loci = this.getLoci(this.id, this.position);
  79. this.events.hover.next({ current: loci, buttons: this.buttons, button: this.button, modifiers: this.modifiers, page: Vec2.create(this.endX, this.endY), position: this.position });
  80. this.prevLoci = loci;
  81. }
  82. tick(t: number) {
  83. if (this.inside && t - this.prevT > 1000 / this.props.maxFps) {
  84. this.prevT = t;
  85. this.currentIdentifyT = t;
  86. this.identify(this.isInteracting ? InputEvent.Drag : InputEvent.Move, t);
  87. }
  88. }
  89. private leave() {
  90. this.inside = false;
  91. if (!Representation.Loci.isEmpty(this.prevLoci)) {
  92. this.prevLoci = Representation.Loci.Empty;
  93. this.events.hover.next({ current: this.prevLoci, buttons: this.buttons, button: this.button, modifiers: this.modifiers });
  94. }
  95. }
  96. private move(x: number, y: number, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys) {
  97. this.inside = true;
  98. this.buttons = buttons;
  99. this.button = button;
  100. this.modifiers = modifiers;
  101. this.endX = x;
  102. this.endY = y;
  103. }
  104. private click(x: number, y: number, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys) {
  105. this.endX = x;
  106. this.endY = y;
  107. this.buttons = buttons;
  108. this.button = button;
  109. this.modifiers = modifiers;
  110. this.identify(InputEvent.Click, 0);
  111. }
  112. private drag(x: number, y: number, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys) {
  113. this.endX = x;
  114. this.endY = y;
  115. this.buttons = buttons;
  116. this.button = button;
  117. this.modifiers = modifiers;
  118. this.identify(InputEvent.Drag, 0);
  119. }
  120. private modify(modifiers: ModifiersKeys) {
  121. if (ModifiersKeys.areEqual(modifiers, this.modifiers)) return;
  122. this.modifiers = modifiers;
  123. this.events.hover.next({ current: this.prevLoci, buttons: this.buttons, button: this.button, modifiers: this.modifiers, page: Vec2.create(this.endX, this.endY), position: this.position });
  124. }
  125. private outsideViewport(x: number, y: number) {
  126. const { input, camera: { viewport } } = this;
  127. x *= input.pixelRatio;
  128. y *= input.pixelRatio;
  129. return (
  130. x > viewport.x + viewport.width ||
  131. input.height - y > viewport.y + viewport.height ||
  132. x < viewport.x ||
  133. input.height - y < viewport.y
  134. );
  135. }
  136. private getLoci(pickingId: PickingId | undefined, position: Vec3 | undefined) {
  137. const { repr, loci } = this.lociGetter(pickingId);
  138. if (position && repr && Bond.isLoci(loci) && loci.bonds.length === 2) {
  139. const { aUnit, aIndex } = loci.bonds[0];
  140. aUnit.conformation.position(aUnit.elements[aIndex], tmpPosA);
  141. Vec3.sub(tmpNorm, this.camera.state.position, this.camera.state.target);
  142. Vec3.projectPointOnPlane(tmpPos, position, tmpNorm, tmpPosA);
  143. const pixelSize = this.camera.getPixelSize(tmpPos);
  144. let radius = repr.theme.size.size(loci.bonds[0]) * (repr.props.sizeFactor ?? 1);
  145. if (repr.props.lineSizeAttenuation === false) {
  146. // divide by two to get radius
  147. radius *= pixelSize / 2;
  148. }
  149. radius += this.props.preferAtomPixelPadding * pixelSize;
  150. if (Vec3.distance(tmpPos, tmpPosA) < radius) {
  151. return { repr, loci: Bond.toFirstStructureElementLoci(loci) };
  152. }
  153. }
  154. return { repr, loci };
  155. }
  156. dispose() {
  157. this.ev.dispose();
  158. }
  159. constructor(private canvasIdentify: Canvas3D['identify'], private lociGetter: Canvas3D['getLoci'], private input: InputObserver, private camera: Camera, props: Partial<Canvas3dInteractionHelperProps> = {}) {
  160. this.props = { ...PD.getDefaultValues(Canvas3dInteractionHelperParams), ...props };
  161. input.drag.subscribe(({ x, y, buttons, button, modifiers }) => {
  162. this.isInteracting = true;
  163. // console.log('drag');
  164. this.drag(x, y, buttons, button, modifiers);
  165. });
  166. input.move.subscribe(({ x, y, inside, buttons, button, modifiers }) => {
  167. if (!inside || this.isInteracting) return;
  168. // console.log('move');
  169. this.move(x, y, buttons, button, modifiers);
  170. });
  171. input.leave.subscribe(() => {
  172. // console.log('leave');
  173. this.leave();
  174. });
  175. input.click.subscribe(({ x, y, buttons, button, modifiers }) => {
  176. if (this.outsideViewport(x, y)) return;
  177. // console.log('click');
  178. this.click(x, y, buttons, button, modifiers);
  179. });
  180. input.interactionEnd.subscribe(() => {
  181. // console.log('interactionEnd');
  182. this.isInteracting = false;
  183. });
  184. input.modifiers.subscribe(modifiers => {
  185. // console.log('modifiers');
  186. this.modify(modifiers);
  187. });
  188. }
  189. }