/** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose */ import { Task, RuntimeContext } from 'mol-task' import { RenderObject } from 'mol-gl/render-object' import { PickingId } from '../mol-geo/geometry/picking'; import { Loci, isEmptyLoci, EmptyLoci } from 'mol-model/loci'; import { MarkerAction } from '../mol-geo/geometry/marker-data'; import { Params, MultiSelectParam } from 'mol-util/parameter'; import { WebGLContext } from 'mol-gl/webgl/context'; import { getQualityProps } from './util'; import { Theme } from 'mol-geo/geometry/geometry'; // import { ColorTheme } from 'mol-theme/color'; // export interface RepresentationProps { // visuals?: string[] // } export type RepresentationProps = { [k: string]: any } export interface RepresentationContext { webgl?: WebGLContext } export interface Representation { readonly label: string readonly params: Params readonly renderObjects: ReadonlyArray readonly props: Readonly

createOrUpdate: (ctx: RepresentationContext, props?: Partial

, data?: D) => Task getLoci: (pickingId: PickingId) => Loci mark: (loci: Loci, action: MarkerAction) => boolean destroy: () => void } export namespace Representation { export function createMulti(label: string, params: Params, defaultProps: P, reprList: Representation[]): Representation { let currentProps: P let currentData: D const visualsOptions: [string, string][] = [] for (let i = 0, il = reprList.length; i < il; ++i) { visualsOptions.push([ i.toString(), reprList[i].label ]) } params['visuals'] = MultiSelectParam('Visuals', '', ['surface'], visualsOptions) if (!defaultProps.visuals) { defaultProps.visuals = reprList.map((r, i) => i.toString()) } return { label, params, get renderObjects() { const { visuals } = currentProps const renderObjects: RenderObject[] = [] for (let i = 0, il = reprList.length; i < il; ++i) { if (!visuals || visuals.includes(i.toString())) { renderObjects.push(...reprList[i].renderObjects) } } return renderObjects }, get props() { const props = {} reprList.forEach(r => Object.assign(props, r.props)) return props as P }, createOrUpdate: (ctx: RepresentationContext, props: Partial

= {}, data?: D) => { if (data) currentData = data const qualityProps = getQualityProps(Object.assign({}, currentProps, props), data) currentProps = Object.assign({}, defaultProps, currentProps, props, qualityProps) const { visuals } = currentProps return Task.create(`Creating '${label}' representation`, async runtime => { for (let i = 0, il = reprList.length; i < il; ++i) { if (!visuals || visuals.includes(i.toString())) { await reprList[i].createOrUpdate(ctx, currentProps, currentData).runInContext(runtime) } } }) }, getLoci: (pickingId: PickingId) => { for (let i = 0, il = reprList.length; i < il; ++i) { const loci = reprList[i].getLoci(pickingId) if (!isEmptyLoci(loci)) return loci } return EmptyLoci }, mark: (loci: Loci, action: MarkerAction) => { let marked = false for (let i = 0, il = reprList.length; i < il; ++i) { marked = reprList[i].mark(loci, action) || marked } return marked }, destroy() { for (let i = 0, il = reprList.length; i < il; ++i) { reprList[i].destroy() } } } } } // export interface VisualContext extends RepresentationContext { runtime: RuntimeContext, } export interface Visual { readonly renderObject: RenderObject | undefined createOrUpdate: (ctx: VisualContext, theme: Theme, props?: Partial

, data?: D) => Promise getLoci: (pickingId: PickingId) => Loci mark: (loci: Loci, action: MarkerAction) => boolean destroy: () => void }