scene_ex.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. *场景逻辑扩展
  3. *删除选中的节点以及节点所绑定的脚本
  4. */
  5. 'use strict';
  6. var path = require('path');
  7. var fs = require('fs');
  8. var md5 = require('md5');
  9. const tools = require('../../tools/tools');
  10. const config = require('../../config');
  11. const fe = Editor.require('packages://simple-code/tools/tools.js');
  12. let USER_NEW_VAR_RULE = path.join(config.cacheDir,"drag_var_rule.js");
  13. // 资源对象读取方法
  14. let LoadAssetObj =
  15. {
  16. load(uuid,callback){
  17. if(cc.AssetLibrary && cc.AssetLibrary.loadAsset)
  18. {
  19. cc.AssetLibrary.loadAsset(uuid,callback);
  20. }else{
  21. cc.loader.load({ type: 'uuid', uuid: uuid},()=>{}, callback);
  22. }
  23. },
  24. // 未知类型
  25. 'tryLoad'(uuid,callback)
  26. {
  27. LoadAssetObj.load(uuid,(err, obj) => {
  28. callback(obj);
  29. })
  30. },
  31. 'cc.SpriteFrame'(uuid,callback)
  32. {
  33. let meta = Editor.remote.assetdb.loadMetaByUuid(uuid)
  34. if(meta.type == 'sprite' && meta.__subMetas__)
  35. {
  36. let spriteFramInfo = meta.__subMetas__[Object.getOwnPropertyNames( meta.__subMetas__)[0]];
  37. if(spriteFramInfo.uuid){
  38. LoadAssetObj.load(spriteFramInfo.uuid,(err, obj) => {
  39. callback(obj);
  40. })
  41. }else{
  42. callback();
  43. }
  44. }else{
  45. LoadAssetObj.load(uuid,(err, obj) => {
  46. callback(obj);
  47. })
  48. }
  49. },
  50. }
  51. let getSelectedComps = (args,callback) =>
  52. {
  53. let rets = []
  54. // 禁止生成拖拽变量
  55. if(args.rule && args.rule.disableGenerated){
  56. callback(rets);
  57. return ;
  58. }
  59. let slsAssets = args.insertUuids && args.insertUuids.length ? args.insertUuids : Editor.Selection.curSelection(args.isAssets ? 'asset' : 'node');
  60. if (args.isAssets)
  61. {
  62. for (let i = 0; i < slsAssets.length; i++)
  63. {
  64. let loadFunc = LoadAssetObj[args.widgetType] || LoadAssetObj['tryLoad'];
  65. loadFunc(slsAssets[i],(obj)=>
  66. {
  67. if(obj) rets.push(obj);
  68. if(i+1 == slsAssets.length) callback(rets);
  69. });
  70. }
  71. }else
  72. {
  73. for (let i = 0; i < slsAssets.length; i++)
  74. {
  75. let node = cc.engine.getInstanceById(slsAssets[i]);
  76. if (args.widgetType == 'cc.Node') {
  77. rets.push(node)
  78. } else {
  79. let comp = node.getComponent(args.widgetType)
  80. if (comp) rets.push(comp)
  81. }
  82. }
  83. callback(rets);
  84. }
  85. }
  86. let getCurrEditorFileBindNodes = (fileUuid,parent)=>{
  87. var canvas = cc.director.getScene();
  88. var bindNodeList = [];
  89. if (canvas && fileUuid) {
  90. parent.getNodeChildren(canvas, (node) => {
  91. // 检测该node是否绑定了该脚本
  92. let code_comp_list = parent.getJsFileList(node);
  93. code_comp_list.forEach((code_comp, i) => {
  94. if (code_comp.__scriptUuid == fileUuid) {
  95. bindNodeList.push({ node_uuid: node.uuid, comp_name: code_comp.__classname__ });
  96. }
  97. });
  98. })
  99. }
  100. return bindNodeList;
  101. }
  102. module.exports = {
  103. /************* 事件 *************/
  104. messages:
  105. {
  106. 'getNodesInfo'(event, uuids, parent)
  107. {
  108. let nodeInfos = []
  109. for (let i = 0; i < uuids.length; i++)
  110. {
  111. const uuid = uuids[i];
  112. let node = cc.engine.getInstanceById(uuid || '');
  113. if(node){
  114. let compNames = []
  115. node._components.forEach((code_comp, i) => {
  116. if (code_comp.__classname__ ) {
  117. compNames.push(code_comp.__classname__)
  118. }
  119. });
  120. nodeInfos.push({
  121. name: node.name,
  122. uuid: uuid,
  123. compNames:compNames,
  124. })
  125. }
  126. }
  127. event.reply(null,nodeInfos);
  128. },
  129. 'getNodeCompNames'(event, uuid, parent)
  130. {
  131. let node = cc.engine.getInstanceById(uuid || '');
  132. if(node)
  133. {
  134. // 检测该node是否绑定了该脚本
  135. let compNames = []
  136. node._components.forEach((code_comp, i) => {
  137. if (code_comp.__classname__ ) {
  138. compNames.push(code_comp.__classname__)
  139. }
  140. });
  141. event.reply(null,compNames);
  142. }else{
  143. event.reply(null,[]);
  144. }
  145. },
  146. 'getCurrEditorFileBindNodes'(event, args, parent) {
  147. let bindNodeList = getCurrEditorFileBindNodes(args.code_uuid, parent);
  148. event.reply(null, bindNodeList);
  149. },
  150. // 获得自定义加载组件列表
  151. 'getCustomWidgetRule'(event,args,parent){
  152. /**
  153. * 1. 获得当前脚本所绑定的Node
  154. * 2. 解析 node.name 生成规则
  155. */
  156. let bindNodeList = getCurrEditorFileBindNodes(args.fileUuid, parent);
  157. if(!bindNodeList.length) {
  158. event.reply(null,{rules:[],bindNodeList});
  159. return;
  160. }
  161. let rules = []
  162. try {
  163. let rootNode = args.rootNodeUuid != null && cc.engine.getInstanceById(args.rootNodeUuid) || cc.director.getScene()
  164. rules = require(USER_NEW_VAR_RULE).getCustomWidgetRule(args.url,bindNodeList,rootNode);
  165. }catch (error) {
  166. Editor.error('生成自定义绑定规则配置出错(getCustomWidgetRule): ',error)
  167. }
  168. event.reply(null,{rules,bindNodeList});
  169. },
  170. // 自定义保存的代码文本
  171. 'saveWidgetCodeFile'(event, args, parent){
  172. try {
  173. let nodes = []
  174. for (let i = 0; i < args.rules.length; i++) {
  175. const rule = args.rules[i];
  176. if(rule.nodeUuid){
  177. let node = cc.engine.getInstanceById(rule.nodeUuid);
  178. nodes.push(node);
  179. }
  180. }
  181. let newCodeText = require(USER_NEW_VAR_RULE).processCode(args.codeText, args.dbUrl, args.rules, null,nodes)
  182. event.reply(null,newCodeText);
  183. } catch (error) {
  184. Editor.error('自定义绑定规则配置出错(saveWidgetCodeFile): ',error)
  185. }
  186. },
  187. // 配置拖拽规则
  188. 'loadWidgetRules'(event, args, parent){
  189. try {
  190. let bindNodeList = getCurrEditorFileBindNodes(args.scriptUuid, parent);
  191. if(require(USER_NEW_VAR_RULE).dragWidgetStart){
  192. args = require(USER_NEW_VAR_RULE).dragWidgetStart(args.rules, args.isArray,args.isQuick)
  193. }
  194. args.bindNodeList = bindNodeList;
  195. event.reply(null,args);
  196. } catch (error) {
  197. Editor.error('自定义绑定规则配置出错(loadWidgetRules): ',error)
  198. }
  199. },
  200. 'insertWidgetInfo'(event, args, parent) {
  201. //1.获取绑定当前脚本的Node
  202. //2.检测该属性是否存在 getComponent('')
  203. //3.获取选取的组件信息
  204. //4.将选取的组件插入到脚本中
  205. let node = cc.engine.getInstanceById(args.bindNodeList[0].node_uuid)
  206. let old_comp_uuid = node && node.getComponent(args.bindNodeList[0].comp_name) && node.getComponent(args.bindNodeList[0].comp_name).uuid;
  207. // 定时检测creator加载新建文件缓存没
  208. let stop_func;
  209. let chk_count = 0;
  210. stop_func = parent.setTimeoutToJS(() =>
  211. {
  212. //等场景加载完脚本
  213. let scriptNode = cc.engine.getInstanceById(args.bindNodeList[0].node_uuid)
  214. if (scriptNode && !scriptNode._objFlags)
  215. {
  216. let scriptComp = scriptNode.getComponent(args.bindNodeList[0].comp_name)
  217. if(!scriptComp) return;
  218. let isUpScene = scriptComp.uuid != old_comp_uuid;
  219. // *:组件uuid改变了说明场景已经刷新了一遍, scriptComp.uuid != old_comp_uuid
  220. // 创建脚本瞬间添加的node组件会丢失,所以需要检测1次组件确定加载了
  221. chk_count++;// 兼容2.4与1.9版本
  222. if (isUpScene || chk_count == 1)
  223. {
  224. if(isUpScene){
  225. stop_func();
  226. }
  227. // sls_comps 获得选择的组件或资源
  228. getSelectedComps(args,(sls_comps)=>
  229. {
  230. let ruleCode = require(USER_NEW_VAR_RULE)
  231. for (let i = 0; i < args.bindNodeList.length; i++) {
  232. const info = args.bindNodeList[i];
  233. let scriptNode = cc.engine.getInstanceById(info.node_uuid)
  234. if (!scriptNode) {
  235. continue
  236. }
  237. // 获得当前打开的脚本对象
  238. let scriptComp = scriptNode.getComponent(info.comp_name);
  239. if (!scriptComp) {
  240. chk_count = 0;
  241. continue;
  242. }
  243. // 给脚本的成员变量赋值
  244. if (scriptComp.hasOwnProperty(args.symbolName) && sls_comps && sls_comps[0] != null) {
  245. scriptComp[args.symbolName] = args.isArray ? sls_comps : sls_comps[0];
  246. }
  247. if(ruleCode.setComponentVar){
  248. ruleCode.setComponentVar(scriptComp,args.widgetType,args.symbolName,args.isArray,args.insertUuids,args.isAssets,args.rule);
  249. }
  250. }
  251. event.reply(null,true);
  252. });
  253. }
  254. }
  255. }, 1, { count: 15 })
  256. },
  257. }
  258. };