lq_base_util.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {LQPlatformUtil} from "./lq_platform_util";
  2. import {LQPlatformType} from "../data/lq_const";
  3. import view = cc.view;
  4. import Vec2 = cc.Vec2;
  5. export class LQBaseUtil {
  6. // public static readonly unit_arr = ['K', 'M', 'B', 'T'];
  7. public static has_value<T>(arr: T[], v: T): boolean {
  8. let has = false;
  9. for (let i = 0; i < arr.length; i++) {
  10. if (arr[i] === v) {
  11. has = true;
  12. break;
  13. }
  14. }
  15. return has;
  16. }
  17. public static get_value_by_duration(percent: number, timeline: Vec2[]): number {
  18. if (timeline.length === 0) {
  19. return 1;
  20. }
  21. let end_index = -1;
  22. for (let i = 1; i < timeline.length; i++) {
  23. if (timeline[i].x > percent) {
  24. end_index = i;
  25. break;
  26. }
  27. }
  28. if (end_index === -1) {
  29. return timeline[timeline.length - 1].y;
  30. }
  31. const start_index = end_index - 1;
  32. return timeline[start_index].y + (timeline[end_index].y - timeline[start_index].y) * ((percent - timeline[start_index].x) / (timeline[end_index].x - timeline[start_index].x));
  33. }
  34. public static number_to_counting(num: number): string {
  35. if (num < 1000) {
  36. return num + '';
  37. } else if (num < 1000000) {
  38. return Math.floor(num / 1000) + 'K';
  39. } else if (num < 1000000000) {
  40. return Math.floor(num / 1000000) + 'M';
  41. } else if (num < 1000000000000) {
  42. return Math.floor(num / 1000000000) + 'B';
  43. } else if (num < 1000000000000000) {
  44. return Math.floor(num / 1000000000000) + 'T';
  45. }
  46. return Math.floor(num / 1000000000000) + 'T';
  47. }
  48. public static number_to_time(time: number): [string, string, string] {
  49. const t = Math.floor(time / (60 * 60));
  50. time = time - t * 60 * 60;
  51. let hour = t.toString();
  52. let min = Math.floor(time / 60).toString();
  53. let sec = (time % 60).toString();
  54. if (hour.length === 1) {
  55. hour = '0' + hour;
  56. }
  57. if (min.length === 1) {
  58. min = '0' + min;
  59. }
  60. if (sec.length === 1) {
  61. sec = '0' + sec;
  62. }
  63. return [hour, min, sec];
  64. }
  65. public static set_normal_angle(angle: number) {
  66. while (angle > 360) {
  67. angle -= 360;
  68. }
  69. while (angle < 0) {
  70. angle += 360;
  71. }
  72. return angle;
  73. }
  74. public static compare_version(v1: string, v2: string): number {
  75. let v1_arr = v1.split('.');
  76. let v2_arr = v2.split('.');
  77. const len = Math.max(v1_arr.length, v2_arr.length);
  78. while (v1_arr.length < len) {
  79. v1_arr.push('0');
  80. }
  81. while (v2_arr.length < len) {
  82. v2_arr.push('0');
  83. }
  84. for (let i = 0; i < len; i++) {
  85. const num1 = parseInt(v1_arr[i]);
  86. const num2 = parseInt(v2_arr[i]);
  87. if (num1 > num2) {
  88. return 1;
  89. } else if (num1 < num2) {
  90. return -1;
  91. }
  92. }
  93. return 0;
  94. }
  95. public static is_today(date: string): boolean {
  96. const d1 = new Date();
  97. let d2;
  98. if (date && date !== '') {
  99. d2 = new Date(date);
  100. } else {
  101. d2 = new Date();
  102. d2.setDate(d2.getDate() - 1);
  103. }
  104. return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
  105. }
  106. public static is_safe_area(): boolean {
  107. const cb = (width: number, height: number) => {
  108. return (width === 2280 && height === 1080) || (width === 1792 && height === 828) || (width === 2436 && height === 1125) || (width === 2688 && height === 1242);
  109. };
  110. switch (LQPlatformUtil.get_platform()) {
  111. case LQPlatformType.baidu:
  112. const sys_info_swan = swan.getSystemInfoSync();
  113. return cb(sys_info_swan.pixelRatio * sys_info_swan.screenWidth, sys_info_swan.pixelRatio * sys_info_swan.screenHeight);
  114. case LQPlatformType.qq:
  115. const sys_info_qq = qq.getSystemInfoSync();
  116. return cb(sys_info_qq.pixelRatio * sys_info_qq.screenWidth, sys_info_qq.pixelRatio * sys_info_qq.screenHeight);
  117. case LQPlatformType.tt:
  118. const sys_info_tt = tt.getSystemInfoSync();
  119. return cb(sys_info_tt.pixelRatio * sys_info_tt.screenWidth, sys_info_tt.pixelRatio * sys_info_tt.screenHeight);
  120. case LQPlatformType.oppo:
  121. case LQPlatformType.vivo:
  122. const sys_info_vivo = qg.getSystemInfoSync();
  123. return cb(sys_info_vivo.pixelRatio * sys_info_vivo.screenWidth, sys_info_vivo.pixelRatio * sys_info_vivo.screenHeight);
  124. case LQPlatformType.wx:
  125. const sys_info_wx = wx.getSystemInfoSync();
  126. return cb(sys_info_wx.pixelRatio * sys_info_wx.screenWidth, sys_info_wx.pixelRatio * sys_info_wx.screenHeight);
  127. case LQPlatformType.android:
  128. break;
  129. case LQPlatformType.ios:
  130. let size = view.getFrameSize();
  131. return cb(size.width, size.height);
  132. }
  133. return false;
  134. }
  135. public static deep_clone(obj: any) {
  136. if (typeof obj !== 'object') {
  137. return obj;
  138. }
  139. let new_obj = (obj instanceof Array ? [] : {}) as any;
  140. for (let key in obj) {
  141. if (typeof obj[key] === 'object') {
  142. new_obj[key] = this.deep_clone(obj[key]);
  143. } else {
  144. new_obj[key] = obj[key];
  145. }
  146. }
  147. return new_obj;
  148. }
  149. }