camera.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /**
  2. * Copyright (c) 2018-2019 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 { Loci } from '../../../mol-model/loci';
  8. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  9. import { PluginBehavior } from '../behavior';
  10. import { ButtonsType, ModifiersKeys } from '../../../mol-util/input/input-observer';
  11. import { Binding } from '../../../mol-util/binding';
  12. const B = ButtonsType
  13. const M = ModifiersKeys
  14. const Trigger = Binding.Trigger
  15. const DefaultFocusLociBindings = {
  16. clickCenterFocus: Binding(Trigger(B.Flag.Primary, M.create()), 'Center and focus the clicked element using ${trigger}.'),
  17. }
  18. const FocusLociParams = {
  19. minRadius: PD.Numeric(8, { min: 1, max: 50, step: 1 }),
  20. extraRadius: PD.Numeric(4, { min: 1, max: 50, step: 1 }, { description: 'Value added to the bounding-sphere radius of the Loci.' }),
  21. durationMs: PD.Numeric(250, { min: 0, max: 1000, step: 1 }, { description: 'Camera transition duration.' }),
  22. bindings: PD.Value(DefaultFocusLociBindings, { isHidden: true }),
  23. }
  24. type FocusLociProps = PD.Values<typeof FocusLociParams>
  25. export const FocusLoci = PluginBehavior.create<FocusLociProps>({
  26. name: 'camera-focus-loci',
  27. category: 'interaction',
  28. ctor: class extends PluginBehavior.Handler<FocusLociProps> {
  29. register(): void {
  30. this.subscribeObservable(this.ctx.behaviors.interaction.click, ({ current, buttons, modifiers }) => {
  31. if (!this.ctx.canvas3d) return;
  32. const p = this.params
  33. if (Binding.match(this.params.bindings.clickCenterFocus, buttons, modifiers)) {
  34. const sphere = Loci.getBoundingSphere(current.loci);
  35. if (sphere) {
  36. const radius = Math.max(sphere.radius + p.extraRadius, p.minRadius);
  37. this.ctx.canvas3d.camera.focus(sphere.center, radius, p.durationMs);
  38. }
  39. }
  40. });
  41. }
  42. },
  43. params: () => FocusLociParams,
  44. display: { name: 'Focus Loci on Canvas' }
  45. });