import { FairyGUI, FairyEditor, System } from 'csharp'; import { genCode } from './GenCode_CSharp'; const App = FairyEditor.App; App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/Common') class CalFGUIJsonDataItem{ public url: string; public isGenerate:boolean; public comment:string; public constructor(url:string){ this.url=url; this.isGenerate=false; this.comment=null; } } //let ListString = $generic(System.Collections.Generic.List$1, System.String); //let DictionaryStringCalFGUIJsonDataItem = $generic(System.Collections.Generic.Dictionary$2, System.String, CalFGUIJsonDataItem); class CalFGUIJsonData { public keys:Array; public values:Array; public constructor(dict:Map ){ this.keys = new Array(); this.values = new Array(); for (let [k,v] of dict) { this.keys.push(k); this.values.push(v); } } public Get():Map{ let dict = new Map(); if(this.keys){ for (let index = 0; index < this.keys.length; index++) { const key = this.keys[index]; const value = this.values[index]; dict.set(key,value); } } return dict; } } export class CalFGUIData extends System.Object { private dic : Map; private path:string; public constructor(path:string) { super(); this.path =path; if(!System.IO.File.Exists(path)) { System.IO.File.Create(path); } this.dic = new Map(); } public SetData(name :string,obj:CalFGUIJsonDataItem):void { this.dic.set(name,obj); } public GetData(name:string):CalFGUIJsonDataItem { if(this.dic.has(name)){ return this.dic.get(name); } return null; } public Serialize() { let json :string = JSON.stringify(new CalFGUIJsonData(this.dic)); json = json.replace(/,\{/g,",\r\n{"); System.IO.File.WriteAllText(this.path,json); } public static Derserialize(calFGUIData:CalFGUIData) { let json :string=System.IO.File.ReadAllText(calFGUIData.path); if(!json){ calFGUIData.dic = new Map(); return; } let jsonData = JSON.parse(json); let calFGUIJsonData:CalFGUIJsonData = Object.create(CalFGUIJsonData.prototype); let ret :CalFGUIJsonData= Object.assign(calFGUIJsonData,jsonData); if(ret) { calFGUIData.dic = ret.Get(); console.log("加载成功"); } } public CheckMissURL() { let list = new Array(); for (const [k,v] of this.dic) { let pi = App.project.GetItemByURL(k); if(!pi||pi.isDisposed){ list.push(k); } } for (const key of list) { console.log(key); this.dic.delete(key); } } } class DemoInspector extends FairyEditor.View.PluginInspector { private button: FairyGUI.GButton; private text: FairyGUI.GTextInput; private data: CalFGUIData; public constructor() { super(); let path = App.project.assetsPath+"/data.txt"; this.data = new CalFGUIData(path); this.OnDeserialize(); this.CheckMissURL(); this.panel = FairyGUI.UIPackage.CreateObject("Common", "UIPlaugin").asCom; this.button = this.panel.GetChild("btn").asButton; this.text = this.panel.GetChild("inp").asTextInput; this.button.onClick.Set(() => { let url =App.activeDoc.packageItem.GetURL(); let itemData = this.data.GetData(url); if(!itemData){ itemData = new CalFGUIJsonDataItem(url); } itemData.isGenerate = this.button.selected; this.data.SetData(url,itemData); this.updateUI(); this.OnSerialize(); }); this.text.onFocusOut.Set(()=>{ let url =App.activeDoc.packageItem.GetURL(); let itemData = this.data.GetData(url); if(!itemData){ itemData = new CalFGUIJsonDataItem(url); } itemData.comment = this.text.text; this.data.SetData(url,itemData); this.updateUI(); this.OnSerialize(); }) this.updateAction = () => { return this.updateUI(); }; this.disposeAction = ()=>{ }; } private OnSerialize() { this.data.Serialize(); } private OnDeserialize() { CalFGUIData.Derserialize(this.data); } private CheckMissURL(){ this.data.CheckMissURL(); } private updateUI(): boolean { let url =App.activeDoc.packageItem.GetURL(); let itemData = this.data.GetData(url); if(!itemData){ itemData = new CalFGUIJsonDataItem(url); } this.button.selected = itemData.isGenerate; this.text.text = itemData.comment; return true; //if everything is ok, return false to hide the inspector } } //Register a inspector App.inspectorView.AddInspector(() => new DemoInspector(), "CustomInspectorJS", "CustomInspector"); //Condition to show it App.docFactory.ConnectInspector("CustomInspectorJS", "mixed", true, false); let hasCalData = false; let data:CalFGUIData; function onPublish(handler: FairyEditor.PublishHandler) { if (!handler.genCode) return; handler.genCode = false; //prevent default output console.log('Handling gen code in plugin'); if(!hasCalData){ hasCalData=true; let path = App.project.assetsPath+"/data.txt"; try { data = new CalFGUIData(path); CalFGUIData.Derserialize(data); } catch (error) { App.consoleView.LogError(error.toString()); } } try { genCode(handler,data); //do it myself } catch (error) { App.consoleView.LogError(error.toString()); } } function onDestroy() { //do cleanup here } export { onPublish, onDestroy };