LayerUI.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import AudioMgr from "../AudioMgr";
  2. import GameLog from "../GameLogMgr";
  3. /**
  4. * 这个是 封装了一些方法 ,例如 注册点击事件 销毁事件 等等
  5. */
  6. const {ccclass} = cc._decorator;
  7. @ccclass
  8. export default class LayerUI extends cc.Component {
  9. private _touchList: { [key: string]: { target, handler, callObj } } = {};
  10. private _touchEndList: { [key: string]: { target, handler, callObj } } = {}
  11. private _enableList: { [key: string]: { enabled: boolean, isGray: boolean } } = {};
  12. /**
  13. * 是否交互 需在target注册onTouch之后
  14. * @param target
  15. * @param v
  16. * @param isGray
  17. */
  18. protected setInteractable(target: cc.Node, v: boolean, isGray: boolean = true) {
  19. if (!target)
  20. return;
  21. let button: cc.Button = target.getComponent(cc.Button);
  22. if (button) {
  23. button.enableAutoGrayEffect = isGray;
  24. button.interactable = v;
  25. }
  26. this._enableList[target.name] = {enabled: v, isGray};
  27. }
  28. /**
  29. * 注册点击事件
  30. * @param target 点击对象
  31. * @param handler 触发事件
  32. * @param sound 播放声音名称
  33. * @param scale 缩放值
  34. * @param stopEvent
  35. */
  36. protected onTouch(target: cc.Node, handler: Function, sound: string = "click", scale = 0.9, stopEvent = true) {
  37. if (!target || !handler) {
  38. GameLog.error("target || handler为空-->", target, handler);
  39. return;
  40. }
  41. let targetName: string = target.name;
  42. if (this._touchList[targetName] && this._touchList[targetName].target == target) {
  43. GameLog.warn("重复设置-->", targetName);
  44. return;
  45. }
  46. //添加一个button 动画
  47. let button = target.getComponent(cc.Button);
  48. if (scale != 1) {
  49. if (!button) {
  50. button = target.addComponent(cc.Button);
  51. button.transition = cc.Button.Transition.SCALE;
  52. button.zoomScale = scale;
  53. }
  54. }
  55. let enabled = true;
  56. let isGray = true;
  57. if (this._enableList[target.name]) {
  58. enabled = this._enableList[target.name].enabled;
  59. isGray = this._enableList[target.name].isGray;
  60. }
  61. this.setInteractable(target, enabled, isGray);
  62. let callObj = this;
  63. let touchHandler = (event) => {
  64. let {enabled = true} = this._enableList[target.name] || {};
  65. if (!enabled) {
  66. return;
  67. }
  68. if (stopEvent) {
  69. event.stopPropagation();
  70. }
  71. if (sound && sound != "") {
  72. // if (sound === "check") {
  73. // sound = "piano/a" + Math.floor(Math.random() * (5 - 1) + 1);
  74. // }
  75. AudioMgr.play(sound).then();
  76. }
  77. handler.call(callObj, event);
  78. };
  79. target.on(cc.Node.EventType.TOUCH_START, touchHandler);
  80. this._touchList[targetName] = {target: target, handler: touchHandler, callObj: callObj};
  81. }
  82. protected onTouchEnd(target: cc.Node, handler: Function) {
  83. if (!target || !handler) {
  84. GameLog.error("target || handle为空 ondTouchEnd -->", target, handler)
  85. return
  86. }
  87. let targetName: string = target.name
  88. if (this._touchEndList[targetName] && this._touchEndList[targetName].target == target) {
  89. GameLog.warn("重复设置 --> onTouchEnd ", targetName)
  90. }
  91. let callObj = this;
  92. let touchHandler = (event) => {
  93. handler.call(callObj, event);
  94. };
  95. target.on(cc.Node.EventType.TOUCH_END, touchHandler);
  96. target.on(cc.Node.EventType.TOUCH_CANCEL, touchHandler);
  97. this._touchEndList[targetName] = {target: target, handler: touchHandler, callObj: callObj};
  98. }
  99. protected offTouchEnd(target: cc.Node) {
  100. if (!target) {
  101. GameLog.error("target 为空 ")
  102. return
  103. }
  104. let targetName: string = target.name
  105. if (this._touchEndList[targetName]) {
  106. let handler = this._touchEndList[targetName].handler
  107. target.off(cc.Node.EventType.TOUCH_END, handler)
  108. target.off(cc.Node.EventType.TOUCH_CANCEL, handler)
  109. delete this._touchEndList[targetName]
  110. }
  111. }
  112. /**
  113. * 移除对象点击事件
  114. * @param target
  115. */
  116. protected offTouch(target: cc.Node) {
  117. if (!target) {
  118. GameLog.error("target 为空");
  119. return
  120. }
  121. let targetName: string = target.name;
  122. if (this._touchList[targetName]) {
  123. let touchHandler = this._touchList[targetName].handler;
  124. target.off(cc.Node.EventType.TOUCH_START, touchHandler);
  125. delete this._touchList[targetName]
  126. }
  127. delete this._enableList[targetName]
  128. }
  129. protected clear() {
  130. for (let key in this._touchList) {
  131. this.offTouch(this._touchList[key].target)
  132. }
  133. }
  134. onDestroy() {
  135. this.clear()
  136. }
  137. /**
  138. *
  139. * @param path 路径或者名字
  140. */
  141. protected getNode(path: string): cc.Node {
  142. let node: cc.Node = null;
  143. if (path == "" || !path)
  144. return null;
  145. if (path.indexOf("/") != -1) {
  146. node = cc.find(path, this.node);
  147. } else {
  148. node = this.node.getChildByName(path);
  149. }
  150. if (!node) {
  151. GameLog.warn("未找到该节点 path=", path);
  152. }
  153. return node;
  154. }
  155. }