-
Notifications
You must be signed in to change notification settings - Fork 0
/
Box.js
66 lines (58 loc) · 2.18 KB
/
Box.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
let instanceNumber = 1;
let chars = [];
module.exports = class Box {
constructor(obj) {
this.childBoxes = [];
Object.assign(this, obj);
obj.boxes.forEach((val, idx) => {
this.childBoxes.push(new Box(val));
});
this.x = this.y = 0;
this.width = this.height = 3;
this.instanceNumber = instanceNumber++;
}
GetCoords(xOffset, yOffset) {
this.x = xOffset;
this.y = yOffset;
let nextOffset = { x: this.x + 1, y: this.y + 1 };
//console.log(`box ${this.instanceNumber} width before: ${this.width}`);
for (let child in this.childBoxes) {
let coords = this.childBoxes[child].GetCoords(
nextOffset.x + 1,
nextOffset.y + 1
);
this.width += coords.width + 1;
this.height += coords.height + 1;
//if (this.stack == "v") this.y += this.height + 1;
//else this.x += this.width + 1;
}
//console.log(`box ${this.instanceNumber} x:${this.x} y:${this.y} w:${this.width} h:${this.height}`);
//console.log(this);
return { x: this.x, y: this.y, width: this.width, height: this.height };
}
Draw(chars) {
if (!chars[this.x]) chars[this.x] = [];
if (!chars[this.y]) chars[this.y] = [];
// corners
chars[this.x][this.y] = "+";
chars[this.x][this.y + this.height - 1] = "+";
chars[this.x + this.width - 1][this.y + this.height - 1] = "+";
chars[this.x + this.width - 1][this.y] = "+";
// top and bottom rows
for (let x = this.x + 1; x < this.x + this.width - 1; x++) {
//console.log(`box ${this.instanceNumber} fill ${this.y},${x} with -`);
chars[this.y][x] = "-";
let y = this.y + this.height - 1;
//console.log(`box ${this.instanceNumber} fill ${y},${x} with -`);
chars[this.y + this.height - 1][x] = "-";
}
for (let row = this.y + 1; row < this.y + this.height - 1; row++) {
chars[row][this.x] = "|";
//console.log(`box ${this.instanceNumber} fill ${row},${this.x} with |`);
chars[row][this.x + this.width - 1] = "|";
//console.log(`box ${this.instanceNumber} fill ${row},${this.x + this.width - 1} with |`);
}
for (let child in this.childBoxes) this.childBoxes[child].Draw(chars);
return chars;
}
};