88 lines
3.0 KiB
TypeScript
88 lines
3.0 KiB
TypeScript
import { _decorator, Button, EventTouch, Node, Vec3 } from "cc";
|
|||
|
|
import AudioManager from "../Manager/AudioManager";
|
||
|
|
|
||
|
|
const { ccclass, property } = _decorator;
|
||
|
|
let _vec3 = new Vec3();
|
||
|
|
|
||
|
|
@ccclass('GButton')
|
||
|
|
export class GButton {
|
||
|
|
/**
|
||
|
|
* 添加绑定事件
|
||
|
|
* @param node
|
||
|
|
* @param func 点击结束回调方法
|
||
|
|
* @param obj 上下文
|
||
|
|
* @param buttonType 点击事件类型(处理音效使用)
|
||
|
|
* @param backStart 点击开始回调
|
||
|
|
* @param backCancle 点击取消回调
|
||
|
|
* @returns
|
||
|
|
*/
|
||
|
|
public static BandClick(node: Node, func: Function, obj: any, buttonType=10000, backStart: Function = null, backCancle: Function = null, clickScale: boolean = true): void {
|
||
|
|
if (!node) {
|
||
|
|
//hg_utils.hgLog("GButton, bandClick, failed by node is null");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (buttonType == null) {
|
||
|
|
buttonType = 10000;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!node['oldScale']) {
|
||
|
|
node['oldScale'] = node.scale.clone();
|
||
|
|
}
|
||
|
|
|
||
|
|
node.on(Node.EventType.TOUCH_START, (ev: EventTouch) => {
|
||
|
|
// let bs = node.getComponent(Button);
|
||
|
|
// if (!bs || (bs && bs.interactable == true)) {
|
||
|
|
// backStart && backStart.call(obj, ev);
|
||
|
|
// }
|
||
|
|
if (clickScale) {
|
||
|
|
node.scale = Vec3.multiplyScalar(_vec3, node['oldScale'], 1.05);
|
||
|
|
}
|
||
|
|
if (backStart) {
|
||
|
|
backStart.call(obj, ev);
|
||
|
|
}
|
||
|
|
}, obj);
|
||
|
|
node.on(Node.EventType.TOUCH_CANCEL, (ev: EventTouch) => {
|
||
|
|
if (clickScale) {
|
||
|
|
node.scale = (node['oldScale'] as Vec3).clone();
|
||
|
|
}
|
||
|
|
if (backCancle) {
|
||
|
|
backCancle.call(obj, ev);
|
||
|
|
}
|
||
|
|
}, obj);
|
||
|
|
node.on(Node.EventType.TOUCH_END, (ev: EventTouch) => {
|
||
|
|
if (clickScale) {
|
||
|
|
node.scale = (node['oldScale'] as Vec3).clone();
|
||
|
|
}
|
||
|
|
//当前节点不是button,或button且不在禁用状态才可以执行点击回调
|
||
|
|
let bs = node.getComponent(Button);
|
||
|
|
if (!bs || (bs && bs.interactable == true)) {
|
||
|
|
func.call(obj, ev);
|
||
|
|
}
|
||
|
|
//点击音效处理
|
||
|
|
if (buttonType > 0) {
|
||
|
|
AudioManager.I.PlayEffect(buttonType); //音效
|
||
|
|
}
|
||
|
|
}, obj);
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* 移除绑定事件
|
||
|
|
* @param node
|
||
|
|
*/
|
||
|
|
public static RemoveClick(node: Node, func?: Function) {
|
||
|
|
if (node.isValid) {
|
||
|
|
node.off(Node.EventType.TOUCH_START, func);
|
||
|
|
node.off(Node.EventType.TOUCH_END, func);
|
||
|
|
node.off(Node.EventType.TOUCH_CANCEL, func);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 先移除再绑定
|
||
|
|
*/
|
||
|
|
public static RemoveAndBandClick(node: Node, func: Function, obj: any = null, buttonType = null, backStart: Function = null, backCancle: Function = null, clickScale: boolean = true): void {
|
||
|
|
obj = obj || node;
|
||
|
|
GButton.RemoveClick(node);
|
||
|
|
GButton.BandClick(node, func, obj, buttonType, backStart, backCancle, clickScale);
|
||
|
|
}
|
||
|
|
}
|