| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import { _decorator, Button, Color, EditBox, instantiate, Label, Node, RichText, Sprite, SpriteFrame, UITransform } from 'cc';
- import { UIViewID } from '@scripts/define/UIDefine';
- import { UICloseReason } from '@scripts/framework/ui/UILayer';
- import { addBtnClick } from '@scripts/utils/UIHelper';
- import { AppKernel } from '@scripts/app/AppKernel';
- import { SelectBox } from '@scripts/components/selectBox/SelectBox';
- import { UIPopup } from '@scripts/framework/ui/UIPopup';
- import { setAvatars, setSpriteFrame } from '@scripts/utils/ResHelper';
- const { ccclass, property } = _decorator;
- @ccclass('ChangeAvatar')
- export class ChangeAvatar extends UIPopup {
- readonly viewId = UIViewID.ChangeAvatar;
- @property(Node)
- private btn_close: Node = null;
- @property(Node)
- private btn_confirm: Node = null;
- @property(Node)
- private item_avatar: Node = null;
- @property(Node)
- private layout_select: Node = null;
- private items_avatar: Node[] = [];
- private selectedAvatarId: number = 0;
- protected onCreate(): void | Promise<void> {
- addBtnClick(this.btn_close, this.onBtnCloseClick, this);
- addBtnClick(this.btn_confirm, this.onBtnConfirmClick, this);
- this.initView();
- }
- private initView() {
- const { FaceID } = AppKernel.stores.mainStore.userInfo;
- for (let i = 1; i <= 10; i++) {
- let node = instantiate(this.item_avatar);
- node.parent = this.layout_select;
- node.active = true;
- const sprite_avatar = node.getChildByName('avatar').getComponent(Sprite);
- const selected = node.getChildByName('selected');
- setAvatars(sprite_avatar, i);
- selected.active = i == FaceID;
- addBtnClick(node, () => this.onSelectAvatar(i), this);
- this.items_avatar.push(node);
- }
- }
- private onBtnCloseClick() {
- AppKernel.ui.close(UIViewID.ChangeAvatar);
- }
- private async onBtnConfirmClick() {
- if (this.selectedAvatarId >= 1) {
- const res = await AppKernel.stores.mainStore.reqChangeUserInfo({ FaceID: this.selectedAvatarId });
- if (res) {
- await AppKernel.ui.close(UIViewID.ChangeAvatar);
- AppKernel.ui.toast("Change success")
- }
- }
- }
- private onSelectAvatar(id: number) {
- if (this.selectedAvatarId == id) return;
- console.log("onSelectAvatar", id);
- this.selectedAvatarId = id;
- this.items_avatar.forEach((item, i) => {
- const selected = item.getChildByName('selected');
- selected.active = (i + 1 == id);
- })
- }
- }
|