blob: 0e2295594ac99b23ec50530e89e717c033156a80 [file] [log] [blame]
Michail Schwabbeb34522018-07-20 08:15:17 -04001// 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
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020015import {TimeSpan} from '../common/time';
16
Michail Schwabbeb34522018-07-20 08:15:17 -040017/**
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020018 * Defines a mapping between number and Milliseconds for the entire application.
Michail Schwabbeb34522018-07-20 08:15:17 -040019 * Linearly scales time values from boundsMs to pixel values in boundsPx and
20 * back.
21 */
22export class TimeScale {
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020023 private timeBounds: TimeSpan;
24 private startPx: number;
25 private endPx: number;
26 private secPerPx = 0;
Michail Schwabbeb34522018-07-20 08:15:17 -040027
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020028 constructor(timeBounds: TimeSpan, boundsPx: [number, number]) {
29 this.timeBounds = timeBounds;
Michail Schwabbeb34522018-07-20 08:15:17 -040030 this.startPx = boundsPx[0];
31 this.endPx = boundsPx[1];
32 this.updateSlope();
33 }
34
35 private updateSlope() {
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020036 this.secPerPx = this.timeBounds.duration / (this.endPx - this.startPx);
Michail Schwabbeb34522018-07-20 08:15:17 -040037 }
38
Primiano Tuccie36ca632018-08-21 14:32:23 +020039 deltaTimeToPx(time: number): number {
40 return Math.round(time / this.secPerPx);
41 }
42
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020043 timeToPx(time: number): number {
44 return this.startPx + (time - this.timeBounds.start) / this.secPerPx;
Michail Schwabbeb34522018-07-20 08:15:17 -040045 }
46
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020047 pxToTime(px: number): number {
48 return this.timeBounds.start + (px - this.startPx) * this.secPerPx;
Michail Schwabbeb34522018-07-20 08:15:17 -040049 }
50
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020051 deltaPxToDuration(px: number): number {
52 return px * this.secPerPx;
Michail Schwabbeb34522018-07-20 08:15:17 -040053 }
54
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020055 setTimeBounds(timeBounds: TimeSpan) {
56 this.timeBounds = timeBounds;
Michail Schwabbeb34522018-07-20 08:15:17 -040057 this.updateSlope();
58 }
59
Primiano Tuccif30cd9c2018-08-13 01:53:26 +020060 setLimitsPx(pxStart: number, pxEnd: number) {
Michail Schwabbeb34522018-07-20 08:15:17 -040061 this.startPx = pxStart;
62 this.endPx = pxEnd;
63 this.updateSlope();
64 }
65}