CocosCreator简易开源框架之吐司

代码

这个没有什么原理性的东西,直接贴上代码【位置、颜色等需要自行扩展】

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
*
* @file Toast.ts
* @author dream
* @description 模拟安卓吐司效果
*
*/

import { BlockInputEvents, Canvas, color, director, ImageAsset, Label, Layers, Node, Sprite, SpriteFrame, Texture2D, tween, UIOpacity, UITransform, v3, view } from "cc";

/**
* 位置
*/
export enum Gravity {

BOTTOM,

}

export class Toast {

static readonly LENGTH_SHORT: number = 2; // 短时间吐司
static readonly LENGTH_LONG: number = 3.5; // 长时间吐司

private static pNode: Node | null = null;


private node: Node;
private _text: string = '';
/**
* 文字
*/
get text() {
return this._text;
}
set text(text: string) {
this._text = text;
this.setText();
}
/**
* 时间
*/
private _time: number = 1;
get time() {
return this._time;
}
set time(time: number) {
this._time = time;
}

/**
* 字体大小
*/
private _textSize: number = 20;
get textSize() {
return this._textSize;
}
set textSize(size: number) {
this._textSize = size;
this.setTextSize();
}

/**
* 位置
*/
private _gravity: Gravity = Gravity.BOTTOM;
get gravity() {
return this._gravity;
}
set gravity(gravity: Gravity) {
this._gravity = gravity;
}

private bgNode: Node;
private textNode: Node;

private constructor(node: Node | null) {
if (null == node) {
this.node = this.getPNode();
} else {
this.node = node;
}

this.bgNode = new Node();
this.bgNode.layer = Layers.Enum.UI_2D;
this.bgNode.addComponent(BlockInputEvents);
let sprite = this.bgNode.addComponent(Sprite);
sprite.type = Sprite.Type.SLICED;
let imageObj = new Image();
imageObj.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAQMAAABIeJ9nAAAAA1BMVEX///+nxBvIAAAACklEQVQI12MAAgAABAABINItbwAAAABJRU5ErkJggg==";
let textureObj = new Texture2D();
textureObj.image = new ImageAsset(imageObj);
let sf = new SpriteFrame();
sf.texture = textureObj;
sprite.spriteFrame = sf;
sprite.color = color(0, 0, 0, 200);
this.bgNode.active = false;

this.textNode = new Node('Text');
this.textNode.layer = Layers.Enum.UI_2D;
let uiTransform = this.textNode.addComponent(UITransform);
uiTransform.width = this.node.getComponent(UITransform)!.width;
let label = this.textNode.addComponent(Label);
label.horizontalAlign = Label.HorizontalAlign.CENTER;
label.verticalAlign = Label.VerticalAlign.CENTER;
this.textSize = 20;
this.textNode.parent = this.bgNode;

this.bgNode.parent = this.node;
}

/**
* 生成吐司
* @param node
* @param text
* @param time
* @returns
*/
static makeText(node: Node | null, text: string, time: number) {
let toast = new Toast(node);
toast.text = text;
toast.time = time;
return toast;
}

/**
* 显示吐司
*/
show() {
this.setOverFlow();
this.bgNode.active = true;
let uiOpacity = this.bgNode.getComponent(UIOpacity);
if (null == uiOpacity) {
uiOpacity = this.bgNode.addComponent(UIOpacity);
}
tween(uiOpacity)
.delay(this.time)
.to(0.3, { opacity: 0 })
.call(() => {
this.bgNode.destroy();
})
.start();
}

private setText() {
let label = this.textNode.getComponent(Label)!;
label.string = this.text;
}

private setTextSize() {
let label = this.textNode.getComponent(Label)!;
label.fontSize = this.textSize;
}

private setGravity() {
let uiTransform = this.node.getComponent(UITransform)!;
let bgUITransform = this.bgNode.getComponent(UITransform)!;
if (Gravity.BOTTOM === this.gravity) {
let y = -uiTransform.height / 2 + bgUITransform.height / 2 + 64;
this.bgNode.position = v3(0, y, 0);
}
}

private setOverFlow() {
let maxLength = this.node.getComponent(UITransform)!.width / 2;
let label = this.textNode.getComponent(Label)!;
let fontLength = this.text.length * label.fontSize;
let uiTransform = this.textNode.getComponent(UITransform)!;
if (fontLength > maxLength) {
uiTransform.width = maxLength;
label.overflow = Label.Overflow.RESIZE_HEIGHT;
} else {
uiTransform.width = fontLength;
label.overflow = Label.Overflow.NONE;
}
let bgUITransform = this.bgNode.getComponent(UITransform)!;
bgUITransform.width = uiTransform.width + label.fontSize * 4;
bgUITransform.height = uiTransform.height;
this.setGravity();
}

private getPNode(): Node {
if (null == Toast.pNode || !Toast.pNode.isValid) {
Toast.pNode = new Node('Toast');
let transform = Toast.pNode.addComponent(UITransform);
Toast.pNode.layer = Layers.Enum.UI_2D;
Toast.pNode.addComponent(Canvas);
director.getScene()?.addChild(Toast.pNode);
let size = view.getVisibleSize();
transform.contentSize = size;
transform.width = size.width;
transform.height = size.height;
Toast.pNode.position = v3(size.width / 2, size.height / 2, 0);
}
return Toast.pNode;
}

}

系列文章

  1. CocosCreator简易开源框架SCL之白皮书
  2. CocosCreator开源框架SCL之弹框管理
  3. CocosCreator开源框架SCL之本地化存储
  4. CososCreator开源框架SCL之屏幕适配

该框架开发的小游戏:翻棋子游戏

代码已上传到git: github / 同步镜像gitee

  • 如需2.x版本,请看CCCLibs仓库该仓库并没有整理,请自行参考

有任何建议或者疑问,可以关注作者公众号

文章目录