SettingViewComp.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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-14 18:01:16
  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. const { ccclass, property } = _decorator;
  25. /** 视图层对象 */
  26. @ccclass('SettingViewComp')
  27. @ecs.register('SettingView', false)
  28. export class SettingViewComp extends CCVMParentComp {
  29. data: any = {
  30. head_url: "",
  31. uid: 100000,
  32. }
  33. @property(Button)
  34. private musicBtn: Button = null!;
  35. @property(Button)
  36. private effectBtn: Button = null!;
  37. private _musicState: boolean = true;
  38. private _effectState: boolean = true;
  39. /** 视图层逻辑代码分离演示 */
  40. start() {
  41. // const entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
  42. this.setButton();
  43. this.updateBtnState();
  44. this.updateHead();
  45. }
  46. /** 视图对象通过 ecs.Entity.remove(SettingViewComp) 删除组件是触发组件处理自定义释放逻辑 */
  47. reset() {
  48. this.node.destroy();
  49. }
  50. private btn_about() {
  51. oops.gui.open(UIID.AboutUs);
  52. }
  53. private btn_close() {
  54. oops.gui.remove(UIID.Setting);
  55. }
  56. //更新按钮状态
  57. private updateBtnState() {
  58. console.log("updateBtnState");
  59. const music_state = oops.storage.getBoolean("music_state");
  60. const effect_state = oops.storage.getBoolean("effect_state");
  61. oops.log.logView(music_state, "music_state");
  62. console.log("effect_state", effect_state);
  63. if (music_state == null) {
  64. oops.storage.set("music_state", true);
  65. this._musicState = true;
  66. } else {
  67. // 将字符串转换为布尔值
  68. let music_state_bool = music_state == true ? true : false;
  69. this._musicState = music_state_bool;
  70. }
  71. if (effect_state == null) {
  72. oops.storage.set("effect_state", true);
  73. this._effectState = true;
  74. } else {
  75. // 将字符串转换为布尔值
  76. let effect_state_bool = effect_state == true ? true : false;
  77. this._effectState = effect_state_bool;
  78. }
  79. oops.audio.switchMusic = this._musicState;
  80. oops.audio.switchEffect = this._effectState;
  81. const music_on = this.musicBtn.node.getChildByPath("btn_on")!;
  82. const music_off = this.musicBtn.node.getChildByPath("btn_off")!;
  83. music_on.active = this._musicState;
  84. music_off.active = !this._musicState;
  85. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  86. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  87. btn_on.active = this._effectState;
  88. btn_off.active = !this._effectState;
  89. }
  90. //更新头像
  91. private updateHead() {
  92. this.data.uid = smc.account.AccountModel.uid;
  93. let url = smc.account.AccountModel.headUrl;
  94. let spriteNode = this.node.getChildByPath("Bg/btn_head/Mask/sp_head");
  95. if (spriteNode) {
  96. const uiTransform = spriteNode.getComponent(UITransform);
  97. uiTransform?.setContentSize(new Size(110, 110))
  98. let sprite = spriteNode.getComponent(Sprite);
  99. if (sprite) {
  100. console.log("更新头像啦")
  101. var opt: IRemoteOptions = { ext: ".png" };
  102. var onComplete = (err: Error | null, data: ImageAsset) => {
  103. const texture = new Texture2D();
  104. texture.image = data;
  105. const spriteFrame = new SpriteFrame();
  106. spriteFrame.texture = texture;
  107. sprite.spriteFrame = spriteFrame;
  108. }
  109. resLoader.loadRemote<ImageAsset>(url, opt, onComplete);
  110. }
  111. }
  112. }
  113. //打开音乐
  114. private btn_music() {
  115. this._musicState = !this._musicState;
  116. oops.storage.set("music_state", this._musicState);
  117. oops.audio.switchMusic = this._musicState;
  118. const btn_on = this.musicBtn.node.getChildByPath("btn_on")!;
  119. const btn_off = this.musicBtn.node.getChildByPath("btn_off")!;
  120. btn_on.active = this._musicState;
  121. btn_off.active = !this._musicState;
  122. }
  123. //打开音效
  124. private btn_effect() {
  125. this._effectState = !this._effectState;
  126. oops.storage.set("effect_state", this._effectState);
  127. oops.audio.switchEffect = this._effectState;
  128. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  129. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  130. btn_on.active = this._effectState;
  131. btn_off.active = !this._effectState;
  132. }
  133. //隐私协议
  134. private btn_privacy() {
  135. smc.game.GameModel.protocolType = 1;
  136. LoginHandler.inst.openAgreement();
  137. }
  138. //用户协议
  139. private btn_protocol() {
  140. smc.game.GameModel.protocolType = 2;
  141. LoginHandler.inst.openAgreement();
  142. }
  143. }