123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import { gameMethod } from "../common/gameMethod";
- import { UserEvent } from "../data/const/EventConst";
- import EventMng from "../manager/EventMng";
- export interface LongPressButtonParam {
- scale?: number;
- aniTime?:number;
- gButton?:fgui.GButton;
- bPalyAni?:boolean;
- callbackFunc?:(bEnd:boolean,bGray:boolean,bLongPress?:boolean)=>void;
- grayClickCallback?:()=>void
- }
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class LongPressButton extends cc.Component {
- @property(cc.Float)
- timerInterval:number = 0.2; //设置长按的间隔,默认0.2秒
- callbackFunc:(bEnd:boolean,bGray:boolean,bLongPress?:boolean)=>void ; // 长按按钮的回调函数
- grayClickCallback:()=>void;
- public keepTime:number = 0; //当次长按时长
- private passTime:number = 0;
- private bLongPress:boolean = false; // 是否长按状态
- private bGray:boolean = false;
- private bInterrupt:boolean = false;
- private bClick:boolean = false;
- private gButton:fgui.GButton;
- private scale:number;
- private targetScale:number;
- private aniTime:number;
- private bPalyAni:boolean; // 是否播放长按动画
- private bCallback:boolean; // 中断事件是否需要回调相应
- private _IsAllowlongPress:boolean = true; // 是否允许长按
- set IsAllowlongPress(bAllowlongPress:boolean){
- this._IsAllowlongPress = bAllowlongPress;
- }
- get IsAllowlongPress(){
- return this._IsAllowlongPress;
- }
-
- protected onEnable() {
- this.node.on(fgui.Event.TOUCH_BEGIN, this.onTouchStart, this);
- this.node.on(fgui.Event.TOUCH_MOVE, this.onTouchMove, this);
- this.node.on(fgui.Event.TOUCH_END, this.onTouchEnd, this);
- }
- protected onDisable() {
- // 清理事件监听
- this.node.off(fgui.Event.TOUCH_BEGIN, this.onTouchStart, this);
- this.node.off(fgui.Event.TOUCH_MOVE, this.onTouchMove, this);
- this.node.off(fgui.Event.TOUCH_END, this.onTouchEnd, this);
- EventMng.emit(UserEvent.BTNTOUCHDISABLE);
- }
- public onchangeState(bGray:boolean){
- if (bGray && this.bGray != bGray){
- if (this.bClick && this.grayClickCallback){
- this.grayClickCallback();
- }
- this.bGray = true;
- this.unschedule(this.longPressDown);
- this.clearTime();
- }
- this.bGray = bGray;
- }
- // 长按事件
- private longPressDown() {
- this.bLongPress = true;
- if (this.bPalyAni){
- this.ShowAni();
- }
- }
-
- private onTouchStart(event) {
- if (this.bGray){
- if (this.grayClickCallback){
- this.grayClickCallback();
- }
- return;
- }
- this.bClick = true;
- this.passTime = 0;
- this.keepTime = 0
- this.bInterrupt = false;
- this.bCallback = true;
- this.unschedule(this.longPressDown);
- if (this._IsAllowlongPress){
- this.scheduleOnce(this.longPressDown,0.2)
- }
- }
-
- private onTouchMove(event) {
- if (this.bLongPress && !gameMethod.isEmpty(this.gButton)){
- // 使用hitTest方法判断触摸点是否在组件范围内
- if (!this.gButton.hitTest(event.pos)) {
- this.onTouchEnd(null);
- this.bInterrupt = true;
- }
- }
- }
-
- private onTouchEnd(event) {
- if (this.bInterrupt){
- // console.log("中断事件,不在相应");
- return;
- }
- this.unschedule(this.longPressDown);
- this.clearTime();
- EventMng.emit(UserEvent.BTNTOUCHEND);
- }
- update(dt) {
- this.passTime += dt;
- this.keepTime += dt
- // 触摸计时
- if (this.bLongPress) {
- if (this.passTime > this.timerInterval){
- this.handleLongPress(false);
- this.passTime = 0;
- }
- }
- }
-
- public clearTime(){
- if (this.bClick && this.bCallback){
- this.handleLongPress(true);
- }
- this.passTime = 0;
- this.keepTime = 0
- this.bLongPress = false;
- this.bClick = false;
- this.StopAni();
- }
- // 长按事件回调
- private handleLongPress(bEnd:boolean) {
- if (this.callbackFunc && !this.bGray){
- this.callbackFunc(bEnd,this.bGray,this.bLongPress);
- }
- }
- public setCallback(callbackFunc:(bEnd:boolean,bGray:boolean)=>void,gButton?:fgui.GButton,grayClickCallback?:()=>void){
- this.callbackFunc = callbackFunc;
- this.grayClickCallback = grayClickCallback;
- this.gButton = gButton;
- }
- public SetLongData(data:LongPressButtonParam){
- this.callbackFunc = data?.callbackFunc;
- this.grayClickCallback = data?.grayClickCallback;
- this.gButton = data?.gButton;
- this.scale = data?.scale || 1;
- this.targetScale = this.scale * 0.9;
- this.aniTime = data?.aniTime || 0.15;
- this.bPalyAni = data?.bPalyAni || false;
- }
- public interrupt(bCallback:boolean = true){
- this.bCallback = bCallback;
- this.passTime = 0;
- this.keepTime = 0
- this.bLongPress = false;
- this.bClick = false;
- this.StopAni();
- }
- public ShowAni(){
- this.StopAni();
- this.PlayAni();
- }
- private PlayAni(){
- cc.tween(this.gButton.node).to(this.aniTime,{scale:this.targetScale}).to(this.aniTime,{scale:this.scale}).union().repeatForever().start();
- }
- private StopAni(){
- cc.Tween.stopAllByTarget(this.gButton.node)
- this.gButton.node.scale = this.scale;
- }
- // 是否处于点击状态
- public IsClickState(){
- return this.bClick;
- }
- // 是否处于点击状态
- public IsLongClickState(){
- return this.bLongPress;
- }
- }
|