core2d 2.10.4 → 2.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Point.mjs CHANGED
@@ -1,38 +1,74 @@
1
1
  "use strict";
2
2
 
3
+ /**
4
+ * Represents a point with x and y coordinates.
5
+ */
3
6
  export class Point {
4
- constructor(x = 0, y = 0) {
5
- this.x = x;
6
- this.y = y;
7
- }
7
+ /**
8
+ * Creates a new Point.
9
+ * @param {number} [x=0] The x-coordinate.
10
+ * @param {number} [y=0] The y-coordinate.
11
+ */
12
+ constructor(x = 0, y = 0) {
13
+ this.x = x;
14
+ this.y = y;
15
+ }
8
16
 
9
- get position() {
10
- return this;
11
- }
17
+ /**
18
+ * The position of the point.
19
+ * @type {Point}
20
+ */
21
+ get position() {
22
+ return this;
23
+ }
12
24
 
13
- setX(x) {
14
- this.x = x;
15
- return this;
16
- }
25
+ /**
26
+ * Sets the x-coordinate of the point.
27
+ * @param {number} x The x-coordinate.
28
+ * @returns {Point} This point.
29
+ */
30
+ setX(x) {
31
+ this.x = x;
32
+ return this;
33
+ }
17
34
 
18
- setY(y) {
19
- this.y = y;
20
- return this;
21
- }
35
+ /**
36
+ * Sets the y-coordinate of the point.
37
+ * @param {number} y The y-coordinate.
38
+ * @returns {Point} This point.
39
+ */
40
+ setY(y) {
41
+ this.y = y;
42
+ return this;
43
+ }
22
44
 
23
- setPosition(point) {
24
- this.x = point.x;
25
- this.y = point.y;
26
- return this;
27
- }
45
+ /**
46
+ * Sets the position of the point.
47
+ * @param {Point} point The new position.
48
+ * @returns {Point} This point.
49
+ */
50
+ setPosition(point) {
51
+ this.x = point.x;
52
+ this.y = point.y;
53
+ return this;
54
+ }
28
55
 
29
- set position(point) {
30
- this.setPosition(point);
31
- }
56
+ /**
57
+ * The position of the point.
58
+ * @type {Point}
59
+ */
60
+ set position(point) {
61
+ this.setPosition(point);
62
+ }
32
63
 
33
- distance(point) {
34
- const dx = point.x - this.x;
35
- const dy = point.y - this.y;
36
- return Math.sqrt(dx * dx + dy * dy);
37
- }
64
+ /**
65
+ * Calculates the distance to another point.
66
+ * @param {Point} point The other point.
67
+ * @returns {number} The distance to the other point.
68
+ */
69
+ distance(point) {
70
+ const dx = point.x - this.x;
71
+ const dy = point.y - this.y;
72
+ return Math.sqrt(dx * dx + dy * dy);
73
+ }
38
74
  }
package/src/Pointer.mjs CHANGED
@@ -3,42 +3,64 @@
3
3
  import { Engine } from "./Engine.mjs";
4
4
  import { Point } from "./Point.mjs";
5
5
 
