-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathviewport.ts
67 lines (58 loc) · 1.89 KB
/
viewport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Type } from 'class-transformer';
import { IsString, IsUUID, ValidateNested } from 'class-validator';
import { UUID, uuid, uuidValidationOptions } from '../utils';
import { IsPosition } from '../utils/validators/is-position';
import { IsValue } from '../utils/validators';
import {
getCreate,
lowerRightCornerOf,
MapPosition,
Position,
Size,
upperLeftCornerOf,
} from './utils';
import type { ImageProperties, MapCoordinates } from './utils';
export class Viewport {
@IsUUID(4, uuidValidationOptions)
public readonly id: UUID = uuid();
@IsValue('viewport' as const)
public readonly type = 'viewport';
/**
* top-left position
*
* @deprecated Do not access directly, use helper methods from models/utils/position/position-helpers(-mutable) instead.
*/
@ValidateNested()
@IsPosition()
public readonly position: Position;
@ValidateNested()
@Type(() => Size)
public readonly size: Size;
@IsString()
public readonly name: string;
/**
* @param position top-left position
* @deprecated Use {@link create} instead
*/
constructor(position: MapCoordinates, size: Size, name: string) {
this.position = MapPosition.create(position);
this.size = size;
this.name = name;
}
static readonly create = getCreate(this);
static image: ImageProperties = {
url: 'assets/viewport.svg',
height: 1800,
aspectRatio: 1600 / 900,
};
static isInViewport(viewport: Viewport, position: MapCoordinates): boolean {
const upperLeftCorner = upperLeftCornerOf(viewport);
const lowerRightCorner = lowerRightCornerOf(viewport);
return (
upperLeftCorner.x <= position.x &&
position.x <= lowerRightCorner.x &&
lowerRightCorner.y <= position.y &&
position.y <= upperLeftCorner.y
);
}
}