UpdateProgress.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // 更新进度条
  2. cc.Class({
  3. editor: {
  4. menu: 'Launch/UpdateProgress'
  5. },
  6. extends: cc.Component,
  7. properties: {
  8. backgroundSprite: cc.Sprite,
  9. progressSprite: cc.Sprite,
  10. statusSprite: cc.Sprite,
  11. progressLabel: cc.Label,
  12. glowSprite: cc.Sprite,
  13. atlas: cc.SpriteAtlas
  14. },
  15. onLoad () {
  16. this.status = JMC.UPDATE_STATUS.UNSTART;
  17. this.downloadedBytes = 0;
  18. this.totalBytes = 0;
  19. // 背景
  20. this.backgroundSpriteMap = {};
  21. this.backgroundSpriteMap[JMC.UPDATE_STATUS.UNSTART] = 'launch_update_progress_green_bg';
  22. this.backgroundSpriteMap[JMC.UPDATE_STATUS.DOWNLOADING] = 'launch_update_progress_green_bg';
  23. this.backgroundSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_PAUSE] = 'launch_update_progress_red_bg';
  24. this.backgroundSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_FINISHED] = 'launch_update_progress_green_bg';
  25. this.backgroundSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_FAILED] = 'launch_update_progress_red_bg';
  26. // 进度条
  27. this.progressSpriteMap = {};
  28. this.progressSpriteMap[JMC.UPDATE_STATUS.UNSTART] = 'launch_update_progress_green_long';
  29. this.progressSpriteMap[JMC.UPDATE_STATUS.DOWNLOADING] = 'launch_update_progress_green_long';
  30. this.progressSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_PAUSE] = 'launch_update_progress_red_long';
  31. this.progressSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_FINISHED] = 'launch_update_progress_green_long';
  32. this.progressSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_FAILED] = 'launch_update_progress_red_long';
  33. // 状态标示
  34. this.statusSpriteMap = {};
  35. this.statusSpriteMap[JMC.UPDATE_STATUS.UNSTART] = '';
  36. this.statusSpriteMap[JMC.UPDATE_STATUS.DOWNLOADING] = 'launch_update_icon_down_jt';
  37. this.statusSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_PAUSE] = 'launch_update_icon_stop';
  38. this.statusSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_FINISHED] = 'launch_update_icon_finish';
  39. this.statusSpriteMap[JMC.UPDATE_STATUS.DOWNLOAD_FAILED] = 'launch_update_icon_error';
  40. },
  41. onEnable () {
  42. this.reloadView();
  43. },
  44. reloadView () {
  45. let percent = (this.totalBytes == 0) ? 0 : this.downloadedBytes / this.totalBytes;
  46. let percentStr = Math.floor((percent) * 100);
  47. this.progressSprite.fillRange = percent;
  48. let downloadedMb = G.UpdateUtils.bytesToMb(this.downloadedBytes).toFixed(1);
  49. let totalMb = G.UpdateUtils.bytesToMb(this.totalBytes).toFixed(1);
  50. this.progressLabel.string = `${percentStr}% (${downloadedMb}M/${totalMb}M)`;
  51. this.backgroundSprite.spriteFrame = this.atlas.getSpriteFrame(this.backgroundSpriteMap[this.status]);
  52. this.progressSprite.spriteFrame = this.atlas.getSpriteFrame(this.progressSpriteMap[this.status]);
  53. this.statusSprite.spriteFrame = this.atlas.getSpriteFrame(this.statusSpriteMap[this.status]);
  54. this.statusSprite.node.x = this.progressLabel.node.x - this.progressLabel.node.width / 2 - this.statusSprite.node.width / 2;
  55. },
  56. setDownloadStatus (status) {
  57. this.status = status;
  58. },
  59. setProgress (downloadedBytes, totalBytes) {
  60. this.downloadedBytes = downloadedBytes;
  61. this.totalBytes = totalBytes;
  62. this.reloadView();
  63. },
  64. doFinishAnim () {
  65. if (!this.glowSprite)
  66. return;
  67. this.glowSprite.opacity = 0;
  68. let anim = cc.tween()
  69. .to(1, {opacity: 0})
  70. .to(1, {opacity: 255});
  71. cc.tween(this.glowSprite)
  72. .repeat(3, anim)
  73. .start();
  74. }
  75. });