86 lines
3.1 KiB
TypeScript
86 lines
3.1 KiB
TypeScript
import { FairyGUI, FairyEditor, System } from 'csharp';
|
|
import { $generic, $typeof } from 'puerts';
|
|
import { IConfig, IComponent, EComponent, EMode, IInspector } from './index';
|
|
import { CustomAttributer } from './TweenCoustomInspector';
|
|
const TweenSettings = require('./TweenSettings').default as any;
|
|
|
|
const App = FairyEditor.App;
|
|
// 首先读取本地文件配置获取是否本地配置还是远程配置
|
|
// local
|
|
let filePath = `${FairyEditor.App.project.basePath}/plugins/whoot-tween/config.json`;
|
|
let config: IConfig;
|
|
try {
|
|
let sw = new System.IO.StreamReader(filePath);
|
|
let data = sw.ReadToEnd();
|
|
sw.Close();
|
|
console.log("获取到本地配置:", data);
|
|
config = JSON.parse(data);
|
|
} catch (e) {
|
|
console.warn(e);
|
|
}
|
|
if (config.remote) {
|
|
// remote
|
|
let url = config.remote;
|
|
let request = System['Net']['WebRequest'].Create(url);
|
|
request.Method = "GET";
|
|
request.ContentType = "text/html;charset=UTF-8";
|
|
let response = request.GetResponse();
|
|
let responseStream = response.GetResponseStream();
|
|
let streamReader = new System.IO.StreamReader(responseStream);
|
|
let res = streamReader.ReadToEnd();
|
|
try {
|
|
console.log("获取到远程配置:", config);
|
|
config = JSON.parse(res);
|
|
} catch (e) {
|
|
console.warn(e);
|
|
}
|
|
}
|
|
|
|
|
|
App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/TweenAttributer')
|
|
for (let i = 0; i < config.inspectors.length; i++) {
|
|
let inspector = config.inspectors[i];
|
|
let { parent, title } = inspector;
|
|
App.inspectorView.AddInspector(() => new CustomAttributer(inspector), title, title);
|
|
App.docFactory.ConnectInspector(title, "mixed", parent, false);
|
|
}
|
|
|
|
// 当层级结构变化(增删控件)时,自动清理 JSON 中已删除组件的记录
|
|
const onHierarchyChanged = (_ctx?: any) => {
|
|
try {
|
|
const activeDoc = App.activeDoc as FairyEditor.View.IDocument;
|
|
if (!activeDoc) return;
|
|
const packageName = activeDoc.packageItem.owner.name;
|
|
const docUrl = activeDoc.docURL;
|
|
const ids: string[] = [];
|
|
const visited: { [k: string]: boolean } = {};
|
|
|
|
const collect = (node: FairyEditor.FObject) => {
|
|
if (!node) return;
|
|
const nid = (node as any).id as string;
|
|
if (nid && !visited[nid]) {
|
|
visited[nid] = true;
|
|
ids.push(nid);
|
|
}
|
|
if (node instanceof FairyEditor.FComponent) {
|
|
const cnt = (node as FairyEditor.FComponent).numChildren;
|
|
for (let i = 0; i < cnt; i++) {
|
|
const child = (node as FairyEditor.FComponent).GetChildAt(i);
|
|
collect(child);
|
|
}
|
|
}
|
|
};
|
|
|
|
const docAny = activeDoc as any;
|
|
if (docAny && docAny.content) collect(docAny.content);
|
|
TweenSettings.cleanupDoc(packageName, docUrl, ids);
|
|
} catch (err) {
|
|
console.error('HierarchyChanged 自动清理失败', err);
|
|
}
|
|
};
|
|
|
|
// 订阅层级变化事件
|
|
// @ts-ignore
|
|
App.On(FairyEditor.EditorEvents.HierarchyChanged, onHierarchyChanged);
|
|
|