-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathCapsule.js
126 lines (110 loc) · 3.56 KB
/
Capsule.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Created by Jerome Renaux (jerome.renaux@gmail.com) on 31-08-19.
*/
function Capsule(scene,x,y,iconAtlas,iconFrame,container){
this.scene = scene;
this.slices = [];
this.icon = null;
this.width = 1;
this.width_ = this.width; // previous width
var capsuleDepth = 2;
var contentDepth = 3;
if(iconFrame) {
this.icon = this.scene.add.sprite(x+8,y+6,iconAtlas,iconFrame);
this.icon.setDepth(contentDepth);
this.icon.setScrollFactor(0);
this.icon.setDisplayOrigin(0,0);
this.icon.setVisible(false);
}
var textX = (this.icon ? x + this.icon.width : x) + 10;
var textY = (this.icon ? y +1: y+2);
this.text = this.scene.add.text(textX, textY, '',
{ font: '16px belwe', fill: '#ffffff', stroke: '#000000', strokeThickness: 3 }
);
this.slices.push(this.scene.add.sprite(x,y,'UI','capsule-left'));
x += 24;
this.slices.push(this.scene.add.tileSprite(x,y,this.width,24,'UI','capsule-middle'));
x += this.width;
this.slices.push(this.scene.add.sprite(x,y,'UI','capsule-right'));
this.slices.forEach(function(e){
e.setDepth(capsuleDepth);
e.setScrollFactor(0);
e.setDisplayOrigin(0,0);
e.setVisible(false);
if(container) container.push(e); // don't use concat
});
this.text.setDepth(contentDepth);
this.text.setScrollFactor(0);
this.text.setDisplayOrigin(0,0);
this.text.setVisible(false);
if(container) {
container.push(this.text);
if (this.icon) container.push(this.icon);
}
}
Capsule.prototype.setHoverText = function(tooltip,title,text){
var w = this.slices[0].width + this.slices[1].width + this.slices[2].width;
var zone = this.scene.add.zone(this.slices[0].x,this.slices[0].y,w,this.slices[1].height);
zone.setDepth(10);
zone.setOrigin(0);
zone.setScrollFactor(0);
zone.setInteractive();
zone.setVisible(false);
zone.on('pointerover',function(){
tooltip.updateInfo('free',{title:title,body:text});
tooltip.display();
});
zone.on('pointerout',function(){
tooltip.hide();
});
this.zone = zone;
};
Capsule.prototype.setText = function(text){
this.text.setText(text);
this.width = this.text.width -25;
if(this.icon) this.width += this.icon.width;
this.slices[1].width = this.width;
if(this.extraW) this.slices[1].width += this.extraW;
this.slices[2].x += (this.width-this.width_);
this.width_ = this.width;
if(this.zone){
var w = this.slices[0].width + this.slices[1].width + this.slices[2].width;
this.zone.setSize(w,this.zone.height,true);
}
};
Capsule.prototype.removeLeft = function(){
//var w = this.slices[0].width;
//console.warn(w);
this.extraW = 17;
this.slices[0].destroy();
this.slices[1].x -= this.extraW;
};
Capsule.prototype.display = function(){
this.slices.forEach(function(e){
e.setVisible(true);
});
this.text.setVisible(true);
if(this.zone) this.zone.setVisible(true);
if(this.icon) this.icon.setVisible(true);
};
Capsule.prototype.hide = function(){
this.slices.forEach(function(e){
e.setVisible(false);
});
this.text.setVisible(false);
if(this.zone) this.zone.setVisible(false);
if(this.icon) this.icon.setVisible(false);
};
Capsule.prototype.move = function(dx,dy){
this.slices.forEach(function(e){
e.x += dx;
e.y += dy;
});
this.text.x += dx;
this.text.y += dy;
if(this.icon) {
this.icon.x += dx;
this.icon.y += dy;
}
};
export default Capsule