SettingViewComp.ts 5.3 KB

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