LongPressButton.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { gameMethod } from "../common/gameMethod";
  2. import { UserEvent } from "../data/const/EventConst";
  3. import EventMng from "../manager/EventMng";
  4. export interface LongPressButtonParam {
  5. scale?: number;
  6. aniTime?:number;
  7. gButton?:fgui.GButton;
  8. bPalyAni?:boolean;
  9. callbackFunc?:(bEnd:boolean,bGray:boolean,bLongPress?:boolean)=>void;
  10. grayClickCallback?:()=>void
  11. }
  12. const {ccclass, property} = cc._decorator;
  13. @ccclass
  14. export default class LongPressButton extends cc.Component {
  15. @property(cc.Float)
  16. timerInterval:number = 0.2; //设置长按的间隔,默认0.2秒
  17. callbackFunc:(bEnd:boolean,bGray:boolean,bLongPress?:boolean)=>void ; // 长按按钮的回调函数
  18. grayClickCallback:()=>void;
  19. public keepTime:number = 0; //当次长按时长
  20. private passTime:number = 0;
  21. private bLongPress:boolean = false; // 是否长按状态
  22. private bGray:boolean = false;
  23. private bInterrupt:boolean = false;
  24. private bClick:boolean = false;
  25. private gButton:fgui.GButton;
  26. private scale:number;
  27. private targetScale:number;
  28. private aniTime:number;
  29. private bPalyAni:boolean; // 是否播放长按动画
  30. private bCallback:boolean; // 中断事件是否需要回调相应
  31. private _IsAllowlongPress:boolean = true; // 是否允许长按
  32. set IsAllowlongPress(bAllowlongPress:boolean){
  33. this._IsAllowlongPress = bAllowlongPress;
  34. }
  35. get IsAllowlongPress(){
  36. return this._IsAllowlongPress;
  37. }
  38. protected onEnable() {
  39. this.node.on(fgui.Event.TOUCH_BEGIN, this.onTouchStart, this);
  40. this.node.on(fgui.Event.TOUCH_MOVE, this.onTouchMove, this);
  41. this.node.on(fgui.Event.TOUCH_END, this.onTouchEnd, this);
  42. }
  43. protected onDisable() {
  44. // 清理事件监听
  45. this.node.off(fgui.Event.TOUCH_BEGIN, this.onTouchStart, this);
  46. this.node.off(fgui.Event.TOUCH_MOVE, this.onTouchMove, this);
  47. this.node.off(fgui.Event.TOUCH_END, this.onTouchEnd, this);
  48. EventMng.emit(UserEvent.BTNTOUCHDISABLE);
  49. }
  50. public onchangeState(bGray:boolean){
  51. if (bGray && this.bGray != bGray){
  52. if (this.bClick && this.grayClickCallback){
  53. this.grayClickCallback();
  54. }
  55. this.bGray = true;
  56. this.unschedule(this.longPressDown);
  57. this.clearTime();
  58. }
  59. this.bGray = bGray;
  60. }
  61. // 长按事件
  62. private longPressDown() {
  63. this.bLongPress = true;
  64. if (this.bPalyAni){
  65. this.ShowAni();
  66. }
  67. }
  68. private onTouchStart(event) {
  69. if (this.bGray){
  70. if (this.grayClickCallback){
  71. this.grayClickCallback();
  72. }
  73. return;
  74. }
  75. this.bClick = true;
  76. this.passTime = 0;
  77. this.keepTime = 0
  78. this.bInterrupt = false;
  79. this.bCallback = true;
  80. this.unschedule(this.longPressDown);
  81. if (this._IsAllowlongPress){
  82. this.scheduleOnce(this.longPressDown,0.2)
  83. }
  84. }
  85. private onTouchMove(event) {
  86. if (this.bLongPress && !gameMethod.isEmpty(this.gButton)){
  87. // 使用hitTest方法判断触摸点是否在组件范围内
  88. if (!this.gButton.hitTest(event.pos)) {
  89. this.onTouchEnd(null);
  90. this.bInterrupt = true;
  91. }
  92. }
  93. }
  94. private onTouchEnd(event) {
  95. if (this.bInterrupt){
  96. // console.log("中断事件,不在相应");
  97. return;
  98. }
  99. this.unschedule(this.longPressDown);
  100. this.clearTime();
  101. EventMng.emit(UserEvent.BTNTOUCHEND);
  102. }
  103. update(dt) {
  104. this.passTime += dt;
  105. this.keepTime += dt
  106. // 触摸计时
  107. if (this.bLongPress) {
  108. if (this.passTime > this.timerInterval){
  109. this.handleLongPress(false);
  110. this.passTime = 0;
  111. }
  112. }
  113. }
  114. public clearTime(){
  115. if (this.bClick && this.bCallback){
  116. this.handleLongPress(true);
  117. }
  118. this.passTime = 0;
  119. this.keepTime = 0
  120. this.bLongPress = false;
  121. this.bClick = false;
  122. this.StopAni();
  123. }
  124. // 长按事件回调
  125. private handleLongPress(bEnd:boolean) {
  126. if (this.callbackFunc && !this.bGray){
  127. this.callbackFunc(bEnd,this.bGray,this.bLongPress);
  128. }
  129. }
  130. public setCallback(callbackFunc:(bEnd:boolean,bGray:boolean)=>void,gButton?:fgui.GButton,grayClickCallback?:()=>void){
  131. this.callbackFunc = callbackFunc;
  132. this.grayClickCallback = grayClickCallback;
  133. this.gButton = gButton;
  134. }
  135. public SetLongData(data:LongPressButtonParam){
  136. this.callbackFunc = data?.callbackFunc;
  137. this.grayClickCallback = data?.grayClickCallback;
  138. this.gButton = data?.gButton;
  139. this.scale = data?.scale || 1;
  140. this.targetScale = this.scale * 0.9;
  141. this.aniTime = data?.aniTime || 0.15;
  142. this.bPalyAni = data?.bPalyAni || false;
  143. }
  144. public interrupt(bCallback:boolean = true){
  145. this.bCallback = bCallback;
  146. this.passTime = 0;
  147. this.keepTime = 0
  148. this.bLongPress = false;
  149. this.bClick = false;
  150. this.StopAni();
  151. }
  152. public ShowAni(){
  153. this.StopAni();
  154. this.PlayAni();
  155. }
  156. private PlayAni(){
  157. cc.tween(this.gButton.node).to(this.aniTime,{scale:this.targetScale}).to(this.aniTime,{scale:this.scale}).union().repeatForever().start();
  158. }
  159. private StopAni(){
  160. cc.Tween.stopAllByTarget(this.gButton.node)
  161. this.gButton.node.scale = this.scale;
  162. }
  163. // 是否处于点击状态
  164. public IsClickState(){
  165. return this.bClick;
  166. }
  167. // 是否处于点击状态
  168. public IsLongClickState(){
  169. return this.bLongPress;
  170. }
  171. }