index.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. const packageJSON = require('../package.json')
  2. const fs = require('fs')
  3. const path = require('path')
  4. const KeyCode = require('./keyCode').KeyCode
  5. const axios = require('axios')
  6. const childProcess = require('child_process')
  7. const vsprintf = require('sprintf-js').vsprintf
  8. const {remote} = require('electron')
  9. const translate = (key) => Editor.T(`${packageJSON.name}.${key}`)
  10. // panel/index.js, this filename needs to match the one registered in package.json
  11. Editor.Panel.extend({
  12. // css style for panel
  13. style: fs.readFileSync(Editor.url('packages://user-guide/static/style/default/index.css', 'utf8')),
  14. // html template for panel
  15. template: fs.readFileSync(Editor.url('packages://user-guide/static/template/default/index.html', 'utf8')),
  16. // element and variable binding
  17. $: {
  18. app: '#app',
  19. },
  20. // method executed when template and styles are successfully loaded and initialized
  21. ready() {
  22. new window.Vue({
  23. el: this.shadowRoot,
  24. data: {
  25. step1I18n: translate('step1'),
  26. titleI18n: translate('title'),
  27. guideI18n: translate('guide'),
  28. moveUpI18n: translate('moveUp'),
  29. moveDownI18n: translate('moveDown'),
  30. deleteI18n: translate('delete'),
  31. touchI18n: translate('touch'),
  32. keyI18n: translate('key'),
  33. useLastGuideI18n: translate('useLastGuide'),
  34. addI18n: translate('add'),
  35. confirmI18n: translate('confirm'),
  36. step2I18n: translate('step2'),
  37. guideSignI18n: translate('guideSign'),
  38. textBgI18n: translate('textBg'),
  39. goBackI18n: translate('goBack'),
  40. editImg: path.join(__dirname, "../static/images/edit.png"),
  41. selectImg: path.join(__dirname, "../static/images/select.png"),
  42. helpImg: path.join(__dirname, "../static/images/help.png"),
  43. prevplayImg: path.join(__dirname, "../static/images/prevplay.png"),
  44. addImg: path.join(__dirname, "../static/images/add.png"),
  45. confirmImg: path.join(__dirname, "../static/images/confirm.png"),
  46. backImg: path.join(__dirname, "../static/images/back.png"),
  47. closeImg: path.join(__dirname, "../static/images/close.png"),
  48. currentStep: 1,
  49. guideTextType: 'zh',
  50. guideArray: [
  51. {
  52. "nodeUUID": "",
  53. "path": "",
  54. "key": "",
  55. "guideText": "",
  56. "guideEnText": "",
  57. "guideType": "touch",
  58. "isDynamic": false,
  59. "isAliased": false,
  60. "alias": "",
  61. "uiType": "",
  62. "arrowPos": [0, 0],
  63. "arrowRotation": 0,
  64. "textBgPos": [0, 0],
  65. "arrowType": 0,
  66. "showTextBg": false,
  67. "showRole": false,
  68. "prevent": true,
  69. }
  70. ],
  71. guideSignPicArray: [
  72. {
  73. "name": `${translate("guideSign")} 1`,
  74. "type": "frame",
  75. "path": path.join(__dirname, "../static/images/frame1.png"),
  76. "value": true
  77. },
  78. {
  79. "name": `${translate("guideSign")} 2`,
  80. "type": "frame",
  81. "path": path.join(__dirname, "../static/images/frame2.png"),
  82. "value": false
  83. },
  84. {
  85. "name": `${translate("guideSign")} 3`,
  86. "type": "frame",
  87. "path": path.join(__dirname, "../static/images/frame3.png"),
  88. "value": false
  89. },
  90. {
  91. "name": `${translate("guideSign")} 4`,
  92. "type": "frame",
  93. "path": path.join(__dirname, "../static/images/frame4.png"),
  94. "value": false
  95. },
  96. {
  97. "name": `${translate("guideSign")} 5`,
  98. "type": "frame",
  99. "path": path.join(__dirname, "../static/images/frame5.png"),
  100. "value": false
  101. },
  102. {
  103. "name": `${translate("guideSign")} 6`,
  104. "type": "frame",
  105. "path": path.join(__dirname, "../static/images/frame6.png"),
  106. "value": false
  107. },
  108. {
  109. "name": `${translate("guideSign")} 7`,
  110. "type": "frame",
  111. "path": path.join(__dirname, "../static/images/frame7.png"),
  112. "value": false
  113. },
  114. {
  115. "name": `${translate("guideSign")} 8`,
  116. "type": "frame",
  117. "path": path.join(__dirname, "../static/images/frame8.png"),
  118. "value": false
  119. },
  120. {
  121. "name": `${translate("guideSign")} 9`,
  122. "type": "frame",
  123. "path": path.join(__dirname, "../static/images/frame9.png"),
  124. "value": false
  125. },
  126. {
  127. "name": `${translate("guideSign")} 10`,
  128. "type": "frame",
  129. "path": path.join(__dirname, "../static/images/frame10.png"),
  130. "value": false
  131. },
  132. {
  133. "name": `${translate("guideSign")} 11`,
  134. "type": "hand",
  135. "path": path.join(__dirname, "../static/images/hand1.png"),
  136. "value": false
  137. },
  138. {
  139. "name": `${translate("guideSign")} 12`,
  140. "type": "hand",
  141. "path": path.join(__dirname, "../static/images/hand2.png"),
  142. "value": false
  143. },
  144. {
  145. "name": `${translate("guideSign")} 13`,
  146. "type": "hand",
  147. "path": path.join(__dirname, "../static/images/hand3.png"),
  148. "value": false
  149. },
  150. {
  151. "name": `${translate("guideSign")} 14`,
  152. "type": "hand",
  153. "path": path.join(__dirname, "../static/images/hand4.png"),
  154. "value": false
  155. },
  156. {
  157. "name": `${translate("guideSign")} 15`,
  158. "type": "hand",
  159. "path": path.join(__dirname, "../static/images/hand5.png"),
  160. "value": false
  161. },
  162. {
  163. "name": `${translate("guideSign")} 16`,
  164. "type": "hand",
  165. "path": path.join(__dirname, "../static/images/hand6.png"),
  166. "value": false
  167. },
  168. {
  169. "name": `${translate("guideSign")} 17`,
  170. "type": "hand",
  171. "path": path.join(__dirname, "../static/images/hand7.png"),
  172. "value": false
  173. },
  174. {
  175. "name": `${translate("guideSign")} 18`,
  176. "type": "hand",
  177. "path": path.join(__dirname, "../static/images/hand8.png"),
  178. "value": false
  179. },
  180. {
  181. "name": `${translate("guideSign")} 19`,
  182. "type": "hand",
  183. "path": path.join(__dirname, "../static/images/hand9.png"),
  184. "value": false
  185. },
  186. {
  187. "name": `${translate("guideSign")} 20`,
  188. "type": "hand",
  189. "path": path.join(__dirname, "../static/images/hand10.png"),
  190. "value": false
  191. },
  192. {
  193. "name": `${translate("guideSign")} 21`,
  194. "type": "hand",
  195. "path": path.join(__dirname, "../static/images/hand11.png"),
  196. "value": false
  197. },
  198. {
  199. "name": `${translate("guideSign")} 22`,
  200. "type": "hand",
  201. "path": path.join(__dirname, "../static/images/hand12.png"),
  202. "value": false
  203. },
  204. {
  205. "name": `${translate("guideSign")} 23`,
  206. "type": "arrow",
  207. "path": path.join(__dirname, "../static/images/arrow1.png"),
  208. "value": false
  209. },
  210. {
  211. "name": `${translate("guideSign")} 24`,
  212. "type": "arrow",
  213. "path": path.join(__dirname, "../static/images/arrow2.png"),
  214. "value": false
  215. },
  216. {
  217. "name": `${translate("guideSign")} 25`,
  218. "type": "arrow",
  219. "path": path.join(__dirname, "../static/images/arrow3.png"),
  220. "value": false
  221. },
  222. {
  223. "name": `${translate("guideSign")} 26`,
  224. "type": "arrow",
  225. "path": path.join(__dirname, "../static/images/arrow4.png"),
  226. "value": false
  227. },
  228. {
  229. "name": `${translate("guideSign")} 27`,
  230. "type": "arrow",
  231. "path": path.join(__dirname, "../static/images/arrow5.png"),
  232. "value": false
  233. },
  234. {
  235. "name": `${translate("guideSign")} 28`,
  236. "type": "arrow",
  237. "path": path.join(__dirname, "../static/images/arrow6.png"),
  238. "value": false
  239. },
  240. {
  241. "name": `${translate("guideSign")} 29`,
  242. "type": "arrow",
  243. "path": path.join(__dirname, "../static/images/arrow7.png"),
  244. "value": false
  245. },
  246. {
  247. "name": `${translate("guideSign")} 30`,
  248. "type": "arrow",
  249. "path": path.join(__dirname, "../static/images/arrow8.png"),
  250. "value": false
  251. },
  252. {
  253. "name": `${translate("guideSign")} 31`,
  254. "type": "arrow",
  255. "path": path.join(__dirname, "../static/images/arrow9.png"),
  256. "value": false
  257. },
  258. {
  259. "name": `${translate("guideSign")} 32`,
  260. "type": "arrow",
  261. "path": path.join(__dirname, "../static/images/arrow10.png"),
  262. "value": false
  263. },
  264. {
  265. "name": `${translate("guideSign")} 33`,
  266. "type": "arrow",
  267. "path": path.join(__dirname, "../static/images/arrow11.png"),
  268. "value": false
  269. },
  270. {
  271. "name": `${translate("guideSign")} 34`,
  272. "type": "arrow",
  273. "path": path.join(__dirname, "../static/images/arrow12.png"),
  274. "value": false
  275. },
  276. ],
  277. selectedGuideSignPicPath: "",
  278. selectedGuideSignType: "",
  279. textBgPicArray: [
  280. {
  281. 'name': `${translate("textBg")} 1`,
  282. 'path': path.join(__dirname, '../static/images/bg1.png'),
  283. 'value': true
  284. },
  285. {
  286. 'name': `${translate("textBg")} 2`,
  287. 'path': path.join(__dirname, '../static/images/bg2.png'),
  288. 'value': false
  289. },
  290. {
  291. 'name': `${translate("textBg")} 3`,
  292. 'path': path.join(__dirname, '../static/images/bg3.png'),
  293. 'value': false
  294. },
  295. {
  296. 'name': `${translate("textBg")} 4`,
  297. 'path': path.join(__dirname, '../static/images/bg4.png'),
  298. 'value': false
  299. },
  300. {
  301. 'name': `${translate("textBg")} 5`,
  302. 'path': path.join(__dirname, '../static/images/bg5.png'),
  303. 'value': false
  304. },
  305. {
  306. 'name': `${translate("textBg")} 6`,
  307. 'path': path.join(__dirname, '../static/images/bg6.png'),
  308. 'value': false
  309. },
  310. {
  311. 'name': `${translate("textBg")} 7`,
  312. 'path': path.join(__dirname, '../static/images/bg7.png'),
  313. 'value': false
  314. },
  315. {
  316. 'name': `${translate("textBg")} 8`,
  317. 'path': path.join(__dirname, '../static/images/bg8.png'),
  318. 'value': false
  319. },
  320. {
  321. 'name': `${translate("textBg")} 9`,
  322. 'path': path.join(__dirname, '../static/images/bg9.png'),
  323. 'value': false
  324. },
  325. {
  326. 'name': `${translate("textBg")} 10`,
  327. 'path': path.join(__dirname, '../static/images/bg10.png'),
  328. 'value': false
  329. },
  330. {
  331. 'name': `${translate("textBg")} 11`,
  332. 'path': path.join(__dirname, '../static/images/bg11.png'),
  333. 'value': false
  334. },
  335. {
  336. 'name': `${translate("textBg")} 12`,
  337. 'path': path.join(__dirname, '../static/images/bg12.png'),
  338. 'value': false
  339. },
  340. {
  341. 'name': `${translate("textBg")} 13`,
  342. 'path': path.join(__dirname, '../static/images/bg13.png'),
  343. 'value': false
  344. },
  345. {
  346. 'name': `${translate("textBg")} 14`,
  347. 'path': path.join(__dirname, '../static/images/bg14.png'),
  348. 'value': false
  349. },
  350. {
  351. 'name': `${translate("textBg")} 15`,
  352. 'path': path.join(__dirname, '../static/images/bg15.png'),
  353. 'value': false
  354. },
  355. {
  356. 'name': `${translate("textBg")} 16`,
  357. 'path': path.join(__dirname, '../static/images/bg16.png'),
  358. 'value': false
  359. }
  360. ],
  361. selectedTextBgPicPath: "",
  362. },
  363. created() {
  364. // 如果不需要更新提示,注释这行代码即可
  365. //this.checkUpdate();
  366. // 用户如果没有进行选择,则默认选择数组中第一个元素
  367. this.selectedGuideSignPicPath = this.guideSignPicArray[0].path;
  368. this.selectedGuideSignType = this.guideSignPicArray[0].type;
  369. this.selectedTextBgPicPath = this.textBgPicArray[0].path;
  370. },
  371. methods: {
  372. checkUpdate() {
  373. // 检查是否有更新
  374. axios.get('https://la-vie.gitee.io/user-guide2/package.json', {headers: {'Cache-Control': 'no-cache'}})
  375. .then(function (response) {
  376. let remoteVersion = response.data.version;
  377. if (packageJSON.version != remoteVersion) {
  378. Editor.warn(`[v${remoteVersion}] ${translate('updateWarn')}`);
  379. }
  380. });
  381. },
  382. changeGuideTextType() {
  383. // 切换引导语言类型
  384. if (this.guideTextType == 'zh') {
  385. this.guideTextType = 'en'
  386. } else {
  387. this.guideTextType = 'zh'
  388. }
  389. },
  390. showAliasInput(index) {
  391. // 显示别名设置输入框
  392. this.guideArray[index].isAliased = true
  393. },
  394. onArrowPosXChange(value, index) {
  395. this.guideArray[index].arrowPos[0] = parseInt(value) ? parseInt(value) : value == '0' ? 0 : value
  396. },
  397. onArrowPosYChange(value, index) {
  398. this.guideArray[index].arrowPos[1] = parseInt(value) ? parseInt(value) : value == '0' ? 0 : value
  399. },
  400. onArrowRotationChange(value, index) {
  401. this.guideArray[index].arrowRotation = parseInt(value) ? parseInt(value) : value == '0' ? 0 : value
  402. },
  403. onTextBgPosXChange(value, index) {
  404. this.guideArray[index].textBgPos[0] = parseInt(value) ? parseInt(value) : value == '0' ? 0 : value
  405. },
  406. onTextBgPosYChange(value, index) {
  407. this.guideArray[index].textBgPos[1] = parseInt(value) ? parseInt(value) : value == '0' ? 0 : value
  408. },
  409. onArrowTypeChange(value, index) {
  410. this.guideArray[index].arrowType = parseInt(value) ? parseInt(value) : value == '0' ? 0 : value
  411. },
  412. onShowTextBgChange(value, index) {
  413. this.guideArray[index].showTextBg = value
  414. },
  415. onShowRoleChange(value, index) {
  416. // 显示别名设置输入框
  417. this.guideArray[index].showRole = value
  418. },
  419. onPreventChange(value, index) {
  420. // 显示别名设置输入框
  421. this.guideArray[index].prevent = value
  422. },
  423. hideAliasInput(index) {
  424. // 隐藏别名设置输入框,并清空内容
  425. this.guideArray[index].isAliased = false
  426. this.guideArray[index].alias = ''
  427. },
  428. setAlias(value, index) {
  429. // 设置别名
  430. this.guideArray[index].alias = value.trim()
  431. },
  432. onUiTypeChange(value, index) {
  433. // 设置别名
  434. this.guideArray[index].uiType = value.trim()
  435. },
  436. moveUp(index) {
  437. // 将当前引导往上移动
  438. if (index == 0) {
  439. return
  440. }
  441. let temp = this.guideArray[index]
  442. this.guideArray[index] = this.guideArray[index - 1]
  443. this.guideArray[index - 1] = temp
  444. guideArrayTemp = JSON.parse(JSON.stringify(this.guideArray))
  445. this.guideArray = Object.assign([], [], guideArrayTemp)
  446. },
  447. moveDown(index) {
  448. // 将当前引导往下移动
  449. if (index == this.guideArray.length - 1) {
  450. return
  451. }
  452. let temp = this.guideArray[index]
  453. this.guideArray[index] = this.guideArray[index + 1]
  454. this.guideArray[index + 1] = temp
  455. guideArrayTemp = JSON.parse(JSON.stringify(this.guideArray))
  456. this.guideArray = Object.assign([], [], guideArrayTemp)
  457. },
  458. addGuide() {
  459. // 新增引导
  460. this.guideArray.push(
  461. {
  462. "nodeUUID": "",
  463. "path": "",
  464. "key": "",
  465. "guideText": "",
  466. "guideEnText": "",
  467. "guideType": "touch",
  468. "isDynamic": false,
  469. "isAliased": false,
  470. "alias": "",
  471. "uiType": "",
  472. "arrowPos": [0, 0],
  473. "arrowRotation": 0,
  474. "textBgPos": [0, 0],
  475. "arrowType": 0,
  476. "showTextBg": false,
  477. "showRole": false,
  478. "prevent": true,
  479. }
  480. )
  481. },
  482. deleteGuide(index) {
  483. // 删除一条引导
  484. // 必须要有一个引导存在
  485. if (this.guideArray.length == 1) {
  486. remote.dialog.showMessageBoxSync({
  487. type: 'warning',
  488. buttons: ['Ok'],
  489. title: '',
  490. message: translate("noGuideArrayWarning")
  491. })
  492. return
  493. }
  494. this.guideArray.splice(index, 1)
  495. },
  496. onGuideTypeSelectionChange(value, index) {
  497. // 更新引导类型(触摸还是按键)
  498. this.guideArray[index].guideType = value
  499. },
  500. switchBetweenStaticAndDynamic(index) {
  501. // 在静态和动态节点之间切换
  502. this.guideArray[index].isDynamic = !this.guideArray[index].isDynamic
  503. },
  504. onNodeSelectionChange(uuid, index) {
  505. // 更新节点UUID
  506. this.guideArray[index].nodeUUID = uuid
  507. },
  508. onDynamicNodePathChange(path, index) {
  509. // 更新动态节点的路径
  510. this.guideArray[index].path = path
  511. },
  512. onKeyChange(key, index) {
  513. // 更新按键
  514. this.guideArray[index].key = key.trim()
  515. },
  516. showKeyHelp() {
  517. // 显示按键帮助
  518. childProcess.exec('start https://docs.cocos.com/creator/api/zh/enumeration/KeyCode')
  519. console.log(translate('keyHelp'))
  520. },
  521. onGuideTextChange(text, index) {
  522. // 更新引导文本
  523. if (this.guideTextType == 'zh') {
  524. this.guideArray[index].guideText = text
  525. } else {
  526. this.guideArray[index].guideEnText = text
  527. }
  528. },
  529. onGuideSignCheckboxConfirm(index) {
  530. // 选择引导图片
  531. for (let i = 0; i < this.guideSignPicArray.length; i++) {
  532. this.guideSignPicArray[i].value = false
  533. }
  534. this.guideSignPicArray[index].value = true
  535. this.selectedGuideSignPicPath = this.guideSignPicArray[index].path
  536. this.selectedGuideSignType = this.guideSignPicArray[index].type
  537. },
  538. onTextBgCheckboxConfirm(index) {
  539. // 选择引导文本背景图片
  540. for (let i = 0; i < this.textBgPicArray.length; i++) {
  541. this.textBgPicArray[i].value = false
  542. }
  543. this.textBgPicArray[index].value = true
  544. this.selectedTextBgPicPath = this.textBgPicArray[index].path
  545. },
  546. back() {
  547. // 返回上一个引导步骤
  548. this.currentStep -= 1
  549. },
  550. generateResources() {
  551. //通过预制体显示,不需要动态图片资源
  552. return
  553. // 将图片文件存放到resources/user-guide文件夹下
  554. let resourcesPath = Editor.url('db://assets/resources')
  555. if (!fs.existsSync(resourcesPath)) {
  556. Editor.assetdb.createOrSave('db://assets/resources')
  557. }
  558. let userGuideInResourcesPath = Editor.url('db://assets/resources/user-guide')
  559. if (!fs.existsSync(userGuideInResourcesPath)) {
  560. Editor.assetdb.createOrSave('db://assets/resources/user-guide')
  561. }
  562. fs.copyFileSync(this.selectedGuideSignPicPath, path.join(__dirname, '../static/images/guideSign.png'))
  563. fs.copyFileSync(this.selectedTextBgPicPath, path.join(__dirname, '../static/images/textBg.png'))
  564. Editor.assetdb.import([
  565. path.join(__dirname, '../static/images/guideSign.png'),
  566. path.join(__dirname, '../static/images/textBg.png'),
  567. path.join(__dirname, '../static/images/default_btn_normal.png')
  568. ], 'db://assets/resources/user-guide')
  569. },
  570. generateTsCode() {
  571. // 生成js和ts代码,开发者将其挂载到Canvas节点上然后在自己的脚本中调用showGuide()方法即可
  572. let self = this
  573. Editor.Ipc.sendToPanel('scene', 'scene:query-hierarchy', (err, sceneID, hierarchy) => {
  574. if (err) {
  575. Editor.error(translate('hierarchyError'))
  576. return
  577. }
  578. // 获取到场景名称
  579. Editor.assetdb.queryInfoByUuid(sceneID, function (err, info) {
  580. let sceneName = ""
  581. if (err) {
  582. Editor.error(translate('sceneNameError'))
  583. sceneName = sceneID
  584. } else if (!info) {
  585. sceneName = 'main'
  586. } else {
  587. sceneName = info.path.replace(/\\/g, '/').split('/').pop().replace('.fire', '').replace(/\s*/g, '')
  588. }
  589. // 获取UUID对应的节点路径
  590. for (let i = 0; i < self.guideArray.length; i++) {
  591. // if (sceneID.length <= 25) {
  592. // Editor.error(translate("dynacmicNodeError"))
  593. // break
  594. // }
  595. let nodeUUID = self.guideArray[i].nodeUUID
  596. for (let j = 0; j < hierarchy.length; j++) {
  597. if (hierarchy[j].name == 'Canvas') {
  598. // Editor.log(Object.keys(hierarchy[j].children[1].children[1]))
  599. // Editor.log(Object.values(hierarchy[j].children[1].children[1]))
  600. let allCanvasNodesArray = hierarchy[j].children
  601. // 如果是动态节点,开发者会手动输入路径,无需赋值
  602. if (!self.guideArray[i].isDynamic) {
  603. self.guideArray[i].path = self.getNodePathByUUID(allCanvasNodesArray, nodeUUID).path
  604. }
  605. break
  606. }
  607. }
  608. }
  609. // 因为下面会将guideArray中的键名称改为键值
  610. // 开发者如果生成了ts文件,又返回第一步想要修改部分按键的话会发现键名称已经改成了键值,这样就不好修改
  611. // 为了防止这种情况出现,这里用一个临时的变量代替guideArray
  612. let guideArrayTemp = JSON.parse(JSON.stringify(self.guideArray))
  613. // 将按键名称转换成键值
  614. for (let i = 0; i < guideArrayTemp.length; i++) {
  615. if (!guideArrayTemp[i].key) {
  616. continue
  617. }
  618. let keyCodes = ''
  619. let keys = guideArrayTemp[i].key.split('+')
  620. for (let j = 0; j < keys.length; j++) {
  621. if (j == keys.length - 1) {
  622. keyCodes += KeyCode[keys[j]]
  623. } else {
  624. keyCodes += (KeyCode[keys[j]] + '+')
  625. }
  626. }
  627. guideArrayTemp[i].key = keyCodes
  628. }
  629. // 生成最终的数据
  630. let guideData = {
  631. "nodesAndTexts": guideArrayTemp,
  632. "guideSignType": self.selectedGuideSignType,
  633. }
  634. let tsCodePath = 'db://assets/script/userguide'
  635. // 生成代码文件
  636. let userGuideCodeFolderPath = Editor.url(tsCodePath)
  637. if (!fs.existsSync(userGuideCodeFolderPath)) {
  638. Editor.assetdb.createOrSave(tsCodePath)
  639. }
  640. sceneNameFolderPath = Editor.url(`${tsCodePath}`)
  641. if (!fs.existsSync(sceneNameFolderPath)) {
  642. Editor.assetdb.createOrSave(`${tsCodePath}`)
  643. }
  644. // let urlJS = `db://assets/user-guide/${sceneName}/UserGuide.js`;
  645. // let codesJS = vsprintf(fs.readFileSync(path.join(__dirname, "../static/codes/UserGuideJS.txt"), "utf-8"), [JSON.stringify(guideData)]);
  646. // Editor.assetdb.createOrSave(urlJS, codesJS);
  647. let urlTS = `${tsCodePath}/UserGuide.ts`;
  648. let codesTS = vsprintf(fs.readFileSync(path.join(__dirname, "../static/codes/UserGuideTS.txt"), "utf-8"), [JSON.stringify(guideData)]);
  649. Editor.assetdb.createOrSave(urlTS, codesTS);
  650. })
  651. });
  652. },
  653. getNodePathByUUID(canvasNodesArray, uuid, path = 'Canvas') {
  654. // 根据节点UUID获取其路径
  655. let pathTemp = path
  656. result = {
  657. path: '',
  658. isOk: false
  659. }
  660. for (let i = 0; i < canvasNodesArray.length; i++) {
  661. path = pathTemp + '/' + canvasNodesArray[i].name
  662. if (canvasNodesArray[i].id == uuid) {
  663. return {
  664. path: path,
  665. isOk: true
  666. }
  667. } else if (canvasNodesArray[i].children && canvasNodesArray[i].children.length) {
  668. result = this.getNodePathByUUID(canvasNodesArray[i].children, uuid, path)
  669. if (result.isOk) {
  670. return result
  671. }
  672. }
  673. }
  674. return result
  675. },
  676. useLastGuide() {
  677. // 使用上次的引导
  678. let choice = remote.dialog.showMessageBoxSync({
  679. type: 'info',
  680. buttons: ['OK', 'Cancel'],
  681. title: '',
  682. message: translate('confirmUseLastGuide')
  683. })
  684. if (choice.response == 1) {
  685. return
  686. }
  687. if (!localStorage.getItem('UserGuideExt')) {
  688. remote.dialog.showMessageBoxSync({
  689. type: 'info',
  690. buttons: ['OK'],
  691. title: '',
  692. message: translate("lastGuideNotExisted")
  693. })
  694. return
  695. }
  696. this.guideArray = Object.assign([], [], JSON.parse(localStorage.getItem('UserGuideExt')))
  697. Editor.Panel.popup('user-guide')
  698. },
  699. confirmStep1() {
  700. // 确认引导的第一步骤
  701. // 引导节点和文本不能都空着
  702. for (let i = 0; i < this.guideArray.length; i++) {
  703. if (this.guideArray[i].guideType == 'touch' && !this.guideArray[i].nodeUUID && !this.guideArray[i].path && !this.guideArray[i].guideText && !this.guideArray[i].guideEnText) {
  704. // remote.dialog.showMessageBoxSync({
  705. // type: 'warning',
  706. // buttons: ['OK'],
  707. // title: '',
  708. // message: translate("emptyNodeTextGuideWarning")
  709. // })
  710. // return
  711. } else if (this.guideArray[i].guideType == 'key' && !this.guideArray[i].key && !this.guideArray[i].guideText && !this.guideArray[i].guideEnText) {
  712. remote.dialog.showMessageBoxSync({
  713. type: 'warning',
  714. buttons: ['OK'],
  715. title: '',
  716. message: translate("emptyKeyTextGuideWarning")
  717. })
  718. return
  719. }
  720. }
  721. // 检查别名是否有重复
  722. let tempArray = []
  723. for (let i = 0; i < this.guideArray.length; i++) {
  724. if (this.guideArray[i].alias == '') {
  725. continue
  726. }
  727. if (tempArray.indexOf(this.guideArray[i].alias) > -1) {
  728. remote.dialog.showMessageBoxSync({
  729. type: 'error',
  730. buttons: ['OK'],
  731. title: '',
  732. message: `${translate('aliasRepeatError')}${tempArray.indexOf(this.guideArray[i].alias) + 1}, ${i + 1}`
  733. })
  734. return
  735. } else {
  736. tempArray.push(this.guideArray[i].alias)
  737. }
  738. }
  739. // 检查按键有没有错误
  740. for (let i = 0; i < this.guideArray.length; i++) {
  741. if (this.guideArray[i].key) {
  742. let keys = this.guideArray[i].key.split('+')
  743. for (let j = 0; j < keys.length; j++) {
  744. if (KeyCode[keys[j]] == undefined) {
  745. remote.dialog.showMessageBoxSync({
  746. type: 'error',
  747. buttons: ['OK'],
  748. title: '',
  749. message: `${translate('guide')} ${i + 1} ${translate('keyError')}${keys[j]}`
  750. })
  751. return
  752. }
  753. }
  754. }
  755. }
  756. // 将当前引导存储到本地
  757. localStorage.setItem('UserGuideExt', JSON.stringify(this.guideArray))
  758. // // 进入下一步
  759. // this.currentStep += 1
  760. this.confirmStep2()
  761. },
  762. confirmStep2() {
  763. // 确认引导的第二步骤
  764. // 生成文件
  765. this.generateResources()
  766. this.generateTsCode()
  767. // 显示提示
  768. remote.dialog.showMessageBoxSync({
  769. type: 'info',
  770. buttons: ['OK'],
  771. title: '',
  772. message: translate('generateSuccess')
  773. })
  774. }
  775. }
  776. });
  777. },
  778. // register your ipc messages here
  779. messages: {
  780. // 'user-guide:hello' (event) {
  781. // this.$label.innerText = 'Hello!';
  782. // }
  783. }
  784. });