395 lines
13 KiB
TypeScript
Raw Normal View History

2025-04-24 17:03:28 +08:00
import Bag from "../../bag/Bag";
import SKLogger from "../util/SKLogger";
import SKUIUtil from "../util/SKUIUtil";
export enum TipAlign {
TOP_RIGHT = 0,
}
export default class FGUtil {
static hasAlert: boolean = false;
private static _root: fgui.GRoot;
// 註冊字體
static regFonts(url: string, completeBlock?: () => void) {
let fonts = cc.loader.getRes(url);
if (fonts) {
return;
}
cc.loader.loadResDir(url, cc.TTFFont, (error: Error, resources: any[], urls: string[]) => {
if (error) {
cc.log(`加載字體錯誤:${error.message}`);
return;
}
for (let resource of resources) {
fgui.registerFont(resource.name, resource);
}
cc.loader.loadResDir(url, cc.BitmapFont, (error: Error, resources: any[], urls: string[]) => {
if (error) {
cc.log(`加載字體錯誤:${error.message}`);
return;
}
for (let resource of resources) {
fgui.registerFont(resource.name, resource);
}
if (completeBlock) {
completeBlock();
}
});
});
}
// 加載包
static load(name: string, progressBlock?: (progress: number) => void, completeBlock?: (error: Error) => void) {
// 判斷是否已加載
if (fgui.UIPackage.getByName(name)) {
if (completeBlock) {
completeBlock(null);
}
return;
}
fgui.addLoadHandler();
fgui.UIPackage.load(name, progressBlock, completeBlock);
}
// 獲得根節點
static root(): fgui.GRoot {
if (this._root == null) {
fgui.GRoot.create();
this._root = fgui.GRoot.inst;
return this._root;
}
let parent = this._root.node.parent;
if (parent != null) {
let valid = SKUIUtil.isValid(parent);
if (valid) {
return this._root;
} else {
SKLogger.warn("fgui.GRoot.inst已釋放");
}
}
fgui.GRoot.create();
this._root = fgui.GRoot.inst;
return this._root;
}
// 適配屏幕
static fitScreen(target: fgui.GObject) {
if (!cc.sys.isNative) {
target.makeFullScreen();
cc.log("H5適配");
} else {
target.makeFullScreen();
cc.log("原生適配");
}
}
// 創建組件
static create(pkgName: string, resName: string): fgui.GComponent {
let object = fgui.UIPackage.createObject(pkgName, resName);
let result: fgui.GComponent = null;
if (object) {
result = object.asCom;
}
return result;
}
// 釋放
static dispose(value: fgui.GObject) {
if (value == null) {
return;
}
if (!SKUIUtil.isValid(value.node)) {
return;
}
value.dispose();
}
// 獲得對像類型
static getObject(target: fgui.GComponent, path: string): fgui.GObject {
if (!target) {
cc.warn(`$警告:獲得fgui.GObject目標為空`);
return target;
}
let list = path.split("/");
let temp: fgui.GComponent = target;
let group: fgui.GGroup = null;
let child: fgui.GObject = null;
for (let name of list) {
if (group == null) {
child = temp.getChild(name);
} else {
child = temp.getChildInGroup(name, group);
group = null;
}
if (child == null) {
return null;
}
temp = child.asCom;
if (temp == null) {
return null;
}
if (temp instanceof fgui.GGroup) {
group = temp;
temp = child.parent;
if (temp == null) {
temp = target;
}
}
}
return temp;
}
// 獲得組件
static getComponent(target: fgui.GComponent, path: string): fgui.GComponent {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到組件:${path}`);
return;
}
let result = temp.asCom;
if (!result) {
cc.log(`${target.name}獲取組件:${path}不是fgui.GComponent類型`);
return null;
}
return result;
}
// 獲得組
static getGroup(target: fgui.GComponent, path: string): fgui.GGroup {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到標籤:${path}`);
return;
}
let result = temp.asGroup;
if (result == null) {
cc.log(`${target.name}获取组:${path}不是fgui.GGroup类型`);
return null;
}
return result;
}
// 獲得控制器
static getControl(target: fgui.GComponent, path: string): fairygui.Controller {
let list = path.split("/");
let name = list[list.length - 1];
list.splice(list.length - 1, 1);
let temp: fgui.GComponent = target;
if (list.length > 0) {
temp = this.getComponent(target, list.join("/"));
if (temp == null) {
cc.log(`${target.name}找不到組件:${path}`);
return;
}
}
let result = temp.getController(name);
if (result == null) {
cc.log(`${target.name}獲取組:${path}不是fairygui.Controller類型`);
return null;
}
return result;
}
// 獲得圖像
static getImage(target: fgui.GComponent, path: string): fgui.GImage {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到組件:${path}`);
return;
}
let result = temp.asImage;
if (!result) {
cc.log(`${target.name}獲取組件:${path}不是fgui.GComponent類型`);
return null;
}
return result;
}
// 獲得標籤
static getTextField(target: fgui.GComponent, path: string): fgui.GTextField {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到標籤:${path}`);
return;
}
let result = temp.asTextField;
if (!result) {
cc.log(`${target.name}獲取標籤:${path}不是fgui.GTextField類型`);
return null;
}
return result;
}
// 獲得按鈕
static getButton(target: fgui.GComponent, path: string): fgui.GButton {
if (target == null) {
return null;
}
let temp = this.getObject(target, path);
if (temp == null) {
cc.log(`${target.name}找不到按鈕:${path}`);
return;
}
let result = temp.asButton;
if (result == null) {
cc.log(`${target.name}獲取按鈕:${path}不是fgui.GButton類型`);
return null;
}
return result;
}
// 獲得輸入文本
static getTextInput(target: fgui.GComponent, path: string): fgui.GTextInput {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到輸入文本:${path}`);
return;
}
let result = temp.asTextInput;
if (!result) {
cc.log(`${target.name}獲取輸入文本:${path}不是fgui.GTextInput類型`);
return null;
}
return result;
}
// 獲得進度條
static getProgressBar(target: fgui.GComponent, path: string): fgui.GProgressBar {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到進度條:${path}`);
return;
}
let result = temp.asProgress;
if (!result) {
cc.log(`${target.name}獲取進度條:${path}不是fgui.GProgressBar類型`);
return null;
}
return result;
}
// 獲得加載器
static getLoader(target: fgui.GComponent, path: string): fgui.GLoader {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到加載器:${path}`);
return;
}
let result = temp.asLoader;
if (!result) {
cc.log(`${target.name}獲取加載器:${path}不是fgui.GLoader類型`);
return null;
}
return result;
}
// 獲得列表
static getList(target: fgui.GComponent, path: string): fgui.GList {
let temp = this.getObject(target, path);
console.log(temp , 777)
if (!temp) {
cc.log(`${target.name}找不到列表:${path}`);
return;
}
let result = temp.asList;
if (!result) {
cc.log(`${target.name}獲取列表:${path}不是fgui.GList類型`);
return null;
}
return result;
}
// 獲得富文本
static getRichTextField(target: fgui.GComponent, path: string): fgui.GRichTextField {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到富文本:${path}`);
return;
}
let result = temp.asRichTextField;
if (!result) {
cc.log(`${target.name}獲取富文本:${path}不是fgui.GRichTextField類型`);
return null;
}
return result;
}
// 獲得動畫
static getAnim(target: fgui.GComponent, path: string): fgui.GMovieClip {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到動畫:${path}`);
return;
}
let result = temp.asMovieClip;
if (!result) {
cc.log(`${target.name}獲取列表:${path}不是fgui.GMovieClip類型`);
return null;
}
return result;
}
// 獲得位圖精靈
static getImageSprite(target: fgui.GComponent, path: string): fgui.Image {
let temp = this.getObject(target, path);
if (!temp) {
cc.log(`${target.name}找不到位圖精靈:${path}`);
return;
}
let result = temp.asImage;
if (!result) {
cc.log(`${target.name}獲取位圖精靈:${path}不是fgui.GImage類型`);
return null;
}
return result._content;
}
// 設置按鈕
static setButton(target: fgui.GComponent, path: string, listener: Function, owner: any): fgui.GButton {
let btn = this.getButton(target, path);
if (!btn) {
cc.log(`設置按鈕:找不到按鈕${path}`);
return null;
}
btn.onClick(listener, owner);
}
// 設置輸入文本
static setTextInput(target: fgui.GComponent, path: string, prompt: string, password: boolean = false): fgui.GTextInput {
let ti = this.getTextInput(target, path);
if (!ti) {
cc.log(`找不到輸入文本${path}`);
return null;
}
ti.password = password;
ti.ubbEnabled = true;
let promptText = `[i][color=#999999]${prompt}[/color][/i]`;
ti.promptText = promptText;
cc.log(`設置${path}輸入文本類型成功!`);
return ti;
}
static align(source: fgui.GObject, target: fgui.GObject, align: TipAlign) {
let pos = source.localToGlobal(0, 0);
let tx = pos.x + source.width - target.width;
let ty = pos.y - target.height;
target.setPosition(tx, ty);
}
// 獲得文本渲染寬度
static labelWidthOf(value: string, fontSize: number): number {
let node = new cc.Node();
cc.director.getScene().addChild(node);
let label = node.addComponent(cc.Label);
label.fontSize = fontSize;
label.string = value;
// _forceUpdateRenderData(); 2.2及後的版本都是用的這個。
// _updateRenderData(true); 這個好像是2.1及之前版本用的。
// 可以看源碼 ...resources\engine\cocos2d\core\components\CCLabel.js
// 直接操作組件對象
(<any>label)._forceUpdateRenderData();
let width = Math.ceil(label.node.getContentSize().width);
node.removeFromParent();
return width;
}
// 獲得富文本渲染宽度
static richTextWidthOf(value: fgui.GRichTextField): number {
let node = new cc.Node();
cc.director.getScene().addChild(node);
let richTF = node.addComponent(cc.RichText);
richTF.fontSize = value.fontSize;
let rt = value.node.getComponent(cc.RichText);
richTF.string = rt.string;
// 可看源碼 ...resources\engine\cocos2d\core\components\CCLabel.js\CCRichText.js
// 可看源碼 ...resources\engine\bin\.cache\dev\cocos2d\core\CCNode.js
// 1. 先操作組件所處節點對象(激活組件所在的節點)
// 2. 再刷新組件
(<any>richTF)._activeInHierarchy = true;
(<any>richTF)._updateRichText();
let width = Math.ceil(richTF.node.getContentSize().width);
node.removeFromParent();
return width;
}
}