SettingViewComp.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 15:38:50
  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. nickName: "金砖大王",
  26. uid: "1234567890",
  27. music_state: true,
  28. effect_state: true
  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.updateHead();
  41. this.updateBtnState();
  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. const music_state = oops.storage.get("music_state");
  56. const effect_state = oops.storage.get("effect_state");
  57. if (music_state == "" || music_state == null) {
  58. oops.storage.set("music_state", true);
  59. this._musicState = true;
  60. } else {
  61. // 将字符串转换为布尔值
  62. let music_state_bool = music_state == "true" ? true : false;
  63. this._musicState = music_state_bool;
  64. }
  65. if (effect_state == "" || effect_state == null) {
  66. oops.storage.set("effect_state", true);
  67. this._effectState = true;
  68. } else {
  69. // 将字符串转换为布尔值
  70. let effect_state_bool = effect_state == "true" ? true : false;
  71. this._effectState = effect_state_bool;
  72. }
  73. oops.audio.switchMusic = this._musicState;
  74. oops.audio.switchEffect = this._effectState;
  75. const music_on = this.musicBtn.node.getChildByPath("btn_on")!;
  76. const music_off = this.musicBtn.node.getChildByPath("btn_off")!;
  77. music_on.active = this._musicState;
  78. music_off.active = !this._musicState;
  79. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  80. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  81. btn_on.active = this._effectState;
  82. btn_off.active = !this._effectState;
  83. }
  84. //更新头像
  85. private updateHead() {
  86. let account = ecs.getEntity<Account>(Account);
  87. let sprite = this.node.getChildByPath("Bg/btn_head/sp_head")!.uiSprite;
  88. // let url = account.AccountModel.head;
  89. let url = "http://www.kuaipng.com/Uploads/pic/w/2020/07-16/89010/water_89010_698_698_.png"
  90. this.data.uid = account.AccountModel.Uid;
  91. var opt: IRemoteOptions = { ext: ".png" };
  92. var onComplete = (err: Error | null, data: ImageAsset) => {
  93. const texture = new Texture2D();
  94. texture.image = data;
  95. const spriteFrame = new SpriteFrame();
  96. spriteFrame.texture = texture;
  97. sprite.spriteFrame = spriteFrame;
  98. }
  99. resLoader.loadRemote<ImageAsset>(url, opt, onComplete);
  100. }
  101. //打开音乐
  102. private btn_music() {
  103. this._musicState = !this._musicState;
  104. oops.storage.set("music_state", this._musicState);
  105. oops.audio.switchMusic = this._musicState;
  106. const btn_on = this.musicBtn.node.getChildByPath("btn_on")!;
  107. const btn_off = this.musicBtn.node.getChildByPath("btn_off")!;
  108. btn_on.active = this._musicState;
  109. btn_off.active = !this._musicState;
  110. }
  111. //打开音效
  112. private btn_effect() {
  113. this._effectState = !this._effectState;
  114. oops.storage.set("effect_state", this._effectState);
  115. oops.audio.switchEffect = this._effectState;
  116. const btn_on = this.effectBtn.node.getChildByPath("btn_on")!;
  117. const btn_off = this.effectBtn.node.getChildByPath("btn_off")!;
  118. btn_on.active = this._effectState;
  119. btn_off.active = !this._effectState;
  120. }
  121. //隐私协议
  122. private btn_privacy() {
  123. }
  124. //用户协议
  125. private btn_protocol() {
  126. }
  127. }