main.js 7.7 KB

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