5d8144768428205eb83d54c7574071768f6c1b5b.js 6.9 KB

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