@next2d/texture-packer 2.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Next2D
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ @next2d/texture-packer
2
+ =============
3
+
4
+ ## Installation
5
+
6
+ ```
7
+ npm install @next2d/texture-packer
8
+ ```
9
+
10
+ ## License
11
+ This project is licensed under the [MIT License](https://opensource.org/licenses/MIT) - see the [LICENSE](LICENSE) file for details.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@next2d/texture-packer",
3
+ "version": "2.0.0",
4
+ "description": "Next2D Texture Packer Package",
5
+ "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
+ "license": "MIT",
7
+ "homepage": "https://next2d.app",
8
+ "bugs": "https://github.com/Next2D/Player/issues",
9
+ "main": "src/index.js",
10
+ "types": "src/index.d.ts",
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "import": "./src/index.js",
15
+ "require": "./src/index.js"
16
+ }
17
+ },
18
+ "keywords": [
19
+ "Next2D",
20
+ "Next2D Texture Packer"
21
+ ],
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/Next2D/Player.git"
25
+ }
26
+ }
@@ -0,0 +1,15 @@
1
+ import { Node } from "../../Node";
2
+ /**
3
+ * @description ノード解放ロジック
4
+ * Node release logic
5
+ *
6
+ * @param {Node} node
7
+ * @param {number} x
8
+ * @param {number} y
9
+ * @param {number} width
10
+ * @param {number} height
11
+ * @return {void}
12
+ * @method
13
+ * @protected
14
+ */
15
+ export declare const execute: (node: Node, x: number, y: number, width: number, height: number) => boolean;
@@ -0,0 +1,37 @@
1
+ /**
2
+ * @description ノード解放ロジック
3
+ * Node release logic
4
+ *
5
+ * @param {Node} node
6
+ * @param {number} x
7
+ * @param {number} y
8
+ * @param {number} width
9
+ * @param {number} height
10
+ * @return {void}
11
+ * @method
12
+ * @protected
13
+ */
14
+ export const execute = (node, x, y, width, height) => {
15
+ if (node.left?.dispose(x, y, width, height)) {
16
+ if (!node.left.used && !node.right?.used) {
17
+ node.left = node.right = null;
18
+ node.used = false;
19
+ }
20
+ return true;
21
+ }
22
+ if (node.right?.dispose(x, y, width, height)) {
23
+ if (!node.right.used && !node.left?.used) {
24
+ node.left = node.right = null;
25
+ node.used = false;
26
+ }
27
+ return true;
28
+ }
29
+ if (x === node.x
30
+ && y === node.y
31
+ && width === node.w
32
+ && height === node.h) {
33
+ node.used = false;
34
+ return true;
35
+ }
36
+ return false;
37
+ };
@@ -0,0 +1,13 @@
1
+ import { Node } from "../../Node";
2
+ /**
3
+ * @description ノード挿入ロジック
4
+ * Node insertion logic
5
+ *
6
+ * @param {Node} node
7
+ * @param {number} width
8
+ * @param {number} height
9
+ * @return {Node | null}
10
+ * @method
11
+ * @protected
12
+ */
13
+ export declare const execute: (node: Node, width: number, height: number) => Node | null;
@@ -0,0 +1,37 @@
1
+ import { Node } from "../../Node";
2
+ /**
3
+ * @description ノード挿入ロジック
4
+ * Node insertion logic
5
+ *
6
+ * @param {Node} node
7
+ * @param {number} width
8
+ * @param {number} height
9
+ * @return {Node | null}
10
+ * @method
11
+ * @protected
12
+ */
13
+ export const execute = (node, width, height) => {
14
+ if (node.used) {
15
+ const newNode = node.left?.insert(width, height);
16
+ return newNode ? newNode : node.right?.insert(width, height) || null;
17
+ }
18
+ if (width > node.w || height > node.h) {
19
+ return null;
20
+ }
21
+ if (width === node.w && height === node.h) {
22
+ node.used = true;
23
+ return node;
24
+ }
25
+ const dw = node.w - width;
26
+ const dh = node.h - height;
27
+ if (dw > dh) {
28
+ node.left = new Node(node.index, node.x, node.y, width, height);
29
+ node.right = new Node(node.index, node.x + width + 1, node.y, dw - 1, node.h);
30
+ }
31
+ else {
32
+ node.left = new Node(node.index, node.x, node.y, node.w, height);
33
+ node.right = new Node(node.index, node.x, node.y + height + 1, node.w, dh - 1);
34
+ }
35
+ node.used = true;
36
+ return node.left.insert(width, height);
37
+ };
package/src/Node.d.ts ADDED
@@ -0,0 +1,102 @@
1
+ /**
2
+ * @description テクスチャパッキングのノードクラス
3
+ * Node class for texture
4
+ *
5
+ * @class
6
+ * @public
7
+ */
8
+ export declare class Node {
9
+ /**
10
+ * @description パッキングされたテクスチャの識別番号
11
+ * Identification number of packed texture
12
+ *
13
+ * @type {number}
14
+ * @public
15
+ */
16
+ index: number;
17
+ /**
18
+ * @description パッキングされたテクスチャのx座標
19
+ * x coordinate of packed texture
20
+ *
21
+ * @type {number}
22
+ * @public
23
+ */
24
+ x: number;
25
+ /**
26
+ * @description パッキングされたテクスチャのy座標
27
+ * y coordinate of packed texture
28
+ *
29
+ * @type {number}
30
+ * @public
31
+ */
32
+ y: number;
33
+ /**
34
+ * @description パッキングされたテクスチャの幅
35
+ * Width of packed texture
36
+ *
37
+ * @type {number}
38
+ * @public
39
+ */
40
+ w: number;
41
+ /**
42
+ * @description パッキングされたテクスチャの高さ
43
+ * Height of packed texture
44
+ *
45
+ * @type {number}
46
+ * @public
47
+ */
48
+ h: number;
49
+ /**
50
+ * @description 左のノード
51
+ * Left node
52
+ *
53
+ * @type {Node}
54
+ * @public
55
+ */
56
+ left: Node | null;
57
+ /**
58
+ * @description 右のノード
59
+ * Right node
60
+ *
61
+ * @type {Node}
62
+ * @public
63
+ */
64
+ right: Node | null;
65
+ /**
66
+ * @description 使用済みフラグ
67
+ * Used flag
68
+ *
69
+ * @type {boolean}
70
+ * @public
71
+ */
72
+ used: boolean;
73
+ /**
74
+ * @param {number} x
75
+ * @param {number} y
76
+ * @param {number} w
77
+ * @param {number} h
78
+ * @constructor
79
+ * @public
80
+ */
81
+ constructor(index: number, x: number, y: number, w: number, h: number);
82
+ /**
83
+ * @description ノードにテクスチャを挿入、挿入したノードを返却します。
84
+ * Insert a texture into the node and return the inserted node.
85
+ *
86
+ * @param {number} width
87
+ * @param {number} height
88
+ * @return {Node | null}
89
+ * @method
90
+ * @public
91
+ */
92
+ insert(width: number, height: number): Node | null;
93
+ /**
94
+ * @description ノードを削除します。
95
+ * Remove the node.
96
+ *
97
+ * @return {boolean}
98
+ * @method
99
+ * @public
100
+ */
101
+ dispose(x: number, y: number, width: number, height: number): boolean;
102
+ }
package/src/Node.js ADDED
@@ -0,0 +1,157 @@
1
+ import { execute as nodeInsertService } from "./Node/service/NodeInsertService";
2
+ import { execute as nodeDisposeService } from "./Node/service/NodeDisposeService";
3
+ /**
4
+ * @description テクスチャパッキングのノードクラス
5
+ * Node class for texture
6
+ *
7
+ * @class
8
+ * @public
9
+ */
10
+ export class Node {
11
+ /**
12
+ * @param {number} x
13
+ * @param {number} y
14
+ * @param {number} w
15
+ * @param {number} h
16
+ * @constructor
17
+ * @public
18
+ */
19
+ constructor(index, x, y, w, h) {
20
+ /**
21
+ * @description パッキングされたテクスチャの識別番号
22
+ * Identification number of packed texture
23
+ *
24
+ * @type {number}
25
+ * @public
26
+ */
27
+ Object.defineProperty(this, "index", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ /**
34
+ * @description パッキングされたテクスチャのx座標
35
+ * x coordinate of packed texture
36
+ *
37
+ * @type {number}
38
+ * @public
39
+ */
40
+ Object.defineProperty(this, "x", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: void 0
45
+ });
46
+ /**
47
+ * @description パッキングされたテクスチャのy座標
48
+ * y coordinate of packed texture
49
+ *
50
+ * @type {number}
51
+ * @public
52
+ */
53
+ Object.defineProperty(this, "y", {
54
+ enumerable: true,
55
+ configurable: true,
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ /**
60
+ * @description パッキングされたテクスチャの幅
61
+ * Width of packed texture
62
+ *
63
+ * @type {number}
64
+ * @public
65
+ */
66
+ Object.defineProperty(this, "w", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: void 0
71
+ });
72
+ /**
73
+ * @description パッキングされたテクスチャの高さ
74
+ * Height of packed texture
75
+ *
76
+ * @type {number}
77
+ * @public
78
+ */
79
+ Object.defineProperty(this, "h", {
80
+ enumerable: true,
81
+ configurable: true,
82
+ writable: true,
83
+ value: void 0
84
+ });
85
+ /**
86
+ * @description 左のノード
87
+ * Left node
88
+ *
89
+ * @type {Node}
90
+ * @public
91
+ */
92
+ Object.defineProperty(this, "left", {
93
+ enumerable: true,
94
+ configurable: true,
95
+ writable: true,
96
+ value: void 0
97
+ });
98
+ /**
99
+ * @description 右のノード
100
+ * Right node
101
+ *
102
+ * @type {Node}
103
+ * @public
104
+ */
105
+ Object.defineProperty(this, "right", {
106
+ enumerable: true,
107
+ configurable: true,
108
+ writable: true,
109
+ value: void 0
110
+ });
111
+ /**
112
+ * @description 使用済みフラグ
113
+ * Used flag
114
+ *
115
+ * @type {boolean}
116
+ * @public
117
+ */
118
+ Object.defineProperty(this, "used", {
119
+ enumerable: true,
120
+ configurable: true,
121
+ writable: true,
122
+ value: void 0
123
+ });
124
+ this.index = index;
125
+ this.x = x;
126
+ this.y = y;
127
+ this.w = w;
128
+ this.h = h;
129
+ this.left = null;
130
+ this.right = null;
131
+ this.used = false;
132
+ }
133
+ /**
134
+ * @description ノードにテクスチャを挿入、挿入したノードを返却します。
135
+ * Insert a texture into the node and return the inserted node.
136
+ *
137
+ * @param {number} width
138
+ * @param {number} height
139
+ * @return {Node | null}
140
+ * @method
141
+ * @public
142
+ */
143
+ insert(width, height) {
144
+ return nodeInsertService(this, width, height);
145
+ }
146
+ /**
147
+ * @description ノードを削除します。
148
+ * Remove the node.
149
+ *
150
+ * @return {boolean}
151
+ * @method
152
+ * @public
153
+ */
154
+ dispose(x, y, width, height) {
155
+ return nodeDisposeService(this, x, y, width, height);
156
+ }
157
+ }
@@ -0,0 +1,50 @@
1
+ import { Node } from "./Node";
2
+ /**
3
+ * @description テクスチャパッカー
4
+ * Texture packer
5
+ *
6
+ * @class
7
+ * @public
8
+ */
9
+ export declare class TexturePacker {
10
+ /**
11
+ * @description ルートノード
12
+ * Root node
13
+ *
14
+ * @type {Node}
15
+ * @private
16
+ */
17
+ private readonly _$root;
18
+ /**
19
+ * @param {number} index
20
+ * @param {number} width
21
+ * @param {number} height
22
+ * @constructor
23
+ * @public
24
+ */
25
+ constructor(index: number, width: number, height: number);
26
+ /**
27
+ * @description テクスチャをパック、挿入したノードを返却します。
28
+ * Pack the texture and return the inserted node.
29
+ *
30
+ * @param {number} width
31
+ * @param {number} height
32
+ * @return {Node | null}
33
+ * @method
34
+ * @public
35
+ */
36
+ insert(width: number, height: number): Node | null;
37
+ /**
38
+ * @description Nodeを破棄します。
39
+ * Dispose of the Node.
40
+ *
41
+ * @param {number} x
42
+ * @param {number} y
43
+ * @param {number} width
44
+ * @param {number} height
45
+ * @return {boolean}
46
+ * @method
47
+ * @public
48
+ */
49
+ dispose(x: number, y: number, width: number, height: number): boolean;
50
+ }
@@ -0,0 +1,61 @@
1
+ import { Node } from "./Node";
2
+ /**
3
+ * @description テクスチャパッカー
4
+ * Texture packer
5
+ *
6
+ * @class
7
+ * @public
8
+ */
9
+ export class TexturePacker {
10
+ /**
11
+ * @param {number} index
12
+ * @param {number} width
13
+ * @param {number} height
14
+ * @constructor
15
+ * @public
16
+ */
17
+ constructor(index, width, height) {
18
+ /**
19
+ * @description ルートノード
20
+ * Root node
21
+ *
22
+ * @type {Node}
23
+ * @private
24
+ */
25
+ Object.defineProperty(this, "_$root", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: void 0
30
+ });
31
+ this._$root = new Node(index, 0, 0, width, height);
32
+ }
33
+ /**
34
+ * @description テクスチャをパック、挿入したノードを返却します。
35
+ * Pack the texture and return the inserted node.
36
+ *
37
+ * @param {number} width
38
+ * @param {number} height
39
+ * @return {Node | null}
40
+ * @method
41
+ * @public
42
+ */
43
+ insert(width, height) {
44
+ return this._$root.insert(width, height);
45
+ }
46
+ /**
47
+ * @description Nodeを破棄します。
48
+ * Dispose of the Node.
49
+ *
50
+ * @param {number} x
51
+ * @param {number} y
52
+ * @param {number} width
53
+ * @param {number} height
54
+ * @return {boolean}
55
+ * @method
56
+ * @public
57
+ */
58
+ dispose(x, y, width, height) {
59
+ return this._$root.dispose(x, y, width, height);
60
+ }
61
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./TexturePacker";
2
+ export * from "./Node";
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./TexturePacker";
2
+ export * from "./Node";