22676a95-dd5b-47a3-95bc-d81b675c15b3.js 12.6 KB
"use strict";
cc._RF.push(module, '22676qV3VtHo5W82BtnXBWz', 'ChatBubbleView');
// script/game/ui/view/impl/message/ChatBubbleView.ts

"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const PlotModel_1 = require("../../../../../avg/model/PlotModel");
const RichTextUtils_1 = require("../../../../../avg/utils/RichTextUtils");
const CCDummyObjects_1 = require("../../../../../common/CCDummyObjects");
const AudioManager_1 = require("../../../../../common/gameplay/managers/AudioManager");
const ResUtils_1 = require("../../../../../common/utils/ResUtils");
const UIManager_1 = require("../../../../../common/gameplay/managers/UIManager");
const ImagePreviewPresenter_1 = require("../../../presenter/message/ImagePreviewPresenter");
const { ccclass, property } = cc._decorator;
const BASE_DELAY = 1;
const PER_CHAR_DELAY = 0.1;
let ChatBubbleView = /** @class */ (() => {
    let ChatBubbleView = class ChatBubbleView extends cc.Component {
        constructor() {
            super(...arguments);
            //#region editor bindings
            this.iconSprite = CCDummyObjects_1.DummySprite;
            this.nameLabel = CCDummyObjects_1.DummyLabel;
            this.textContentLabel = CCDummyObjects_1.DummyRichText;
            this.inputtingNode = undefined;
            this.contentNode = CCDummyObjects_1.DummyNode;
            this.bgNode = CCDummyObjects_1.DummyNode;
            this.itemIconSprite = undefined;
            this.imageSprite = undefined;
            this.container = undefined;
        }
        playVoice() {
            var _a;
            if (this._voicePath) {
                (_a = this.contentNode.children[1].getComponent(cc.Animation)) === null || _a === void 0 ? void 0 : _a.play("voice_play");
                AudioManager_1.AudioManager.playVoice(this._voicePath).then(() => {
                    var _a;
                    (_a = this.contentNode.children[1].getComponent(cc.Animation)) === null || _a === void 0 ? void 0 : _a.play("voice_stop");
                });
            }
        }
        previewImage() {
            UIManager_1.UIManager.pushPresenter(ImagePreviewPresenter_1.ImagePreviewPresenter, this._previewImage);
        }
        delay(time) {
            return new Promise((resolve) => {
                this._timer = setTimeout(() => {
                    this._timer = undefined;
                    resolve();
                }, time * 1000);
            });
        }
        init() {
            this._nodeHeight = this.node.height;
            this._bgWidth = this.bgNode.width;
            this._bgHeight = this.bgNode.height;
            this._textHeight = this.textContentLabel.node.height;
        }
        reuse() {
            if (this._timer) {
                clearTimeout(this._timer);
                this._timer = undefined;
            }
            if (this._nodeHeight === undefined)
                return;
            if (this._extraNode)
                this._extraNode.destroy();
            this.node.height = this._nodeHeight;
            this.bgNode.width = this._bgWidth;
            this.bgNode.height = this._bgHeight;
            this.textContentLabel.string = "";
            this._previewImage = "";
            this.bgNode.active = true;
            if (this.imageSprite)
                this.imageSprite.node.parent.active = false;
        }
        async resizeContent() {
            if (this._previewImage) { // 图片类型
                let sf = this.imageSprite.spriteFrame;
                let width = this.imageSprite.node.width = Math.max(sf.getOriginalSize().width, 200);
                this.imageSprite.node.height = width / sf.getOriginalSize().width * sf.getOriginalSize().height;
                this.node.height = -this.imageSprite.node.y + this.imageSprite.node.height + 40;
            }
            else {
                this.textContentLabel.node.active = true;
                this.textContentLabel.maxWidth = 0;
                while (!this.enabledInHierarchy) {
                    if (!this.enabled)
                        return;
                    await this.delay(0.5);
                }
                this.textContentLabel._updateRichText();
                if (400 < this.textContentLabel.node.width) {
                    this.textContentLabel.maxWidth = 400;
                    let diff = this.textContentLabel.node.height - this._textHeight;
                    this.node.height = this._nodeHeight + diff;
                    this.bgNode.height = this._bgHeight + diff;
                }
                if (this.bgNode.anchorX == 0) {
                    this.bgNode.width = Math.max(this._bgWidth, this.textContentLabel.node.x + this.textContentLabel.node.width + 40);
                }
                else {
                    this.bgNode.width = Math.max(this._bgWidth, -this.textContentLabel.node.x + this.textContentLabel.node.width + 40);
                }
            }
        }
        setName(name) {
            this.nameLabel.string = name;
        }
        setIcon(icon) {
            if (icon && this._icon !== icon) {
                this._icon = icon;
                ResUtils_1.ResUtils.loadRes(icon, cc.SpriteFrame).then((sf) => {
                    if (this._icon === icon)
                        this.iconSprite.spriteFrame = sf;
                });
            }
        }
        setText(txt) {
            this.textContentLabel.string = txt;
        }
        setItemIcon(icon) {
            if (icon && this._itemIcon !== icon && this.itemIconSprite) {
                this._itemIcon = icon;
                ResUtils_1.ResUtils.loadRes(icon, cc.SpriteFrame).then((sf) => {
                    if (this._itemIcon === icon)
                        this.itemIconSprite.spriteFrame = sf;
                });
            }
        }
        setExtraContent(node) {
            if (this.container) {
                this._extraNode = node;
                node.parent = this.container;
            }
        }
        async setContent(content, select, shouldDelay = false) {
            if (this._nodeHeight === undefined)
                this.init();
            let textCount = 0;
            let setContent1 = async (content) => {
                if (content.type === PlotModel_1.SentenceType.TEXT) {
                    textCount = RichTextUtils_1.richNodeTextCount(content.value);
                    this.textContentLabel.string = RichTextUtils_1.richNodesToCocosString(content.value);
                    this.contentNode.children[0].active = true;
                    this.contentNode.children[1].active = false;
                    this.contentNode.children[2].active = false;
                }
                else if (content.type === PlotModel_1.SentenceType.AUDIO) {
                    this._voicePath = "voice/" + content.value;
                    this.contentNode.children[0].active = false;
                    this.contentNode.children[1].active = true;
                    this.contentNode.children[2].active = false;
                }
                else if (content.type === PlotModel_1.SentenceType.IMAGE && this.imageSprite) {
                    this.contentNode.children[0].active = false;
                    this.contentNode.children[1].active = false;
                    this.contentNode.children[2].active = true;
                    if (content.value) {
                        this._previewImage = "textures/stickers/" + content.value;
                        let thumb = "textures/stickers/thumb/" + content.value;
                        let info = cc.resources.getInfoWithPath(thumb);
                        console.log("getInfoWithPath", info);
                        if (!info) {
                            thumb = this._previewImage;
                        }
                        try {
                            await ResUtils_1.ResUtils.loadRes(thumb, cc.SpriteFrame).then((sf) => {
                                // if (this.imageSprite!.node.activeInHierarchy) {
                                this.imageSprite.spriteFrame = sf;
                                // }
                            });
                        }
                        catch (e) {
                        }
                    }
                }
                else {
                    console.error("content type not supported yet.");
                }
            };
            let inputtingForever = false;
            let promise = undefined;
            if (content.type === PlotModel_1.SentenceType.SELECT) {
                if (select === undefined) {
                    inputtingForever = true;
                }
                else {
                    let option = content.value[select];
                    if (option.content) {
                        promise = setContent1(option.content);
                    }
                    else {
                        this.contentNode.children[0].active = true;
                        this.contentNode.children[1].active = false;
                        this.textContentLabel.string = option.summary;
                    }
                }
            }
            else {
                promise = setContent1(content);
            }
            if (this.inputtingNode) {
                if (shouldDelay || inputtingForever) {
                    this.inputtingNode.active = true;
                    this.contentNode.active = false;
                    this.inputtingNode.children[0].runAction(cc.repeatForever(cc.rotateBy(1, 360)));
                    if (inputtingForever)
                        return;
                    if (promise)
                        await promise;
                    await this.delay(BASE_DELAY + PER_CHAR_DELAY * textCount);
                    // return new Promise<void>((resolve) => {
                    //     this.scheduleOnce(() => {
                    this.inputtingNode.children[0].stopAllActions();
                    this.inputtingNode.active = false;
                    this.contentNode.active = true;
                    if (this._previewImage)
                        this.bgNode.active = false;
                    this.resizeContent();
                    //     resolve();
                    // }, BASE_DELAY + PER_CHAR_DELAY * textCount);
                    // })
                }
                else {
                    if (promise)
                        await promise;
                    this.inputtingNode.children[0].stopAllActions();
                    this.inputtingNode.active = false;
                    this.contentNode.active = true;
                    if (this._previewImage)
                        this.bgNode.active = false;
                    this.resizeContent();
                }
            }
        }
        clearInputting() {
            if (this._timer) {
                clearTimeout(this._timer);
                this._timer = undefined;
                if (this.inputtingNode) {
                    this.inputtingNode.children[0].stopAllActions();
                    this.inputtingNode.active = false;
                }
                this.contentNode.active = true;
                this.resizeContent();
            }
        }
    };
    __decorate([
        property({ type: cc.Sprite })
    ], ChatBubbleView.prototype, "iconSprite", void 0);
    __decorate([
        property({ type: cc.Label })
    ], ChatBubbleView.prototype, "nameLabel", void 0);
    __decorate([
        property({ type: cc.RichText })
    ], ChatBubbleView.prototype, "textContentLabel", void 0);
    __decorate([
        property({ type: cc.Node })
    ], ChatBubbleView.prototype, "inputtingNode", void 0);
    __decorate([
        property({ type: cc.Node })
    ], ChatBubbleView.prototype, "contentNode", void 0);
    __decorate([
        property({ type: cc.Node })
    ], ChatBubbleView.prototype, "bgNode", void 0);
    __decorate([
        property({ type: cc.Sprite })
    ], ChatBubbleView.prototype, "itemIconSprite", void 0);
    __decorate([
        property({ type: cc.Sprite })
    ], ChatBubbleView.prototype, "imageSprite", void 0);
    __decorate([
        property({ type: cc.Node })
    ], ChatBubbleView.prototype, "container", void 0);
    ChatBubbleView = __decorate([
        ccclass
    ], ChatBubbleView);
    return ChatBubbleView;
})();
exports.default = ChatBubbleView;

cc._RF.pop();