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