SettingViewComp.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-21 18:47:35
  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. const music_state = oops.storage.getBoolean("music_state");
  59. const effect_state = oops.storage.getBoolean("effect_state");
  60. oops.log.logView(music_state, "music_state");
  61. console.log("effect_state", effect_state);
  62. if (music_state == null) {
  63. oops.storage.set("music_state", true);
  64. this._musicState = true;
  65. } else {
  66. // 将字符串转换为布尔值
  67. let music_state_bool = music_state == true ? true : false;
  68. this._musicState = music_state_bool;
  69. }
  70. if (effect_state == null) {
  71. oops.storage.set("effect_state", true);
  72. this._effectState = true;
  73. } else {
  74. // 将字符串转换为布尔值
  75. let effect_state_bool = effect_state == true ? true : false;
  76. this._effectState = effect_state_bool;
  77. }
  78. oops.audio.switchMusic = this._musicState;
  79. oops.audio.switchEffect = this._effectState;
  80. const music_on = this.musicBtn.node.getChildByPath("btn_on")!;
  81. const music_off = this.musicBtn.node.getChildByPath("btn_off")!;
  82. music_on.active = this._musicState;
  83. music_off.active = !this._musicState;
  84. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  85. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  86. btn_on.active = this._effectState;
  87. btn_off.active = !this._effectState;
  88. }
  89. //更新头像
  90. private updateHead() {
  91. this.data.uid = smc.account.AccountModel.uid;
  92. let url = smc.account.AccountModel.headUrl;
  93. let spriteNode = this.node.getChildByPath("Bg/btn_head/Mask/sp_head");
  94. if (spriteNode) {
  95. const uiTransform = spriteNode.getComponent(UITransform);
  96. uiTransform?.setContentSize(new Size(110, 110))
  97. let sprite = spriteNode.getComponent(Sprite);
  98. if (sprite) {
  99. console.log("更新头像啦")
  100. var opt: IRemoteOptions = { ext: ".png" };
  101. var onComplete = (err: Error | null, data: ImageAsset) => {
  102. const texture = new Texture2D();
  103. texture.image = data;
  104. const spriteFrame = new SpriteFrame();
  105. spriteFrame.texture = texture;
  106. sprite.spriteFrame = spriteFrame;
  107. }
  108. resLoader.loadRemote<ImageAsset>(url, opt, onComplete);
  109. }
  110. }
  111. }
  112. //打开音乐
  113. private btn_music() {
  114. this._musicState = !this._musicState;
  115. oops.storage.set("music_state", this._musicState);
  116. oops.audio.switchMusic = this._musicState;
  117. const btn_on = this.musicBtn.node.getChildByPath("btn_on")!;
  118. const btn_off = this.musicBtn.node.getChildByPath("btn_off")!;
  119. btn_on.active = this._musicState;
  120. btn_off.active = !this._musicState;
  121. }
  122. //打开音效
  123. private btn_effect() {
  124. this._effectState = !this._effectState;
  125. oops.storage.set("effect_state", this._effectState);
  126. oops.audio.switchEffect = this._effectState;
  127. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  128. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  129. btn_on.active = this._effectState;
  130. btn_off.active = !this._effectState;
  131. }
  132. //隐私协议
  133. private btn_privacy() {
  134. smc.game.GameModel.protocolType = 1;
  135. LoginHandler.inst.openAgreement();
  136. }
  137. //用户协议
  138. private btn_protocol() {
  139. smc.game.GameModel.protocolType = 2;
  140. LoginHandler.inst.openAgreement();
  141. }
  142. }