6
+ /**
7
+ * Represents a pointer, which can be a mouse or a touch.
8
+ * @extends Point
9
+ */
6
10
  export class Pointer extends Point {
7
- constructor() {
8
- super();
9
- this._active = false;
10
- this._device = null;
11
- this._hold = false;
12
- }
13
-
14
- get down() {
15
- return this._active;
16
- }
17
-
18
- get push() {
19
- return this._active && !this._hold;
20
- }
21
-
22
- setDevice(device) {
23
- this._device = device;
24
- }
25
-
26
- update() {
27
- if (!this._device) {
28
- return;
29
- }
30
-
31
- this._hold = false;
32
- const LAST = this._active;
33
- this._active = this._device.command;
34
-
35
- if (this._active && LAST) {
36
- this._hold = true;
37
- }
38
-
39
- const REAL_X = this._device.x - Engine.offsetLeft;
40
- const REAL_Y = this._device.y - Engine.offsetTop;
41
- this.x = Math.floor(REAL_X * Engine.width / Engine.realWidth);
42
- this.y = Math.floor(REAL_Y * Engine.height / Engine.realHeight);
43
- }
11
+ /**
12
+ * Creates a new Pointer.
13
+ */
14
+ constructor() {
15
+ super();
16
+ this._active = false;
17
+ this._device = null;
18
+ this._hold = false;
19
+ }
20
+
21
+ /**
22
+ * Whether the pointer is down.
23
+ * @type {boolean}
24
+ */
25
+ get down() {
26
+ return this._active;
27
+ }
28
+
29
+ /**
30
+ * Whether the pointer was just pushed.
31
+ * @type {boolean}
32
+ */
33
+ get push() {
34
+ return this._active && !this._hold;
35
+ }
36
+
37
+ /**
38
+ * Sets the device of the pointer.
39
+ * @param {object} device The device.
40
+ */
41
+ setDevice(device) {
42
+ this._device = device;
43
+ }
44
+
45
+ /**
46
+ * Updates the pointer.
47
+ */
48
+ update() {
49
+ if (!this._device) {
50
+ return;
51
+ }
52
+
53
+ this._hold = false;
54
+ const LAST = this._active;
55
+ this._active = this._device.command;
56
+
57
+ if (this._active && LAST) {
58
+ this._hold = true;
59
+ }
60
+
61
+ const REAL_X = this._device.x - Engine.offsetLeft;
62
+ const REAL_Y = this._device.y - Engine.offsetTop;
63
+ this.x = Math.floor((REAL_X * Engine.width) / Engine.realWidth);
64
+ this.y = Math.floor((REAL_Y * Engine.height) / Engine.realHeight);
65
+ }
44
66
  }
package/src/Rect.mjs CHANGED
@@ -2,126 +2,248 @@
2
2
 
3
3
  import { Point } from "./Point.mjs";
4
4
 
