import { FairyGUI, FairyEditor, System } from 'csharp'; const App = FairyEditor.App; const File = System.IO.File; const Directory = System.IO.Directory; export class TweenComponentChildData { id: string; key: string; useable: number; } // class TweenComponentData { // url: string; // childs: object = {}; // } export class TweenPackageData { name: string; components: object = {}; } export class TweenSettings { private static instance: TweenSettings; static getInstance() { if (!this.instance) { this.instance = new TweenSettings(); } return this.instance; } public componentsSettingBasePath: string = ""; private readonly _TweenMap: Map = new Map(); private readonly _TweenKeyValues: object = {}; constructor() { this.init() // this._TweenKeyValues['k1'] = "hahha1" // this._TweenKeyValues['k2'] = "hahha2" // this._TweenKeyValues['k3'] = "hahha3" //'CustomProperties' let path = App.project.settingsPath + "/CustomProperties.json" if (File.Exists(path)) { let jsonStr = File.ReadAllText(path); let settings = JSON.parse(jsonStr) as object let TweenKey = "en" if (settings.hasOwnProperty("TweenKey")) { TweenKey = settings['TweenKey']; } if (settings.hasOwnProperty("TweenPath")) { let TweenPath = App.project.basePath + "/" + settings['TweenPath'] console.log("动效路径=", TweenPath) if (File.Exists(TweenPath)) { let TweenStr = File.ReadAllText(TweenPath); let datas = JSON.parse(TweenStr) as Array if (datas != null && datas.length > 0) { for (const data of datas) { this._TweenKeyValues[data['key']] = data[TweenKey] } } } } } } private init() { console.log("初始化加载动效配置===") this.componentsSettingBasePath = App.project.settingsPath + "/whootTween" if (!System.IO.Directory.Exists(this.componentsSettingBasePath)) { console.log("whoot配置文件目录不存在,创建") System.IO.Directory.CreateDirectory(this.componentsSettingBasePath) } this.readAll() } /** * 清理无效的配置 */ clearFailureConfig() { // App.d } getTween(key: string): string { let value = this._TweenKeyValues[key] if (value == null) { return key } return value; } /** * 获取所有动效配置 * @returns */ getAllPackage(): Map { return this._TweenMap; } /** * 获取一个动效配置 * @param packageName * @param docUrl * @param componentId */ get(packageName, docUrl, componentId): TweenComponentChildData { let pack = this._TweenMap.get(packageName) if (pack != null) { let doc = pack.components[docUrl] if (doc != null) { let child = doc[componentId] return child as TweenComponentChildData } } return null; } /** * 获取一个组件的动效配置 * @param packageName * @param docUrl * @returns */ getDoc(packageName, docUrl): object { let pack = this._TweenMap.get(packageName) if (pack != null) { let doc = pack.components[docUrl] if (doc != null) { return doc as object } } return null; } /** * 获取一个包的动效配置 * @param packageName */ getPackage(packageName): object { let pack = this._TweenMap.get(packageName) if (pack) { return pack.components; } return {}; } /** * 更新一个动效配置 * @param packageName * @param docUrl * @param componentId * @param TweenKey * @param useable 是否可用的 */ update(packageName, docUrl, componentId, TweenKey, useable: boolean = true): boolean { let chang = false let pack = this._TweenMap.get(packageName) if (pack == null) { pack = new TweenPackageData() pack.name = packageName this._TweenMap.set(packageName, pack) chang = true } let doc = pack.components[docUrl] if (doc == null) { doc = {}; pack.components[docUrl] = doc;//.set(docUrl, doc) chang = true } let child = doc[componentId] if (child == null) { child = new TweenComponentChildData(); child.id = componentId doc[componentId] = child//.set(componentId, child) } let u = useable ? 1 : 0 if (child.key != TweenKey || child.useable != u) { child.key = TweenKey child.useable = u chang = true } if (chang) { //有数据变化,变更文件 this.saveFile(packageName) } return chang } /** * 删除一个动效配置(当选择“无”或需清除时) * @param packageName * @param docUrl * @param componentId */ remove(packageName: string, docUrl: string, componentId: string): boolean { let changed = false; const pack = this._TweenMap.get(packageName); if (!pack) return false; const doc = pack.components[docUrl] as any; if (!doc) return false; if (doc.hasOwnProperty(componentId)) { delete doc[componentId]; changed = true; } if (changed) { // 若当前文档节点已空,清理条目 if (Object.keys(doc).length === 0) { delete pack.components[docUrl]; } this.saveFile(packageName); } return changed; } //region 工具方法 updateItemTitle(obj: FairyEditor.FObject, key: string) { // console.log("obj类型=", obj.GetType()) if (obj instanceof FairyEditor.FTextInput) { let textInput = obj as FairyEditor.FTextInput textInput.promptText = this.getTween(key) } else if (obj instanceof FairyEditor.FButton) { let button = obj as FairyEditor.FButton button.title = this.getTween(key) } else if (obj instanceof FairyEditor.FLabel) { let lable = obj as FairyEditor.FLabel lable.title = this.getTween(key) } else { obj.text = this.getTween(key) } } //endregion //region file //获取所有的文件的路径 private getAllDirector(path: string, list: Array) { console.log("开始读取所有配置=", path) let files = Directory.GetFiles(path, "*.json") for (let f = 0; f < files.Length; f++) { list.push(files.get_Item(f)) } } /** * 读取全部 */ private readAll() { try { let list: Array = [] this.getAllDirector(this.componentsSettingBasePath, list) this._TweenMap.clear() console.log("初始化所有动效配置") for (let index = 0; index < list.length; index++) { let p = list[index]; let jsonStr = File.ReadAllText(p); let data: TweenPackageData = JSON.parse(jsonStr); this._TweenMap.set(data.name, data) } } catch { console.error("读取动效配置文件失败") } } /** * 将所有配置保存成配置文件 */ private saveAllFile() { let keys = this._TweenMap.keys() for (const key of keys) { this.saveFile(key) } } /** * 清理某个文档下已删除的组件配置 * @param packageName 包名 * @param docUrl 文档URL * @param existComponentIds 该文档中仍然存在的组件id列表 * @returns 是否有变更 */ public cleanupDoc(packageName: string, docUrl: string, existComponentIds: System.Array$1 | Array): boolean { const pack = this._TweenMap.get(packageName) if (!pack) return false; const doc = pack.components[docUrl] as any; if (!doc) return false; // 归一到 JS 数组 let ids: Array = []; if ((existComponentIds as any)["Length"] != null) { // System.Array const len = (existComponentIds as System.Array$1).Length; for (let i = 0; i < len; i++) { ids.push((existComponentIds as any).get_Item(i)); } } else { ids = existComponentIds as Array; } const idSet: { [k: string]: boolean } = {}; for (const id of ids) { if (id) idSet[id] = true; } let changed = false; for (const key in doc) { if (!doc.hasOwnProperty(key)) continue; if (key === "__root__") continue; // 保留父级占位符 if (!idSet[key]) { delete doc[key]; changed = true; } } if (changed) { if (Object.keys(doc).length === 0) { delete pack.components[docUrl]; } this.saveFile(packageName); } return changed; } /** * 保存json文件 * @param packageName */ private saveFile(packageName: string) { let path = this.componentsSettingBasePath + "/" + packageName + ".json"; let data = this._TweenMap.get(packageName) if (data != null) { let jsonStr = JSON.stringify(data); File.WriteAllText(path, jsonStr) } } //endregion } export default TweenSettings.getInstance();