4cd639e0db4c94a196056ad914f9c631df7cdf8a.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 = 18;
  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(0, 0, 0, 200);
  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 = 20;
  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.setOverFlow();
  94. this._bgNode.active = true;
  95. const uiOpacity = this._bgNode.getComponent(UIOpacity);
  96. tween(uiOpacity).delay(this._time).to(0.3, {
  97. opacity: 0
  98. }).call(() => {
  99. this._bgNode.destroy();
  100. }).start();
  101. }
  102. /**
  103. * 设置文字
  104. * @param text 文字
  105. * @returns
  106. */
  107. setText(text) {
  108. this._text = text;
  109. let label = this._textNode.getComponent(Label);
  110. label.string = this._text;
  111. return this;
  112. }
  113. /**
  114. * 设置文字大小
  115. * @param textSize 文字大小
  116. * @returns
  117. */
  118. setFontSize(textSize) {
  119. this._textSize = textSize;
  120. let label = this._textNode.getComponent(Label);
  121. label.fontSize = this._textSize;
  122. return this;
  123. }
  124. /**
  125. * 设置时间
  126. * @param time 时间
  127. * @returns
  128. */
  129. setTime(time) {
  130. this._time = time;
  131. return this;
  132. }
  133. /**
  134. * 设置位置
  135. * @param gravity 位置
  136. * @returns
  137. */
  138. setGravity(gravity) {
  139. this._gravity = gravity;
  140. return this;
  141. }
  142. setPosition() {
  143. let uiTransform = this._node.getComponent(UITransform);
  144. let bgUITransform = this._bgNode.getComponent(UITransform);
  145. if (Gravity.BOTTOM === this._gravity) {
  146. let y = -uiTransform.height / 2 + bgUITransform.height / 2 + 64;
  147. this._bgNode.position = v3(0, y, 0);
  148. }
  149. }
  150. setOverFlow() {
  151. let maxLength = this._node.getComponent(UITransform).width / 2;
  152. let label = this._textNode.getComponent(Label);
  153. let fontLength = this._text.length * label.fontSize;
  154. let uiTransform = this._textNode.getComponent(UITransform);
  155. if (fontLength > maxLength) {
  156. uiTransform.width = maxLength;
  157. label.overflow = Label.Overflow.RESIZE_HEIGHT;
  158. } else {
  159. uiTransform.width = fontLength;
  160. label.overflow = Label.Overflow.NONE;
  161. }
  162. let bgUITransform = this._bgNode.getComponent(UITransform);
  163. bgUITransform.width = uiTransform.width + label.fontSize * 4;
  164. bgUITransform.height = uiTransform.height;
  165. this.setPosition();
  166. }
  167. getPNode(root) {
  168. if (null == Toast.pNode || !Toast.pNode.isValid) {
  169. Toast.pNode = new Node('Toast');
  170. let transform = Toast.pNode.addComponent(UITransform);
  171. Toast.pNode.layer = Layers.Enum.UI_2D;
  172. root.addChild(Toast.pNode);
  173. Toast.pNode.setSiblingIndex(root.children.length);
  174. let size = view.getVisibleSize();
  175. transform.contentSize = size;
  176. transform.width = size.width;
  177. transform.height = size.height;
  178. }
  179. return Toast.pNode;
  180. }
  181. });
  182. Toast.LENGTH_SHORT = 2;
  183. // 短时间吐司
  184. Toast.LENGTH_LONG = 3.5;
  185. // 长时间吐司
  186. Toast.pNode = null;
  187. _cclegacy._RF.pop();
  188. _crd = false;
  189. }
  190. };
  191. });
  192. //# sourceMappingURL=4cd639e0db4c94a196056ad914f9c631df7cdf8a.js.map