5
+ /**
6
+ * Represents a rectangle.
7
+ * @extends Point
8
+ */
5
9
  export class Rect extends Point {
6
- constructor(x, y, width = 0, height = 0) {
7
- super(x, y);
8
- this.width = width;
9
- this.height = height;
10
- }
11
-
12
- get left() {
13
- return this.x;
14
- }
15
-
16
- get right() {
17
- return this.x + this.width - 1;
18
- }
19
-
20
- get top() {
21
- return this.y;
22
- }
23
-
24
- get bottom() {
25
- return this.y + this.height - 1;
26
- }
27
-
28
- get centerX() {
29
- return this.x + Math.floor(this.width / 2);
30
- }
31
-
32
- get centerY() {
33
- return this.y + Math.floor(this.height / 2);
34
- }
35
-
36
- get center() {
37
- return new Point(this.centerX, this.centerY);
38
- }
39
-
40
- setWidth(width) {
41
- this.width = width;
42
- return this;
43
- }
44
-
45
- setHeight(height) {
46
- this.height = height;
47
- return this;
48
- }
49
-
50
- setSize(rect) {
51
- this.setWidth(rect.width);
52
- this.setHeight(rect.height);
53
- return this;
54
- }
55
-
56
- setLeft(x) {
57
- this.x = x;
58
- return this;
59
- }
60
-
61
- setRight(x) {
62
- this.x = x - this.width + 1;
63
- return this;
64
- }
65
-
66
- setTop(y) {
67
- this.y = y;
68
- return this;
69
- }
70
-
71
- setBottom(y) {
72
- this.y = y - this.height + 1;
73
- return this;
74
- }
75
-
76
- setCenterX(x) {
77
- this.x = x - Math.floor(this.width / 2);
78
- return this;
79
- }
80
-
81
- setCenterY(y) {
82
- this.y = y - Math.floor(this.height / 2);
83
- return this;
84
- }
85
-
86
- setCenter(point) {
87
- this.setCenterX(point.x);
88
- this.setCenterY(point.y);
89
- return this;
90
- }
91
-
92
- set left(x) {
93
- this.setLeft(x);
94
- }
95
-
96
- set right(x) {
97
- this.setRight(x);
98
- }
99
-
100
- set top(y) {
101
- this.setTop(y);
102
- }
103
-
104
- set bottom(y) {
105
- this.setBottom(y);
106
- }
107
-
108
- set centerX(x) {
109
- this.setCenterX(x);
110
- }
111
-
112
- set centerY(y) {
113
- this.setCenterY(y);
114
- }
115
-
116
- set center(point) {
117
- this.setCenter(point);
118
- }
119
-
120
- makeUnion(rect) {
121
- const RECT = new Rect(Math.min(this.x, rect.x), Math.min(this.y, rect.y));
122
-
123
- return RECT
124
- .setWidth(Math.max(this.right, rect.right) - RECT.x + 1)
125
- .setHeight(Math.max(this.bottom, rect.bottom) - RECT.y + 1);
126
- }
10
+ /**
11
+ * Creates a new Rect.
12
+ * @param {number} x The x-coordinate of the rectangle.
13
+ * @param {number} y The y-coordinate of the rectangle.
14
+ * @param {number} [width=0] The width of the rectangle.
15
+ * @param {number} [height=0] The height of the rectangle.
16
+ */
17
+ constructor(x, y, width = 0, height = 0) {
18
+ super(x, y);
19
+ this.width = width;
20
+ this.height = height;
21
+ }
22
+
23
+ /**
24
+ * The x-coordinate of the left edge of the rectangle.
25
+ * @type {number}
26
+ */
27
+ get left() {
28
+ return this.x;
29
+ }
30
+
31
+ /**
32
+ * The x-coordinate of the right edge of the rectangle.
33
+ * @type {number}
34
+ */
35
+ get right() {
36
+ return this.x + this.width - 1;
37
+ }
38
+
39
+ /**
40
+ * The y-coordinate of the top edge of the rectangle.
41
+ * @type {number}
42
+ */
43
+ get top() {
44
+ return this.y;
45
+ }
46
+
47
+ /**
48
+ * The y-coordinate of the bottom edge of the rectangle.
49
+ * @type {number}
50
+ */
51
+ get bottom() {
52
+ return this.y + this.height - 1;
53
+ }
54
+
55
+ /**
56
+ * The x-coordinate of the center of the rectangle.
57
+ * @type {number}
58
+ */
59
+ get centerX() {
60
+ return this.x + Math.floor(this.width / 2);
61
+ }
62
+
63
+ /**
64
+ * The y-coordinate of the center of the rectangle.
65
+ * @type {number}
66
+ */
67
+ get centerY() {
68
+ return this.y + Math.floor(this.height / 2);
69
+ }
70
+
71
+ /**
72
+ * The center of the rectangle.
73
+ * @type {Point}
74
+ */
75
+ get center() {
76
+ return new Point(this.centerX, this.centerY);
77
+ }
78
+
79
+ /**
80
+ * Sets the width of the rectangle.
81
+ * @param {number} width The new width.
82
+ * @returns {Rect} This rectangle.
83
+ */
84
+ setWidth(width) {
85
+ this.width = width;
86
+ return this;
87
+ }
88
+
89
+ /**
90
+ * Sets the height of the rectangle.
91
+ * @param {number} height The new height.
92
+ * @returns {Rect} This rectangle.
93
+ */
94
+ setHeight(height) {
95
+ this.height = height;
96
+ return this;
97
+ }
98
+
99
+ /**
100
+ * Sets the size of the rectangle.
101
+ * @param {Rect} rect The new size.
102
+ * @returns {Rect} This rectangle.
103
+ */
104
+ setSize(rect) {
105
+ this.setWidth(rect.width);
106
+ this.setHeight(rect.height);
107
+ return this;
108
+ }
109
+
110
+ /**
111
+ * Sets the x-coordinate of the left edge of the rectangle.
112
+ * @param {number} x The new x-coordinate.
113
+ * @returns {Rect} This rectangle.
114
+ */
115
+ setLeft(x) {
116
+ this.x = x;
117
+ return this;
118
+ }
119
+
120
+ /**
121
+ * Sets the x-coordinate of the right edge of the rectangle.
122
+ * @param {number} x The new x-coordinate.
123
+ * @returns {Rect} This rectangle.
124
+ */
125
+ setRight(x) {
126
+ this.x = x - this.width + 1;
127
+ return this;
128
+ }
129
+
130
+ /**
131
+ * Sets the y-coordinate of the top edge of the rectangle.
132
+ * @param {number} y The new y-coordinate.
133
+ * @returns {Rect} This rectangle.
134
+ */
135
+ setTop(y) {
136
+ this.y = y;
137
+ return this;
138
+ }
139
+
140
+ /**
141
+ * Sets the y-coordinate of the bottom edge of the rectangle.
142
+ * @param {number} y The new y-coordinate.
143
+ * @returns {Rect} This rectangle.
144
+ */
145
+ setBottom(y) {
146
+ this.y = y - this.height + 1;
147
+ return this;
148
+ }
149
+
150
+ /**
151
+ * Sets the x-coordinate of the center of the rectangle.
152
+ * @param {number} x The new x-coordinate.
153
+ * @returns {Rect} This rectangle.
154
+ */
155
+ setCenterX(x) {
156
+ this.x = x - Math.floor(this.width / 2);
157
+ return this;
158
+ }
159
+
160
+ /**
161
+ * Sets the y-coordinate of the center of the rectangle.
162
+ * @param {number} y The new y-coordinate.
163
+ * @returns {Rect} This rectangle.
164
+ */
165
+ setCenterY(y) {
166
+ this.y = y - Math.floor(this.height / 2);
167
+ return this;
168
+ }
169
+
170
+ /**
171
+ * Sets the center of the rectangle.
172
+ * @param {Point} point The new center.
173
+ * @returns {Rect} This rectangle.
174
+ */
175
+ setCenter(point) {
176
+ this.setCenterX(point.x);
177
+ this.setCenterY(point.y);
178
+ return this;
179
+ }
180
+
181
+ /**
182
+ * The x-coordinate of the left edge of the rectangle.
183
+ * @type {number}
184
+ */
185
+ set left(x) {
186
+ this.setLeft(x);
187
+ }
188
+
189
+ /**
190
+ * The x-coordinate of the right edge of the rectangle.
191
+ * @type {number}
192
+ */
193
+ set right(x) {
194
+ this.setRight(x);
195
+ }
196
+
197
+ /**
198
+ * The y-coordinate of the top edge of the rectangle.
199
+ * @type {number}
200
+ */
201
+ set top(y) {
202
+ this.setTop(y);
203
+ }
204
+
205
+ /**
206
+ * The y-coordinate of the bottom edge of the rectangle.
207
+ * @type {number}
208
+ */
209
+ set bottom(y) {
210
+ this.setBottom(y);
211
+ }
212
+
213
+ /**
214
+ * The x-coordinate of the center of the rectangle.
215
+ * @type {number}
216
+ */
217
+ set centerX(x) {
218
+ this.setCenterX(x);
219
+ }
220
+
221
+ /**
222
+ * The y-coordinate of the center of the rectangle.
223
+ * @type {number}
224
+ */
225
+ set centerY(y) {
226
+ this.setCenterY(y);
227
+ }
228
+
229
+ /**
230
+ * The center of the rectangle.
231
+ * @type {Point}
232
+ */
233
+ set center(point) {
234
+ this.setCenter(point);
235
+ }
236
+
237
+ /**
238
+ * Creates a new rectangle that is the union of this rectangle and another rectangle.
239
+ * @param {Rect} rect The other rectangle.
240
+ * @returns {Rect} The new rectangle.
241
+ */
242
+ makeUnion(rect) {
243
+ const RECT = new Rect(Math.min(this.x, rect.x), Math.min(this.y, rect.y));
244
+
245
+ return RECT.setWidth(
246
+ Math.max(this.right, rect.right) - RECT.x + 1
247
+ ).setHeight(Math.max(this.bottom, rect.bottom) - RECT.y + 1);
248
+ }
127
249
  }
@@ -1,19 +1,19 @@
1
1
  "use strict";
2
2
 
3
3
  export class RenderableList {
4
- constructor() {
5
- this._elements = [];
6
- }
4
+ constructor() {
5
+ this._elements = [];
6
+ }
7
7
 
8
- add(renderable) {
9
- this._elements.push(renderable);
10
- }
8
+ add(renderable) {
9
+ this._elements.push(renderable);
10
+ }
11
11
 
12
- render(context) {
13
- for (let i = 0; i < this._elements.length; ++i) {
14
- this._elements[i].render(context);
15
- }
12
+ render(context) {
13
+ for (let i = 0; i < this._elements.length; ++i) {
14
+ this._elements[i].render(context);
15
+ }
16
16
 
17
- this._elements.length = 0;
18
- }
17
+ this._elements.length = 0;
18
+ }
19
19
  }