UI_Set.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Button, Size, Toggle } from "cc";
  2. import { Layout_Set } from "./Layout_Set";
  3. import { ModuleDef } from "../../scripts/ModuleDef";
  4. import { GameUILayers, gui, ui_base } from "../../core/ui/ui";
  5. import { ch } from "../../ch/ch";
  6. import get_new_wait from "../../core/util_class/Wait";
  7. export class UI_Set extends ui_base {
  8. constructor() {
  9. super(ModuleDef.BASIC, 'ui_set/UI_Set', GameUILayers.POPUP, Layout_Set);
  10. }
  11. protected onCreated(is_in_game: boolean): void {
  12. let layout = this.getLayout<Layout_Set>();
  13. this.onButtonEvent(layout.btnClose, this.onClose, this);
  14. this.onButtonEvent(layout.btnRestart, this.onRestart, this);
  15. this.onButtonEvent(layout.btnMain, this.onMain, this);
  16. layout.toogleMusic.isChecked = ch.audio.volumeMusic == 0;
  17. layout.toogleSound.isChecked = !ch.audio.switchEffect;
  18. this.onToggleEvent(layout.toogleMusic, this.onMusic, this);
  19. this.onToggleEvent(layout.toogleSound, this.onSound, this);
  20. ch.audio.pause();
  21. gui.scale_anim(layout.btnClose.node.parent);
  22. //
  23. layout.btnRestart.node.active = is_in_game;
  24. layout.btnMain.node.active = is_in_game;
  25. }
  26. private onClose(button: Button): void {
  27. this.wait.resolve(null);
  28. this.close();
  29. ch.audio.resume();
  30. }
  31. private wait = get_new_wait<string>();
  32. public async await_choose(): Promise<string> {
  33. return this.wait.wait();
  34. }
  35. private onRestart(button: Button): void {
  36. this.wait.resolve('restart');
  37. this.close();
  38. }
  39. private onMain(button: Button): void {
  40. this.wait.resolve('remain');
  41. this.close();
  42. }
  43. private onMusic(toggle: Toggle): void {
  44. ch.audio.volumeMusic = toggle.isChecked ? 0 : 1;
  45. ch.audio.save();
  46. }
  47. private onSound(toggle: Toggle): void {
  48. ch.audio.switchEffect = !toggle.isChecked;
  49. ch.audio.save();
  50. }
  51. protected onDispose(): void {
  52. this.wait?.resolve(null);
  53. this.wait?.dispose();
  54. this.wait = null;
  55. }
  56. }