pfbCommonCell.js 6.43 KB
//推荐页
var Common = require('Common');
var Network = require('Network');

cc.Class({
    extends: cc.Component,

    properties: {
        id: 0,
        pic: cc.Sprite,
        activeName1: cc.Label,
        activeName2: cc.Label,
        normalName: cc.Label,
    },

    // data: {id,iconSF,itemName,itemPrice}
    init: function (data, funCallback, strCellName, i, iCurrentIndex) {
        // cc.log("pfbRecommend->init: "+data.tvlink);
        // this.id = data.id;
        this.node.width = data.width;
        this.node.height = data.height;
        this.node.getChildByName('Pic').width = data.width;
        this.node.getChildByName('Pic').height = data.height;
        if (data.imageURL) {
            var self = this;
            // cc.loader.loadRes(data.imageURL, cc.Texture2D, function (err, texture) {
            //     if (strCellName == "topNavi") {
            //         self.pic.spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 0, data.width, data.height));
            //         if (i == iCurrentIndex) {
            //             self.pic.spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, data.height, data.width, data.height));
            //         }
            //     } else {
            //         self.pic.spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 0, data.width, data.height));
            //     }
            //     // if (null != funCallback && typeof funCallback == 'function') {
            //     //     funCallback.call();
            //     // }
            // });

            Network.loadImageInNativeRuntime(
                Common.TOPDRAW_IMAGE_SERVER + data.imageURL
                , null,
                function (texture, iRequestId) {
                    if (strCellName == "topNavi") {
                        self.pic.spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 0, data.width, data.height));
                        if (i == iCurrentIndex) {
                            self.pic.spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, data.height, data.width, data.height));
                        }
                    } else {
                        self.pic.spriteFrame = new cc.SpriteFrame(texture, cc.rect(0, 0, data.width, data.height));
                    }
                },
                function () {
                    
                },
                this
            );
        }

        if (strCellName == "topNavi" && i > 0) {
            this.node.getComponent(cc.Widget).top = data.top - data.height / 2;
        } else {
            this.node.getComponent(cc.Widget).top = data.top;
        }
        this.node.getComponent(cc.Widget).left = data.left;


        if (data.title_visible == 1) {    //前三个不需要显示文字
            cc.find('Name', this.node).active = true;
            cc.find('Name', this.node).width = data.width - 10;
            cc.find('Name/TextBg', this.node).width = data.width;
            cc.find('Name/NormalName', this.node).width = data.width - 10;
            cc.find('Name/ActiveName', this.node).width = data.width - 10;
            cc.find('Name/ActiveName/NameContainer', this.node).width = data.width - 10;
            cc.find('Name/ActiveName/NameContainer/NameText', this.node).width = data.width - 10;
            this.normalName.node.width = data.width - 10;
            this.activeName1.string = data.title;
            this.activeName2.string = data.title;
            this.normalName.string = data.title;
            this.activeName1._updateRenderData(true);
            this.activeName2._updateRenderData(true);
            this.normalName._updateRenderData(true);

            //调整文字位置
            let nodeCellName = this.node.getChildByName('Name');
            let nodeScreenTitleText = cc.find('ActiveName/NameContainer/NameText', nodeCellName);
            let nodeScreenTitleText1 = nodeScreenTitleText.getChildByName('NameText1');
            if (nodeScreenTitleText1.width > nodeScreenTitleText.width) {
                this.normalName.overflow = cc.Label.Overflow.CLAMP;
                this.normalName.enableWrapText = false;
                this.normalName.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
            }
        }
    },

    setUIWithFocus: function () {
        let nodeCellName = this.node.getChildByName('Name');
        cc.find("NormalName", nodeCellName).opacity = 255;
        //超出滚动
        let nodeScreenTitleText = cc.find('ActiveName/NameContainer/NameText', nodeCellName);

        let fOriginalX = nodeScreenTitleText.x;
        let fOriginalY = nodeScreenTitleText.y;

        let nodeScreenTitleText1 = nodeScreenTitleText.getChildByName('NameText1');
        let nodeScreenTitleText2 = nodeScreenTitleText.getChildByName('NameText2');
        nodeScreenTitleText2.x = nodeScreenTitleText1.x + nodeScreenTitleText1.width + 10;//TODO:补丁,解决滚动重叠
        cc.log(nodeScreenTitleText1.width + "------------" + nodeScreenTitleText.width);
        if (nodeScreenTitleText1.width > nodeScreenTitleText.width) {
            cc.find("ActiveName/NameContainer", nodeCellName).opacity = 255;
            cc.find("NormalName", nodeCellName).opacity = 0;
            let fEndPositionDelta = (nodeScreenTitleText1.width + 30);
            //下方时长和字有关系,才能速度一样
            var ftaScreenTitleMoveToTarget = cc.moveTo(10 * (nodeScreenTitleText1.width / nodeScreenTitleText.width), fOriginalX - fEndPositionDelta, fOriginalY);
            var ftaScreenTitleMoveToBack = cc.moveTo(0, fOriginalX, fOriginalY);
            var sequenceMediaTitle = cc.sequence(ftaScreenTitleMoveToTarget, ftaScreenTitleMoveToBack);
            var repeatScreenTitle = cc.repeat(sequenceMediaTitle, 10);
            repeatScreenTitle.setTag(Common.OVERLENGTH_MOVING);
            nodeScreenTitleText.runAction(repeatScreenTitle);
            nodeScreenTitleText2.active = true;
        }
    },

    setUIWithoutFocus: function () {
        let nodeCellName = this.node.getChildByName('Name');
        cc.find("NormalName", nodeCellName).opacity = 255;
        //如有滚动停止滚动
        let nodeScreenTitleText = cc.find('ActiveName/NameContainer/NameText', nodeCellName);
        if (null != nodeScreenTitleText.getActionByTag(Common.OVERLENGTH_MOVING)) {
            nodeScreenTitleText.stopActionByTag(Common.OVERLENGTH_MOVING);
            nodeScreenTitleText.getChildByName('NameText2').active = false;
            cc.find("ActiveName/NameContainer", nodeCellName).opacity = 0;
        }
        nodeScreenTitleText.x = 0;
    },
});