main_ex.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. *主线程扩展
  3. *creator菜单事件管理
  4. */
  5. 'use strict';
  6. var path = require('path');
  7. var fs = require('fs');
  8. var md5 = require('md5');
  9. const MENU_PANEL_TYPE = {"创建节点":"layerMenu","Create":"layerMenu","新建":"assetMenu","New":"assetMenu"};
  10. module.exports = {
  11. // 初始化
  12. onLoad(parent)
  13. {
  14. // 主线程对象: main.js
  15. this.parent = parent;
  16. this.menuCfgs = {}
  17. // hook 菜单
  18. if (!Editor.Menu["__simple-code-hooked__"]) {
  19. this.old_menu = Editor.Menu;
  20. Editor.Menu = this.hookMenu(Editor.Menu, this.hookMenuFunc.bind(this));
  21. Editor.Menu["__simple-code-hooked__"] = true;
  22. }
  23. },
  24. hookMenu(orginMenu, hookFunc) {
  25. const menu = function () {
  26. hookFunc(...arguments);
  27. return new orginMenu(...arguments);
  28. };
  29. let menuProps = Object.getOwnPropertyNames(orginMenu);
  30. for (let prop of menuProps) {
  31. const object = Object.getOwnPropertyDescriptor(orginMenu, prop);
  32. if (object.writable) {
  33. menu[prop] = orginMenu[prop];
  34. }
  35. }
  36. menu.prototype = orginMenu.prototype;
  37. return menu;
  38. },
  39. applyItem(item,parnetPaths,args){
  40. if(item.submenu)
  41. {
  42. for (let n = 0; n < item.submenu.length; n++)
  43. {
  44. let sub_item = item.submenu[n];
  45. let paths = JSON.parse( JSON.stringify(parnetPaths) )
  46. paths.push(sub_item.label)
  47. this.applyItem(sub_item,paths)
  48. }
  49. }else {
  50. if(item.message == null){
  51. let paths = JSON.parse( JSON.stringify(parnetPaths) )
  52. let toArgs = item.params || {label:item.label,paths,args:args||{}}
  53. item.click = ()=>{
  54. Editor.Ipc.sendToPanel('simple-code', item.cmd,toArgs);
  55. };
  56. }else{
  57. let paths = JSON.parse( JSON.stringify(parnetPaths) )
  58. item.params = item.params || {label:item.label,paths,args:args||{}}
  59. }
  60. }
  61. },
  62. hookMenuFunc(template)
  63. {
  64. for (let i = 0; i < template.length; i++) {
  65. const item = template[i];
  66. if(item.label == '点击来为属性赋值'){
  67. template.splice(i,1);
  68. break
  69. }
  70. }
  71. const firstMenu = template[0];
  72. let menuType = MENU_PANEL_TYPE[firstMenu.label];
  73. for (const id in this.menuCfgs)
  74. {
  75. let menuCfg = this.menuCfgs[id];
  76. if(!menuCfg) continue;
  77. let list = menuCfg[menuType];
  78. if(!list) continue;
  79. for (let i = 0; i < list.length; i++)
  80. {
  81. const item = list[i];
  82. if(item.type != 'separator'){
  83. this.applyItem(item,[item.label],firstMenu.params);
  84. }
  85. template.push(item);
  86. }
  87. }
  88. },
  89. // 窗口销毁
  90. onDestroy()
  91. {
  92. if(this.old_menu){
  93. Editor.Menu = this.old_menu;
  94. delete this.old_menu;
  95. }
  96. },
  97. /************* 事件 *************/
  98. messages:
  99. {
  100. // 设置右击菜单选项
  101. 'setMenuConfig'(e,args){
  102. this.menuCfgs[args.id] = args.menuCfg;
  103. },
  104. // 清除所有自定义的菜单
  105. 'cleanMenuConfigAll'(){
  106. this.menuCfgs = {};
  107. },
  108. }
  109. };