main.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { FairyGUI, FairyEditor, System } from 'csharp';
  2. import { genCodeTs } from './GenCode_TypeScript';
  3. import { genCodeCS } from './GenCode_CSharp';
  4. const App = FairyEditor.App;
  5. App.pluginManager.LoadUIPackage(App.pluginManager.basePath + "/" + eval("__dirname") + '/CustomInspector')
  6. class ExportCodeFlagInspector extends FairyEditor.View.PluginInspector {
  7. // 是否生成代码按钮
  8. private combo: FairyGUI.GButton;
  9. private ctrl_ref: FairyGUI.Controller;
  10. // UIType下拉框
  11. private uitypeBox: FairyGUI.GComboBox;
  12. // UILayer下拉框
  13. private uilayerBox: FairyGUI.GComboBox;
  14. private genCodeKey = "gencode";
  15. private genuitypeKey = "genuitype";
  16. private genuilayerKey = "genuilayer";
  17. public constructor() {
  18. super();
  19. this.panel = FairyGUI.UIPackage.CreateObject("CustomInspector", "ExportCodeFlag").asCom;
  20. this.ctrl_ref = this.panel.GetController("ref")
  21. this.combo = this.panel.GetChild("check").asButton;
  22. this.uitypeBox = this.panel.GetChild("UITypeBox").asComboBox;
  23. this.uilayerBox = this.panel.GetChild("UILayerBox").asComboBox;
  24. //生成代码开关
  25. this.combo.onChanged.Add(() => {
  26. let obj = App.activeDoc.inspectingTarget
  27. //use obj.docElement.SetProperty('xxx',..) instead of obj.xxx = ... to enable undo/redo mechanism
  28. // obj.docElement.SetProperty("customData", this.combo.value)
  29. // console.log("set gencode:" + obj._res.packageItem.id)
  30. this.SetScriptData(obj, this.genCodeKey, this.combo.selected ? "1" : "0");
  31. // if (obj.docElement.isRoot) {
  32. // obj.docElement.SetScriptData("gencode" + obj._res.packageItem.id, this.combo.selected ? "1" : "0")
  33. // } else {
  34. // obj.parent.docElement.SetScriptData("gencode" + obj._res.packageItem.id, this.combo.selected ? "1" : "0")
  35. // }
  36. });
  37. //选择UIType
  38. this.uitypeBox.onChanged.Add(() => {
  39. App.consoleView.Log(`uitypeBox onChanged index:${this.uitypeBox.selectedIndex}`);
  40. let obj = App.activeDoc.inspectingTarget;
  41. this.SetScriptData(obj, this.genuitypeKey, this.uitypeBox.selectedIndex.toString());
  42. });
  43. //选择UILayer
  44. this.uilayerBox.onChanged.Add(() => {
  45. App.consoleView.Log(`uilayerBox onChanged index:${this.uilayerBox.selectedIndex}`);
  46. let obj = App.activeDoc.inspectingTarget;
  47. this.SetScriptData(obj, this.genuilayerKey, this.uilayerBox.selectedIndex.toString());
  48. });
  49. this.updateAction = () => { return this.updateUI(); };
  50. }
  51. private updateUI(): boolean {
  52. let sels = App.activeDoc.inspectingTargets
  53. let obj = sels.get_Item(0) as FairyEditor.FComponent
  54. this.ctrl_ref.SetSelectedPage("false")
  55. this.uitypeBox.selectedIndex = 0; // 默认UIPanel
  56. if (obj.docElement.isRoot) {
  57. this.combo.selected = this.GetAttribute(obj, this.genCodeKey) == "1";
  58. //TODO 初始化赋值
  59. this.uitypeBox.selectedIndex = Number(this.GetAttribute(obj, this.genuitypeKey));
  60. let genuilayer = this.GetAttribute(obj, this.genuilayerKey);
  61. if (genuilayer == null || genuilayer == "")
  62. genuilayer = "1"; // 默认为Normal
  63. this.uilayerBox.selectedIndex = Number(genuilayer);
  64. } else {
  65. this.combo.selected = this.GetAttribute(obj.parent, this.genCodeKey) == "1";
  66. this.uitypeBox.selectedIndex = Number(this.GetAttribute(obj.parent, this.genuitypeKey));
  67. let genuilayer = this.GetAttribute(obj.parent, this.genuilayerKey);
  68. if (genuilayer == null || genuilayer == "")
  69. genuilayer = "1"; // 默认为Normal
  70. this.uilayerBox.selectedIndex = Number(genuilayer);
  71. this.ctrl_ref.SetSelectedPage("true")
  72. // if (obj._pkg.name != obj.parent.pkg.name) {
  73. // this.ctrl_ref.SetSelectedPage("true")
  74. // } else {
  75. // this.ctrl_ref.SetSelectedPage("false")
  76. // }
  77. }
  78. // console.log("current gencode " + obj._res.packageItem.id + " : " + this.combo.selected)
  79. return true; //if everything is ok, return false to hide the inspector
  80. }
  81. private SetScriptData(obj: FairyEditor.FObject, key: string, value: string) {
  82. if (!obj.docElement.isRoot) {
  83. obj = obj.parent;
  84. }
  85. obj.docElement.SetScriptData(key + obj._res.packageItem.id, value);
  86. }
  87. private GetAttribute(obj: FairyEditor.FComponent, key: string): string {
  88. return obj.scriptData.GetAttribute(key + obj._res.packageItem.id);
  89. }
  90. }
  91. //Register a inspector
  92. App.inspectorView.AddInspector(() => new ExportCodeFlagInspector(), "GenCodeFlag", "标记是否生成代码");
  93. //Condition to show it
  94. //App.docFactory.ConnectInspector("GenCodeFlag", "mixed", true, false);
  95. App.docFactory.ConnectInspector("GenCodeFlag", "component", false, false);
  96. App.docFactory.ConnectInspector("GenCodeFlag", "component", true, false);
  97. class LangFlagInspector extends FairyEditor.View.PluginInspector {
  98. private combo: FairyGUI.GButton;
  99. public constructor() {
  100. super();
  101. this.panel = FairyGUI.UIPackage.CreateObject("CustomInspector", "LangFlag").asCom;
  102. this.combo = this.panel.GetChild("check").asButton;
  103. this.combo.onChanged.Add(() => {
  104. let obj = App.activeDoc.inspectingTarget
  105. //use obj.docElement.SetProperty('xxx',..) instead of obj.xxx = ... to enable undo/redo mechanism
  106. // obj.docElement.SetProperty("customData", this.combo.value)
  107. console.log("set lang:" + obj.id)
  108. if (obj.docElement.isRoot) {
  109. obj.docElement.SetScriptData("lang" + obj.id, this.combo.selected ? "1" : "0")
  110. } else {
  111. obj.parent.docElement.SetScriptData("lang" + obj.id, this.combo.selected ? "1" : "0")
  112. }
  113. });
  114. this.updateAction = () => { return this.updateUI(); };
  115. }
  116. private updateUI(): boolean {
  117. let sels = App.activeDoc.inspectingTargets
  118. let obj = sels.get_Item(0) as FairyEditor.FComponent
  119. // console.log(obj.objectType)
  120. if (obj.objectType == "component") {
  121. let ext = checkOtherPackageItemSuperClassType(obj._res.packageItem)
  122. // console.log(ext)
  123. if(!(ext == "Button" || ext == "Label")) {
  124. return false
  125. }
  126. } else if (!(obj.objectType == "loader" || obj.objectType == "text" || obj.objectType == "richtext")) {
  127. return false
  128. }
  129. console.log("lang" + obj.id)
  130. if (obj.docElement.isRoot) {
  131. this.combo.selected = obj.scriptData.GetAttribute("lang" + obj.id) == "1"
  132. } else {
  133. this.combo.selected = obj.parent.scriptData.GetAttribute("lang" + obj.id) == "1"
  134. }
  135. return true; //if everything is ok, return false to hide the inspector
  136. }
  137. }
  138. function checkOtherPackageItemSuperClassType(pkgItem: FairyEditor.FPackageItem) {
  139. let file = System.IO.File.ReadAllText(pkgItem.file)
  140. let xml = new FairyGUI.Utils.XML(file)
  141. let ext = xml?.GetAttribute("extention")
  142. return ext
  143. }
  144. //Register a inspector
  145. App.inspectorView.AddInspector(() => new LangFlagInspector(), "LangFlag", "标记是否多语言对象");
  146. //Condition to show it
  147. App.docFactory.ConnectInspector("LangFlag", "mixed", false, false);
  148. App.docFactory.ConnectInspector("LangFlag", "loader", false, false);
  149. App.docFactory.ConnectInspector("LangFlag", "text", false, false);
  150. App.docFactory.ConnectInspector("LangFlag", "richtext", false, false);
  151. // -------------------开始生成代码----------------------------
  152. function onPublish(handler: FairyEditor.PublishHandler) {
  153. if (!handler.genCode) return;
  154. handler.genCode = false; //prevent default output
  155. console.log('开始生成CS代码');
  156. // genCodeTs(handler);
  157. genCodeCS(handler);
  158. }
  159. function onDestroy() {
  160. //do cleanup here
  161. }
  162. export { onPublish, onDestroy };