293 lines
9.3 KiB
TypeScript
293 lines
9.3 KiB
TypeScript
import { _decorator, Button, Component, EventHandler, find, Node, Slider, SpriteFrame, Toggle, ToggleContainer } from 'cc';
|
|
import { resLoader } from '../../core_tgx/base/utils/ResLoader';
|
|
import { ModuleDef } from '../../scripts/ModuleDef';
|
|
const { ccclass, property } = _decorator;
|
|
|
|
@ccclass('BaseUI')
|
|
export class BaseUI extends Component {
|
|
|
|
private scriptName:string = "";
|
|
protected onLoad(): void {
|
|
this.scriptName = this.constructor.name;
|
|
console.log("BaseUI onLoad:",this.scriptName);
|
|
}
|
|
|
|
/**
|
|
* 获取道具图标
|
|
* @param iconPath 图标路径
|
|
* @returns
|
|
*/
|
|
public async getPropIconSprite(iconPath:string):Promise< SpriteFrame>{
|
|
let str = "res/Image/Prop/";
|
|
let spriteFrame :SpriteFrame = resLoader.getSpriteFrame(str + iconPath,ModuleDef.Arean);
|
|
if(!spriteFrame){
|
|
spriteFrame = await resLoader.getAsyncSpriteFrame(str + iconPath,ModuleDef.Arean);
|
|
}
|
|
return spriteFrame;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取价格数量
|
|
* @param prices 价格数组
|
|
* @param priceType 价格类型
|
|
* @returns
|
|
*/
|
|
public getPriceCount(prices:number[][], priceType:number=0){
|
|
let count = 0;
|
|
if(prices.length == 0){
|
|
return count;
|
|
}else{
|
|
count = prices[priceType][1];
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/**
|
|
* 获取价格id
|
|
* @param prices
|
|
* @param priceType
|
|
* @returns
|
|
*/
|
|
public getPriceId(prices:number[][], priceType:number=0){
|
|
let id = 0;
|
|
if(prices.length == 0){
|
|
return id;
|
|
}else{
|
|
id = prices[priceType][0];
|
|
}
|
|
return id;
|
|
}
|
|
|
|
/**
|
|
* @en add button event handler
|
|
* @zh 添加按钮事件
|
|
* @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
|
|
* @param cb will be called when event emits. method format:(btn:Button,args:any)=>void
|
|
* @param target the `this` argument of `cb`
|
|
* */
|
|
onButtonEvent(relativeNodePath: string | Node | Button, cb: Function, target?: any, args?: any) {
|
|
console.log("onButtonEvent:",cb.name);
|
|
this.scriptName = this.constructor.name;
|
|
let buttonNode: Node = null;
|
|
if (relativeNodePath instanceof Node) {
|
|
buttonNode = relativeNodePath;
|
|
}
|
|
else if (relativeNodePath instanceof Button) {
|
|
buttonNode = relativeNodePath.node;
|
|
}
|
|
else {
|
|
buttonNode = find(relativeNodePath, this.node);
|
|
}
|
|
|
|
if (!buttonNode) {
|
|
console.error("没有找到按钮节点:", relativeNodePath);
|
|
return null;
|
|
}
|
|
let btn = buttonNode.getComponent(Button);
|
|
let clickEvents = btn.clickEvents;
|
|
let handler = new EventHandler();
|
|
handler.target = target?? this.node;
|
|
handler.component =this.scriptName ;
|
|
handler.handler = cb.name;
|
|
// handler.customEventData = args;
|
|
|
|
//附加额外信息 供事件转发使用
|
|
handler['$cb$'] = cb;
|
|
handler['$target$'] = target;
|
|
handler['$args$'] = args;
|
|
|
|
clickEvents.push(handler);
|
|
btn.clickEvents = clickEvents;
|
|
}
|
|
|
|
/**
|
|
* @en remove button event handler
|
|
* @zh 移除按钮事件
|
|
* @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
|
|
* @param cb will be called when event emits.
|
|
* @param target the `this` argument of `cb`
|
|
* */
|
|
offButtonEvent(relativeNodePath: string | Node | Button, cb: Function, target: any) {
|
|
this.scriptName = this.constructor.name;
|
|
let buttonNode: Node = null;
|
|
if (relativeNodePath instanceof Node) {
|
|
buttonNode = relativeNodePath;
|
|
|
|
}
|
|
else if (relativeNodePath instanceof Button) {
|
|
buttonNode = relativeNodePath.node;
|
|
}
|
|
else {
|
|
buttonNode = find(relativeNodePath, this.node);
|
|
}
|
|
|
|
if (!buttonNode) {
|
|
return; ``
|
|
}
|
|
let btn = buttonNode.getComponent(Button);
|
|
if (!btn) {
|
|
return;
|
|
}
|
|
let clickEvents = btn.clickEvents;
|
|
for (let i = 0; i < clickEvents.length; ++i) {
|
|
let h = clickEvents[i];
|
|
if (h['$cb$'] == cb && h['$target$'] == target) {
|
|
clickEvents.splice(i, 1);
|
|
btn.clickEvents = clickEvents;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @en add toggle event handler
|
|
* @zh 添加Toggle事件
|
|
* @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
|
|
* @param cb will be called when event emits. method format:(btn:Toggle,args:any)=>void
|
|
* @param target the `this` argument of `cb`
|
|
* */
|
|
|
|
onToggleEvent(relativeNodePath: string | Node | Toggle | ToggleContainer, cb: Function, target?: any, args?: any) {
|
|
let buttonNode: Node = null;
|
|
this.scriptName = this.constructor.name;
|
|
if (relativeNodePath instanceof Node) {
|
|
buttonNode = relativeNodePath;
|
|
}
|
|
else if (relativeNodePath instanceof Toggle) {
|
|
buttonNode = relativeNodePath.node;
|
|
}
|
|
else if (relativeNodePath instanceof ToggleContainer) {
|
|
buttonNode = relativeNodePath.node;
|
|
}
|
|
else {
|
|
buttonNode = find(relativeNodePath, this.node);
|
|
}
|
|
|
|
if (!buttonNode) {
|
|
return null;
|
|
}
|
|
let btn = buttonNode.getComponent(Toggle) as any;
|
|
if (!btn) {
|
|
btn = buttonNode.getComponent(ToggleContainer) as any;
|
|
}
|
|
let checkEvents = btn.checkEvents;
|
|
let handler = new EventHandler();
|
|
handler.target = this.node;
|
|
handler.component = this.scriptName;
|
|
handler.handler = cb.name;
|
|
|
|
//附加额外信息 供事件转发使用
|
|
handler['$cb$'] = cb;
|
|
handler['$target$'] = target;
|
|
handler['$args$'] = args;
|
|
|
|
checkEvents.push(handler);
|
|
btn.checkEvents = checkEvents;
|
|
}
|
|
|
|
/**
|
|
* @en remove toggle event handler
|
|
* @zh 移除Toggle事件
|
|
* @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
|
|
* @param cb will be called when event emits. method format:(btn:Toggle,args:any)=>void
|
|
* @param target the `this` argument of `cb`
|
|
* */
|
|
offToggleEvent(relativeNodePath: string | Node | Toggle | ToggleContainer, cb: Function, target: any) {
|
|
let buttonNode: Node = null;
|
|
this.scriptName = this.constructor.name;
|
|
if (relativeNodePath instanceof Node) {
|
|
buttonNode = relativeNodePath;
|
|
}
|
|
else if (relativeNodePath instanceof Toggle) {
|
|
buttonNode = relativeNodePath.node;
|
|
}
|
|
else if (relativeNodePath instanceof ToggleContainer) {
|
|
buttonNode = relativeNodePath.node;
|
|
}
|
|
else {
|
|
buttonNode = find(relativeNodePath, this.node);
|
|
}
|
|
|
|
if (!buttonNode) {
|
|
return null;
|
|
}
|
|
let btn = buttonNode.getComponent(Toggle) as any;
|
|
if (!btn) {
|
|
btn = buttonNode.getComponent(ToggleContainer) as any;
|
|
}
|
|
let checkEvents = btn.checkEvents;
|
|
for (let i = 0; i < checkEvents.length; ++i) {
|
|
let h = checkEvents[i];
|
|
if (h['$cb$'] == cb && h['$target$'] == target) {
|
|
checkEvents.splice(i, 1);
|
|
btn.checkEvents = checkEvents;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
onSlideEvent(relativeNodePath: string | Node | Slider, cb: Function, target?: any, args?: any) {
|
|
let sliderNode: Node = null;
|
|
this.scriptName = this.constructor.name;
|
|
if (relativeNodePath instanceof Node) {
|
|
sliderNode = relativeNodePath;
|
|
}
|
|
else if (relativeNodePath instanceof Slider) {
|
|
sliderNode = relativeNodePath.node;
|
|
}
|
|
else {
|
|
sliderNode = find(relativeNodePath, this.node);
|
|
}
|
|
|
|
if (!sliderNode) {
|
|
return null;
|
|
}
|
|
|
|
let slider = sliderNode.getComponent(Slider);
|
|
let slideEvents = slider.slideEvents;
|
|
let handler = new EventHandler();
|
|
handler.target = this.node;
|
|
handler.component = this.scriptName;
|
|
handler.handler = cb.name;
|
|
|
|
//附加额外信息 供事件转发使用
|
|
handler['$cb$'] = cb;
|
|
handler['$target$'] = target;
|
|
handler['$args$'] = args;
|
|
|
|
slideEvents.push(handler);
|
|
slider.slideEvents = slideEvents;
|
|
}
|
|
|
|
offSlideEvent(relativeNodePath: string | Node | Slider, cb: Function, target: any) {
|
|
let sliderNode: Node = null;
|
|
if (relativeNodePath instanceof Node) {
|
|
sliderNode = relativeNodePath;
|
|
}
|
|
else if (relativeNodePath instanceof Slider) {
|
|
sliderNode = relativeNodePath.node;
|
|
}
|
|
else {
|
|
sliderNode = find(relativeNodePath, this.node);
|
|
}
|
|
|
|
if (!sliderNode) {
|
|
return null;
|
|
}
|
|
let slider = sliderNode.getComponent(Slider);
|
|
let slideEvents = slider.slideEvents;
|
|
for (let i = 0; i < slideEvents.length; ++i) {
|
|
let h = slideEvents[i];
|
|
if (h['$cb$'] == cb && h['$target$'] == target) {
|
|
slideEvents.splice(i, 1);
|
|
slider.slideEvents = slideEvents;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|