NodeEx.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. nodeProto._delaySort = function(){
  6. director.on(Director.EVENT_AFTER_UPDATE, this.sortByPosZ, this);
  7. }
  8. nodeProto.autoSort = function(){
  9. this.on(NodeEventType.CHILD_ADDED, this._delaySort, this);
  10. }
  11. nodeProto.sortByPosZ = function(){
  12. let _children = this._children;
  13. if (_children.length > 1) {
  14. // insertion sort
  15. let child, child2;
  16. for (let i = 1, count = _children.length; i < count; i++) {
  17. child = _children[i];
  18. let j = i;
  19. for (; j > 0 &&
  20. (child2 = _children[j - 1])._lpos.z > child._lpos.z; j--) {
  21. _children[j] = child2;
  22. }
  23. _children[j] = child;
  24. }
  25. }
  26. this._updateSiblingIndex();
  27. if(sys.isNative){
  28. this._setChildren(_children);
  29. }
  30. director.off(Director.EVENT_AFTER_UPDATE, this._delaySort, this);
  31. }
  32. nodeProto.toX = function(x: number){
  33. Vec3.copy(temp_v3, this._lpos);
  34. temp_v3.x = x;
  35. this.setPosition(temp_v3);
  36. }
  37. nodeProto.addX = function(add_x: number){
  38. Vec3.copy(temp_v3, this._lpos);
  39. temp_v3.x += add_x;
  40. this.setPosition(temp_v3);
  41. }
  42. nodeProto.toY = function(y: number){
  43. Vec3.copy(temp_v3, this._lpos);
  44. temp_v3.y = y;
  45. this.setPosition(temp_v3);
  46. }
  47. nodeProto.addY = function(add_y: number){
  48. Vec3.copy(temp_v3, this._lpos);
  49. temp_v3.y += add_y;
  50. this.setPosition(temp_v3);
  51. }
  52. nodeProto.toZ = function(z: number, need_sort:boolean){
  53. this._lpos.z = z;
  54. // Vec3.copy(temp_v3, this._lpos);
  55. // temp_v3.z = z;
  56. // this.setPosition(temp_v3);
  57. if(need_sort && this.parent){
  58. this.parent._delaySort();
  59. }
  60. }
  61. nodeProto.toXY = function(x:number, y:number){
  62. Vec3.copy(temp_v3, this._lpos);
  63. temp_v3.x = x;
  64. temp_v3.y = y;
  65. this.setPosition(temp_v3);
  66. }
  67. nodeProto.to0 = function(){
  68. Vec3.copy(temp_v3, this._lpos);
  69. temp_v3.x = 0;
  70. temp_v3.y = 0;
  71. this.setPosition(temp_v3);
  72. }
  73. nodeProto.toXYByArr = function(pos:Array<number>){
  74. Vec3.copy(temp_v3, this._lpos);
  75. temp_v3.x = pos[0];
  76. temp_v3.y = pos[1];
  77. this.setPosition(temp_v3);
  78. }
  79. nodeProto.toScaleX = function(x: number){
  80. Vec3.copy(temp_v3, this._lscale);
  81. temp_v3.x = x;
  82. this.setScale(temp_v3);
  83. }
  84. nodeProto.toScaleY = function(y: number){
  85. Vec3.copy(temp_v3, this._lscale);
  86. temp_v3.y = y;
  87. this.setScale(temp_v3);
  88. }
  89. nodeProto.toScaleXY = function(scale: number){
  90. Vec3.copy(temp_v3, this._lscale);
  91. temp_v3.x = temp_v3.y = scale;
  92. this.setScale(temp_v3);
  93. }
  94. function setLocalOpacityDirty (node: Node, dirty: boolean, opacity: number) {
  95. if (!node.isValid) {
  96. return;
  97. }
  98. const render = node._uiProps.uiComp as UIRenderer;
  99. let final_opacity = node._uiProps.localOpacity * opacity;
  100. if (render && render.color) { // exclude UIMeshRenderer which has not color
  101. render.renderEntity.colorDirty = dirty;
  102. render.renderEntity.localOpacity = final_opacity
  103. }
  104. for (let i = 0; i < node.children.length; i++) {
  105. setLocalOpacityDirty(node.children[i], dirty, final_opacity);
  106. }
  107. }
  108. nodeProto.toOpacity = function(opacity: number){
  109. // console.log(this.name, opacity)
  110. this._uiProps.localOpacity = opacity;
  111. if (JSB) {
  112. setLocalOpacityDirty(this, true, 1);
  113. }
  114. }