ChangeAvatar.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { _decorator, Button, Color, EditBox, instantiate, Label, Node, RichText, Sprite, SpriteFrame, UITransform } from 'cc';
  2. import { UIViewID } from '@scripts/define/UIDefine';
  3. import { UICloseReason } from '@scripts/framework/ui/UILayer';
  4. import { addBtnClick } from '@scripts/utils/UIHelper';
  5. import { AppKernel } from '@scripts/app/AppKernel';
  6. import { SelectBox } from '@scripts/components/selectBox/SelectBox';
  7. import { UIPopup } from '@scripts/framework/ui/UIPopup';
  8. import { setSpriteFrame } from '@scripts/utils/ResHelper';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('ChangeAvatar')
  11. export class ChangeAvatar extends UIPopup {
  12. readonly viewId = UIViewID.ChangeAvatar;
  13. @property(Node)
  14. private btn_close: Node = null;
  15. @property(Node)
  16. private btn_confirm: Node = null;
  17. @property(Node)
  18. private item_avatar: Node = null;
  19. @property(Node)
  20. private layout_select: Node = null;
  21. private items_avatar: Node[] = [];
  22. private selectedAvatarId: number = 0;
  23. protected onCreate(): void | Promise<void> {
  24. addBtnClick(this.btn_close, this.onBtnCloseClick, this);
  25. addBtnClick(this.btn_confirm, this.onBtnConfirmClick, this);
  26. this.initView();
  27. }
  28. private initView() {
  29. const { FaceID } = AppKernel.stores.mainStore.userInfo;
  30. for (let i = 1; i <= 10; i++) {
  31. let node = instantiate(this.item_avatar);
  32. node.parent = this.layout_select;
  33. node.active = true;
  34. const sprite_avatar = node.getChildByName('avatar').getComponent(Sprite);
  35. const selected = node.getChildByName('selected');
  36. setSpriteFrame(sprite_avatar, "common/avatars/" + i);
  37. selected.active = i == FaceID;
  38. addBtnClick(node, () => this.onSelectAvatar(i), this);
  39. this.items_avatar.push(node);
  40. }
  41. }
  42. private onBtnCloseClick() {
  43. AppKernel.ui.close(UIViewID.ChangeAvatar);
  44. }
  45. private async onBtnConfirmClick() {
  46. if (this.selectedAvatarId >= 1) {
  47. const res = await AppKernel.stores.mainStore.reqChangeUserInfo({ FaceID: this.selectedAvatarId });
  48. if (res) {
  49. await AppKernel.ui.close(UIViewID.ChangeAvatar);
  50. AppKernel.ui.toast("Change success")
  51. }
  52. }
  53. }
  54. private onSelectAvatar(id: number) {
  55. if (this.selectedAvatarId == id) return;
  56. console.log("onSelectAvatar", id);
  57. this.selectedAvatarId = id;
  58. this.items_avatar.forEach((item, i) => {
  59. const selected = item.getChildByName('selected');
  60. selected.active = (i + 1 == id);
  61. })
  62. }
  63. }