RollLabel.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import GameDataCenter from "../../data/GameDataCenter";
  2. import FguiLoadMgr from "../fgui/FguiLoadMgr";
  3. const { ccclass, disallowMultiple } = cc._decorator;
  4. /**
  5. * rolllabel
  6. */
  7. @ccclass
  8. @disallowMultiple
  9. export default class RollLabel extends cc.Component {
  10. private targetVal: number = null;
  11. private startVal: number = null;
  12. private cb: Function = null;
  13. private curVal: number = null;
  14. private isChange: boolean = false;
  15. //目标和起始值的差值
  16. private min: number;
  17. //管理文本对象
  18. obj: fairygui.GLabel | fairygui.GTextField | fairygui.GRichTextField = null;
  19. private upArrCom: fairygui.GComponent;
  20. private upEff: cc.Tween;
  21. private arr: fairygui.GComponent;
  22. changeTime: number;
  23. readonly defaulChangeTime = 0.5;
  24. readonly defaulOffsetX = 0.5;
  25. readonly defaulOffsetY = 0;
  26. endCb: Function;
  27. //累计时间
  28. private curTime: number = 0;
  29. setData(targetVal: number, cb: (curval: number) => void = null, param: RollLabelParam = null) {
  30. this.endCb = param.endCb;
  31. this.changeTime = param?.changeTime ? param.changeTime : this.defaulChangeTime;
  32. if (cb) this.cb = cb.bind(param?.target);
  33. if (param?.oldval) {
  34. this.initLabel(param.oldval);
  35. }
  36. if (this.targetVal != null) {
  37. if (this.targetVal == targetVal) return;
  38. this.StartRollNum(targetVal);
  39. if (param?.needArr) {
  40. if (this.upArrCom == null) {
  41. this.upArrCom = fgui.UIPackage.createObject("Common", "RollLabelEff") as fairygui.GComponent;
  42. this.obj.node.addChild(this.upArrCom.node);
  43. this.arr = this.upArrCom.getChild("arr") as fairygui.GComponent;
  44. // let leftupx = -this.obj.node.anchorX * this.obj.node.width;
  45. // let offsetx = param?.arrOffsetX != null ? param.arrOffsetX : this.defaulOffsetX;
  46. // let posx = leftupx + offsetx * this.obj.node.width;
  47. // let leftupy = (1 - this.obj.node.anchorY) * this.obj.node.height;
  48. // let offsety = param?.arrOffsetY != null ? param.arrOffsetY : this.defaulOffsetY;
  49. // let posy = leftupy + offsety * this.obj.node.height + this.arr.height / 2;
  50. let leftupx = 0;
  51. let offsetx = param?.arrOffsetX != null ? param.arrOffsetX : this.defaulOffsetX;
  52. let posx = leftupx + offsetx * this.obj.width;
  53. let leftupy = 0;
  54. let offsety = param?.arrOffsetY != null ? param.arrOffsetY : this.defaulOffsetY;
  55. let posy = leftupy - offsety * this.arr.height;
  56. this.upArrCom.x = posx;
  57. this.upArrCom.y = posy;
  58. this.upArrCom.touchable = false;
  59. }
  60. this.StartEff();
  61. }
  62. } else {
  63. this.initLabel(targetVal);
  64. }
  65. }
  66. initLabel(val) {
  67. this.targetVal = val;
  68. this.curVal = val;
  69. this.ChangeLabel(val);
  70. }
  71. ChangeLabel(num: number) {
  72. if (this.cb) {
  73. this.cb(num);
  74. } else {
  75. FguiLoadMgr.setLabel(this.obj, num?.toString());
  76. }
  77. }
  78. StartEff() {
  79. if (this.upEff) this.upEff.stop();
  80. this.upArrCom.visible = true;
  81. this.arr.y = 0;
  82. this.arr.alpha = 0;
  83. this.upEff = cc
  84. .tween(this.arr)
  85. .to(0.15, {
  86. alpha: 1,
  87. })
  88. .delay(0.2)
  89. .to(0.15, {
  90. alpha: 0,
  91. y: -16,
  92. })
  93. .call(() => {
  94. this.upArrCom.visible = false;
  95. })
  96. .start();
  97. }
  98. StartRollNum(num) {
  99. if (this.isChange) {
  100. this.curVal = this.targetVal;
  101. this.startVal = this.curVal;
  102. this.SetChangeState(num);
  103. } else {
  104. this.startVal = this.curVal;
  105. this.SetChangeState(num);
  106. }
  107. }
  108. SetChangeState(targetVal: number) {
  109. this.targetVal = targetVal;
  110. this.min = this.targetVal - this.startVal;
  111. this.curTime = 0;
  112. this.isChange = true;
  113. }
  114. protected update(dt: number): void {
  115. if (this.isChange) {
  116. this.curTime += dt;
  117. if (this.curTime > this.changeTime) this.curTime = this.changeTime;
  118. let persent = this.curTime / this.changeTime;
  119. this.curVal = Math.floor(persent * this.min + this.startVal);
  120. if (this.curVal == this.targetVal || this.curTime >= this.changeTime) {
  121. this.curVal = this.targetVal;
  122. this.isChange = false;
  123. if (this.endCb) this.endCb();
  124. }
  125. this.ChangeLabel(this.curVal);
  126. }
  127. }
  128. protected onDisable(): void {
  129. this.clearState();
  130. }
  131. private clearState() {
  132. if (this.upArrCom) this.upArrCom.visible = false;
  133. this.isChange = false;
  134. this.targetVal = null;
  135. this.curVal = null;
  136. this.startVal = null;
  137. this.min = 0;
  138. this.curTime = 0;
  139. }
  140. }
  141. export class RollLabelParam {
  142. /**
  143. * cb需要绑定的bind(target)
  144. */
  145. target?: any;
  146. /**
  147. * 是否需要箭头特效
  148. */
  149. needArr?: boolean;
  150. /**
  151. * 箭头特效向右偏移百分比,默认0.5(以文本宽度为100%
  152. */
  153. arrOffsetX?: number;
  154. /**
  155. * 箭头特效向上偏移百分比,默认0.5(以箭头高度为100%
  156. */
  157. arrOffsetY?: number;
  158. /**
  159. * 是否需要一开始就改变数字
  160. */
  161. oldval?: number;
  162. /**
  163. * 改变时间
  164. */
  165. changeTime?: number;
  166. /**
  167. * 完成时的回调
  168. */
  169. endCb?: Function;
  170. }