SettingViewComp.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * @Author: mojunshou 1637302775@qq.com
  3. * @Date: 2025-03-20 15:40:20
  4. * @LastEditors: mojunshou 1637302775@qq.com
  5. * @LastEditTime: 2025-04-23 15:50:37
  6. * @Description:设置界面
  7. */
  8. import { _decorator } from "cc";
  9. import { oops } from "db://oops-framework/core/Oops";
  10. import { ecs } from "db://oops-framework/libs/ecs/ECS";
  11. import { CCComp } from "db://oops-framework/module/common/CCComp";
  12. import { UIID } from "../config/GameUIConfig";
  13. import { CCVMParentComp } from "db://oops-framework/module/common/CCVMParentComp";
  14. import { Toggle } from "cc";
  15. import { Account } from "../../account/Account";
  16. import { ImageAsset, Texture2D, SpriteFrame, Sprite } from "cc";
  17. import { IRemoteOptions, resLoader } from "db://oops-framework/core/common/loader/ResLoader";
  18. import { Button } from "cc";
  19. import { smc } from "../SingletonModuleComp";
  20. import { CocosHandler } from "../manager/CocosHandler";
  21. import { LoginHandler } from "../manager/LoginHandler";
  22. import { UITransform } from "cc";
  23. import { Size } from "cc";
  24. import { GameEvent } from "../config/GameEvent";
  25. import { DCHandler } from "../manager/DCHandler";
  26. const { ccclass, property } = _decorator;
  27. /** 视图层对象 */
  28. @ccclass('SettingViewComp')
  29. @ecs.register('SettingView', false)
  30. export class SettingViewComp extends CCVMParentComp {
  31. data: any = {
  32. head_url: "",
  33. uid: 100000,
  34. }
  35. @property(Button)
  36. private musicBtn: Button = null!;
  37. @property(Button)
  38. private effectBtn: Button = null!;
  39. private _musicState: boolean = true;
  40. private _effectState: boolean = true;
  41. /** 视图层逻辑代码分离演示 */
  42. start() {
  43. // const entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
  44. this.setButton();
  45. this.updateBtnState();
  46. this.updateHead();
  47. DCHandler.inst.reportData(3001000);
  48. }
  49. /** 视图对象通过 ecs.Entity.remove(SettingViewComp) 删除组件是触发组件处理自定义释放逻辑 */
  50. reset() {
  51. this.node.destroy();
  52. }
  53. private btn_about() {
  54. oops.gui.open(UIID.AboutUs);
  55. DCHandler.inst.reportData(3001004);
  56. }
  57. private btn_close() {
  58. oops.gui.remove(UIID.Setting);
  59. oops.message.dispatchEvent(GameEvent.updateGameState, "playing");
  60. DCHandler.inst.reportData(3001001);
  61. }
  62. //更新按钮状态
  63. private updateBtnState() {
  64. const music_state = oops.storage.getBoolean("music_state");
  65. const effect_state = oops.storage.getBoolean("effect_state");
  66. oops.log.logView(music_state, "music_state");
  67. console.log("effect_state", effect_state);
  68. if (music_state == null) {
  69. oops.storage.set("music_state", true);
  70. this._musicState = true;
  71. } else {
  72. // 将字符串转换为布尔值
  73. let music_state_bool = music_state == true ? true : false;
  74. this._musicState = music_state_bool;
  75. }
  76. if (effect_state == null) {
  77. oops.storage.set("effect_state", true);
  78. this._effectState = true;
  79. } else {
  80. // 将字符串转换为布尔值
  81. let effect_state_bool = effect_state == true ? true : false;
  82. this._effectState = effect_state_bool;
  83. }
  84. oops.audio.switchMusic = this._musicState;
  85. oops.audio.switchEffect = this._effectState;
  86. const music_on = this.musicBtn.node.getChildByPath("btn_on")!;
  87. const music_off = this.musicBtn.node.getChildByPath("btn_off")!;
  88. music_on.active = this._musicState;
  89. music_off.active = !this._musicState;
  90. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  91. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  92. btn_on.active = this._effectState;
  93. btn_off.active = !this._effectState;
  94. }
  95. //更新头像
  96. private updateHead() {
  97. this.data.uid = smc.account.AccountModel.uid;
  98. let url = smc.account.AccountModel.headUrl;
  99. let spriteNode = this.node.getChildByPath("Bg/btn_head/Mask/sp_head");
  100. if (spriteNode) {
  101. const uiTransform = spriteNode.getComponent(UITransform);
  102. uiTransform?.setContentSize(new Size(110, 110))
  103. let sprite = spriteNode.getComponent(Sprite);
  104. if (sprite) {
  105. console.log("更新头像啦")
  106. var opt: IRemoteOptions = { ext: ".png" };
  107. var onComplete = (err: Error | null, data: ImageAsset) => {
  108. const texture = new Texture2D();
  109. texture.image = data;
  110. const spriteFrame = new SpriteFrame();
  111. spriteFrame.texture = texture;
  112. sprite.spriteFrame = spriteFrame;
  113. }
  114. resLoader.loadRemote<ImageAsset>(url, opt, onComplete);
  115. }
  116. }
  117. }
  118. //打开音乐
  119. private btn_music() {
  120. this._musicState = !this._musicState;
  121. if (this._musicState) {
  122. DCHandler.inst.reportData(3001002);
  123. } else {
  124. DCHandler.inst.reportData(3001003);
  125. }
  126. oops.storage.set("music_state", this._musicState);
  127. oops.audio.switchMusic = this._musicState;
  128. const btn_on = this.musicBtn.node.getChildByPath("btn_on")!;
  129. const btn_off = this.musicBtn.node.getChildByPath("btn_off")!;
  130. btn_on.active = this._musicState;
  131. btn_off.active = !this._musicState;
  132. }
  133. //打开音效
  134. private btn_effect() {
  135. this._effectState = !this._effectState;
  136. oops.storage.set("effect_state", this._effectState);
  137. oops.audio.switchEffect = this._effectState;
  138. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  139. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  140. btn_on.active = this._effectState;
  141. btn_off.active = !this._effectState;
  142. }
  143. //隐私协议
  144. private btn_privacy() {
  145. smc.game.GameModel.protocolType = 1;
  146. LoginHandler.inst.openAgreement();
  147. }
  148. //用户协议
  149. private btn_protocol() {
  150. smc.game.GameModel.protocolType = 2;
  151. LoginHandler.inst.openAgreement();
  152. }
  153. }