Overriding __indexFromOffset and __offsetFromIndex from TableView

Hi, sorry for asking…
I want to override these 2 function : indexFromOffset andoffsetFromIndex() from TableView extension to create a CCMultiColumnTableView as mentioned here http://www.cocos2d-x.org/forums/18/topics/15777

What I have done :

  • I’ve successfully override the *updateContentSize function, but those 2 functions is not called at all.
  • I’ve added new code at 3 jsb_cocos2dx_extension_auto.xx files
    at .hpp :

JSBool js_cocos2dx_extension_CCTableView**indexFromOffset;
at .js
>*indexFromOffset:function () {},

at .cpp :

JSBool js_cocos2dx_extension_CCTableView**indexFromOffset{
jsval argv = JS_ARGV;
JSBool ok = JS_TRUE;
JSObject
obj = JS_THIS_OBJECT;
js_proxy_t proxy = jsb_get_js_proxy;
cocos2d::extension::CCTableView
cobj = ;
JSB_PRECONDITION2;
if {
cocos2d::CCPoint arg0;
ok &= jsval_to_ccpoint;
int ret = cobj->*indexFromOffset;
jsval jsret;
jsret = int32_to_jsval;
JS_SET_RVAL;
return JS_TRUE;
>
}
>
JS_ReportError;
return JS_FALSE;
}

and my code at cc.MultiColumnTableView.js
@
cc.MultiColumnTableView = cc.TableView.extend {
this.*super();
},

setColCount:function(cols){
this.m_colCount = cols;

cc.log(“setColCount”);
if (this.getDirection() cc.SCROLLVIEW_DIRECTION_BOTH) {
this._updateContentSize();
}
},

\_\_indexFromOffset:function(offset){
    cc.log("\_\_indexFromOffset -\> "+this.m\_colCount);
    if(this.\_dataSource != null){
        var index = 0;
        var cellSize;
        var col;
        var row;
        var spaceWidth;

        cellSize = this.\_dataSource.cellSizeForTable(this);

        switch(this.getDirection()) {
            case cc.SCROLLVIEW\_DIRECTION\_HORIZONTAL:
                spaceWidth = this.getContainer().getContentSize().height / this.m\_colCount;
                col        = parseInt((offset.y - (spaceWidth - cellSize.height)\*0.5) /spaceWidth);
                row        = parseInt(offset.x / cellSize.width);
                break;
            default:
                spaceWidth = this.getContainer().getContentSize().width / this.m\_colCount;
                col        = parseInt((offset.x - (spaceWidth - cellSize.width)\*0.5) / spaceWidth);
                row        = parseInt(offset.y / cellSize.height);
                break;
        }

        index = col + (row \* this.m\_colCount);

        return index;
    }
    else{
        cc.log("\_\_indexFromOffset null");
        return 0;
    }
},

\_\_offsetFromIndex:function(index){
    cc.log("\_\_offsetFromIndex -\> "+this.m\_colCount);
    if(this.\_dataSource != null){
        var offset;
        var cellSize;
        var spaceWidth;
        var col;
        var row;

        cellSize = this.\_dataSource.cellSizeForTable(this);

        switch (this.getDirection()) {
            case cc.SCROLLVIEW\_DIRECTION\_HORIZONTAL:
                row        = parseInt(index / this.m\_colCount);
                col        = parseInt(index % this.m\_colCount);
                spaceWidth = this.getContainer().getContentSize().height / this.m\_colCount;

                var posX = parseInt(row \* cellSize.height) +6;
                var posY = parseInt(col \* spaceWidth + (spaceWidth - cellSize.width) \* 0.5);
                offset   = cc.p(posX,posY);
                break;

            default:
                row        = parseInt(index / this.m\_colCount);
                col        = parseInt(index % this.m\_colCount);
                spaceWidth = this.getContainer().getContentSize().width / this.m\_colCount;

                var posX = parseInt(col \* spaceWidth + (spaceWidth - cellSize.width) \* 0.5) +4;
                var posY = parseInt(row \* cellSize.height) -2;
                offset   = cc.p(posX, posY);
                break;
        }

        return offset;
    }
    else
        return cc.p(0,0);
},

\_updateContentSize:function(){
    cc.log("\_dataSource");
    cc.log(this.\_dataSource);

    if(this.\_dataSource != null){
        cc.log("\_dataSource 2 -\> "+this.m\_colCount);
        var size;
        var cellSize;
        var viewSize;
        var cellCount;
        var rows;

        cellSize  = this.\_dataSource.cellSizeForTable(this);
        cellCount = this.\_dataSource.numberOfCellsInTableView(this);
        viewSize  = cc.size(this.getViewSize().width/this.getContainer().getScaleX(), this.getViewSize().height/this.getContainer().getScaleY());

        switch (this.getDirection())
        {
            case cc.SCROLLVIEW\_DIRECTION\_HORIZONTAL:
                this.m\_colCount = parseInt(viewSize.height / cellSize.height);
                rows     = Math.ceil(cellCount/(this.m\_colCount));
                size     = cc.size(rows \* cellSize.width, this.m\_colCount \* cellSize.height);
                break;
            default:
                this.m\_colCount = parseInt(viewSize.width / cellSize.width);
                rows     = Math.ceil(cellCount/(this.m\_colCount));
                size     = cc.size(cellSize.width \* this.m\_colCount, rows \* cellSize.height);
                break;
        }
        this.setContentSize(size);

        //FIXME? unsure if that code makes sense
        if (this.\_oldDirection != this.\_direction) {
            if (this.\_direction  cc.SCROLLVIEW\_DIRECTION\_HORIZONTAL) {

this.setContentOffset(cc.p(0, 0));
} else {
this.setContentOffset(cc.p(0, this.minContainerOffset().y));
}
this.*oldDirection = this.*direction;
}
}
}

});@

indexFromOffset andoffsetFromIndex are protected function.
We only exposed public methods to JS. Therefore, your way of implementing CCMultiColumnTableView was not correct.
I think that exposing @ CCMultiColumnTableView@ which is implemented in Native to JS will be a better idea.

We’ve change the interface, so it doesn’t use CCTableView anymore…
Thanks for asking though, and great job for the Cocos2D HTML5. I really appreciate it and it does help our HTML5 developments a lot.