interaction-events.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright (c) 2018-2020 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. type Canvas3D = import('../canvas3d').Canvas3D
  14. type HoverEvent = import('../canvas3d').Canvas3D.HoverEvent
  15. type DragEvent = import('../canvas3d').Canvas3D.DragEvent
  16. type ClickEvent = import('../canvas3d').Canvas3D.ClickEvent
  17. const enum InputEvent { Move, Click, Drag }
  18. export class Canvas3dInteractionHelper {
  19. private ev = RxEventHelper.create();
  20. readonly events = {
  21. hover: this.ev<HoverEvent>(),
  22. drag: this.ev<DragEvent>(),
  23. click: this.ev<ClickEvent>(),
  24. };
  25. private startX = -1;
  26. private startY = -1;
  27. private endX = -1;
  28. private endY = -1;
  29. private id: PickingId | undefined = void 0;
  30. private position: Vec3 | undefined = void 0;
  31. private currentIdentifyT = 0;
  32. private isInteracting = false;
  33. private prevLoci: Representation.Loci = Representation.Loci.Empty;
  34. private prevT = 0;
  35. private inside = false;
  36. private buttons: ButtonsType = ButtonsType.create(0);
  37. private button: ButtonsType.Flag = ButtonsType.create(0);
  38. private modifiers: ModifiersKeys = ModifiersKeys.None;
  39. private identify(e: InputEvent, t: number) {
  40. const xyChanged = this.startX !== this.endX || this.startY !== this.endY;
  41. if (e === InputEvent.Drag) {
  42. if (xyChanged && !Representation.Loci.isEmpty(this.prevLoci)) {
  43. 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) });
  44. this.startX = this.endX;
  45. this.startY = this.endY;
  46. }
  47. return;
  48. }
  49. if (xyChanged) {
  50. const pickData = this.canvasIdentify(this.endX, this.endY);
  51. this.id = pickData?.id;
  52. this.position = pickData?.position;
  53. this.startX = this.endX;
  54. this.startY = this.endY;
  55. }
  56. if (e === InputEvent.Click) {
  57. const loci = this.getLoci(this.id);
  58. this.events.click.next({ current: loci, buttons: this.buttons, button: this.button, modifiers: this.modifiers, position: this.position });
  59. this.prevLoci = loci;
  60. return;
  61. }
  62. if (!this.inside || this.currentIdentifyT !== t || !xyChanged) {
  63. return;
  64. }
  65. const loci = this.getLoci(this.id);
  66. 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 });
  67. this.prevLoci = loci;
  68. }
  69. tick(t: number) {
  70. if (this.inside && t - this.prevT > 1000 / this.maxFps) {
  71. this.prevT = t;
  72. this.currentIdentifyT = t;
  73. this.identify(this.isInteracting ? InputEvent.Drag : InputEvent.Move, t);
  74. }
  75. }
  76. private leave() {
  77. this.inside = false;
  78. if (Representation.Loci.isEmpty(this.prevLoci)) {
  79. this.prevLoci = Representation.Loci.Empty;
  80. this.events.hover.next({ current: this.prevLoci, buttons: this.buttons, button: this.button, modifiers: this.modifiers });
  81. }
  82. }
  83. private move(x: number, y: number, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys) {
  84. this.inside = true;
  85. this.buttons = buttons;
  86. this.button = button;
  87. this.modifiers = modifiers;
  88. this.endX = x;
  89. this.endY = y;
  90. }
  91. private click(x: number, y: number, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys) {
  92. this.endX = x;
  93. this.endY = y;
  94. this.buttons = buttons;
  95. this.button = button;
  96. this.modifiers = modifiers;
  97. this.identify(InputEvent.Click, 0);
  98. }
  99. private drag(x: number, y: number, buttons: ButtonsType, button: ButtonsType.Flag, modifiers: ModifiersKeys) {
  100. this.endX = x;
  101. this.endY = y;
  102. this.buttons = buttons;
  103. this.button = button;
  104. this.modifiers = modifiers;
  105. this.identify(InputEvent.Drag, 0);
  106. }
  107. private modify(modifiers: ModifiersKeys) {
  108. if (ModifiersKeys.areEqual(modifiers, this.modifiers)) return;
  109. this.modifiers = modifiers;
  110. 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 });
  111. }
  112. private outsideViewport(x: number, y: number) {
  113. const { input, camera: { viewport } } = this;
  114. x *= input.pixelRatio;
  115. y *= input.pixelRatio;
  116. return (
  117. x > viewport.x + viewport.width ||
  118. input.height - y > viewport.y + viewport.height ||
  119. x < viewport.x ||
  120. input.height - y < viewport.y
  121. );
  122. }
  123. dispose() {
  124. this.ev.dispose();
  125. }
  126. constructor(private canvasIdentify: Canvas3D['identify'], private getLoci: Canvas3D['getLoci'], private input: InputObserver, private camera: Camera, private maxFps: number = 30) {
  127. input.drag.subscribe(({x, y, buttons, button, modifiers }) => {
  128. this.isInteracting = true;
  129. // console.log('drag');
  130. this.drag(x, y, buttons, button, modifiers);
  131. });
  132. input.move.subscribe(({x, y, inside, buttons, button, modifiers }) => {
  133. if (!inside || this.isInteracting) return;
  134. // console.log('move');
  135. this.move(x, y, buttons, button, modifiers);
  136. });
  137. input.leave.subscribe(() => {
  138. // console.log('leave');
  139. this.leave();
  140. });
  141. input.click.subscribe(({x, y, buttons, button, modifiers }) => {
  142. if (this.outsideViewport(x, y)) return;
  143. // console.log('click');
  144. this.click(x, y, buttons, button, modifiers);
  145. });
  146. input.interactionEnd.subscribe(() => {
  147. // console.log('interactionEnd');
  148. this.isInteracting = false;
  149. });
  150. input.modifiers.subscribe(modifiers => {
  151. // console.log('modifiers');
  152. this.modify(modifiers);
  153. });
  154. }
  155. }