-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathToast.js
More file actions
141 lines (132 loc) · 5.16 KB
/
Copy pathToast.js
File metadata and controls
141 lines (132 loc) · 5.16 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var Toast = {
LENGTH_LONG: 3.5,
LENGTH_SHORT: 2,
CENTER: 0,
TOP: 1,
TOP_LEFT: 2,
LEFT: 3,
BOTTOM_LEFT: 4,
BOTTOM: 5,
BOTTOM_RIGHT: 6,
RIGHT: 7,
TOP_RIGHT: 8,
};
Toast.makeText = function (text, duration) {
let _text;
let _duration;
let _gravity;
let _x = 0;
let _y = 0;
let ToastObject = function (tText, tDuration) {
_text = tText;
_duration = tDuration;
//
this.setGravity = function (gravity, x, y) {
_gravity = gravity;
_x = x;
_y = y;
}
//
this.show = function () {
// 加载背景纹理
if (Toast.bgSpriteFrame === undefined) {
self = this;
(function () {
cc.loader.load({ 'uuid': 'b43ff3c2-02bb-4874-81f7-f2dea6970f18' },
function (error, result) {
if (error) {
cc.error(error);
return;
}
Toast.bgSpriteFrame = new cc.SpriteFrame(result);
Toast.bgSpriteFrame.insetTop = 3;
Toast.bgSpriteFrame.insetBottom = 3;
Toast.bgSpriteFrame.insetLeft = 4;
Toast.bgSpriteFrame.insetRight = 4;
//加载完再调用
self.show();
})
})();
return;
}
// canvas
var canvas = cc.director.getScene().getComponentInChildren(cc.Canvas);
var width = canvas.node.width;
var height = canvas.node.height;
if (_duration === undefined) {
_duration = LENGTH_SHORT;
}
// 背景图片设置
let bgNode = new cc.Node();
// 背景图片透明度
bgNode.opacity = 200;
let bgSprite = bgNode.addComponent(cc.Sprite);
bgSprite.type = cc.Sprite.Type.SLICED;
let bgLayout = bgNode.addComponent(cc.Layout);
bgLayout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
// Lable文本格式设置
let textNode = new cc.Node();
let textLabel = textNode.addComponent(cc.Label);
textLabel.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
textLabel.verticalAlign = cc.Label.VerticalAlign.CENTER;
textLabel.fontSize = 20;
textLabel.string = _text;
//背景图片与文本内容的间距
let hPadding = textLabel.fontSize / 8;
let vPadding = 2;
bgLayout.paddingLeft = hPadding;
bgLayout.paddingRight = hPadding;
bgLayout.paddingTop = vPadding;
bgLayout.paddingBottom = vPadding;
// 当文本宽度过长时,设置为自动换行格式
if (_text.length * textLabel.fontSize > width / 3) {
textNode.width = width / 3;
textLabel.overflow = cc.Label.Overflow.RESIZE_HEIGHT;
}
bgNode.addChild(textNode);
if (Toast.bgSpriteFrame) {
bgSprite.spriteFrame = Toast.bgSpriteFrame;
}
// gravity 设置Toast显示的位置
if (_gravity == Toast.CENTER) {
textNode.y = 0;
textNode.x = 0;
} else if (_gravity == Toast.TOP) {
textNode.y = textNode.y + (height / 5) * 2;
} else if (_gravity == Toast.TOP_LEFT) {
textNode.y = textNode.y + (height / 5) * 2;
textNode.x = textNode.x + (width / 5);
} else if (_gravity == Toast.LEFT) {
textNode.x = textNode.x + (width / 5);
} else if (_gravity == Toast.BOTTOM_LEFT) {
textNode.y = textNode.y - (height / 5) * 2;
textNode.x = textNode.x + (width / 5);
} else if (_gravity == Toast.BOTTOM) {
textNode.y = textNode.y - (height / 5) * 2;
} else if (_gravity == Toast.BOTTOM_RIGHT) {
textNode.y = textNode.y - (height / 5) * 2;
textNode.x = textNode.x - (width / 5);
} else if (_gravity == Toast.RIGHT) {
textNode.x = textNode.x - (width / 5);
} else if (_gravity == Toast.TOP_RIGHT) {
textNode.y = textNode.y + (height / 5) * 2;
textNode.x = textNode.x - (width / 5);
} else {
// 默认情况 BOTTOM
textNode.y = textNode.y - (height / 5) * 2;
}
textNode.x = textNode.x + _x;
textNode.y = textNode.y + _y;
canvas.node.addChild(bgNode);
let finished = cc.callFunc(function (target) {
bgNode.destroy();
}, self);
let action = cc.sequence(cc.moveBy(_duration,cc.p(0,0)),cc.fadeOut(0.3), finished);
bgNode.runAction(action);
}
}
return new ToastObject(text, duration);
};
Toast.showText = function (text, duration) {
Toast.makeText(text, duration).show();
}