-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf.js
60 lines (52 loc) · 1.34 KB
/
shelf.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
class Shelf extends BaseObject {
dragging = false;
constructor(id, width, height, X, Y) {
super(id, width, height, X, Y);
}
createNewShelf(mouseX, mouseY) {
const newShelf = {
id: `S-${makeid(6)}`,
width: this.width,
height: this.height,
X: mouseX - this.width/2,
Y: mouseY - this.height/2,
};
shelfs.push(newShelf);
return newShelf;
}
isMouseClicked(mouseX, mouseY) {
return (mouseX > this.X && mouseX < (this.X + this.width) && mouseY > this.Y && mouseY < (this.Y + this.height))
}
render() {
if (this.id.includes("ST")) {
fill("gray");
} else {
fill("black");
}
super.render();
}
clicked(mouseX, mouseY, callback) {
if (this.id.includes("ST")) {
console.log(`Clicked Shelf Type with id: ${this.id}`);
// Product type element clicked
// Create new product
const newShelf = this.createNewShelf(mouseX, mouseY);
callback(newShelf);
} else {
console.log(`Clicked Shelf with id: ${this.id}`);
// Select product, drag it
this.X = mouseX - this.width / 2;
this.Y = mouseY - this.height / 2;
}
}
isDragging() {
return this.dragging;
}
move(mouseX, mouseY) {
this.X = mouseX - this.width / 2;
this.Y = mouseY - this.height / 2;
}
release() {
this.dragging = false;
}
}