-
Notifications
You must be signed in to change notification settings - Fork 1
/
types-mixins.js
83 lines (78 loc) · 2.54 KB
/
types-mixins.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @flow
*/
import type {
ScreenRect
} from './types'
import {
CanvasPoint,
PointDifference,
ScreenPoint
} from './types';
import {
CanvasPointMixinTemplate,
PointDifferenceMixinTemplate,
ScreenRectMixinTemplate,
ScreenPointMixinTemplate
} from './types-mixin-templates';
export class ScreenRectMixin extends ScreenRectMixinTemplate {
containsPoint(point: ScreenPoint): boolean {
return point.onBottomRight(this.topLeft) &&
this.bottomRight.onBottomRight(point);
}
overlapsWith(other: ScreenRect): boolean {
return this.bottomRight.onBottomRight(other.topLeft) &&
other.bottomRight.onBottomRight(this.topLeft);
}
rightSide(): ScreenRect {
return this.lens().topLeft().screenX().replace(this.bottomRight.screenX);
}
plusDiff(diff: PointDifference): ScreenRect {
return this
.updateTopLeft(topLeft => topLeft.plusDiff(diff))
.updateBottomRight(bottomRight => bottomRight.plusDiff(diff));
}
}
export class ScreenPointMixin extends ScreenPointMixinTemplate {
onBottomRight(other: ScreenPoint): boolean {
return this.screenX >= other.screenX && this.screenY >= other.screenY;
}
minus(other: ScreenPoint): PointDifference {
return PointDifference.make(
this.screenX - other.screenX, this.screenY - other.screenY);
}
plusDiff(diff: PointDifference): ScreenPoint {
return this
.updateScreenX(x => x + diff.dx)
.updateScreenY(y => y + diff.dy);
}
/**
* Reinterpret the screen point as a difference from the screen origin.
*/
asDiff(): PointDifference {
return PointDifference.make(this.screenX, this.screenY);
}
}
export class CanvasPointMixin extends CanvasPointMixinTemplate {
plusDiff(diff: PointDifference): CanvasPoint {
return CanvasPoint.make(
this.canvasX + diff.dx, this.canvasY + diff.dy);
}
minus(other: CanvasPoint): PointDifference {
return PointDifference.make(
this.canvasX - other.canvasX, this.canvasY - other.canvasY);
}
minusDiff(diff: PointDifference): CanvasPoint {
return CanvasPoint.make(
this.canvasX - diff.dx, this.canvasY - diff.dy);
}
}
export class PointDifferenceMixin extends PointDifferenceMixinTemplate {
/**
* Reinterpret the point difference as a screen point (i.e. a difference
* from the screen origin).
*/
asScreenPoint(): ScreenPoint {
return ScreenPoint.make(this.dx, this.dy);
}
}