NodeEx.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { Director, director, Node, NodeEventType, sys, UIOpacity, UIRenderer, Vec3 } from 'cc';
  2. import { JSB } from 'cc/env';
  3. const nodeProto: any = Node.prototype;
  4. const temp_v3 = new Vec3();
  5. //需要刷新层级的时候再调用
  6. export function sortChildrenByZIndex(parent: Node): void {
  7. if (!parent) {
  8. return;
  9. }
  10. let children = parent.children.concat();
  11. children.sort((a, b): number => {
  12. if (a.zIndex == null) {
  13. a.zIndex = 0;
  14. }
  15. if (b.zIndex == null) {
  16. b.zIndex = 0;
  17. }
  18. return a.zIndex - b.zIndex;
  19. });
  20. let maxIndex = children.length;
  21. for (const node of children) {
  22. node.setSiblingIndex(maxIndex);
  23. }
  24. }
  25. nodeProto._delaySort = function(){
  26. director.on(Director.EVENT_AFTER_UPDATE, this.sortByPosZ, this);
  27. }
  28. nodeProto.autoSort = function(){
  29. this.on(NodeEventType.CHILD_ADDED, this._delaySort, this);
  30. }
  31. nodeProto.sortByPosZ = function(){
  32. let _children = this._children;
  33. if (_children.length > 1) {
  34. // insertion sort
  35. let child, child2;
  36. for (let i = 1, count = _children.length; i < count; i++) {
  37. child = _children[i];
  38. let j = i;
  39. for (; j > 0 &&
  40. (child2 = _children[j - 1])._lpos.z > child._lpos.z; j--) {
  41. _children[j] = child2;
  42. }
  43. _children[j] = child;
  44. }
  45. }
  46. this._updateSiblingIndex();
  47. if(sys.isNative){
  48. this._setChildren(_children);
  49. }
  50. director.off(Director.EVENT_AFTER_UPDATE, this._delaySort, this);
  51. }
  52. nodeProto.toX = function(x: number){
  53. Vec3.copy(temp_v3, this._lpos);
  54. temp_v3.x = x;
  55. this.setPosition(temp_v3);
  56. }
  57. nodeProto.addX = function(add_x: number){
  58. Vec3.copy(temp_v3, this._lpos);
  59. temp_v3.x += add_x;
  60. this.setPosition(temp_v3);
  61. }
  62. nodeProto.toY = function(y: number){
  63. Vec3.copy(temp_v3, this._lpos);
  64. temp_v3.y = y;
  65. this.setPosition(temp_v3);
  66. }
  67. nodeProto.addY = function(add_y: number){
  68. Vec3.copy(temp_v3, this._lpos);
  69. temp_v3.y += add_y;
  70. this.setPosition(temp_v3);
  71. }
  72. nodeProto.toZ = function(z: number, need_sort:boolean){
  73. this._lpos.z = z;
  74. // Vec3.copy(temp_v3, this._lpos);
  75. // temp_v3.z = z;
  76. // this.setPosition(temp_v3);
  77. if(need_sort && this.parent){
  78. this.parent._delaySort();
  79. }
  80. }
  81. nodeProto.toXY = function(x:number, y:number){
  82. Vec3.copy(temp_v3, this._lpos);
  83. temp_v3.x = x;
  84. temp_v3.y = y;
  85. this.setPosition(temp_v3);
  86. }
  87. nodeProto.to0 = function(){
  88. Vec3.copy(temp_v3, this._lpos);
  89. temp_v3.x = 0;
  90. temp_v3.y = 0;
  91. this.setPosition(temp_v3);
  92. }
  93. nodeProto.toXYByArr = function(pos:Array<number>){
  94. Vec3.copy(temp_v3, this._lpos);
  95. temp_v3.x = pos[0];
  96. temp_v3.y = pos[1];
  97. this.setPosition(temp_v3);
  98. }
  99. nodeProto.toScaleX = function(x: number){
  100. Vec3.copy(temp_v3, this._lscale);
  101. temp_v3.x = x;
  102. this.setScale(temp_v3);
  103. }
  104. nodeProto.toScaleY = function(y: number){
  105. Vec3.copy(temp_v3, this._lscale);
  106. temp_v3.y = y;
  107. this.setScale(temp_v3);
  108. }
  109. nodeProto.toScaleXY = function(scale: number){
  110. Vec3.copy(temp_v3, this._lscale);
  111. temp_v3.x = temp_v3.y = scale;
  112. this.setScale(temp_v3);
  113. }
  114. function setLocalOpacityDirty (node: Node, dirty: boolean, opacity: number) {
  115. if (!node.isValid) {
  116. return;
  117. }
  118. const render = node._uiProps.uiComp as UIRenderer;
  119. let final_opacity = node._uiProps.localOpacity * opacity;
  120. if (render && render.color) { // exclude UIMeshRenderer which has not color
  121. render.renderEntity.colorDirty = dirty;
  122. render.renderEntity.localOpacity = final_opacity
  123. }
  124. for (let i = 0; i < node.children.length; i++) {
  125. setLocalOpacityDirty(node.children[i], dirty, final_opacity);
  126. }
  127. }
  128. nodeProto.toOpacity = function(opacity: number){
  129. // console.log(this.name, opacity)
  130. this._uiProps.localOpacity = opacity;
  131. if (JSB) {
  132. setLocalOpacityDirty(this, true, 1);
  133. }
  134. }