大侠
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

222 lines
8.6 KiB

import { _decorator, Animation, assetManager, AssetManager, Button, CCBoolean, Component, Font, game, Label, Node, PageView, RichText, ScrollView, Slider, sp, Sprite, ToggleContainer, TTFFont } from "cc";
import { EDITOR } from "cc/env";
import MesMgr from "../MesEvent/MesMgr";
import { LocalDataMgr } from "../Mgr_LocalData/LocalDataMgr";
import { Utils } from "../ExUtils/Utils";
import { SysDefine } from "./Config/SysDefine";
import { UIParseTools } from "./Helps/UIParseTools";
const { ccclass, property} = _decorator;
const fontUUID:string = "c1d020c7-6629-4ed0-b610-7d4b7c098c1a"
@ccclass
export default class UIParse extends Component{
/**是否自动绑定 */
@property({ displayName: '是否自动绑定' })
public mAutoBind: boolean = true;
@property({ displayName: '自动获取节点', editorOnly: true })
mAutoFindNode: boolean = true;
@property({ displayName: "点击自动获取节点"})
get mEditorAutoBind(): boolean {
return false;
};
set mEditorAutoBind(value: boolean) {
let last = this.mAutoFindNode;
this.mAutoFindNode = true;
this.onFocusInEditor();
this.mAutoFindNode = last;
};
@property({ type: CCBoolean, displayName: "重新解析节点"})
get mEditorAutoCreateBind(): boolean {
return false;
};
set mEditorAutoCreateBind(value: boolean) {
if (value) {
window["UIScriptsCreate"]?.ReParsing(this, this.node);
}
};
@property({ displayName: "点击替换节点下所有字体"})
get mEditorSetFont(): boolean {
return false;
};
set mEditorSetFont(value: boolean) {
EDITOR && this.onParseEditorLabelView(this.views, this.node);
};
/**本地数据管理 */
get mDataMgr(){
return LocalDataMgr;
}
/**工具类 */
get mUtils(){
return Utils;
}
/** 当前所有的节点 */
public views: Map<string, Node> = new Map();
protected onLoad(): void {
this.onParseView(this.views,this.node);
this.addEvent();
}
addEvent(){};
/**
* 解析当前节点
* @param views 当前节点的保存位置
* @param node 当前需要解析的节点
* @param pName 当前解析的初始名字
* **/
onParseView(views: Map<string, Node>, node: Node, pName = '') {
let children = node.children;
for (let i = 0; i < children.length; i++) {
views.set(pName + children[i].name, children[i]);
if (this.mAutoBind && children[i].getComponent(Button) && typeof this['onButtonClick'] == 'function') {
CNodeUtils.addBtnLister(children[i], this['onButtonClick'].bind(this, children[i].getComponent(Button)), this);
}
//当前自动绑定
if (this.mAutoBind && children[i].getComponent(Button) && typeof this['onClick_' + children[i].name] == 'function') {
CNodeUtils.addBtnLister(children[i], this['onClick_' + children[i].name].bind(this, children[i]), this);
}
if (this.mAutoBind && children[i].getComponent(PageView) && typeof this['onPage_' + children[i].name] == 'function') {
let class_name = this.node.name + SysDefine.NameSuffix;
let pageView_handler = CNodeUtils.addEventHandler(this.node, class_name, 'onPage_' + children[i].name);
children[i].getComponent(PageView).pageEvents.push(pageView_handler);
}
if (this.mAutoBind && children[i].getComponent(ToggleContainer) && typeof this['onToggle_' + children[i].name] == 'function') {
let class_name = this.node.name + SysDefine.NameSuffix;
let pageView_handler = CNodeUtils.addEventHandler(this.node, class_name, 'onToggle_' + children[i].name);
children[i].getComponent(ToggleContainer).checkEvents.push(pageView_handler);
}
if (this.mAutoBind && children[i].getComponent(Slider) && typeof this['onSlider_' + children[i].name] == 'function') {
let class_name = this.node.name + SysDefine.NameSuffix;
let pageView_handler = CNodeUtils.addEventHandler(this.node, class_name, 'onSlider_' + children[i].name);
children[i].getComponent(Slider).slideEvents.push(pageView_handler);
}
if (this.mAutoBind && children[i].getComponent(ScrollView) && typeof this['onScrollView_' + children[i].name] == 'function') {
let class_name = this.node.name + SysDefine.NameSuffix;
let pageView_handler = CNodeUtils.addEventHandler(this.node, class_name, 'onScrollView_' + children[i].name);
children[i].getComponent(ScrollView).scrollEvents.push(pageView_handler);
}
//如果当前是文字 则自动切换语言
if (children[i].getComponent(Label)) {
/** 记录原始多语言值 */
children[i].attr({ original: children[i].getComponent(Label).string });
CNodeUtils.switchText(children[i], children[i].getComponent(Label).string)
}
//如果当前是文字 则自动切换语言
if (children[i].getComponent(RichText)) {
/** 记录原始多语言值 */
children[i].attr({ original: children[i].getComponent(RichText).string });
CNodeUtils.switchRichText(children[i], children[i].getComponent(RichText).string)
}
// if(this.mRedDot && children[i].name == 'red'){
// children[i].addComponent(RedDotComponent);
// }
if (children[i].children.length) {
this.onParseView(views, children[i], pName + children[i].name + '/');
}
}
}
/**
* * 解析当前节点
* * @param views 当前节点的保存位置
* * @param node 当前需要解析的节点
* * @param pName 当前解析的初始名字
*/
onParseEditorView(views: Map<string, Node>, node: Node, pName = '') {
let children = node.children;
for (let i = 0; i < children.length; i++) {
//当前自动绑定
if (this.mAutoBind && typeof this[children[i].name] != "undefined") {
UIParseTools.AutoBindNode(children[i],this);
}
if (children[i].children.length) {
if(children[i].getComponent(UIParse)){
continue;
}
this.onParseEditorView(views, children[i], pName + children[i].name + '/');
}
}
}
/**
* * 解析当前节点
* * @param views 当前节点的保存位置
* * @param node 当前需要解析的节点
* * @param pName 当前解析的初始名字
*/
onParseEditorLabelView(views: Map<string, Node>, node: Node) {
let children = node.children;
for (let i = 0; i < children.length; i++) {
const tLabel = children[i].getComponent(Label) ;
let isAddFont = true;
if (isAddFont) {
if (tLabel && tLabel.useSystemFont) {
tLabel.useSystemFont = false;
tLabel.isBold = false;
AssetManager.instance.loadAny(fontUUID, (err, Font: Font) => {
tLabel.font = Font;
})
}
} else {
if (tLabel && !tLabel.useSystemFont) {
tLabel.useSystemFont = true;
tLabel.font = null;
}
}
const tRichText = children[i].getComponent(RichText);
if (isAddFont) {
if (tRichText && tRichText.useSystemFont) {
tRichText.useSystemFont = false;
AssetManager.instance.loadAny(fontUUID, (err, Font: Font) => {
//@ts-ignore
tRichText.font = Font;
})
}
} else {
if (tRichText && !tRichText.useSystemFont) {
tRichText.useSystemFont = true;
tRichText.font = null;
}
}
if (children[i].children.length) {
if (children[i].getComponent(UIParse)) {
continue;
}
this.onParseEditorLabelView(views, children[i]);
}
}
}
protected onDestroy(): void {
MesMgr.offAllCaller(this);
game.targetOff(this);
}
onFocusInEditor(): void {
if (EDITOR){
if (this.mAutoFindNode) {
this.onParseEditorView(this.views, this.node)
}
}
}
}