• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2018 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15import {TrackState} from '../common/state';
16import {globals} from './globals';
17
18/**
19 * This interface forces track implementations to have some static properties.
20 * Typescript does not have abstract static members, which is why this needs to
21 * be in a seperate interface.
22 */
23export interface TrackCreator {
24  // Store the kind explicitly as a string as opposed to using class.kind in
25  // case we ever minify our code.
26  readonly kind: string;
27
28  // We need the |create| method because the stored value in the registry is an
29  // abstract class, and we cannot call 'new' on an abstract class.
30  create(TrackState: TrackState): Track;
31}
32
33/**
34 * The abstract class that needs to be implemented by all tracks.
35 */
36export abstract class Track<Config = {}, Data = {}> {
37  constructor(protected trackState: TrackState) {}
38  abstract renderCanvas(ctx: CanvasRenderingContext2D): void;
39
40  get config(): Config {
41    return this.trackState.config as Config;
42  }
43
44  data(): Data|undefined {
45    return globals.trackDataStore.get(this.trackState.id) as Data;
46  }
47
48  getHeight(): number {
49    return 40;
50  }
51
52  onMouseMove(_position: {x: number, y: number}) {}
53
54  /**
55   * Returns whether the mouse click has selected something.
56   * Used to prevent further propagation if necessary.
57   */
58  onMouseClick(_position: {x: number, y: number}): boolean {
59    return false;
60  }
61
62  onMouseOut() {}
63}
64