22676a95-dd5b-47a3-95bc-d81b675c15b3.js 17.3 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 simba_cc_audio_manager_1 = require("simba-cc-audio-manager");
const simba_cc_resutils_1 = require("simba-cc-resutils");
const simba_config_manager_1 = require("simba-config-manager");
const AVG_1 = require("../../../../../avg/AVG");
// import { DailyQuestType } from "../../../../../avg/EditorEnums";
const CCDummyObjects_1 = require("../../../../../common/CCDummyObjects");
const GameTextData_1 = require("../../../../../common/gameplay/gamedata/GameTextData");
const UIManager_1 = require("../../../../../common/gameplay/managers/UIManager");
const CharacterVoiceConfig_1 = require("../../../../../config/CharacterVoiceConfig");
const Enums_1 = require("../../../../Enums");
const GameModelManager_1 = require("../../../../model/GameModelManager");
const AlertDialogViewPresenter_1 = require("../../../presenter/AlertDialogViewPresenter");
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.nameSpr = CCDummyObjects_1.DummySprite;
            this.textContentLabel = CCDummyObjects_1.DummyRichText;
            this.inputtingNode = undefined;
            this.contentNode = CCDummyObjects_1.DummyNode;
            this.bgNode = CCDummyObjects_1.DummyNode;
            this.imageSprite = undefined;
            this.container = undefined;
            this.voiceNode = CCDummyObjects_1.DummyNode;
            this.voiceLabel = CCDummyObjects_1.DummyLabel;
            this.playVoice = () => {
                var _a;
                if (this._voicePath) {
                    (_a = this.contentNode.children[1].getComponent(cc.Animation)) === null || _a === void 0 ? void 0 : _a.play("voice_play");
                    simba_cc_audio_manager_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");
                    });
                }
            };
        }
        onVoiceClick() {
            if (this._voiceCfg) {
                let needAd = GameModelManager_1.GameModelManager.checkVoiceNeedShowAD(this._voiceCfg);
                if (needAd) {
                    this.showVoiceAdAlert();
                }
                else {
                    this.playVoice();
                }
            }
        }
        autoPopVoiceDialog(isCheckContent = true) {
            if (this._voiceCfg) {
                if (isCheckContent) {
                    let needAd = GameModelManager_1.GameModelManager.checkVoiceNeedShowAD(this._voiceCfg);
                    if (needAd) {
                        this.showVoiceAdAlert();
                    }
                }
                else {
                    this.showVoiceAdAlert();
                }
            }
        }
        showVoiceAdAlert() {
            let temp = {
                dataptr: {},
                titlecontent: GameModelManager_1.GameModelManager.getLanguageTxt(GameTextData_1.GameTextData.GAME_TEXT_VOICE_TITLE_VALUE),
                content: GameModelManager_1.GameModelManager.getLanguageTxt(GameTextData_1.GameTextData.GAME_TEXT_VOICE_CONTENT_VALUE),
                ishasad: true,
                istwobtn: true,
                adconfig: "inject_fruit",
                // items: [],
                location: Enums_1.CommonLocation.Audio,
                hasBanner: false,
                callback: (type, ret) => {
                    if (ret) {
                        GameModelManager_1.GameModelManager.pushVoiceShowAdToRecord(this._voiceCfg.id);
                        this.playVoice();
                        if (type === "video") {
                            // GameModelManager.addDailyTasks(DailyQuestType.Dq_AdsWatch, 1);
                        }
                    }
                }
            };
            UIManager_1.UIManager.pushPresenter(AlertDialogViewPresenter_1.default, temp);
        }
        previewImage() {
            UIManager_1.UIManager.pushPresenter(ImagePreviewPresenter_1.ImagePreviewPresenter, this._previewImage);
        }
        delay(time) {
            return new Promise((resolve) => {
                this._timer = setTimeout(() => {
                    this._timer = undefined;
                    resolve(null);
                }, 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.removeFromParent(true);
                this._extraNode = undefined;
            }
            this.node.height = this._nodeHeight;
            this.bgNode.width = this._bgWidth;
            this.bgNode.height = this._bgHeight;
            this.textContentLabel.string = "";
            this._previewImage = "";
            this.bgNode.active = true;
            this.voiceLabel.string = "";
            this._voiceCfg = undefined;
            this._voicePath = "";
            if (this.imageSprite)
                this.imageSprite.node.parent.active = false;
        }
        async resizeContent() {
            // console.log("resizeContent begin");
            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);
                // }
                let mw = 440;
                if (mw < this.textContentLabel.node.width) {
                    this.textContentLabel.maxWidth = mw;
                }
                this.bgNode.height = this.textContentLabel.node.height + 70;
                if (this._voiceCfg) {
                    this.voiceNode.y = this.bgNode.height / 2 * (-1);
                }
                this.node.height = this.bgNode.height + this._textHeight;
                let gapW = 120;
                if (this.bgNode.anchorX == 0) {
                    if (this._voiceCfg) {
                        this.bgNode.width = this.voiceLabel.node.x + this.voiceLabel.node.width + gapW + 32;
                    }
                    else {
                        this.bgNode.width = this.textContentLabel.node.x + this.textContentLabel.node.width + gapW;
                    }
                }
                else {
                    if (this._voiceCfg) {
                        this.bgNode.width = -this.voiceLabel.node.x + this.voiceLabel.node.width + gapW + 32;
                    }
                    else {
                        this.bgNode.width = -this.textContentLabel.node.x + this.textContentLabel.node.width + gapW;
                    }
                }
                this.node.height = this.bgNode.height + this.nameSpr.node.height;
            }
            // console.log("resizeContent end");
        }
        setName(name) {
            // console.log("setName begin");
            // this.nameLabel.string = name;
            // console.log("setName end");
        }
        setNameIcon(icon) {
            this.nameSpr.spriteFrame = new cc.SpriteFrame();
            if (icon && "" !== icon.trim()) {
                simba_cc_resutils_1.ResUtils.loadRes(icon, cc.SpriteFrame).then((sf) => {
                    this.nameSpr.spriteFrame = sf;
                });
            }
        }
        setIcon(icon) {
            if (icon && this._icon !== icon) {
                this._icon = icon;
                simba_cc_resutils_1.ResUtils.loadRes(icon, cc.SpriteFrame).then((sf) => {
                    if (this._icon === icon)
                        this.iconSprite.spriteFrame = sf;
                });
            }
        }
        setText(txt) {
            // console.log("setText begin");
            this.textContentLabel.string = txt;
            // console.log("setText end");
        }
        setExtraContent(node) {
            if (this.container) {
                let pre = this.container.getChildByName("LikeTip");
                if (pre) {
                    this.container.removeChild(pre, true);
                }
                node.name = "LikeTip";
                node.active = true;
                this.container.addChild(node);
            }
        }
        updateVoiceLastTime() {
            this.voiceLabel.string = this._voiceCfg.last + "''";
        }
        async setContent(plotId, content, select, shouldDelay = false) {
            // console.log("setContent begin");
            if (this._nodeHeight === undefined)
                this.init();
            let textCount = 0;
            let setContent1 = async (pid, content) => {
                if (content.type === AVG_1.SentenceType.TEXT) {
                    textCount = AVG_1.richNodeTextCount(content.value);
                    let str = AVG_1.richNodesToCocosString(content.value);
                    this.textContentLabel.string = str;
                    this.contentNode.children[0].active = true;
                    this.contentNode.children[1].active = false;
                    this.contentNode.children[2].active = false;
                }
                else if (content.type === AVG_1.SentenceType.AUDIO) {
                    let cfg = simba_config_manager_1.ConfigManager.getConfig(CharacterVoiceConfig_1.characterVoiceConfig, parseInt(content.value));
                    if (cfg) {
                        this._voiceCfg = cfg;
                        this._voicePath = cfg.name;
                        this.updateVoiceLastTime();
                        this.contentNode.children[0].active = false;
                        this.contentNode.children[1].active = true;
                        this.contentNode.children[2].active = false;
                        let hasAd = cfg.ads === 1;
                        let needAd = GameModelManager_1.GameModelManager.checkVoiceNeedShowAD(this._voiceCfg);
                        if (hasAd && needAd) {
                            let curMainLinePlotId = GameModelManager_1.GameModelManager.getCurMainLinePlotId();
                            if (curMainLinePlotId === pid) {
                                this.autoPopVoiceDialog(false);
                            }
                        }
                    }
                }
                else if (content.type === AVG_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 simba_cc_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 === AVG_1.SentenceType.SELECT) {
                if (select === undefined) {
                    inputtingForever = true;
                }
                else {
                    let option = content.value[select];
                    if (option.content) {
                        promise = setContent1(plotId, option.content);
                    }
                    else {
                        this.contentNode.children[0].active = true;
                        this.contentNode.children[1].active = false;
                        this.textContentLabel.string = option.summary;
                    }
                }
            }
            else {
                promise = setContent1(plotId, 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();
                }
            }
            // console.log("setContent end");
        }
        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.Sprite })
    ], ChatBubbleView.prototype, "nameSpr", 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, "imageSprite", void 0);
    __decorate([
        property({ type: cc.Node })
    ], ChatBubbleView.prototype, "container", void 0);
    __decorate([
        property({ type: cc.Node })
    ], ChatBubbleView.prototype, "voiceNode", void 0);
    __decorate([
        property({ type: cc.Label })
    ], ChatBubbleView.prototype, "voiceLabel", void 0);
    ChatBubbleView = __decorate([
        ccclass
    ], ChatBubbleView);
    return ChatBubbleView;
})();
exports.default = ChatBubbleView;

cc._RF.pop();