CCUtils.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. export default class CCUtils {
  2. // node.addChild 当你已经有parent的情况下再addChild会出现看不到的问题
  3. public static addChild(child: cc.Node, parent: cc.Node, needCleanUp: boolean = false): void {
  4. const lastParent: cc.Node = child.parent;
  5. if (lastParent != null) {
  6. child.removeFromParent(needCleanUp);
  7. }
  8. child.setParent(parent);
  9. }
  10. // node避免触发onEnable onDisable
  11. public static setActive(node: cc.Node, active: boolean): void {
  12. if (node.active == active) {
  13. return;
  14. }
  15. node.active = active;
  16. }
  17. public static findChild(node: cc.Node, name: string): cc.Node {
  18. if (name == '') {
  19. return node;
  20. }
  21. const splitArr: any[] = name.split('/');
  22. let tempNode: cc.Node = node;
  23. for (let i: number = 0; i < splitArr.length; i++) {
  24. tempNode = tempNode.getChildByName(splitArr[i]);
  25. if (tempNode == null) {
  26. break;
  27. }
  28. }
  29. return tempNode;
  30. }
  31. public static getSprite(node: cc.Node, name: string): cc.Sprite {
  32. return this.findChild(node, name).getComponent(cc.Sprite);
  33. }
  34. public static getLabel(node: cc.Node, name: string): cc.Label {
  35. return this.findChild(node, name).getComponent(cc.Label);
  36. }
  37. public static getRichText(node: cc.Node, name: string): cc.RichText {
  38. return this.findChild(node, name).getComponent(cc.RichText);
  39. }
  40. public static getCanvas(node: cc.Node, name: string): cc.Canvas {
  41. return this.findChild(node, name).getComponent(cc.Canvas);
  42. }
  43. public static getWidget(node: cc.Node, name: string): cc.Widget {
  44. return this.findChild(node, name).getComponent(cc.Widget);
  45. }
  46. public static getLayout(node: cc.Node, name: string): cc.Layout {
  47. return this.findChild(node, name).getComponent(cc.Layout);
  48. }
  49. public static getScrollView(node: cc.Node, name: string): cc.ScrollView {
  50. return this.findChild(node, name).getComponent(cc.ScrollView);
  51. }
  52. public static getScrollBar(node: cc.Node, name: string): cc.Scrollbar {
  53. return this.findChild(node, name).getComponent(cc.Scrollbar);
  54. }
  55. public static getProgressBar(node: cc.Node, name: string): cc.ProgressBar {
  56. return this.findChild(node, name).getComponent(cc.ProgressBar);
  57. }
  58. public static getToggle(node: cc.Node, name: string): cc.Toggle {
  59. return this.findChild(node, name).getComponent(cc.Toggle);
  60. }
  61. public static getToggleContainer(node: cc.Node, name: string): cc.ToggleContainer {
  62. return this.findChild(node, name).getComponent(cc.ToggleContainer);
  63. }
  64. public static getPageView(node: cc.Node, name: string): cc.PageView {
  65. return this.findChild(node, name).getComponent(cc.PageView);
  66. }
  67. public static getPageviewIndicator(node: cc.Node, name: string): cc.PageViewIndicator {
  68. return this.findChild(node, name).getComponent(cc.PageViewIndicator);
  69. }
  70. public static getSlider(node: cc.Node, name: string): cc.Slider {
  71. return this.findChild(node, name).getComponent(cc.Slider);
  72. }
  73. public static getBlockInputEvents(node: cc.Node, name: string): cc.BlockInputEvents {
  74. return this.findChild(node, name).getComponent(cc.BlockInputEvents);
  75. }
  76. public static getGraphics(node: cc.Node, name: string): cc.Graphics {
  77. return this.findChild(node, name).getComponent(cc.Graphics);
  78. }
  79. public static getAnimation(node: cc.Node, name: string): cc.Animation {
  80. return this.findChild(node, name).getComponent(cc.Animation);
  81. }
  82. public static getPolygonCollider(node: cc.Node, name: string): cc.PolygonCollider {
  83. return this.findChild(node, name).getComponent(cc.PolygonCollider);
  84. }
  85. public static getBoxCollider(node: cc.Node, name: string): cc.BoxCollider {
  86. return this.findChild(node, name).getComponent(cc.BoxCollider);
  87. }
  88. public static getVideoPlayer(node: cc.Node, name: string): cc.VideoPlayer {
  89. return this.findChild(node, name).getComponent(cc.VideoPlayer);
  90. }
  91. public static isInBox(worldPos: cc.Vec2, boxCollider: cc.BoxCollider): boolean {
  92. const innerPos: cc.Vec2 = boxCollider.node.convertToNodeSpaceAR(worldPos);
  93. let isInX: boolean = false;
  94. let isInY: boolean = false;
  95. if (innerPos.x >= -boxCollider.node.anchorX * boxCollider.size.width && innerPos.x <= boxCollider.size.width * (1 - boxCollider.node.anchorX)) {
  96. isInX = true;
  97. }
  98. if (innerPos.y >= -boxCollider.node.anchorY * boxCollider.size.height && innerPos.y <= boxCollider.size.height * (1 - boxCollider.node.anchorY)) {
  99. isInY = true;
  100. }
  101. return isInX && isInY;
  102. }
  103. public static findLastNumber(name: string): number {
  104. const reg: RegExp = /.*([0-9]+)$/;
  105. const result: any = name.match(reg);
  106. if (result != null) {
  107. return Number(result[1]);
  108. }
  109. return -1;
  110. }
  111. public static changeToNodePos(node1: cc.Node, node2: cc.Node): cc.Vec2 {
  112. const worldPos: cc.Vec2 = node1.parent.convertToWorldSpaceAR(node1.getPosition());
  113. const nodeContentPos: cc.Vec2 = node2.parent.convertToNodeSpaceAR(worldPos);
  114. return nodeContentPos;
  115. }
  116. public static getOrAddComponent<T extends cc.Component>(node: cc.Node, type: new () => T): T {
  117. const component: any = node.getComponent(type);
  118. if (component) {
  119. return component;
  120. }
  121. return node.addComponent(type);
  122. }
  123. // public static createRegion(texture) {
  124. // let skeletonTexture = new sp.SkeletonTexture()
  125. // skeletonTexture.setRealTexture(texture)
  126. // let page = new sp.spine.TextureAtlasPage()
  127. // page.name = texture.name
  128. // page.uWrap = sp.spine.TextureWrap.ClampToEdge
  129. // page.vWrap = sp.spine.TextureWrap.ClampToEdge
  130. // page.texture = skeletonTexture
  131. // page.texture.setWraps(page.uWrap, page.vWrap)
  132. // page.width = texture.width
  133. // page.height = texture.height
  134. // let region = new sp.spine.TextureAtlasRegion()
  135. // region.page = page
  136. // region.width = texture.width
  137. // region.height = texture.height
  138. // region.originalWidth = texture.width
  139. // region.originalHeight = texture.height
  140. // region.rotate = false
  141. // region.u = 0
  142. // region.v = 0
  143. // region.u2 = 1
  144. // region.v2 = 1
  145. // region.texture = skeletonTexture
  146. // return region
  147. // }
  148. public static shuffle(arr: Array<any>): void {
  149. for (let i: number = arr.length - 1; i >= 1; i--) {
  150. const random: number = Math.floor(Math.random() * i);
  151. const temp: any = arr[i];
  152. arr[i] = arr[random];
  153. arr[random] = temp;
  154. }
  155. }
  156. public static getClassName(clas: any): string {
  157. const name: string = cc.js.getClassName(clas);
  158. return name;
  159. }
  160. public static getClassByName(name: string): Function {
  161. const clas: Function = cc.js.getClassByName(name);
  162. return clas;
  163. }
  164. public static setClass(className: string, clas: Function): void {
  165. cc.js.setClassName(className, clas);
  166. }
  167. }