panel_ex.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. 面板扩展
  3. 功能: 下拉框转跳文件,转跳场景,重命名,打开项目到外部编辑器...
  4. */
  5. 'use strict';
  6. const path = require('path');
  7. const md5 = require('md5');
  8. const fs = require('fs');
  9. const cfg = Editor.require('packages://simple-code/config.js');
  10. const tools = Editor.require('packages://simple-code/tools/tools.js');
  11. module.exports = {
  12. // 面板初始化
  13. onLoad(parent){
  14. // index.js 对象
  15. this.parent = parent;
  16. // 键盘事件:重名
  17. this.parent.addKeybodyEventByName('renameNodeOrFile',(e)=>
  18. {
  19. // 不是输入状态是时
  20. if ( this.openRename()){
  21. e.preventDefault();// 吞噬捕获事件
  22. }
  23. },0)
  24. },
  25. // 显示重命名框, list = [{value,meta,score,args}]
  26. showRenameBox(type,reType,list){
  27. if(list.length == 0) return;
  28. for (let i = 0; i < list.length ; i++) {
  29. list[i]._value = list[i].value;
  30. }
  31. // 重命名规则函数
  32. let changeListName = (name)=>{
  33. if(reType == 'sort'){
  34. let findObj = name.match(/@([0-9]+)/)
  35. let start_num = findObj ? Number( findObj[1] ) : 0;
  36. for (let i = 0; i < list.length ; i++) {
  37. let ind = (start_num+i)
  38. if (findObj == null){
  39. list[i].value = name // 重命名
  40. }else{
  41. list[i].value = name.substr(0,findObj.index) + String(ind) + name.substr(findObj.index+findObj[0].length) // 重命名
  42. }
  43. }
  44. }else if(reType == 'prefix'){
  45. for (let i = 0; i < list.length ; i++) {
  46. list[i].value = name+list[i]._value;
  47. }
  48. }else if(reType == 'suffix'){
  49. for (let i = 0; i < list.length ; i++) {
  50. list[i].value = list[i]._value+name;
  51. }
  52. }else if(reType == 'remove_name'){
  53. for (let i = 0; i < list.length ; i++) {
  54. try {
  55. let findObj = list[i]._value.match(new RegExp(name));
  56. if(findObj){
  57. list[i].value = list[i]._value.substr(0,findObj.index) + list[i]._value.substr(findObj.index+findObj[0].length);
  58. }
  59. } catch (error) {
  60. }
  61. }
  62. }else if(reType == 'com'){
  63. for (let i = 0; i < list.length ; i++) {
  64. list[i].value = name;
  65. }
  66. }
  67. }
  68. // 修改搜索框时,通过该函数读取显示的实时显示下拉列表内容, cmdLine为输入文本框对象
  69. let onCompletionsFunc = (cmdLine)=>{
  70. let name = cmdLine.getValue();
  71. // if (name == '') return list;
  72. changeListName(name);
  73. return list;
  74. }
  75. // 选中后处理
  76. let onAccept = (data,cmdLine)=>{
  77. let name = cmdLine.getValue();
  78. changeListName(name);
  79. if(type == "asset")
  80. {
  81. // 重命名资源
  82. list.forEach((info)=>{
  83. let to_path = info.args.dir_path+info.value+info.args.suffix;
  84. Editor.remote.assetdb.move(info.args.url,to_path);
  85. })
  86. }else if(type == "node")
  87. {
  88. // 重命名节点
  89. list.forEach((info)=>{
  90. let rename = info.value;
  91. Editor.Ipc.sendToPanel('scene', 'scene:set-property',{
  92. id: info.args.uuid,
  93. path: "name",//要修改的属性
  94. type: "String",
  95. value: rename,
  96. isSubProp: false,
  97. });
  98. })
  99. }
  100. }
  101. let texts = {
  102. 'sort':list[0].value + "@0" ,
  103. 'prefix': 'type_',
  104. 'suffix': '_type',
  105. 'remove_name': '[0-9]+',
  106. 'com': list[0].value ,
  107. }
  108. // 显示下拉框
  109. this.parent.ace.openSearchBox( texts[reType] || '' ,list,onAccept,onCompletionsFunc);
  110. this.parent.ace.setMiniSearchBoxToTouchPos(200)
  111. },
  112. getFileName(path){
  113. let s_i = path.lastIndexOf("/")+1
  114. let e_i = path.lastIndexOf(".")
  115. e_i = e_i < s_i ? -1 :e_i
  116. let name = path.substr(s_i, e_i == -1 ? undefined : e_i - s_i )
  117. let suffix = e_i == -1 ? "" : path.substr(e_i)
  118. return {name,suffix,dir_path:path.substr(0,s_i)}
  119. },
  120. // 面板销毁
  121. onDestroy(){
  122. },
  123. // 重命名
  124. openRename(type,reType = 'sort'){
  125. let isOpen = false
  126. let activeInfo = Editor.Selection.curGlobalActivate() // 检测面板焦点在资源管理器还是层级管理器
  127. if (!activeInfo) return;
  128. type = type || activeInfo.type;
  129. let list = []
  130. let name = ""
  131. if (type == "asset")
  132. {
  133. // 获得选中的资源
  134. let asset_list = Editor.Selection.curSelection("asset");
  135. asset_list.forEach((uuid)=>
  136. {
  137. let info = Editor.remote.assetdb.assetInfoByUuid(uuid);
  138. if (!info) return;
  139. let file = this.getFileName(info.url);
  140. info.suffix = file.suffix;
  141. info.name = file.name
  142. info.dir_path = file.dir_path
  143. // 加载资源列表
  144. list.push( this.parent.getItem(file.name,file.name,0,info) );
  145. })
  146. this.showRenameBox(type,reType,list)
  147. isOpen = list.length > 0
  148. }
  149. else if(type == "node")
  150. {
  151. // 获得选中的节点
  152. Editor.Scene.callSceneScript('simple-code', 'get-select-node-info' ,"", (err, args)=>
  153. {
  154. // 加载节点列表
  155. args.forEach((info)=>{
  156. list.push( this.parent.getItem(info.name,info.name,0,info) );
  157. })
  158. this.showRenameBox(type,reType,list)
  159. });
  160. isOpen = Editor.Selection.curSelection("node").length> 0
  161. }
  162. return isOpen;
  163. },
  164. /************* 事件 *************/
  165. messages:{
  166. 'applyRename'(e,args){
  167. let types = {'重命名':'com','加前缀':'prefix','加后缀':'suffix','数字序列':'sort','正则匹配删除命名':'remove_name',}
  168. let type = args.paths[0] == '节点批量重命名' ? 'node' : 'asset'
  169. let reType = types[args.paths[1]]
  170. this.openRename(type,reType)
  171. },
  172. 'selection:activated'(e,type){
  173. let asset_list = Editor.Selection.curSelection(type);
  174. if(asset_list && asset_list.length >1)
  175. {
  176. let submenu = [
  177. { label: '重命名', enabled: true, cmd:'applyRename'},
  178. { label: '数字序列', enabled: true, cmd:'applyRename'},
  179. { label: '加前缀', enabled: true, cmd:'applyRename'},
  180. { label: '加后缀', enabled: true, cmd:'applyRename'},
  181. { label: '正则匹配删除命名', enabled: true, cmd:'applyRename'},
  182. ];
  183. let menuCfg = {
  184. layerMenu : [
  185. { label : "节点批量重命名", enabled:true, enabled:true, submenu:submenu,},
  186. ],
  187. assetMenu : [
  188. { label : "资源批量重命名", enabled:true, enabled:true, submenu:submenu,},
  189. ],
  190. }
  191. Editor.Ipc.sendToMain('simple-code:setMenuConfig',{id:"rename-ex",menuCfg:menuCfg});
  192. }else{
  193. Editor.Ipc.sendToMain('simple-code:setMenuConfig',{id:"rename-ex",menuCfg:undefined});
  194. }
  195. },
  196. // 下拉框批量重命名
  197. 'rename'(event,info)
  198. {
  199. this.openRename()
  200. },
  201. },
  202. };