-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBoxContain.js
63 lines (52 loc) · 1.72 KB
/
BoxContain.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
function BoxContain(object) {
this.pos = createVector(object.pos.x, object.pos.y);
this.size = createVector(object.size.x, object.size.y);
this.choosed = false;
this.allowChangesize = false;
this.allowChangepos = false;
this.object = object;
this.show = function(){
this.update();
noFill();
stroke(255);
rect(this.pos.x, this.pos.y, this.size.x, this.size.y);
// change size shape
if(this.allowChangesize) fill(255); else noFill();
rect(this.pos.x+this.size.x/2, this.pos.y+this.size.y/2, 20, 20);
// change position shape
if(this.allowChangepos) fill(255); else noFill();
ellipse(this.pos.x, this.pos.y, 20, 20);
}
this.update = function(){
if(dist(mouseX, mouseY, this.pos.x+this.size.x/2, this.pos.y+this.size.y/2) < 10){
this.allowChangesize = true;
} else if(dist(mouseX, mouseY, this.pos.x, this.pos.y) < 10){
this.allowChangepos = true;
} else {
this.allowChangesize = false;
this.allowChangepos = false;
}
}
this.mouseChoose = function(){
if(dist(mouseX, mouseY, this.pos.x+this.size.x/2, this.pos.y+this.size.y/2) < 10
|| dist(mouseX, mouseY, this.pos.x, this.pos.y) < 10){
this.choosed = true;
} else this.choosed = false;
}
this.drag = function() {
if( this.choosed){
if(this.allowChangesize){
var delx = mouseX-(this.pos.x+this.size.x/2);
var dely = mouseY-(this.pos.y+this.size.y/2);
this.pos.add(delx/2, dely/2);
this.size.add(delx, dely);
this.object.setPosition(this.pos.x, this.pos.y);
this.object.setSize(this.size.x, this.size.y);
} else if(this.allowChangepos){
this.pos= createVector(mouseX, mouseY);
this.object.setPosition(this.pos.x, this.pos.y);
this.object.setSize(this.size.x, this.size.y);
}
}
}
}