CCTVFocus.js 20.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/**
 * 扩展TVFocus,新增字段_bChangeZIndex,Layout模式下禁止改动zIndex,扰乱Layout排序
 */

var Common = require('Common');
var TVFocus = require('TVFocus');
//var AbstractTVFocus=require('AbstractTVFocus');

cc.Class({
    //name:"TVFocus",
    extends: TVFocus,
    //extends: cc.Node,  引擎团队的人不推荐这么做

    properties: {
        _bChangeZIndex: false,
    },

    init: function (strName, oScene, fiCurrentFocus,//Left,fTop,fObjectWidth,fObjectHeight,
        fScreenWidth, fScreenHeight, fCornerOuterRadius, fCornerInnerRadius, fScaleFactor, bChangeZIndex) {
        this._bChangeZIndex = bChangeZIndex;
        //TODO:这个函数里面是否要做onbeforeFocus和onAfterFocus的操作 --question from eva
        //cc.log(this instanceof Sprite);    // true
        if (strName) {
            this.name = strName;
        } else {
            cc.log('No Focus Name Specified');
            return;
        }
        if (oScene) {
            this._oScene = oScene;
        } else {
            cc.log('No Scene Specified for Focus');
        }
        fScaleFactor = fScaleFactor || fiCurrentFocus.getScaleFactor();
        //this.width=fObjectWidth;
        //this.height=fObjectHeight;
        var fLeft = fiCurrentFocus.getLeft();
        var fTop = fiCurrentFocus.getTop();
        var fObjectWidth = fiCurrentFocus.getWidth();
        var fObjectHeight = fiCurrentFocus.getHeight();
        if (fScreenWidth) {
            this._screenWidth = fScreenWidth;
        }
        if (fScreenHeight) {
            this._screenHeight = fScreenHeight;
        }
        if (fCornerOuterRadius) {
            this._cornerOuterRadius = fCornerOuterRadius;
        }
        if (fCornerInnerRadius) {
            this._cornerInnerRadius = fCornerInnerRadius;
        }
        var oTransformedPosition = this.transformCoordinatesWeb2Cocos(fLeft, fTop, fObjectWidth, fObjectHeight);
        this.node.x = oTransformedPosition.x;
        this.node.y = oTransformedPosition.y;
        var urlFocus = null;
        var textureFocus = null;
        //1 2 3
        //4   6
        //7 8 9
        for (var i = 1; i <= 9; i++) {
            //this.node.children[i-1] = new cc.Node('focus_'+i);
            this.node.addChild(new cc.Node('focus_' + i), 10);
            if (5 == i) {
                continue;
            }
            // urlFocus = cc.url.raw('resources/Texture/Focus/'+i+'.png');
            // textureFocus = cc.textureCache.addImage(urlFocus);
            var spriteTemp = this.node.getChildByName('focus_' + i).addComponent(cc.Sprite);
            // spriteTemp.spriteFrame = new cc.SpriteFrame(textureFocus);
            var ftaScaleTo = null
            if (2 == i || 8 == i) {
                //spriteTemp.sizeMode = cc.Sprite.SizeMode.CUSTOM;
                //spriteTemp.type= cc.Sprite.Type.FILLED;
                //spriteTemp.fillType = cc.Sprite.FillType.HORIZONTAL;
                //spriteTemp.width=fObjectWidth-2*fCornerInnerRadius;
                //this.node.children[i-1].width=fObjectWidth-2*fCornerInnerRadius;
                //this.node.children[i-1].height=fCornerOuterRadius;
                ftaScaleTo = cc.scaleTo(0, fScaleFactor * (fObjectWidth - 2 * fCornerInnerRadius), fScaleFactor * 1.0);
            } else if (4 == i || 6 == i) {
                //spriteTemp.sizeMode = cc.Sprite.SizeMode.CUSTOM;
                //spriteTemp.type= cc.Sprite.Type.FILLED;
                //spriteTemp.fillType = cc.Sprite.FillType.VERTICAL;
                //spriteTemp.height=fObjectHeight-2*fCornerInnerRadius;
                //this.node.children[i-1].setContentSize();
                //this.node.children[i-1].width=fCornerOuterRadius;
                //this.node.children[i-1].height=fObjectHeight-2*fCornerInnerRadius;
                ftaScaleTo = cc.scaleTo(0, fScaleFactor * 1.0, fScaleFactor * (fObjectHeight - 2 * fCornerInnerRadius));
            } else {
                ftaScaleTo = cc.scaleTo(0, fScaleFactor, fScaleFactor);
            }
            ftaScaleTo.setTag(Common.FOCUS_CELL_TRANSFER);
            this.node.children[i - 1].stopAllActions();
            this.node.children[i - 1].runAction(ftaScaleTo);
        }
        //TODO:2.0版本采用异步加载本地图片,在焦点初始完成后在批量处理图片
        //TODO:循环中焦点框不是连续的,而批量加载的图地址是连续的需特殊处理下
        var self = this;
        // cc.loader.loadResDir("focus", cc.Texture2D, function (err, textures) {
        //     for (let i = 1; i < 10; i++) {
        //         if (i == 2 || i == 4 || i == 6 || i == 8) continue;  //暂时将焦点框一部分隐藏
        //         if (i >= 5) {
        //             if (i == 5) continue;
        //             self.node.getChildByName('focus_' + i).getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(textures[i - 2]);
        //         } else {
        //             self.node.getChildByName('focus_' + i).getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(textures[i - 1]);
        //         }
        //     }
        // });

        for (let i = 1; i < 10; i++) {
            if (i == 2 || i == 4 || i == 5 || i == 6 || i == 8) continue;
            cc.loader.loadRes('focus/' + i, cc.Texture2D, function (err, texture) {
                self.node.getChildByName('focus_' + i).getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
            });
        }



        this.node.children[0].x = fScaleFactor * (-fObjectWidth / 2 - (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[0].y = fScaleFactor * (fObjectHeight / 2 + (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[1].x = 0;
        this.node.children[1].y = fScaleFactor * (fObjectHeight / 2 + (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[2].x = fScaleFactor * (fObjectWidth / 2 + (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[2].y = fScaleFactor * (fObjectHeight / 2 + (fCornerOuterRadius / 2 - fCornerInnerRadius));

        this.node.children[3].x = fScaleFactor * (-fObjectWidth / 2 - (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[3].y = 0;
        //this.node.children[4].x=0;
        //this.node.children[4].y=0;
        this.node.children[5].x = fScaleFactor * (fObjectWidth / 2 + (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[5].y = 0;

        this.node.children[6].x = fScaleFactor * (-fObjectWidth / 2 - (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[6].y = fScaleFactor * (-fObjectHeight / 2 - (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[7].x = 0;
        this.node.children[7].y = fScaleFactor * (-fObjectHeight / 2 - (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[8].x = fScaleFactor * (fObjectWidth / 2 + (fCornerOuterRadius / 2 - fCornerInnerRadius));
        this.node.children[8].y = fScaleFactor * (-fObjectHeight / 2 - (fCornerOuterRadius / 2 - fCornerInnerRadius));

        //放大 当前选中的Node
        var ftaScaleCurrentFocus = cc.scaleTo(0, fScaleFactor, fScaleFactor);
        ftaScaleCurrentFocus.setTag(Common.FEATURE_NODE_TRANSFER);

        if (bChangeZIndex) {

        } else {
            fiCurrentFocus.node.zIndex = 40;
        }

        fiCurrentFocus.node.stopAllActions();
        fiCurrentFocus.node.runAction(ftaScaleCurrentFocus);

        //开始呼吸动画
        var aiFocusBreath = cc.repeatForever(cc.sequence(cc.fadeTo(1, 100), cc.fadeTo(1, 255), cc.fadeTo(2, 255)));
        aiFocusBreath.setTag(Common.FOCUS_CONTAINER_BREATH);
        this.node.stopAllActions();
        this.node.runAction(aiFocusBreath);

        this._oScene.setCurrentFocus(fiCurrentFocus);
    },

    flyFocus: function (fiCurrentFocus, fiFocusTarget, iDirection, fScaleFactor, oScrollParameter, iDuration) {
        if (null == fiFocusTarget) {//todo:先判断这个是否更划得来
            //找不到目标的动画小交互
            //var blink1=cc.blink(0.6,20);
            //this.node.runAction(blink1);

            let aiJoggle = cc.repeat(
                cc.sequence(
                    cc.rotateTo(0.02, -2),
                    cc.rotateTo(0.02, 0),
                    cc.rotateTo(0.02, 2),
                    cc.rotateTo(0.02, 0)
                ),
                4
            );
            aiJoggle.setTag(Common.FOCUS_CONTAINER_JOGGLE);
            //if (null!=this.node.getActionByTag(Common.FOCUS_CONTAINER_JOGGLE)){
            //    this.node.stopAction(Common.FOCUS_CONTAINER_JOGGLE);
            //}
            this.node.stopAction(this.node.getActionByTag(Common.FOCUS_CONTAINER_JOGGLE));
            this.node.runAction(aiJoggle);
            return 0;
        }

        if (null == fiCurrentFocus) {
            fiCurrentFocus = this._oScene.getCurrentFocusInfo();
        }

        fScaleFactor = fScaleFactor || fiFocusTarget.getScaleFactor();
        if (null == fScaleFactor) {
            fScaleFactor = 1.0;
        }

        if (null == iDuration) {
            iDuration = Common.FOCUS_FLY_DURATION;
        }

        //是否是有效方向
        if (Common.MOVE_DIRECTION_UP <= iDirection && iDirection <= Common.MOVE_DIRECTION_LEFT) {
            //滚屏相关参数 elScroll,iScrollStep 如果都有效开始处理滚屏
            //oScrollParameter.element   需要滚动的元素 Object[Element]
            //oScrollParameter.step      滚动的距离 Integer
            //oScrollParameter.relation  滚动是否和焦点元素有关?  Integer [0|1]
            //oScrollParameter.direction 滚动方向  ['left'|'right'|'up'|'down']

            //旧的时间通知
            //this.node.dispachEvent('before_focus_change', { from: fiCurrentFocus, to: fiFocusTarget});
            let eventBeforeFocus = new cc.Event.EventCustom('before_focus_change', true);
            eventBeforeFocus.setUserData({ from: fiCurrentFocus, to: fiFocusTarget });
            this.node.dispatchEvent(eventBeforeFocus);

            let eventOnBlur = new cc.Event.EventCustom('blur', true);
            eventOnBlur.setUserData({ from: fiCurrentFocus, to: fiFocusTarget });
            this.node.dispatchEvent(eventOnBlur);
            //let eventOnFocus=new cc.Event.
            let nodeCurrent = fiCurrentFocus.node;//node;
            let nodeTarget = fiFocusTarget.node;//node;
            //if (fiCurrentFocus && fiCurrentFocus.onBlur && typeof fiCurrentFocus.onBlur =='function' ){
            //   fiCurrentFocus.onBlur();
            //}
            //处理因焦点移动导致的部分界面滑动
            let fXScrollStep = 0;
            let fYScrollStep = 0;
            let fXScrollTarget = 0; //新方法
            let fYScrollTarget = 0; //新方法
            if (null != oScrollParameter && null != oScrollParameter.node
                && null != oScrollParameter.getStep() && null != oScrollParameter.getHasRelation()) {  //满足此条件才考虑界面滚动
                //MOVE_DIRECTION_UP:0,
                //MOVE_DIRECTION_RIGHT:1,
                //MOVE_DIRECTION_DOWN:2,
                //MOVE_DIRECTION_LEFT:3,
                //默认反向
                if (null == oScrollParameter.getDirection()) {
                    switch (iDirection) {
                        case Common.MOVE_DIRECTION_UP: oScrollParameter.setDirection(Common.MOVE_DIRECTION_DOWN); break;
                        case Common.MOVE_DIRECTION_RIGHT: oScrollParameter.setDirection(Common.MOVE_DIRECTION_LEFT); break;
                        case Common.MOVE_DIRECTION_DOWN: oScrollParameter.setDirection(Common.MOVE_DIRECTION_UP); break;
                        case Common.MOVE_DIRECTION_LEFT: oScrollParameter.setDirection(Common.MOVE_DIRECTION_RIGHT); break;
                    }
                }
                //Deprecated Start 
                switch (oScrollParameter.getDirection()) {
                    case Common.MOVE_DIRECTION_UP:
                        fYScrollStep = oScrollParameter.getStep();
                        break;
                    case Common.MOVE_DIRECTION_DOWN:
                        fYScrollStep = -oScrollParameter.getStep();
                        break;
                    case Common.MOVE_DIRECTION_LEFT:
                        fXScrollStep = -oScrollParameter.getStep();
                        break;
                    case Common.MOVE_DIRECTION_RIGHT:
                        fXScrollStep = oScrollParameter.getStep();
                        break;

                }
                //let ftaMoveToScrollParameter = cc.moveTo(iDuration, 
                //                   oScrollParameter.node.x+fXScrollStep,
                //                    oScrollParameter.node.y-fYScrollStep
                //                );
                //Deprecated End
                switch (oScrollParameter.getDirection()) { //正负在外面算好了
                    case Common.MOVE_DIRECTION_UP:
                        fYScrollTarget = oScrollParameter.getTargetPosition();
                        break;
                    case Common.MOVE_DIRECTION_DOWN:
                        fYScrollTarget = oScrollParameter.getTargetPosition();
                        break;
                    case Common.MOVE_DIRECTION_LEFT:
                        fXScrollTarget = oScrollParameter.getTargetPosition();
                        break;
                    case Common.MOVE_DIRECTION_RIGHT:
                        fXScrollTarget = oScrollParameter.getTargetPosition();
                        break;

                }
                let finished = cc.callFunc((oScrollParameter.callback ? oScrollParameter.callback : function () {
                    cc.log("do something");
                }), this, oScrollParameter.scope);//todo:这里是否考虑要个回调
                let ftaMoveToScrollParameter = cc.sequence(cc.moveTo(iDuration, fXScrollTarget, fYScrollTarget), finished);

                ftaMoveToScrollParameter.setTag(Common.SCROLL_BY_FOCUS_CHANGE);
                oScrollParameter.node.stopAction(oScrollParameter.node.getActionByTag(Common.SCROLL_BY_FOCUS_CHANGE));
                //oScrollParameter.node.stopAllActions();
                oScrollParameter.node.runAction(ftaMoveToScrollParameter);
                oScrollParameter.setDirection(null);
                //oScrollParameter.node.x+=fXScrollStep;
                //oScrollParameter.node.y-=fYScrollStep;
            }

            //由于浮点数变成坐标必须取整所以不可避免会形成误差,其中的+1 -1为了保证边框不留空隙
            //Target会比边框稍大

            //let fDestFocusLeft = iScaledTargetLeft - g_iFocusFrameWidth  + iXScrollStep + "px";
            //let fDestFocusTop = iScaledTargetTop - g_iFocusFrameHeight  + iYScrollStep + "px";
            //let fDestFocusWidth = iScaledTargetWidth + 2 * g_iFocusFrameWidth + "px";
            //let fDestFocusHeight = iScaledTargetHeight + 2 * g_iFocusFrameHeight + "px";

            //只要改掉相对Left Top 就可
            //此处需很小心处理和上下代码的关系
            /*
            if (null != oScrollParameter && null != oScrollParameter.node
                && null != oScrollParameter.getStep() && null != oScrollParameter.getHasRelation()) {  //满足此条件才考虑界面滚动
                let sizeVisible = cc.Director.getVisibleSize();
                let fDestScrollElementLeft = oScrollParameter.node.x + fXScrollStep;
                let fDestScrollElementTop = sizeVisible.height - oScrollParameter.node.y + fYScrollStep;

                //这里还缺少动画 必须一起开始动
                oScrollParameter.node.x=fDestScrollElementLeft;
                oScrollParameter.node.y=oScrollParameter.node.y - fYScrollStep;
            }*/

            //准备好了所有数据  这里开始一起行动
            //if (g_fnOnBeforeFocusChange) {
            //  g_fnOnBeforeFocusChange(elFocusNow, elFocusTarget, strDirection);
            //}

            //按比例将焦点目标的区域进行缩放 得注意浮点数的问题

            let ftaScaleCurrentFocus = cc.scaleTo(iDuration, 1.0, 1.0);
            let ftaScaleFocusTarget = cc.scaleTo(iDuration, fScaleFactor, fScaleFactor);

            /*
            log("ArchorPoint:%f,%f,%f,%f", pfiCurrentFocus->node->getAnchorPoint().x, 
                pfiCurrentFocus->node->getAnchorPoint().y,
                pfiCurrentFocus->node->getAnchorPointInPoints().x,
                pfiCurrentFocus->node->getAnchorPointInPoints().y );
            */
            /*
            //红点测试
            auto sprite1 = cocos2d::Sprite::create("image/red_point.png");
            sprite1->setPosition(0,0
                //  pfiCurrentFocus->node->getAnchorPointInPoints().x
                //, pfiCurrentFocus->node->getAnchorPointInPoints().y);
            
            pfiCurrentFocus->node->addChild(sprite1, 20, "PointFlag");
            */

            // nodeCurrent.setLocalZOrder(10);
            // nodeTarget.setLocalZOrder(40);
            if (this._bChangeZIndex) {

            } else {
                nodeCurrent.zIndex = 10;
                nodeTarget.zIndex = 40;
            }

            ftaScaleCurrentFocus.setTag(Common.FEATURE_NODE_TRANSFER);
            ftaScaleFocusTarget.setTag(Common.FEATURE_NODE_TRANSFER);
            //if (null!=nodeCurrent.getActionByTag(Common.FEATURE_NODE_TRANSFER)){
            //    nodeCurrent.stopAction(Common.FEATURE_NODE_TRANSFER);    
            //}
            //if (null!=nodeTarget.getActionByTag(Common.FEATURE_NODE_TRANSFER)){
            //    nodeTarget.stopAction(Common.FEATURE_NODE_TRANSFER);    
            //}
            nodeCurrent.stopAction(nodeCurrent.getActionByTag(Common.FEATURE_NODE_TRANSFER));
            nodeTarget.stopAction(nodeTarget.getActionByTag(Common.FEATURE_NODE_TRANSFER));
            nodeCurrent.runAction(ftaScaleCurrentFocus);
            nodeTarget.runAction(ftaScaleFocusTarget);
            cc.log(fiFocusTarget.getLeft());
            cc.log(fiFocusTarget.getTop());
            this.fly(fiFocusTarget.getLeft() + fXScrollStep, fiFocusTarget.getTop() + fYScrollStep,
                fiFocusTarget.getWidth(), fiFocusTarget.getHeight(),
                iDuration, fScaleFactor);
            //if (fiTarget.onFocus && typeof fiTarget.onFocus =='function' ){
            //    fiTarget.onFocus();
            //}
            let eventOnFocus = new cc.Event.EventCustom('focus', true);
            eventOnFocus.setUserData({ from: fiCurrentFocus, to: fiFocusTarget });
            this.node.dispatchEvent(eventOnFocus);
            //旧的事件通知
            //this.node.emit('after_focus_change', { from: fiCurrentFocus, to: fiFocusTarget});
            let eventAfterFocus = new cc.Event.EventCustom('after_focus_change', true);
            eventAfterFocus.setUserData({ from: fiCurrentFocus, to: fiFocusTarget });
            this.node.dispatchEvent(eventAfterFocus);
            //新的事件通知方式 是否科学有待检验
            //let eventFocusLeave=new cc.Event.EventCustom('focus_leave',true);
            //eventFocusLeave.setUserData({from:fiCurrentFocus,to:fiFocusTarget});
            //nodeCurrent.dispatchEvent(eventFocusLeave);

            //let eventFocusHover=new cc.Event.EventCustom('focus_hover',true);
            //eventFocusHover.setUserData({from:fiCurrentFocus,to:fiFocusTarget});
            //nodeTarget.dispatchEvent(eventFocusHover);
            this._oScene.setCurrentFocus(fiFocusTarget);
            //计算完成以下开始完成所有动作
            //elFocusNow.style.zIndex = 0;
            //elFocusTarget.style.zIndex = 20;
            /*
            var elFocusContainer = document.getElementById("focus_container");
            //document.getElementById('view_port').style.left = destViewPortLeft;
            elFocusContainer.style.left = destFocusLeft;
            elFocusContainer.style.top = destFocusTop;
            //document.getElementById("focus5").style.width = destFocusWidth;
            //document.getElementById("focus5").style.height = destFocusHeight;
            elFocusContainer.style.width = destFocusWidth;
            elFocusContainer.style.height = destFocusHeight;
            */
            //if (g_fnOnAfterFocusChange) {
            //  g_fnOnAfterFocusChange(elFocusNow, elFocusTarget, strDirection);
            //}
            //总要先将前面那个复原
            //scaleElement(g_strFocusElementId, 1, 1);
            //g_strFocusElementId = elFocusTarget.id;
            return 1;
        } else {
            cc.log("No Target Found to Fly: Unavailable Direction");
            return 0;
        }
    },

});