SettingViewComp.ts 5.4 KB

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