JMAlertFrame.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * 弹框边框
  3. * 主要目的:方便统一边距
  4. */
  5. cc.Class({
  6. extends: cc.Component,
  7. editor: {
  8. executeInEditMode: true,
  9. menu: '嘉米公用/JMAlertFrame'
  10. },
  11. properties: {
  12. target: {
  13. type: cc.Node,
  14. get () {
  15. return this._target;
  16. },
  17. set (value) {
  18. this._clearTarget ();
  19. this._target = value;
  20. this._initTarget ();
  21. },
  22. tooltip: '弹框内容'
  23. },
  24. _target: cc.Node,
  25. top: {
  26. get () {
  27. return this._top;
  28. },
  29. set (value) {
  30. this._top = value;
  31. if (CC_EDITOR) {
  32. this._debouncedUpdate();
  33. }
  34. },
  35. tooltip: '顶部边距'
  36. },
  37. _top: 0,
  38. bottom: {
  39. get () {
  40. return this._bottom;
  41. },
  42. set (value) {
  43. this._bottom = value;
  44. if (CC_EDITOR) {
  45. this._debouncedUpdate();
  46. }
  47. },
  48. tooltip: '底部边距'
  49. },
  50. _bottom: 0,
  51. left: {
  52. get () {
  53. return this._left;
  54. },
  55. set (value) {
  56. this._left = value;
  57. if (CC_EDITOR) {
  58. this._debouncedUpdate();
  59. }
  60. },
  61. tooltip: '左边距'
  62. },
  63. _left: 0,
  64. right: {
  65. get () {
  66. return this._right;
  67. },
  68. set (value) {
  69. this._right = value;
  70. if (CC_EDITOR) {
  71. this._debouncedUpdate();
  72. }
  73. },
  74. tooltip: '右边距'
  75. },
  76. _right: 0
  77. },
  78. onLoad () {
  79. if (CC_EDITOR) {
  80. this._debouncedUpdate = G.ViewUtils.debounce(this.updateFrame, 200);
  81. }
  82. },
  83. onEnable () {
  84. this._initTarget ();
  85. },
  86. onDisable () {
  87. this._clearTarget ();
  88. },
  89. /**
  90. * 刷新
  91. *
  92. * @author Pyden
  93. * @date 2019-03-21
  94. */
  95. updateFrame () {
  96. if (CC_EDITOR) {
  97. this.delayUpdateFrame();
  98. return;
  99. }
  100. // 因为逻辑层和UI层分离,改变UI属性需要下一帧才改变
  101. // 导致死循环,需要新增延迟下一帧刷新
  102. if (this.updateFrameMark) {
  103. return;
  104. }
  105. // 延迟刷新标记
  106. this.updateFrameMark = true;
  107. this.scheduleOnce(() => {
  108. this.delayUpdateFrame();
  109. // 还原标记
  110. this.updateFrameMark = false;
  111. });
  112. },
  113. /**
  114. * 延后刷新
  115. *
  116. * @author Pyden
  117. * @date 2020-05-19
  118. */
  119. delayUpdateFrame () {
  120. if (this.target) {
  121. let scaleX = this.target.scaleX;
  122. let scaleY = this.target.scaleY;
  123. let anchorX = this.target.anchorX;
  124. let anchorY = this.target.anchorY;
  125. let width = this.target.width * scaleX + this.left + this.right;
  126. let height = this.target.height * scaleY + this.top + this.bottom;
  127. this.node.width = width;
  128. this.node.height = height;
  129. let leftX = -width / 2 + this.left;
  130. let bottomY = -height / 2 + this.bottom;
  131. let x = leftX + this.target.width * scaleX * anchorX;
  132. let y = bottomY + this.target.height * scaleY * anchorY;
  133. this.target.x = x;
  134. this.target.y = y;
  135. }
  136. },
  137. // ---------------- 私有方法分割线 ----------------
  138. /**
  139. * 初始化弹框内容(增加监听、立即调整)
  140. *
  141. * @author Pyden
  142. * @date 2019-03-27
  143. */
  144. _initTarget () {
  145. if (this.target) {
  146. this.target.on(cc.Node.EventType.SIZE_CHANGED, this.updateFrame, this);
  147. this.target.on(cc.Node.EventType.SCALE_CHANGED, this.updateFrame, this);
  148. this.target.on(cc.Node.EventType.ANCHOR_CHANGED, this.updateFrame, this);
  149. this.target.on(cc.Node.EventType.POSITION_CHANGED, this.updateFrame, this);
  150. this.updateFrame();
  151. }
  152. },
  153. /**
  154. * 清理弹框内容(删除监听)
  155. *
  156. * @author Pyden
  157. * @date 2019-03-27
  158. */
  159. _clearTarget () {
  160. if (this.target) {
  161. this.target.targetOff(this);
  162. }
  163. }
  164. });