Cocos2d-JS extend之FocusButton

var FocusBase = {

    useKeyboard: true,

    nextFocusArray: null,

    ctor: function () {
        this._super.apply(this, arguments);
        this.nextFocusArray = {};
    },

    setNextFocus: function (up, down, left, right) {
        this.nextFocusArray[cc.KEY.up] = up;
        this.nextFocusArray[cc.KEY.down] = down;
        this.nextFocusArray[cc.KEY.left] = left;
        this.nextFocusArray[cc.KEY.right] = right;

        this.nextFocusArray[cc.KEY.dpadUp] = up;
        this.nextFocusArray[cc.KEY.dpadDown] = down;
        this.nextFocusArray[cc.KEY.dpadLeft] = left;
        this.nextFocusArray[cc.KEY.dpadRight] = right;
    },

    setNextFocusUp: function (up) {
        this.nextFocusArray[cc.KEY.up] = up;
        this.nextFocusArray[cc.KEY.dpadUp] = up;
    },

    setNextFocusDown: function (down) {
        this.nextFocusArray[cc.KEY.down] = down;
        this.nextFocusArray[cc.KEY.dpadDown] = down;
    },

    setNextFocusLeft: function (left) {
        this.nextFocusArray[cc.KEY.left] = left;
        this.nextFocusArray[cc.KEY.dpadLeft] = left;
    },

    setNextFocusRight: function (right) {
        this.nextFocusArray[cc.KEY.right] = right;
        this.nextFocusArray[cc.KEY.dpadRight] = right;
    }
};
var FocusButton = ccui.Button.extend(FocusBase).extend({

    normalImage: null,
    selectedImage: null,
    texType: null,

    onClickCallback: null,
    onTouchCallback: null,

    ctor: function (normalImage, selectedImage, disableImage, texType, useImage) {
        this._super(normalImage, this.useKeyboard ? "" : selectedImage, disableImage, texType);

        this.normalImage = normalImage;
        this.selectedImage = selectedImage;
        this.texType = texType;
    },

    setNormalTexture: function () {
        this.loadTextureNormal(this.normalImage, this.texType);
    },

    setSelectedTexture: function () {
        this.loadTextureNormal(this.selectedImage, this.texType);
    },

    setNormalColor: function () {
        this.getRendererNormal().setColor(new cc.Color(255, 255, 255));
    },

    setSelectedColor: function () {
        this.getRendererNormal().setColor(new cc.Color(255, 128, 128));
    },

    setNormal: function () {
        if (this.selectedImage != null && this.selectedImage != "")
            this.setNormalTexture();
        else
            this.setNormalColor();
    },

    setSelected: function () {
        if (this.selectedImage != null && this.selectedImage != "")
            this.setSelectedTexture();
        else
            this.setSelectedColor();
    },

    addTouchEventListener: function (selector, target) {
        this._super(selector, target);

        this.onTouchCallback = target ? selector.bind(target) : selector;
    },

    addClickEventListener: function (callback) {
        this._super(callback);

        this.onClickCallback = callback;
    },

    onClick: function () {
        this.onTouchCallback && this.onTouchCallback(this, ccui.Widget.TOUCH_BEGAN);
        this.onTouchCallback && this.onTouchCallback(this, ccui.Widget.TOUCH_ENDED);
        this.onClickCallback && this.onClickCallback(this);
    }
});

你可能感兴趣的:(Cocos2d-JS extend之FocusButton)