Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 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 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 15 | import {TimeSpan} from '../common/time'; |
| 16 | |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 17 | /** |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 18 | * Defines a mapping between number and Milliseconds for the entire application. |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 19 | * Linearly scales time values from boundsMs to pixel values in boundsPx and |
| 20 | * back. |
| 21 | */ |
| 22 | export class TimeScale { |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 23 | private timeBounds: TimeSpan; |
| 24 | private startPx: number; |
| 25 | private endPx: number; |
| 26 | private secPerPx = 0; |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 27 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 28 | constructor(timeBounds: TimeSpan, boundsPx: [number, number]) { |
| 29 | this.timeBounds = timeBounds; |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 30 | this.startPx = boundsPx[0]; |
| 31 | this.endPx = boundsPx[1]; |
| 32 | this.updateSlope(); |
| 33 | } |
| 34 | |
| 35 | private updateSlope() { |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 36 | this.secPerPx = this.timeBounds.duration / (this.endPx - this.startPx); |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Primiano Tucci | e36ca63 | 2018-08-21 14:32:23 +0200 | [diff] [blame] | 39 | deltaTimeToPx(time: number): number { |
| 40 | return Math.round(time / this.secPerPx); |
| 41 | } |
| 42 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 43 | timeToPx(time: number): number { |
| 44 | return this.startPx + (time - this.timeBounds.start) / this.secPerPx; |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 47 | pxToTime(px: number): number { |
| 48 | return this.timeBounds.start + (px - this.startPx) * this.secPerPx; |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 49 | } |
| 50 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 51 | deltaPxToDuration(px: number): number { |
| 52 | return px * this.secPerPx; |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 55 | setTimeBounds(timeBounds: TimeSpan) { |
| 56 | this.timeBounds = timeBounds; |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 57 | this.updateSlope(); |
| 58 | } |
| 59 | |
Primiano Tucci | f30cd9c | 2018-08-13 01:53:26 +0200 | [diff] [blame] | 60 | setLimitsPx(pxStart: number, pxEnd: number) { |
Michail Schwab | beb3452 | 2018-07-20 08:15:17 -0400 | [diff] [blame] | 61 | this.startPx = pxStart; |
| 62 | this.endPx = pxEnd; |
| 63 | this.updateSlope(); |
| 64 | } |
| 65 | } |