citrus.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. 'use strict';
  2. var CopyStr = function (str) {
  3. var input = str;
  4. const el = document.createElement('textarea');
  5. el.value = input;
  6. el.setAttribute('readonly', '');
  7. el.style.contain = 'strict';
  8. el.style.position = 'absolute';
  9. el.style.left = '-9999px';
  10. el.style.fontSize = '12pt'; // Prevent zooming on iOS
  11. const selection = getSelection();
  12. var originalRange = false;
  13. if (selection.rangeCount > 0) {
  14. originalRange = selection.getRangeAt(0);
  15. }
  16. document.body.appendChild(el);
  17. el.select();
  18. el.selectionStart = 0;
  19. el.selectionEnd = input.length;
  20. var success = false;
  21. try {
  22. success = document.execCommand('copy');
  23. } catch (err) { }
  24. document.body.removeChild(el);
  25. if (originalRange) {
  26. selection.removeAllRanges();
  27. selection.addRange(originalRange);
  28. }
  29. return success;
  30. }
  31. //第三个参数是否需要let
  32. var FindNodeParentName = function (CurrNode, ParentNode, HaveLet) {
  33. let temp_node = CurrNode
  34. let NameList = ""
  35. while (1) {
  36. if (temp_node.parent == ParentNode) {
  37. NameList += temp_node.name
  38. // NameList += temp_node.parent.name
  39. let c = NameList.split("-").reverse()
  40. let script_str
  41. if (HaveLet) {
  42. script_str = `let ${CurrNode.name} =this.node`
  43. for (let index = 0; index < c.length; index++) {
  44. const element = c[index];
  45. script_str += `.getChildByName("${element}")`
  46. }
  47. } else {
  48. script_str = []
  49. script_str.push(ParentNode.name)
  50. for (let index = 0; index < c.length; index++) {
  51. const element = c[index];
  52. script_str.push(element)
  53. }
  54. }
  55. return script_str
  56. } else {
  57. NameList += temp_node.name + "-"
  58. temp_node = temp_node.parent
  59. }
  60. }
  61. }
  62. //返回所有后代所有带精灵帧
  63. var SumNode = []//sprite[]
  64. var Return_ALL_Sp_By_Node = function (Curr_Node) {
  65. SumNode = []
  66. Return_ALL_Child(Curr_Node)
  67. return SumNode
  68. }
  69. //返回所有后代节点
  70. var Return_ALL_Child = function (m_node) {
  71. if (m_node.getComponent(cc.Sprite)) {
  72. SumNode.push(m_node)
  73. }
  74. m_node.children.forEach(e => {
  75. if (e.childrenCount > 0) {
  76. Return_ALL_Child(e)
  77. } else {
  78. if (e.getComponent(cc.Sprite)) {
  79. SumNode.push(e)
  80. }
  81. }
  82. })
  83. }
  84. //判断是否是默认图片
  85. var Check_Is_Default_Image = function (data) {
  86. let d = "atom@atom@default_panel@default_sprite@default-particle@default_scrollbar@default_btn_normal@default_editbox_bg@default_btn_pressed@default_progressbar@default_btn_disabled@default_scrollbar_bg@default_sprite_splash@default_toggle_normal@default_progressbar_bg@default_toggle_pressed@default_radio_button_on@default_toggle_disabled@default_radio_button_off@default_toggle_checkmark@default_scrollbar_vertical@default_scrollbar_vertical_bg"
  87. if (d.split("@").indexOf(data) > -1) {
  88. return true
  89. } else {
  90. return false
  91. }
  92. }
  93. //在资源文件夹得到资源文件夹里面的UUid的路径
  94. var In_Assent_By_UUID_Return_DbPath = function (uuid) { return Editor.remote.assetdb.uuidToUrl(uuid) }
  95. //在资源文件夹根据uuid获得名字
  96. var In_Assent_By_UUID_Return_Name = function (uuid) {
  97. let d = Editor.assetdb.assetInfoByUuid(uuid).path.split("/")[c.length - 1]
  98. let c = d
  99. return c
  100. }
  101. //在场景中得到节点上的Sp的UUid
  102. var In_Scene_By_Node_SpriteFrame_Return_UUID = function (m_node) { return m_node.getComponent(cc.Sprite).spriteFrame._uuid }
  103. var Get_One_Prefab_Json = function (Curr_Node) {
  104. var Prefabjson = []
  105. Return_ALL_Sp_By_Node(Curr_Node).forEach(e => {
  106. //如果图片帧存在 并且不是默认图片
  107. if (e.getComponent(cc.Sprite).spriteFrame && !Check_Is_Default_Image(e.getComponent(cc.Sprite).spriteFrame.name)) {
  108. let subJson = {}
  109. let nameList
  110. if (e.uuid == Curr_Node.uuid) {
  111. nameList = Curr_Node.name
  112. } else {
  113. nameList = FindNodeParentName(e, Curr_Node, false)
  114. }
  115. subJson["FindMe"] = nameList
  116. let split_str_arry = In_Assent_By_UUID_Return_DbPath(In_Scene_By_Node_SpriteFrame_Return_UUID(e)).slice(5).split("/")
  117. let temp_ppp = ""
  118. for (let index = 0; index < split_str_arry.length - 1; index++) {
  119. const element = split_str_arry[index];
  120. if (index == split_str_arry.length - 2) {
  121. temp_ppp += element
  122. break
  123. }
  124. temp_ppp += element + "/"
  125. }
  126. subJson["ImagePath"] = temp_ppp
  127. // Alljson[Curr_Node.name + "-" + e.name] = subJson
  128. Prefabjson.push(subJson)
  129. }
  130. })
  131. return Prefabjson
  132. }
  133. var Get_One_Prefab_image = function (Curr_Node) {
  134. let Prefabimage = []
  135. Return_ALL_Sp_By_Node(Curr_Node).forEach(e => {
  136. //如果图片帧存在 并且不是默认图片
  137. if (e.getComponent(cc.Sprite).spriteFrame && !Check_Is_Default_Image(e.getComponent(cc.Sprite).spriteFrame.name)) {
  138. Prefabimage.push(e)
  139. }
  140. })
  141. return Prefabimage
  142. }
  143. var WriteFile = function (data) {
  144. Editor.assetdb.createOrSave('db://assets/citrus.json', data);
  145. }
  146. //图片字典
  147. var iamgeData = new Map();
  148. //加载几个图片
  149. var imageCount
  150. let CitrusFuncs =
  151. {
  152. //设置显示与否
  153. 'citrus-active'(event) {
  154. let nodes = Editor.Selection.curSelection('node');
  155. if (nodes && nodes.length != 0) {
  156. let active = cc.engine.getInstanceById(nodes[0]).active;
  157. for (let i = 0; i < nodes.length; i++) {
  158. const id = nodes[i];
  159. let node = cc.engine.getInstanceById(id)
  160. if (node) {
  161. node.active = !active;
  162. }
  163. }
  164. }
  165. },
  166. 'citrus-getThree'(event) {
  167. let CurrNodeThree = Editor.Selection.curSelection('node');
  168. if (CurrNodeThree.length > 1) {
  169. let Curr_Node_Parent = cc.engine.getInstanceById(CurrNodeThree[0])
  170. let citrus_script_str = ""
  171. for (let index = 1; index < CurrNodeThree.length; index++) {
  172. const element = cc.engine.getInstanceById(CurrNodeThree[index])
  173. citrus_script_str += FindNodeParentName(element, Curr_Node_Parent, true) + "\n"
  174. }
  175. CopyStr(citrus_script_str)
  176. Editor.info('Citrus_复制节点树成功!');
  177. return
  178. } else {
  179. Editor.info('Citrus_复制节点树失败!')
  180. return
  181. }
  182. },
  183. //不能有/ - @ 字符
  184. 'citrus-getImageThree'(event) {
  185. // 检测面板焦点在资源管理器还是层级管理器
  186. let activeInfo = Editor.Selection.curGlobalActivate()
  187. let CurrNodeThree
  188. if (activeInfo && activeInfo.type == "node") {
  189. CurrNodeThree = Editor.Selection.curSelection('node');
  190. if (CurrNodeThree.length > 0) {
  191. var AllJson = {}
  192. for (let index = 0; index < CurrNodeThree.length; index++) {
  193. const element = cc.engine.getInstanceById(CurrNodeThree[index]);
  194. AllJson[element.name] = Get_One_Prefab_Json(element)
  195. }
  196. WriteFile(JSON.stringify(AllJson))
  197. Editor.info('Citrus_json树成功!')
  198. }
  199. }
  200. else if (activeInfo && activeInfo.type == "asset") {
  201. CurrNodeThree = Editor.Selection.curSelection('asset');
  202. }
  203. },
  204. 'citrus-LoadingImagee'(event) {
  205. cc.log('Citrus_你等我会儿!')
  206. //先加载图片
  207. Editor.assetdb.queryAssets('db://assets/CitrusImage/**\/*', 'sprite-frame', function (err, results) {
  208. imageCount = results.length
  209. results.forEach(function (result, iindex) {
  210. let temp = (result.url).split('/')
  211. let imageName = temp[temp.length - 1]
  212. cc.loader.load({ uuid: result.uuid }, (err, image) => {
  213. iamgeData.set(imageName, image);
  214. imageCount -= 1
  215. if (imageCount == 0) {
  216. Editor.info('Citrus_资源图片个数' + results.length)
  217. Editor.info('Citrus_资源加载完毕!')
  218. }
  219. });
  220. });
  221. });
  222. },
  223. //替换图片
  224. 'citrus-ReplaceImagee'(event) {
  225. // 检测面板焦点在资源管理器还是层级管理器
  226. let activeInfo = Editor.Selection.curGlobalActivate()
  227. let CurrNodeThree
  228. if (activeInfo && activeInfo.type == "node") {
  229. CurrNodeThree = Editor.Selection.curSelection('node');
  230. if (CurrNodeThree.length > 0) {
  231. //赋值
  232. for (let index = 0; index < CurrNodeThree.length; index++) {
  233. const element = cc.engine.getInstanceById(CurrNodeThree[index]);
  234. let vvvv = Get_One_Prefab_image(element)
  235. for (let iii = 0; iii < vvvv.length; iii++) {
  236. let e = vvvv[iii];
  237. e.getComponent(cc.Sprite).spriteFrame = iamgeData.get(e.getComponent(cc.Sprite).spriteFrame.name)
  238. }
  239. }
  240. Editor.info('Citrus_替换成功!')
  241. }
  242. }
  243. },
  244. //得到nodejson属性
  245. 'citrus-Nodejson'(event) {
  246. // 检测面板焦点在资源管理器还是层级管理器
  247. let activeInfo = Editor.Selection.curGlobalActivate()
  248. let CurrNodeThree
  249. if (activeInfo && activeInfo.type == "node") {
  250. CurrNodeThree = Editor.Selection.curSelection('node');
  251. if (CurrNodeThree.length > 0) {
  252. let setNode = cc.engine.getInstanceById(CurrNodeThree[0]);
  253. let parentJson = {}
  254. for (let index = 1; index < CurrNodeThree.length; index++) {
  255. const element = cc.engine.getInstanceById(CurrNodeThree[index]);
  256. let subJson = {}
  257. if (setNode.position.x != 0) { subJson["PosX"] = element.position.x }
  258. if (setNode.position.y != 0) { subJson["PosY"] = element.position.y }
  259. if (setNode.rotation != 0) { subJson["rotation"] = element.rotation }
  260. if (setNode.scaleX != 0) { subJson["scaleX"] = element.scaleX }
  261. if (setNode.scaleY != 0) { subJson["scaleY"] = element.scaleY }
  262. if (setNode.anchorX != 0) { subJson["anchorX"] = element.anchorX }
  263. if (setNode.anchorY != 0) { subJson["anchorY"] = element.anchorY }
  264. if (setNode.getContentSize().width != 0) { subJson["width"] = element.getContentSize().width }
  265. if (setNode.getContentSize().height != 0) { subJson["height"] = element.getContentSize().height }
  266. if (setNode.opacity != 0) { subJson["opacity"] = element.opacity }
  267. if (setNode.skewX != 0) { subJson["skewX"] = element.skewX }
  268. if (setNode.skewY != 0) { subJson["skewY"] = element.skewY }
  269. parentJson[element.name] = subJson
  270. }
  271. CopyStr(JSON.stringify(parentJson))
  272. Editor.info('Citrus_NodeJson-剪切板成功!')
  273. }
  274. }
  275. },
  276. };
  277. module.exports = CitrusFuncs;