preview.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. const app = new Vue({
  2. el: '#app',
  3. vuetify: new Vuetify({
  4. theme: { dark: true }
  5. }),
  6. data: {
  7. isShowTop: true,
  8. drawer: false,
  9. cacheDialog: false,
  10. cacheTitle: '',
  11. cacheHeaders: [
  12. { text: 'Type', value: 'type' },
  13. { text: 'Name', value: 'name' },
  14. { text: 'Preivew', value: 'preview' },
  15. { text: 'ID', value: 'id' },
  16. { text: 'Size', value: 'size' },
  17. ],
  18. cacheRawData: [],
  19. cacheData: [],
  20. cacheSearchText: null,
  21. cacheOnlyTexture: true,
  22. treeData: [],
  23. selectedNodes: [],
  24. intervalId: -1,
  25. treeSearchText: null,
  26. nodeSchema: {},
  27. componentsSchema: [],
  28. },
  29. created() {
  30. if (window.innerHeight === window.outerHeight) { // 手机端,chrome device模式
  31. this.isShowTop = false;
  32. }
  33. this.waitCCInit().then(() => {
  34. if (this.isShowTop) {
  35. this.startUpdateTree();
  36. }
  37. initConsoleUtil();
  38. });
  39. },
  40. watch: {
  41. cacheOnlyTexture() {
  42. this.updateCacheData();
  43. }
  44. },
  45. computed: {
  46. treeFilter() {
  47. return (item, search, textKey) => item[textKey].indexOf(search) > -1;
  48. },
  49. selectedNode() {
  50. if (!this.selectedNodes.length) return undefined
  51. let node = getNodeById(this.selectedNodes[0]);
  52. if (node) {
  53. if (!node.hex_color) {
  54. cc.js.getset(node, 'hex_color', () => {
  55. return '#' + node.color.toHEX('#rrggbb');
  56. }, (hex) => {
  57. node.color = new cc.Color().fromHEX(hex);
  58. }, false, true);
  59. }
  60. let superPreLoad = node._onPreDestroy;
  61. node._onPreDestroy = () => {
  62. superPreLoad.apply(node);
  63. if (this.selectedNodes.length > 0 && this.selectedNodes[0] === node._id) {
  64. this.selectedNodes.pop();
  65. }
  66. }
  67. this.nodeSchema = NEX_CONFIG.nodeSchema.node2d;
  68. let componentsSchema = [];
  69. for (let component of node._components) {
  70. let schema = NEX_CONFIG.componentsSchema[component.__classname__];
  71. if (schema) {
  72. node[schema.key] = node.getComponent(schema.key);
  73. for (let i = 0; i < schema.rows.length; i++) {
  74. if (schema.rows[i].type === 'color') {
  75. if (!node[schema.key][schema.rows[i].key]) {
  76. cc.js.getset(node[schema.key], schema.rows[i].key, () => {
  77. return '#' + node.getComponent(schema.key)[schema.rows[i].rawKey].toHEX('#rrggbb');
  78. }, (hex) => {
  79. node.getComponent(schema.key)[schema.rows[i].rawKey] = new cc.Color().fromHEX(hex);
  80. }, false, true);
  81. }
  82. }
  83. }
  84. } else {
  85. schema = {
  86. title: component.__classname__,
  87. key: component.__classname__
  88. };
  89. node[schema.key] = node.getComponent(schema.key);
  90. }
  91. componentsSchema.push(schema);
  92. }
  93. this.componentsSchema = componentsSchema;
  94. }
  95. return node;
  96. },
  97. },
  98. methods: {
  99. waitCCInit() {
  100. return new Promise((resolve, reject) => {
  101. let id = setInterval(() => {
  102. if (window.cc) {
  103. resolve();
  104. clearInterval(id);
  105. }
  106. }, 500);
  107. });
  108. },
  109. refreshTree: function () {
  110. if (!this.$data.drawer || !window.cc || !cc.director.getScene() || !cc.director.getScene().children) return;
  111. this.$data.treeData = getChildren(cc.director.getScene());
  112. },
  113. startUpdateTree: function () {
  114. this.$data.intervalId = setInterval(() => {
  115. this.refreshTree();
  116. }, 200);
  117. },
  118. stopUpdateTree: function () {
  119. clearInterval(this.$data.intervalId);
  120. },
  121. outputNodeHandler(id) {
  122. let i = 1;
  123. while (window['temp' + i] !== undefined) {
  124. i++;
  125. }
  126. window['temp' + i] = this.selectedNode;
  127. console.log('temp' + i);
  128. console.log(window['temp' + i]);
  129. },
  130. outputComponentHandler(component) {
  131. let i = 1;
  132. while (window['temp' + i] !== undefined) {
  133. i++;
  134. }
  135. window['temp' + i] = this.selectedNode.getComponent(component);
  136. console.log('temp' + i);
  137. console.log(window['temp' + i]);
  138. },
  139. drawNodeRect() {
  140. cc.where(this.selectedNode);
  141. },
  142. updateCacheData() {
  143. if (this.$data.cacheOnlyTexture) {
  144. this.$data.cacheData = this.$data.cacheRawData.filter(item => item.type === 'cc.Texture2D');
  145. } else {
  146. this.$data.cacheData = this.$data.cacheRawData;
  147. }
  148. },
  149. openCacheDialog() {
  150. [this.$data.cacheRawData, this.$data.cacheTitle] = cc.cache();
  151. this.updateCacheData();
  152. this.$data.cacheDialog = true;
  153. },
  154. openGithub() {
  155. window.open('https://github.com/potato47/ccc-devtools');
  156. },
  157. openCocosForum() {
  158. window.open('https://forum.cocos.com/');
  159. },
  160. openCocosDocs() {
  161. window.open('https://docs.cocos.com/');
  162. }
  163. }
  164. });
  165. function getChildren(node) {
  166. return node.children.map(child => {
  167. let children = (child.children && child.children.length > 0) ? getChildren(child) : [];
  168. return { id: child._id, name: child.name, active: child.activeInHierarchy, children };
  169. });
  170. }
  171. function getNodeById(id) {
  172. let target;
  173. const search = function (node) {
  174. if (node._id === id) {
  175. target = node;
  176. return;
  177. }
  178. if (node.childrenCount) {
  179. for (let i = 0; i < node.childrenCount; i++) {
  180. if (!target) {
  181. search(node.children[i]);
  182. }
  183. }
  184. }
  185. }
  186. const scene = cc.director.getScene();
  187. search(scene);
  188. return target;
  189. }