327 lines
9.7 KiB
JavaScript
327 lines
9.7 KiB
JavaScript
"use strict";
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.TweenSettings = exports.TweenPackageData = exports.TweenComponentChildData = void 0;
|
||
const csharp_1 = require("csharp");
|
||
const App = csharp_1.FairyEditor.App;
|
||
const File = csharp_1.System.IO.File;
|
||
const Directory = csharp_1.System.IO.Directory;
|
||
class TweenComponentChildData {
|
||
id;
|
||
key;
|
||
useable;
|
||
}
|
||
exports.TweenComponentChildData = TweenComponentChildData;
|
||
// class TweenComponentData {
|
||
// url: string;
|
||
// childs: object = {};
|
||
// }
|
||
class TweenPackageData {
|
||
name;
|
||
components = {};
|
||
}
|
||
exports.TweenPackageData = TweenPackageData;
|
||
class TweenSettings {
|
||
static instance;
|
||
static getInstance() {
|
||
if (!this.instance) {
|
||
this.instance = new TweenSettings();
|
||
}
|
||
return this.instance;
|
||
}
|
||
componentsSettingBasePath = "";
|
||
_TweenMap = new Map();
|
||
_TweenKeyValues = {};
|
||
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);
|
||
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);
|
||
if (datas != null && datas.length > 0) {
|
||
for (const data of datas) {
|
||
this._TweenKeyValues[data['key']] = data[TweenKey];
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
init() {
|
||
console.log("初始化加载动效配置===");
|
||
this.componentsSettingBasePath = App.project.settingsPath + "/whootTween";
|
||
if (!csharp_1.System.IO.Directory.Exists(this.componentsSettingBasePath)) {
|
||
console.log("whoot配置文件目录不存在,创建");
|
||
csharp_1.System.IO.Directory.CreateDirectory(this.componentsSettingBasePath);
|
||
}
|
||
this.readAll();
|
||
}
|
||
/**
|
||
* 清理无效的配置
|
||
*/
|
||
clearFailureConfig() {
|
||
// App.d
|
||
}
|
||
getTween(key) {
|
||
let value = this._TweenKeyValues[key];
|
||
if (value == null) {
|
||
return key;
|
||
}
|
||
return value;
|
||
}
|
||
/**
|
||
* 获取所有动效配置
|
||
* @returns
|
||
*/
|
||
getAllPackage() {
|
||
return this._TweenMap;
|
||
}
|
||
/**
|
||
* 获取一个动效配置
|
||
* @param packageName
|
||
* @param docUrl
|
||
* @param componentId
|
||
*/
|
||
get(packageName, docUrl, componentId) {
|
||
let pack = this._TweenMap.get(packageName);
|
||
if (pack != null) {
|
||
let doc = pack.components[docUrl];
|
||
if (doc != null) {
|
||
let child = doc[componentId];
|
||
return child;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
/**
|
||
* 获取一个组件的动效配置
|
||
* @param packageName
|
||
* @param docUrl
|
||
* @returns
|
||
*/
|
||
getDoc(packageName, docUrl) {
|
||
let pack = this._TweenMap.get(packageName);
|
||
if (pack != null) {
|
||
let doc = pack.components[docUrl];
|
||
if (doc != null) {
|
||
return doc;
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
/**
|
||
* 获取一个包的动效配置
|
||
* @param packageName
|
||
*/
|
||
getPackage(packageName) {
|
||
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 = true) {
|
||
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, docUrl, componentId) {
|
||
let changed = false;
|
||
const pack = this._TweenMap.get(packageName);
|
||
if (!pack)
|
||
return false;
|
||
const doc = pack.components[docUrl];
|
||
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, key) {
|
||
// console.log("obj类型=", obj.GetType())
|
||
if (obj instanceof csharp_1.FairyEditor.FTextInput) {
|
||
let textInput = obj;
|
||
textInput.promptText = this.getTween(key);
|
||
}
|
||
else if (obj instanceof csharp_1.FairyEditor.FButton) {
|
||
let button = obj;
|
||
button.title = this.getTween(key);
|
||
}
|
||
else if (obj instanceof csharp_1.FairyEditor.FLabel) {
|
||
let lable = obj;
|
||
lable.title = this.getTween(key);
|
||
}
|
||
else {
|
||
obj.text = this.getTween(key);
|
||
}
|
||
}
|
||
//endregion
|
||
//region file
|
||
//获取所有的文件的路径
|
||
getAllDirector(path, list) {
|
||
console.log("开始读取所有配置=", path);
|
||
let files = Directory.GetFiles(path, "*.json");
|
||
for (let f = 0; f < files.Length; f++) {
|
||
list.push(files.get_Item(f));
|
||
}
|
||
}
|
||
/**
|
||
* 读取全部
|
||
*/
|
||
readAll() {
|
||
try {
|
||
let list = [];
|
||
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 = JSON.parse(jsonStr);
|
||
this._TweenMap.set(data.name, data);
|
||
}
|
||
}
|
||
catch {
|
||
console.error("读取动效配置文件失败");
|
||
}
|
||
}
|
||
/**
|
||
* 将所有配置保存成配置文件
|
||
*/
|
||
saveAllFile() {
|
||
let keys = this._TweenMap.keys();
|
||
for (const key of keys) {
|
||
this.saveFile(key);
|
||
}
|
||
}
|
||
/**
|
||
* 清理某个文档下已删除的组件配置
|
||
* @param packageName 包名
|
||
* @param docUrl 文档URL
|
||
* @param existComponentIds 该文档中仍然存在的组件id列表
|
||
* @returns 是否有变更
|
||
*/
|
||
cleanupDoc(packageName, docUrl, existComponentIds) {
|
||
const pack = this._TweenMap.get(packageName);
|
||
if (!pack)
|
||
return false;
|
||
const doc = pack.components[docUrl];
|
||
if (!doc)
|
||
return false;
|
||
// 归一到 JS 数组
|
||
let ids = [];
|
||
if (existComponentIds["Length"] != null) {
|
||
// System.Array
|
||
const len = existComponentIds.Length;
|
||
for (let i = 0; i < len; i++) {
|
||
ids.push(existComponentIds.get_Item(i));
|
||
}
|
||
}
|
||
else {
|
||
ids = existComponentIds;
|
||
}
|
||
const idSet = {};
|
||
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
|
||
*/
|
||
saveFile(packageName) {
|
||
let path = this.componentsSettingBasePath + "/" + packageName + ".json";
|
||
let data = this._TweenMap.get(packageName);
|
||
if (data != null) {
|
||
let jsonStr = JSON.stringify(data);
|
||
File.WriteAllText(path, jsonStr);
|
||
}
|
||
}
|
||
}
|
||
exports.TweenSettings = TweenSettings;
|
||
exports.default = TweenSettings.getInstance();
|