UINotify.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { Label, Prefab, instantiate, Node, math } from "cc";
  2. import { Layout_UI_Notify } from "./Layout_UI_Notify";
  3. import { GameUILayers, gui, ui_base } from "../../core/ui/ui";
  4. import { ModuleDef } from "../../Scripts/ModuleDef";
  5. class NotifyInfo {
  6. private static readonly time: number = 2;
  7. private static readonly init_y: number = 120;
  8. private static readonly up_y: number = 80;//上升一格
  9. private static readonly move_speed: number = 880;//向上移动速度
  10. private static readonly sc_state0: number = 0.8;//变小大小
  11. private static readonly sc_state0_time: number = 0.22;
  12. private static readonly sc_state1: number = 1.2;//变大大小
  13. private static readonly sc_state1_time: number = 0.41;
  14. private static readonly sc_state2: number = 1;//正常大小
  15. private time: number;
  16. private node: Node;
  17. private state: number;
  18. private _state_time: number = 0;
  19. public constructor(node: Node, parent: Node, txt: string) {
  20. this.time = NotifyInfo.time;
  21. this.node = node;
  22. this.state = 0;
  23. this._state_time = 0;
  24. let label = node.getComponentInChildren(Label);
  25. label.string = txt;
  26. node.parent = parent;
  27. node.setPosition(0, NotifyInfo.init_y, 0);
  28. node.setScale(NotifyInfo.sc_state0, NotifyInfo.sc_state0, NotifyInfo.sc_state0);
  29. node.active = true;
  30. }
  31. public up(): void {
  32. // this.node.setPosition(this.node.position.x, this.node.position.y + NotifyInfo.up_y, this.node.position.z);
  33. }
  34. public run(dt: number): boolean {
  35. this.time -= dt;
  36. if (this.time <= 0) {
  37. this.node.destroy();
  38. this.node = null;
  39. return true;
  40. } else if (this.time < 0.5) {
  41. // this.node.setPosition(this.node.position.x, this.node.position.y + NotifyInfo.move_speed * dt, this.node.position.z);
  42. } else {
  43. if (this.state === 0) {
  44. this._state_time += dt;
  45. let ratio = this._state_time / NotifyInfo.sc_state0_time;
  46. if (ratio >= 1) {
  47. this.state = 1;
  48. this._state_time = 0;
  49. this.node.setScale(NotifyInfo.sc_state1, NotifyInfo.sc_state1, NotifyInfo.sc_state1);
  50. } else {
  51. let sc = math.lerp(NotifyInfo.sc_state0, NotifyInfo.sc_state1, ratio);
  52. this.node.setScale(sc, sc, sc);
  53. }
  54. } else if (this.state === 1) {
  55. this._state_time += dt;
  56. let ratio = this._state_time / NotifyInfo.sc_state1_time;
  57. if (ratio >= 1) {
  58. this.state = 2;
  59. this._state_time = 0;
  60. this.node.setScale(NotifyInfo.sc_state2, NotifyInfo.sc_state2, NotifyInfo.sc_state2);
  61. } else {
  62. let sc = math.lerp(NotifyInfo.sc_state1, NotifyInfo.sc_state2, ratio);
  63. this.node.setScale(sc, sc, sc);
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69. }
  70. export class UINotify extends ui_base {
  71. constructor() {
  72. super(ModuleDef.BASIC, 'ui_notify/UI_Notify', GameUILayers.NOTICE, Layout_UI_Notify);
  73. }
  74. static async show(msg: string) {
  75. let ui = gui.get(UINotify);
  76. if (!ui) ui = await gui.show(UINotify);
  77. ui.newNotify(msg);
  78. }
  79. private _info_prefab: Prefab;
  80. private notifys: NotifyInfo[] = [];
  81. protected onCreated(): void {
  82. let layout = this.getLayout<Layout_UI_Notify>();
  83. this._info_prefab = layout.info_prefab;
  84. }
  85. public newNotify(txt: string) {
  86. let info = new NotifyInfo(instantiate(this._info_prefab), this.node, txt);
  87. for (var i = this.notifys.length - 1; i >= 0; i--)this.notifys[i].up();
  88. this.notifys.push(info);
  89. }
  90. protected onUpdate(dt?: number): void {
  91. for (var i = this.notifys.length - 1; i >= 0; i--)if (this.notifys[i].run(dt)) this.notifys.splice(i, 1);
  92. }
  93. dispose(): void {
  94. this._info_prefab = null;
  95. }
  96. }