Toast.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { BlockInputEvents, color, ImageAsset, Label, Layers, Node, Sprite, SpriteFrame, Texture2D, tween, UIOpacity, UITransform, v3, view } from "cc";
  2. /**
  3. * 位置
  4. */
  5. export enum Gravity {
  6. BOTTOM,
  7. }
  8. const imageObj = new Image();
  9. imageObj.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAQMAAABIeJ9nAAAAA1BMVEX///+nxBvIAAAACklEQVQI12MAAgAABAABINItbwAAAABJRU5ErkJggg==";
  10. const imageAsset = new ImageAsset(imageObj);
  11. /*
  12. Toast.makeText(gui.getLayerNode(5),"可笑可笑1").show();
  13. */
  14. export class Toast {
  15. static readonly LENGTH_SHORT = 1; // 短时间吐司
  16. static readonly LENGTH_LONG = 3.5; // 长时间吐司
  17. private static pNode: Node | null = null;
  18. private _bgNode: Node = null!;
  19. private _textNode: Node = null!;
  20. private _node: Node = null!;
  21. private _text = '';
  22. private _time = 0;
  23. private _textSize = 26;
  24. private _gravity = Gravity.BOTTOM;
  25. private constructor(root:Node) {
  26. this._node = this.getPNode(root);
  27. this._bgNode = new Node();
  28. this._bgNode.layer = Layers.Enum.UI_2D;
  29. this._bgNode.addComponent(BlockInputEvents);
  30. const sprite = this._bgNode.addComponent(Sprite);
  31. sprite.type = Sprite.Type.SLICED;
  32. const textureObj = new Texture2D();
  33. textureObj.image = imageAsset;
  34. const sf = new SpriteFrame();
  35. sf.texture = textureObj;
  36. sprite.spriteFrame = sf;
  37. sprite.color = color(128, 128, 128, 255);
  38. this._bgNode.addComponent(UIOpacity);
  39. this._bgNode.active = false;
  40. this._textNode = new Node('Text');
  41. this._textNode.layer = Layers.Enum.UI_2D;
  42. const uiTransform = this._textNode.addComponent(UITransform);
  43. uiTransform.width = this._node.getComponent(UITransform)!.width;
  44. const label = this._textNode.addComponent(Label);
  45. label.horizontalAlign = Label.HorizontalAlign.CENTER;
  46. label.verticalAlign = Label.VerticalAlign.CENTER;
  47. this._textSize = 26;
  48. this._textNode.parent = this._bgNode;
  49. this._bgNode.parent = this._node;
  50. }
  51. /**
  52. * 生成吐司
  53. * @param node
  54. * @param text
  55. * @param time
  56. * @returns
  57. */
  58. static makeText(parent:Node, text: string, time:number=Toast.LENGTH_SHORT) {
  59. let toast = new Toast(parent);
  60. toast.setText(text);
  61. toast.setTime(time);
  62. return toast;
  63. }
  64. /**
  65. * 显示吐司
  66. */
  67. show() {
  68. this.setFontSize(this._textSize);
  69. this.setOverFlow();
  70. this._bgNode.active = true;
  71. const uiOpacity = this._bgNode.getComponent(UIOpacity);
  72. tween(uiOpacity)
  73. .delay(this._time)
  74. .to(0.3, { opacity: 0 })
  75. .call(() => {
  76. this._bgNode.destroy();
  77. })
  78. .start();
  79. }
  80. /**
  81. * 设置文字
  82. * @param text 文字
  83. * @returns
  84. */
  85. setText(text: string): Toast {
  86. this._text = text;
  87. let label = this._textNode.getComponent(Label)!;
  88. label.string = this._text;
  89. return this;
  90. }
  91. /**
  92. * 设置文字大小
  93. * @param textSize 文字大小
  94. * @returns
  95. */
  96. setFontSize(textSize: number): Toast {
  97. this._textSize = textSize;
  98. let label = this._textNode.getComponent(Label)!;
  99. label.fontSize = this._textSize;
  100. return this;
  101. }
  102. /**
  103. * 设置时间
  104. * @param time 时间
  105. * @returns
  106. */
  107. setTime(time: number) {
  108. this._time = time;
  109. return this;
  110. }
  111. /**
  112. * 设置位置
  113. * @param gravity 位置
  114. * @returns
  115. */
  116. setGravity(gravity: Gravity): Toast {
  117. this._gravity = gravity;
  118. return this;
  119. }
  120. private setPosition() {
  121. let uiTransform = this._node.getComponent(UITransform)!;
  122. let bgUITransform = this._bgNode.getComponent(UITransform)!;
  123. if (Gravity.BOTTOM === this._gravity) {
  124. let y = bgUITransform.height / 2;
  125. this._bgNode.position = v3(0, y, 0);
  126. }
  127. }
  128. private setOverFlow() {
  129. let maxLength = this._node.getComponent(UITransform)!.width / 2;
  130. let label = this._textNode.getComponent(Label)!;
  131. let fontLength = this._text.length * label.fontSize;
  132. let uiTransform = this._textNode.getComponent(UITransform)!;
  133. if (fontLength > maxLength) {
  134. uiTransform.width = maxLength;
  135. label.overflow = Label.Overflow.RESIZE_HEIGHT;
  136. } else {
  137. uiTransform.width = fontLength;
  138. label.overflow = Label.Overflow.NONE;
  139. }
  140. let bgUITransform = this._bgNode.getComponent(UITransform)!;
  141. bgUITransform.width = uiTransform.width + label.fontSize * 4;
  142. bgUITransform.height = uiTransform.height;
  143. this.setPosition();
  144. }
  145. private getPNode(root:Node): Node {
  146. if (null == Toast.pNode || !Toast.pNode.isValid) {
  147. Toast.pNode = new Node('Toast');
  148. let transform = Toast.pNode.addComponent(UITransform);
  149. Toast.pNode.layer = Layers.Enum.UI_2D;
  150. root.addChild(Toast.pNode);
  151. Toast.pNode.setSiblingIndex(root.children.length)
  152. let size = view.getVisibleSize();
  153. transform.contentSize = size;
  154. transform.width = size.width;
  155. transform.height = size.height;
  156. }
  157. return Toast.pNode;
  158. }
  159. }