Skip to content

Commit 988f35f

Browse files
eivers88etsaaron
authored andcommitted
texture packer progress
1 parent f9d3093 commit 988f35f

File tree

4 files changed

+331
-97
lines changed

4 files changed

+331
-97
lines changed

assets/js/app/controller.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class Controller {
2121
this.view.bindRemoveBtn(this.removeImage.bind(this));
2222
this.view.bindSettingsInputs(this.updateSettingsValues.bind(this));
2323

24-
this.texturePacker = new TexturePacker(this.view.$canvas);
24+
this.texturePacker = new TexturePacker(this.view.$canvas, this.view.getSettingsValues());
2525

2626
// console.log(this)
2727

@@ -76,8 +76,6 @@ export default class Controller {
7676

7777

7878
onLoadSuccess (texture) {
79-
// TODO: Pass on to our texture packer
80-
// console.log('onLoadSuccess', texture);
8179

8280
this.texturePacker.addTexture(texture);
8381

@@ -94,8 +92,9 @@ export default class Controller {
9492
this.loadInProgress = false;
9593
this.imgQueued = 0;
9694
this.imgLoaded = 0;
97-
// TODO: Sort and generate our sprite sheet
9895
this.texturePacker.sort();
96+
this.texturePacker.fit();
97+
this.texturePacker.draw();
9998
}
10099

101100
updateSettingsValues (settings) {

assets/js/app/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let app = {
1818

1919
new Controller(store, view);
2020

21-
console.log('app started');
21+
console.log('app started!');
2222

2323
}
2424

assets/js/app/texture-packer.js

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,130 @@
1+
function findNode(root, w, h) {
2+
if (root.used) {
3+
return findNode(root.right, w, h) || findNode(root.down, w, h);
4+
} else if ((w <= root.w) && (h <= root.h)) {
5+
return root;
6+
} else {
7+
return null;
8+
}
9+
}
10+
11+
function splitNode(node, w, h) {
12+
node.used = true;
13+
node.down = {x: node.x, y: node.y + h, w: node.w, h: node.h - h};
14+
node.right = {x: node.x + w, y: node.y, w: node.w - w, h: h};
15+
return node;
16+
}
17+
118
export default class TexturePacker {
219

3-
constructor(canvas) {
20+
constructor(canvas, {padding, prefix, path}) {
421

522
this.canvas = canvas;
623
this.textures = [];
724

25+
this.root = {
26+
x: 0, // origin x
27+
y: 0, // origin y
28+
w: 256 - padding, // width
29+
h: 256 - padding, // height
30+
p: padding
31+
};
32+
33+
this.prefix = prefix;
34+
this.path = path;
35+
36+
console.log('packer', this);
37+
838
}
939

1040
addTexture(texture) {
1141
this.textures.push(texture);
1242
}
1343

1444
sort() {
15-
console.log('sort', this.textures);
45+
this.textures.sort((a, b) => {
46+
if (a.h < b.h) {
47+
return 1;
48+
}
49+
if (a.h > b.h) {
50+
return -1;
51+
}
52+
return 0;
53+
});
54+
}
55+
56+
fit() {
57+
let i, node, texture, pad = this.root.p;
58+
for (i = 0; i < this.textures.length; i++) {
59+
texture = this.textures[i];
60+
texture.fit = false;
61+
node = findNode(this.root, texture.w + pad, texture.h + pad);
62+
if (node) {
63+
texture.fit = splitNode(node, texture.w + pad, texture.h + pad);
64+
}
65+
if (!texture.fit) {
66+
this.resize();
67+
break;
68+
}
69+
}
70+
}
71+
72+
resize() {
73+
let w, h, pad = this.root.p;
74+
if (this.root.w > this.root.h) {
75+
w = (this.root.w + pad);
76+
h = (this.root.h + pad) * 2;
77+
} else {
78+
w = (this.root.w + pad) * 2;
79+
h = (this.root.h + pad);
80+
}
81+
this.root = {
82+
x: 0, // origin x
83+
y: 0, // origin y
84+
w: w - pad, // width
85+
h: h - pad, // height
86+
p: pad
87+
};
88+
this.fit();
89+
}
90+
91+
draw() {
92+
93+
// TODO: Calc CSS output
94+
95+
let canvas = this.canvas;
96+
let ctx = canvas.getContext('2d');
97+
let pad = this.root.p;
98+
let width = this.root.w + pad;
99+
let height = this.root.h + pad;
100+
101+
canvas.width = width;
102+
canvas.height = height;
103+
104+
ctx.clearRect(0, 0, width, height);
105+
106+
this.textures.sort((a, b) => {
107+
let nameA = a.name.toUpperCase();
108+
let nameB = b.name.toUpperCase();
109+
return (nameA < nameB) ? -1 : (nameA > nameB) ? 1 : 0;
110+
});
111+
112+
for(let i = 0; i < this.textures.length; i++) {
113+
let texture = this.textures[i];
114+
if(texture.fit) {
115+
116+
// turn on for testing
117+
ctx.fillRect(texture.fit.x + pad, texture.fit.y + pad, texture.w, texture.h);
118+
ctx.stroke();
119+
120+
ctx.drawImage(texture.img, texture.fit.x + pad, texture.fit.y + pad);
121+
}
122+
}
123+
124+
}
125+
126+
update() {
127+
// TODO: Update texture packer when settings change or images are added/removed
16128
}
17129

18130
}

0 commit comments

Comments
 (0)