summaryrefslogtreecommitdiff
path: root/src/Map/Shapes.js
blob: e394b692132be711d1b66eb5b1b2d04cce60a393 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Enum } from '../Util/Util.js';
import { dist, AABB } from '../Geometry/Geometry.js';


class Node {
	constructor(x, y) {
		this.moveTo(x, y);
	}

	moveTo(x, y) {
		this.x = x;
		this.y = y;
	}
}


class ShapeType extends Enum {
	constructor(name) { super('Shape', name); }
}


class Shape {
	static Point = new ShapeType('Point');
	static Path = new ShapeType('Path');
	static Polygon = new ShapeType('Polygon');
	static Text = new ShapeType('Text');

	constructor(type, nodes) {
		this.type = type;
		this.nodes = nodes;

		this.boundingBox = this.computeBoundingBox();
	}

	computeBoundingBox() {
		let xMin = Infinity;
		let xMax = -Infinity;
		let yMin = Infinity;
		let yMax = -Infinity;

		for (let node of this.nodes) {
			if (node.x < xMin) xMin = node.x;
			if (node.x > xMax) xMax = node.x;

			if (node.y < yMin) yMin = node.y;
			if (node.y > yMax) yMax = node.y;
		}

		return new AABB(xMin, yMin, xMax-xMin, yMax-yMin);
	}
}

class Point extends Shape {
	constructor(node) {
		super(Shape.Point, [node]);
	}
}

class Path extends Shape {
	constructor(nodes) {
		super(Shape.Path, nodes);
	}
}

class Polygon extends Shape {
	constructor(nodes) {
		super(Shape.Polygon, nodes);
	}
}

class Text extends Shape {
	constructor(node, text, fontSize) {
		super(Shape.Text, [node]);
		this.text = text;
		this.fontSize = fontSize;
	}
}


class ZoomLevel extends Enum {
	static Globe = new ZoomLevel('Globe');
	static Nation = new ZoomLevel('Nation');
	static Province = new ZoomLevel('Province');
	static City = new ZoomLevel('City');
	static Neighborhoot = new ZoomLevel('Neighborhood');
	static Building = new ZoomLevel('Building');

	constructor(name) { super('ZoomLevel', name); }
}

class MapObject {
	constructor(core, layerId, minZoom, maxZoom=minZoom) {
		this.core = core;
		this.layerId = layerId;
		this.minZoom = minZoom;
		this.maxZoom = maxZoom;

		this.shapes = [];
		this.boundingBox = this.computeBoundingBox();
	}

	computeBoundingBox() {
		let xMin = Infinity;
		let xMax = -Infinity;
		let yMin = Infinity;
		let yMax = -Infinity;

		for (let shape of this.shapes) {
			const box = shape.boundingBox;
			if (box.x < xMin) xMin = box.x;
			if (box.x + box.width > xMax) xMax = box.x + box.width;

			if (box.y < yMin) yMin = box.y;
			if (box.y + box.height > yMax) yMax = box.y + box.height;
		}

		return new AABB(xMin, yMin, xMax-xMin, yMax-yMin);
	}
}


export { Node, Shape, Point, Path, Polygon, Text, ZoomLevel, MapObject };