panel_ex.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. 面板扩展
  3. 功能: 新建脚本并绑定组件
  4. */
  5. 'use strict';
  6. const path = require('path');
  7. const fs = require('fs');
  8. const config = require('../../config');
  9. const tools = require('../../tools/tools');
  10. const exec = require('child_process').exec;
  11. let TEMPLE_PATH = path.join(path.resolve(__dirname, './'), 'new_file_temple');
  12. let USER_TEMPLE_PATH = path.join(config.cacheDir, 'new_file_temple');
  13. let NEW_FILE_RULE = path.join(
  14. path.resolve(__dirname, './'),
  15. 'new_script_rule.js'
  16. );
  17. let USER_NEW_FILE_RULE = path.join(config.cacheDir, 'new_script_rule.js');
  18. module.exports = {
  19. USER_NEW_FILE_RULE,
  20. /** @type import('../../panel/vs-panel/vs-panel-base') */
  21. parent : null,
  22. // 面板初始化
  23. ready(parent) {
  24. // index.js 对象
  25. this.parent = parent;
  26. this.currSelectInfo = {}
  27. },
  28. // monaco 编辑器初始化
  29. onLoad() {
  30. this.temples = {};
  31. // 首次使用拷贝模板到可写路径
  32. if (
  33. !tools.isDirectory(USER_TEMPLE_PATH) &&
  34. tools.isDirectory(TEMPLE_PATH)
  35. ) {
  36. tools.createDir(USER_TEMPLE_PATH);
  37. let fileList = tools.getFileList(TEMPLE_PATH, []);
  38. for (let i = 0; i < fileList.length; i++) {
  39. const filePath = fileList[i];
  40. tools.copyFile(
  41. filePath,
  42. path.join(USER_TEMPLE_PATH, path.basename(filePath))
  43. );
  44. }
  45. }
  46. // 首次使用拷贝模板到可写路径
  47. if (!tools.isFileExit(USER_NEW_FILE_RULE)) {
  48. tools.copyFile(NEW_FILE_RULE, USER_NEW_FILE_RULE);
  49. }
  50. this.upTempletList();
  51. },
  52. upTempletList() {
  53. this.temples = {};
  54. let fileList = tools.getFileList(USER_TEMPLE_PATH, []);
  55. for (let i = 0; i < fileList.length; i++) {
  56. const filePath = fileList[i];
  57. if (
  58. filePath.indexOf('.DS_Store') != -1 ||
  59. filePath.indexOf('desktop.ini') != -1
  60. ) {
  61. continue;
  62. }
  63. this.temples[path.basename(filePath)] = filePath; // ['file.js'] = 'dir/game/file.js'
  64. }
  65. },
  66. newFileAndBindNode(templePath,type,uuid) {
  67. if (templePath == null || !tools.isFileExit(templePath)) {
  68. console.log('新建脚本文件不存在');
  69. return;
  70. }
  71. Editor.Scene.callSceneScript('simple-code','get-curr-scene-url-and-node',{type,uuid},(err, args)=> {
  72. if (args == null) {
  73. return;
  74. }
  75. try {
  76. let saveUrl = require(USER_NEW_FILE_RULE).getSavePath(
  77. templePath,
  78. args.url,
  79. args.currNodeName
  80. );
  81. if(!saveUrl || saveUrl == ''){
  82. // 返回空的保存路径不执行后续步骤
  83. return;
  84. }
  85. let saveFspath = Editor.remote.assetdb.urlToFspath(saveUrl);
  86. tools.createDir(saveFspath);
  87. args = { templePath, saveUrl, saveFspath };
  88. args.type = type;
  89. args.uuid = uuid;
  90. Editor.Scene.callSceneScript('simple-code','new-js-file',args,(err, event) => {
  91. Editor.Ipc.sendToPanel(
  92. 'simple-code',
  93. 'custom-cmd',
  94. { cmd: 'openFile' }
  95. );
  96. });
  97. } catch (error) {
  98. Editor.error(
  99. tools.translateZhAndEn(
  100. '检测新建脚本规则是否填错:',
  101. 'Check if new script rule is filled incorrectly:'
  102. ),
  103. error
  104. );
  105. }
  106. });
  107. },
  108. /** 需要刷新creator右键菜单
  109. * @param type = node | asset
  110. * */
  111. onRefreshCreatorMenu(type,uuid){
  112. this.updateMenu(type,uuid)
  113. },
  114. updateMenu(type,uuid){
  115. if (uuid == null) {
  116. // 清除菜单
  117. Editor.Ipc.sendToMain('simple-code:setMenuConfig', {
  118. id: 'cc-new-file',
  119. menuCfg: undefined,
  120. });
  121. return;
  122. }
  123. let submenu = [];
  124. for (const key in this.temples) {
  125. let item = { label: key, enabled: true, cmd: 'new-script-templet' };
  126. submenu.push(item);
  127. }
  128. submenu.push({ type: 'separator' });
  129. submenu.push({
  130. label: tools.translateZhAndEn('刷新模板', 'Refresh Templates'),
  131. enabled: true,
  132. cmd: 'refresh-template',
  133. });
  134. submenu.push({
  135. label: tools.translateZhAndEn('自定义模板', 'Custom Template'),
  136. enabled: true,
  137. cmd: 'custom-template',
  138. });
  139. submenu.push({
  140. label: tools.translateZhAndEn(
  141. '自定义生成规则',
  142. 'Custom Build Rules'
  143. ),
  144. enabled: true,
  145. cmd: 'custom-build-templet-rules',
  146. });
  147. let menuCfg = {
  148. layerMenu: [
  149. { type: 'separator' },
  150. {
  151. label: tools.translate('new-script-bind'),
  152. enabled: true,
  153. submenu: submenu,
  154. }, // 生成拖拽组件
  155. ],
  156. assetMenu: [
  157. { type: 'separator' },
  158. {
  159. label: tools.translate('new-script-templet'),
  160. enabled: true,
  161. submenu: submenu,
  162. }, // 生成拖拽组件
  163. ],
  164. };
  165. this.menuCfg = menuCfg;
  166. Editor.Ipc.sendToMain('simple-code:setMenuConfig', {
  167. id: 'cc-new-file',
  168. menuCfg: menuCfg,
  169. });
  170. },
  171. // 面板销毁
  172. onDestroy() {},
  173. messages: {
  174. 'new-js-file'() {
  175. let filePath = this.temples['define.' + this.parent.cfg.newFileType];
  176. let info = Editor.Selection.curGlobalActivate()
  177. this.newFileAndBindNode(filePath,info.type,info.id);
  178. },
  179. // 刷新模板
  180. 'refresh-template'(e, args) {
  181. this.upTempletList();
  182. let selectInfo = this.parent.currCreatorEditorSelectInfo;
  183. this.updateMenu(selectInfo.type,selectInfo.uuid);
  184. },
  185. // 自定模板
  186. 'custom-template'(e, args) {
  187. exec((Editor.isWin32 ? 'start ' : 'open ') + USER_TEMPLE_PATH);
  188. },
  189. // 自定规则
  190. 'custom-build-templet-rules'(e, args) {
  191. this.parent.openOutSideFile(USER_NEW_FILE_RULE, true);
  192. },
  193. 'new-script-templet'(e, args) {
  194. let selectInfo = this.parent.currCreatorEditorSelectInfo;
  195. if (selectInfo.uuid == null) {
  196. return;
  197. }
  198. // 在资源管理中创建
  199. if (selectInfo.type == 'asset') {
  200. let templePath = this.temples[args.label];
  201. let filePath = Editor.remote.assetdb.uuidToFspath(
  202. selectInfo.uuid
  203. );
  204. let fspath = tools.isDirectory(filePath)
  205. ? filePath
  206. : path.dirname(filePath);
  207. if (!templePath || !tools.isDirectory(fspath)) {
  208. return;
  209. }
  210. let s_ind = fspath.indexOf(config.prsPath);
  211. if (
  212. s_ind == -1 ||
  213. !fspath.substr(config.prsPath.length).match('assets')
  214. ) {
  215. alert(
  216. tools.translateZhAndEn(
  217. '不能选择在根目录创建模板',
  218. 'Cannot choose to create template in root directory'
  219. )
  220. );
  221. return;
  222. }
  223. let data = fs.readFileSync(templePath);
  224. fspath = path.join(fspath, args.label);
  225. let saveUrl = Editor.remote.assetdb.fspathToUrl(fspath);
  226. Editor.assetdb.create(saveUrl, data);
  227. } else {
  228. // 节点上创建
  229. let templePath = this.temples[args.label];
  230. this.newFileAndBindNode(templePath,this.parent.currCreatorEditorSelectInfo.type,this.parent.currCreatorEditorSelectInfo.uuid);
  231. }
  232. },
  233. },
  234. };